微信登录

这几天项目中要接微信,我选择了sharesdk,花了几天时间研究,终于登陆成功,过程中遇到了很多的坑,好记性不如烂笔头,以此博文记录我的微信接入过程 
*一。关于sharesdk 
1.在mob.com官网下载sharesdkforunity 
这里写图片描述
这里写图片描述
温馨提示:这儿我是没下载下来,客服大大帮我下载的,如果有什么不懂,一定要问客服大大哦,他们人很好的 
ios客服大大QQ:800820690 
Android客服大大QQ:4006852216 
2.接下来导入 
这里写图片描述 
此处先告一段落 
二.将准备工作做好,准备工作如下 
第一步:微信开放平台去注册开发者并认证 缴纳300元一年的认证费

第二步:创建自己的应用 填写包名和签名(包名与签名很重要),提交审核,审核通过后可以得到APPID和key 
一定要填对这个包名和签名啊,这儿很重要,如果填错,回调函数无法执行的,我就是填错了,导致一直在 找错,最后发现错误居然是这个,简直了…所以一定要填对,填对,当然,这个后期是可以修改的 
包名就是:Unity里面的com.xxx.xxxx(看自己项目是什么) 
签名的问题:客服大大是这么说的看图吧 
这里写图片描述 
温馨提示:这个实在微信开放平台里面的应用签名和包名

