Creator接微信登录步骤

2019-12-20  本文已影响0人  浪荡少年

花了200块钱在网上找大佬学的,

weixinSDK脚本如下:

package org.cocos2dx.javascript;

import com.tencent.mm.opensdk.modelmsg.SendAuth;

import com.tencent.mm.opensdk.openapi.IWXAPI;

import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import static org.cocos2dx.lib.Cocos2dxHelper.getActivity;

public class weixinSDK {

public static Stringwx_appid ="自己的appid";

public static IWXAPIwx_api;

public static void weixin_Init() {

wx_api = WXAPIFactory.createWXAPI(getActivity(),wx_appid,true);

wx_api.registerApp(wx_appid);

}

public static void weixin_login(String data) {

SendAuth.Req req =new  SendAuth.Req();

req.scope ="snsapi_userinfo";

req.state = data;

System.out.println("req is " + req);

//利用微信api发送请求

        wx_api.sendReq(req);

}

}

WXEntryActivity脚本如下:

package org.cocos2dx.wxapi;

import android.app.Activity;

import android.os.Bundle;

import android.os.Message;

import android.widget.Toast;

import com.tencent.mm.opensdk.modelbase.BaseReq;

import com.tencent.mm.opensdk.modelbase.BaseResp;

import com.tencent.mm.opensdk.modelmsg.SendAuth;

import com.tencent.mm.opensdk.openapi.IWXAPI;

import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import org.cocos2dx.javascript.AppActivity;

import org.cocos2dx.javascript.weixinSDK;

import java.util.ArrayList;

public class WXEntryActivityextends Activityimplements IWXAPIEventHandler

{

public static int ReqState = -1;//0为登录, 1为分享

    private IWXAPIAPI;

@Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

System.out.println("enter the wxEntryActivity");

//这句话很关键

        try {

weixinSDK.wx_api.handleIntent(getIntent(),this);

}catch (Exception e) {

e.printStackTrace();

}

}

@Override

    public void onReq(BaseReq baseReq) {

System.out.println("Enter the onResp");

}

//向微信发送的请求的响应信息回调该方法

    @Override

    public void onResp(BaseResp baseResp)

{

System.out.println("Enter the onResp");

System.out.println("api is " +API);

switch (baseResp.errCode) {

case BaseResp.ErrCode.ERR_OK:

Toast.makeText(this,"请求成功", Toast.LENGTH_LONG).show();

if (ReqState ==0)//0为登录, 1为分享

                {

}

if (ReqState ==1)

{

}

break;

//发送取消

            case BaseResp.ErrCode.ERR_USER_CANCEL:

//发送被拒绝

//Toast.makeText(this, "请求取消", Toast.LENGTH_LONG).show();

            case BaseResp.ErrCode.ERR_AUTH_DENIED:

if (ReqState ==0)//0为登录, 1为分享

                {

}

// Toast.makeText(this, "请求被拒绝", Toast.LENGTH_LONG).show();

                break;

default:

//发送返回

                Toast.makeText(this,"请求返回", Toast.LENGTH_LONG).show();

break;

}

ReqState = -1;

finish();

}

}

上一篇下一篇

猜你喜欢

热点阅读