unity 微信小游戏登录(通过code获取openid),充值

1.初始化:
 WX.InitSDK((jsCode) =>
 {
     Debug.Log("***  InitSDK  success ");
     FunGetSysten();
     Init1();
 });
 
 2.获取系统信息
  private void FunGetSysten()
 {
     Debug.Log("***  GetSystemInfo   ");
     GetSystemInfoOption getSystemInfoOption = new GetSystemInfoOption();
     getSystemInfoOption.success = ((e) =>
     {
         systemInfo = e;
         switch (e.platform)
         {
             case "android":
                 weChatSystemType = WeChatSystemType.android;
                 break;
             case "ios":
                 weChatSystemType = WeChatSystemType.ios;
                 break;
             case "windows":
                 weChatSystemType = WeChatSystemType.windows;
                 break;
             case "mac":
                 weChatSystemType = WeChatSystemType.mac;
                 break;
             case "devtools":
                 weChatSystemType = WeChatSystemType.devtools;
                 break;
             default:
                 weChatSystemType = WeChatSystemType.other;
                 break;
         }
         if (e.platform == "android")
         {
             
         }
         else
         if (e.platform == "ios")
         {
             weChatSystemType = WeChatSystemType.ios;
         }
         Debug.Log("***  GetSystemInfo  success " + e.platform);
     });
     getSystemInfoOption.fail = ((e) =>
     {
         Debug.Log("****** FunGetSysten  fail  :" + e.errMsg);
     });
     WX.GetSystemInfo(getSystemInfoOption);


var info = WX.GetSystemInfoSync();
if (info != null)
{
    Systen2_top = (float)info.safeArea.top;
    Systen2_windowHeight = (float)info.windowHeight;
    Systen2_pixelRatio = (float)info.pixelRatio;
}
 }
 
3.获取code
    public void Init1(Action action = null)
    {
        LoginOption loginOption = new LoginOption();
        loginOption.success = ((e) =>
        {
            jsCode = e.code;

            Debug.Log("****** Login success " + jsCode);
			//SERVERURLConfig.WEWECHATINFO:项目服务器地址 具体的构成形式,属于项目后台处理
			//根据具体情况处理
            string url = SERVERURLConfig.WEWECHATINFO + jsCode;
            StartCoroutine(GetRequest(url));
        });
        loginOption.fail = ((e) =>
        {
            Debug.Log("****** e.errMsg " + e.errMsg);
        });
        WX.Login(loginOption);
    }


    4.根据code获取openid
     IEnumerator GetRequest(string url)
 {
     Debug.Log("***  GetRequest " + url);
     var webRequest = new UnityEngine.Networking.UnityWebRequest(url, "GET");
     webRequest.downloadHandler = (UnityEngine.Networking.DownloadHandler)new UnityEngine.Networking.DownloadHandlerBuffer();
     yield return webRequest.SendWebRequest();
     if (webRequest.isNetworkError || webRequest.isHttpError)
     {
         Debug.Log("***  " + webRequest.error);
         //网络错误 相关处理逻辑
         
     }
     else
     {
         yield return new WaitUntil(() => webRequest.isDone);
         Debug.Log("success:" + webRequest.downloadHandler.text);

         var result = GersonFrame.LitJson.JsonMapper.ToObject<GetOpenIDAndUnionIDDataToken>(webRequest.downloadHandler.text);
         if (result != null && result.code == 200)
         {
             if (result.data != null)
             {
                 Debug.Log("*** 获取成功  " + webRequest.downloadHandler.text);
                 openId = result.data.openid;
                 unionId = result.data.unionid;
                 isInit = true;
                 if (sendShowNum==0)
                 {
                     FunOnShow(0);
                     sendShowNum = 1;
                 }
             }
             //相关处理逻辑
         }
         else
         {
             //网络错误
             // 相关处理逻辑
         }

     }
 }