第三步:去Mob官网注册成为开发者,获得APPID和key,进入后台创建应用 
这三部都是很重要的哈,但是也很简单,不会的自行百度即可 
三.在接sharesdk之前,在unity里面你需要修改的东西如下: 
1.首先修改Appid和Appkey(mob.com的以及微信官方平台申请的appkey和appid) 
这里写图片描述 
2.同时将BypassApproval的√去掉,如上图所示 
3.代码中需要修改的部分 
修改Androidmainfest里面的东西这里写图片描述这里写图片描述 
修改shareSDKDevInfo里面的东西 
这里写图片描述
修改sharesdk里面的东西!(将appid和appkey换成自己在mob.com申请的) 
这里写图片描述
至此所有准备工作就都做完了,然后就开始写自己的Demo 
四.微信接入demo 
接入的demo可以参考shareSdk官方demo,直接上代码

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
using cn.sharesdk.unity3d; 
using UnityEngine.SceneManagement; 
public class wechatLogging : MonoBehaviour {

// Use this for initialization
ShareSDK sharesdk;
void Start()
{
    sharesdk = GameObject.Find("Main Camera").GetComponent<ShareSDK>();
    sharesdk.authHandler += OnAuthResultHandler;
    sharesdk.showUserHandler += OnGetUserInfoResultHandler;

    GameObject.Find("login_Wechat").GetComponent<Button>().onClick.AddListener(delegate
    {
        print("登录");
        //sharesdk.Authorize(PlatformType.WeChat);
        sharesdk.GetUserInfo(PlatformType.WeChat);
    });
}
void OnAuthResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
{
    if (state == ResponseState.Success)
    {
        sharesdk.GetUserInfo(type);
        SceneManager.LoadScene("MainScene");
       // GameObject.Find("MessageText").GetComponent<Text>().text = "授权成功";
    }
    else if (state == ResponseState.Fail)
    {
        #if UNITY_ANDROID
        print("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
        #elif UNITY_IPHONE
        print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
        #endif
    }
    else if (state == ResponseState.Cancel)
    {
        print("cancel !");
    }
}
void OnGetUserInfoResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
{
    if (state == ResponseState.Success)
    {
        switch (type)
        {
            case PlatformType.WeChat:
                Dictionary<string, object> jsondata = new Dictionary<string, object>();
                jsondata = (Dictionary<string, object>)minijson.Json.Deserialize(MiniJSON.jsonEncode(result).ToString());
               // GameObject.Find("MessageText").GetComponent<Text>().text = "获取用户信息成功";
                SceneManager.LoadScene("MainScene");
                break;
        }
    }
    else if (state == ResponseState.Fail)
    {
        print("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
    }
    else if (state == ResponseState.Cancel)
    {
        print("cancel !");
    }
    //if (state == ResponseState.Success)
    //{
    //    print("get user info result :");
    //    print(MiniJSON.jsonEncode(result));
    //    GameObject.Find("MessageText").GetComponent<Text>().text = ("AuthInfo:" + MiniJSON.jsonEncode(sharesdk.GetAuthInfo(PlatformType.WeChat)));
    //    print("Get userInfo success !Platform :" + type);
    //}
    //else if (state == ResponseState.Fail)
    //{
    //    #if UNITY_ANDROID
    //                print("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
    //    #elif UNITY_IPHONE
    //                print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
    //    #endif
    //}
    //else if (state == ResponseState.Cancel)
    //{
    //    print("cancel !");
    //}
}

这是C#的 回调

  /// <summary>
    /// 获取用户信息回调
    /// </summary>
    /// <param name="reqID"></param>
    /// <param name="state"></param>
    /// <param name="type"></param>
    /// <param name="result"></param>
    void OnGetUserInfoResultHandler(int reqID, ResponseState state, PlatformType type, Hashtable result)
    {
        Debug.LogError("获取用户信息回调");
        if (state == ResponseState.Success)
        {
            Debug.Log("获取微信信息成功,正在登陆中...");
          
            Dictionary<string, string> uuInfo = new Dictionary<string, string>();
            foreach (DictionaryEntry de in result)
            {
           //     Debug.LogError  ( string.Format("Key -- {0}; Value --{1}.", de.Key, de.Value));
                uuInfo.Add(de.Key.ToString(),de.Value.ToString());
            }


         //   print("get user info result :");
        //    print(MiniJSON.jsonEncode(result));
        //    print("Get userInfo success ! Platform :" + type);


            //获取用户信息
        //    print(MiniJSON.jsonEncode(ssdk.GetAuthInfo(PlatformType.WeChat)));
         //   Debug.LogError(MiniJSON.jsonEncode(ssdk.GetAuthInfo(PlatformType.WeChat)));
      
         //   Hashtable uuInfo = result;


#if UNITY_ANDROID
      //      uuInfo = ssdk.GetAuthInfo(PlatformType.WeChat);
            #else
     //       uuInfo = result;
            #endif
            if (uuInfo.ContainsKey("openid") || uuInfo.ContainsKey("res"))
            {
            
                WxUserInfo kWxUserInfo = new WxUserInfo();
                kWxUserInfo.openid = (string)uuInfo["openid"];// MCWebReq::getDataValueStr(pDoc, "openid");
                kWxUserInfo.nickname = (string)uuInfo["nickname"];// MCWebReq::getDataValueStr(pDoc, "nickname");
                //微信返回 1男 2女
                kWxUserInfo.sex =  (byte.Parse((string)uuInfo["sex"])==2)?(byte)0:(byte)1; //MCWebReq::getDataValueInt(pDoc, "sex");
                kWxUserInfo.szAccounts = "WeiXin" + kWxUserInfo.openid;
                kWxUserInfo.szPassword = Util.md5("WeiXin"+kWxUserInfo.openid);
                kWxUserInfo.headimgurl = (string)uuInfo["headimgurl"];// MCWebReq::getDataValueStr(pDoc, "headimgurl");
        //        kWxUserInfo.unionid = uuInfo["unionid"].ToString();// MCWebReq::getDataValueStr(pDoc, "unionid");
                EventSystem.Instance.pushEvent(EventDefine_en.event_wx_shouquan_suc,0,0,kWxUserInfo);
          //      Debug.LogError ("账号:"+ kWxUserInfo.szAccounts);
            //    Debug.LogError("密码:" + kWxUserInfo.szPassword);
            }
            else{
               Debug.LogError("微信信息获取错误 。。。。。。。"+uuInfo);
}
        }
        else if (state == ResponseState.Fail)
        {
            EventSystem.Instance.pushEvent(EventDefine_en.event_wx_shouquan_faild, 0, 0);
            this.CanelWeChat();
            Debug.LogError("微信获取信息失败");
#if UNITY_ANDROID
            print("fail! throwable stack = " + result["stack"] + "; error msg = " + result["msg"]);
#elif UNITY_IPHONE
print ("fail! error code = " + result["error_code"] + "; error msg = " + result["error_msg"]);
#endif
        }
        else if (state == ResponseState.Cancel)
        {
            EventSystem.Instance.pushEvent(EventDefine_en.event_wx_shouquan_cancel, 0, 0);
            Debug.LogError("微信获取信息cancel");
            print("cancel !");
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值