H5部分

后端负责与第三方支付平台交互,提交表单生成订单等等。
前端只需要打开链接或者用手机扫码即可,意料之外的简单。

以下是前端部分代码:

using EYFW.Extend;
using Game.Data;
using Hotfix.Module;
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ZXing;
using ZXing.Common;

namespace Hotfix.UI
{
    public partial class UIRechargeQRCode : UIBase
    {
        private LoginModule _loginModule;

        /// <summary>
        /// 初始化调用一次,用于一次性注册事件,或者初始化数据
        /// </summary>
        protected override void OnInit()
        {
            _loginModule = BindModule<LoginModule>();

            Btn_Complete.AddListener(() =>
            {
                _loginModule.OnGetNetMemberInfo(new ILAction(() =>
                {
                   CloseSelf();
                    //发送消息给需要刷新的界面 todo

                }),new ILAction<int>((code)=> {
                    CloseSelf();
                    OpenUI<UITips>(UILevel.PopUp2, "我们暂时无法确认您的支付结果,建议您等待3分钟后再查看充值结果。");
                }));
            });
            Btn_Question.AddListener(() =>
            {
                    //支付遇到问题?
            });
            Btn_Close.AddListener(CloseSelf);
        }

        /// <summary>
        /// 每次打开都会调用一次
        /// </summary>
        protected override void OnOpen(object args1, object args2, object args3)
        {
            var jsonData = args1 as JsonData;
            //1 = ZFB ,2 = WeChat
            E_PayType type = (E_PayType)args2;
            var url = jsonData["data"]["payUrl"].ToString();

#if UNITY_ANDROID
            //安卓下 直接使用SDK调起ZFB

#elif UNITY_WEBGL
            //网页下 根据平台选择
            if (Application.isMobilePlatform)
            {
                //打开网页
                Txt_Tips.text = type == E_PayType.AliPay ? "请您在支付宝页面里完成支付" : "请您在微信页面里完成支付";
                Application.OpenURL(url);
            }
            else
            {
                //手机扫码
                Txt_Tips.text = type == E_PayType.AliPay ? "请打开支付宝[扫一扫]进行支付" : "请打开微信[扫一扫]进行支付";
                Raw_QRCode.texture = GenerateQRCode(url, 256, 256);
            }
#endif
        }

        /// <summary>
        /// 使用ZXing生成二维码
        /// </summary>
        /// <param name="content"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        private Texture2D GenerateQRCode(string content, int width, int height)
        {
            MultiFormatWriter writer = new MultiFormatWriter();
            Dictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();
            hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            //转换成Texture2D
            int w = bitMatrix.Width;
            int h = bitMatrix.Height;
            Texture2D texture = new Texture2D(w, h);
            //更新像素点
            for (int x = 0; x < h; x++)
            {
                for (int y = 0; y < w; y++)
                {
                    if (bitMatrix[x, y])
                    {
                        texture.SetPixel(y, x, Color.black);
                    }
                    else
                    {
                        texture.SetPixel(y, x, Color.white);
                    }
                }
            }
            texture.Apply();
            return texture;
        }

        protected override void OnShow() { }
        protected override void OnHide() { }
        protected override void OnBeforeClose() { }
    }
}

扩展:安卓部分

最后修改:2023 年 06 月 15 日
如果觉得我的文章对你有用,请随意赞赏