6.充值
    #region 安卓充值
    public void OnPay(OrderBackData orderBackData)
    {
#if UNITY_WEBGL
        Debug.Log("****** 安卓充值   ");
        RequestMidasPaymentOption requestMidasPayment = new RequestMidasPaymentOption();

        requestMidasPayment.currencyType = orderBackData.currencyType;//币种  CNY	人民币
        requestMidasPayment.mode = "game";//支付的类型,不同的支付类型有各自额外要传的附加参数。  game	购买游戏币
        requestMidasPayment.offerId = orderBackData.offerId;//在米大师侧申请的应用 id
        requestMidasPayment.buyQuantity = orderBackData.buyQuantity;//购买数量。mode=game 时必填。购买数量
        requestMidasPayment.env = System.Convert.ToInt32(orderBackData.env);//环境配置 0:米大师正式环境,1:米大师沙箱环境
        requestMidasPayment.platform = orderBackData.platform;//申请接入时的平台,platform 与应用id有关。 android	android
        requestMidasPayment.zoneId = orderBackData.zoneId;//分区 ID
        requestMidasPayment.outTradeNo = "";

        requestMidasPayment.success = (e) =>
        {
            string GN_SERVER_HEAD = string.Format("https://cwdj.mantianfei.net/gpays/wx_notify_url2?appId={0}&orderNo={1}", orderBackData.appId, orderBackData.orderNo);
            StartCoroutine(GetPayOverInfo(GN_SERVER_HEAD));
        };
        requestMidasPayment.fail = async (e) =>
        {
            //callback(false);
            UIManager.HideView("WaitMsgRespon");
            await UIManager.ShowView("TopTips", true, "支付失败");
        };
        WX.RequestMidasPayment(requestMidasPayment);
#endif
    }
    IEnumerator GetPayOverInfo(string url)
    {
        Uri uri = new Uri(url);
        UnityWebRequest unityWebRequest = UnityWebRequest.Get(uri);
        yield return unityWebRequest.SendWebRequest();
        if (unityWebRequest.isHttpError || unityWebRequest.isNetworkError)
        {
            UIManager.HideView("WaitMsgRespon");
            //callback(false);
            Debug.Log("*** 支付信息返回失败  " + url);
        }
        else
        {
            var result = GersonFrame.LitJson.JsonMapper.ToObject<PayBackInfoData>(unityWebRequest.downloadHandler.text);
            if (result != null && result.code == 200)
            {
                UIManager.HideView("WaitMsgRespon");
                //callback(true);
            }
            else
            {
                UIManager.HideView("WaitMsgRespon");
                //callback(false);
                Debug.Log("*** 支付信息返回失败  " + unityWebRequest.downloadHandler.text);
            }
        }
    }
    #endregion

    #region  ios 支付
    public void FunIosPay(OrderBackData data2, GetOrderInfoConmfig getOrderInfoConmfig)
    {
#if UNITY_WEBGL
        Debug.Log("****** ios充值   ");
        getOrderInfoConmfig.orderNo = data2.orderNo;
        getOrderInfoConmfig.price *= 10;
        Debug.Log("****** 打开客服   " + LitJson.JsonMapper.ToJson(getOrderInfoConmfig));
        string jsonInfo= LitJson.JsonMapper.ToJson(getOrderInfoConmfig);
        OpenWechatServiceConversation(jsonInfo,"点我充值");
#endif
    }


    public void OpenWechatServiceConversation(string fromInfo,string title)
    {
#if UNITY_WEBGL
        WX.OpenCustomerServiceConversation(new OpenCustomerServiceConversationOption
        {
            sessionFrom = fromInfo,//会话来源
            showMessageCard = true,//是否显示会话内消息卡片,设置此参数为 true,用户进入客服会话会在右下角显示"可能要发送的小程序"提示,用户点击后可以快速发送小程序消息
            sendMessageTitle =title ,//会话内消息卡片标题  
            //sendMessageImg: this.imageUrl,

            success = (e) =>
            {
                UIManager.HideView("WaitMsgRespon");
                Debug.Log("****** 打开客服  success " + e.errMsg);
            },
            fail = (e) =>
            {
                UIManager.HideView("WaitMsgRespon");
                Debug.Log("****** 打开客服  fail " + e.errMsg);
            }

            //public string sendMessageImg;//会话内消息卡片图片路径
            //public string sendMessagePath;//会话内消息卡片路径
        });
#endif
    }
    

    #endregion

public class GetOrderInfoConmfig
{
    public string uid;//用户在渠道方登录后返回的id
    public string serverId;//用户所在区服
    public string roleUID;//游戏内部自定义角色唯一id
    public int lv;//游戏id
    public string ItemId;//支付商品id
    public float price;//支付价格
    public string channel;//渠道id  "test" 为测试渠道
    public string appId; //游戏的appid(我方提供)
    public string product_name;
    public string timestamp;
    public string orderNo;
}

public class GetOpenIDAndUnionIDDataToken
{
    public string msg;
    public int code;
    public WechatInfoData data;

}
public class WechatInfoData
{
    public string appId;
    public string platform;
    public string code;
    public string openid;
    public string session_key;
    public string unionid;

}


public class PayBackDataConfig
{
    public string msg;
    public int code;
    public OrderBackData data;
}
public class OrderBackData
{
    public string currencyType;
    public string uid;
    public string orderNo;
    public string appId;
    public string offerId;
    public int buyQuantity;
    public string zoneId;
    public string env;
    public string platform;
}
public class PayBackInfoData
{
    public string msg;
    public int code;
    public string data;
}

public class OnShowClass
{
    public string app_id;
    public string open_id;
    public int event_type;
    public string app_version;
    public string sub_app_id;
    public long ts;
    public string os;
    public int pay_amount;
    public string union_id;
    public string other;
    public string request_id;
}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity粉末状在校生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值