From d9f8fef12efc5a9795c30b506cdc334f6407da15 Mon Sep 17 00:00:00 2001 From: "YZ\\249929363" <249929363@qq.com> Date: Wed, 2 Jul 2025 15:43:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=80=E7=82=B9=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/Scene/GameScene.fire | 4 +- assets/Script/GameManager.ts | 4 + assets/Script/JiaZai.ts | 51 +--- assets/Script/module/Pay.meta | 13 + assets/Script/module/Pay/Utils.ts | 367 +++++++++++++++++++++++++ assets/Script/module/Pay/Utils.ts.meta | 10 + 6 files changed, 404 insertions(+), 45 deletions(-) create mode 100644 assets/Script/module/Pay.meta create mode 100644 assets/Script/module/Pay/Utils.ts create mode 100644 assets/Script/module/Pay/Utils.ts.meta diff --git a/assets/Scene/GameScene.fire b/assets/Scene/GameScene.fire index bb229a2..3fa4d32 100644 --- a/assets/Scene/GameScene.fire +++ b/assets/Scene/GameScene.fire @@ -9901,7 +9901,7 @@ "__id__": 335 } ], - "_active": false, + "_active": true, "_components": [ { "__id__": 356 @@ -10219,7 +10219,7 @@ "__id__": 285 } ], - "_active": true, + "_active": false, "_components": [], "_prefab": null, "_opacity": 255, diff --git a/assets/Script/GameManager.ts b/assets/Script/GameManager.ts index 994811f..0e62fd1 100644 --- a/assets/Script/GameManager.ts +++ b/assets/Script/GameManager.ts @@ -5,6 +5,7 @@ // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html +import Utils from "./module/Pay/Utils"; import { MiniGameManager } from "./Sdk/MiniGameManager"; import { MiniGameSdk } from "./Sdk/MiniGameSdk"; @@ -211,6 +212,9 @@ export default class GameManager extends cc.Component { //@ts-ignore if (typeof wx !== 'undefined' && wx !== null) { MiniGameSdk.API.shushu_Init(); + Utils.getUserInfo((data)=>{ + console.log("登陆成功",data); + }); let levelInfo = cc.fx.StorageMessage.getStorage("level"); //旧的读取数据设置数据方法,以强联网为主 if (levelInfo == undefined || levelInfo == "" || levelInfo == null) { diff --git a/assets/Script/JiaZai.ts b/assets/Script/JiaZai.ts index 8b290d1..c5db0b2 100644 --- a/assets/Script/JiaZai.ts +++ b/assets/Script/JiaZai.ts @@ -9,6 +9,7 @@ import GameManager from "./GameManager"; import NumberToImage from "./NumberToImage"; import { MiniGameSdk } from "./Sdk/MiniGameSdk"; import { LQCollideSystem } from "./lq_collide_system/lq_collide_system"; +import Utils from "./module/Pay/Utils"; const { ccclass, property } = cc._decorator; @ccclass @@ -211,50 +212,14 @@ export default class NewClass extends cc.Component { clickShop() { // 假设已经获取到了 userId 和 productId - const userId = cc.fx.GameConfig.GM_INFO.openid; - const productId = '1'; - console.log("即将创建createorder"); - //@ts-ignore - wx.cloud.callFunction({ - name: "createOrder", - data: { - "userId": userId, - "productId": productId, - }, - success: (res) => { - // 取得云函数返回的订单信息 - console.log("createOrder结果:", res.result); - const payment = res.result.paymentResult.payment; - console.log("payment:", payment); - // 调起微信客户端支付 - //@ts-ignore - // 调起微信客户端支付 - //@ts-ignore - wx.requestPayment({ - // 时间戳,从 1970 年 1 月 1 日 00:00:00 至今的秒数,即当前的时间 - timeStamp: payment.timeStamp, - // 随机字符串,长度为32个字符以下 - nonceStr: payment.nonceStr, - // 统一下单接口返回的 prepay_id 参数值 - package: payment.package, - // 签名算法,暂支持 MD5、HMAC-SHA256 - signType: payment.signType || 'MD5', - // 支付签名 - paySign: payment.paySign, - success(res) { - /* 成功回调 */ - console.log("支付成功", res); - }, - fail(res) { - /* 失败回调 */ - console.log("支付失败", res); - }, - complete(res) { - console.log("支付完成", res); - } - }); - }, + Utils.buyProp((res)=>{ + console.log("获得充值结果",res); + Utils.getPayInfo((data) =>{ + console.log("获得轮训结果:",data); + console.log(data.data.pay_state) + }) }); + } diff --git a/assets/Script/module/Pay.meta b/assets/Script/module/Pay.meta new file mode 100644 index 0000000..de1b76d --- /dev/null +++ b/assets/Script/module/Pay.meta @@ -0,0 +1,13 @@ +{ + "ver": "1.1.3", + "uuid": "6fcc6549-a36e-45d2-a845-79719c1be068", + "importer": "folder", + "isBundle": false, + "bundleName": "", + "priority": 1, + "compressionType": {}, + "optimizeHotUpdate": {}, + "inlineSpriteFrames": {}, + "isRemoteBundle": {}, + "subMetas": {} +} \ No newline at end of file diff --git a/assets/Script/module/Pay/Utils.ts b/assets/Script/module/Pay/Utils.ts new file mode 100644 index 0000000..1a7a2f0 --- /dev/null +++ b/assets/Script/module/Pay/Utils.ts @@ -0,0 +1,367 @@ +export default class Utils { + static uid:string=""; + static session_key=""; + static appid:string=""; + static openid:string=""; + static outTradeNo:string =""; + static httpip:string=`https://laf.nika4games.com/`; +//#region 支付 + /**获取用户信息*/ + static getUserInfo(callBack){ + wx.login({ + success (res) { + console.log("登录成功"); + console.log(res); + if (res.code) { + Utils.POST("login",{code:res.code},ret=>{ + console.log("请求结果:"); + console.log(ret); + Utils.openid=ret.data.openid; + Utils.session_key=ret.data.session_key; + Utils.uid=ret.data._id; + callBack(Utils.uid); + }) + } else { + console.log('登录失败!' + res.errMsg) + } + } + }) + } + + /**道具直购接口*/ + static buyProp(callBack){ + Utils.POST("wx/orderPaySig",{uid:Utils.uid,itemid:"10011",itemCount:1,itemPrice:100},res=>{ + if(res.code==1){ + Utils.outTradeNo=res.data.outTradeNo; + //@ts-ignore + wx.requestMidasPaymentGameItem({ + signData:res.data.signData, + paySig: res.data.paySig, + signature: res.data.signature, + success(res, errCode) { + console.log('成功', res, errCode); + callBack(res); + }, + fail({errMsg,errCode}) { + console.error('失败'); + console.error(errMsg, errCode) + callBack(res); + } + }) + } + + }); + + + } + static getPayInfo(callBack){ + console.log("请求uid:"+Utils.uid); + console.log("outTradeNo:"+this.outTradeNo); + Utils.POST("wx/getPayInfo",{uid:Utils.uid,outTradeNo:Utils.outTradeNo},res=>{ + console.log("查询字符结果"); + console.log(res); + if(res.code==1){ + if(res.data.pay_state==1){ + console.log("充值取消") + callBack(res); + }else{ + console.log("充值成功") + callBack(res); + + } + } + }); + + } + + /**跳转客服*/ + static GoKEFu(){ + // 打开客服会话 + //wx.navigateTo({ url: '/pages/webview?url=https://xxx.com/customer' }); + //@ts-ignore + wx.openCustomerServiceConversation({ + sessionFrom: '你好这是充值中心', // 会话来源(可选) + showMessageCard: true, // 是否展示消息卡片 + sendMessageTitle:"扫描二维码进行充值", + sendMessageImg:"https://img2.huashi6.com/images/resource/thumbnail/2023/07/23/193722_24254930385.jpg?imageMogr2/quality/100/interlace/1/thumbnail/2000x%3E", + success() { + console.log('客服会话已打开'); + } + }); + } + + + +//#endregion + +//#region 微信云 + static initServer(){ + if(cc.sys.platform!=cc.sys.WECHAT_GAME)return; + //@ts-ignore + wx.cloud.init({ + env: 'cloudbase-1gl7iex89268f11e' + }) + } + + static getUserCode(){ + //@ts-ignore + wx.login({ + success (res) { + console.log("登录成功"); + console.log(res); + if (res.code) { + //@ts-ignore + wx.cloud.callFunction({ + // 云函数名称 + name: 'login', + // 传给云函数的参数 + data: { + code:res.code, + }, + success: function(ret) { + console.log("请求成功"); + console.log("请求结果:"); + console.log(ret); + Utils.openid=ret.data.openid; + Utils.session_key=ret.data.session_key; + Utils.uid=ret.data._id; + }, + fail:function(res){ + console.log("请求失败"); + console.log(res); + } + }) + } else { + console.log('登录失败!' + res.errMsg) + } + } + }) + } + + static PayOder(){ + if(cc.sys.platform!=cc.sys.WECHAT_GAME)return; + //@ts-ignore + wx.cloud.callFunction({ + // 云函数名称 + name: 'userPayOrder', + // 传给云函数的参数 + data: { + itemid:"10011", + itemCount:1, + itemPrice:100 + }, + success: function(res) { + console.log("请求成功"); + console.log(res) + if(res.result.code==1){ + Utils.outTradeNo=res.result.data.outTradeNo; + console.log("订单号:"+Utils.outTradeNo); + //@ts-ignore + wx.requestMidasPaymentGameItem({ + signData:res.result.data.signData, + paySig: res.result.data.paySig, + signature: res.result.data.signature, + success(res, errCode) { + console.log('成功', res, errCode); + }, + fail({errMsg,errCode}) { + console.error('失败'); + console.error(errMsg, errCode) + } + }) + } + + }, + fail:function(res){ + console.log("请求失败"); + console.log(res); + } + }) + } + + static getPayInfo1(callBack){ + if(cc.sys.platform!=cc.sys.WECHAT_GAME)return; + //@ts-ignore + wx.cloud.callFunction({ + // 云函数名称 + name: 'userGetPayState', + // 传给云函数的参数 + data: { + outTradeNo:Utils.outTradeNo + }, + success: function(res) { + console.log("请求成功"); + console.log(res) + callBack("成功"); + }, + fail:function(res){ + console.log("请求失败"); + console.log(res); + callBack("失败"); + } + }) + } + + static toke(){ + if(cc.sys.platform!=cc.sys.WECHAT_GAME)return; + //@ts-ignore + wx.cloud.callFunction({ + // 云函数名称 + name: 'uerToken', + // 传给云函数的参数 + data: { + + }, + success: function(res) { + console.log("请求成功"); + console.log(res) + }, + fail:function(res){ + console.log("请求失败"); + console.log(res); + } + }) + } +//#endregion + + + /** + * POST请求 + * + * @static + * @param {*} url + * @param {object} [param={}] + * @param {*} callback + * @memberof HttpUtil + */ + public static POST(url, param:object|any,callback) { + var xhr = cc.loader.getXMLHttpRequest(); + let dataStr = ''; + Object.keys(param).forEach(key => { + dataStr += key + '=' + encodeURIComponent(param[key]) + '&'; + }) + if (dataStr !== '') { + dataStr = dataStr.substr(0, dataStr.lastIndexOf('&')); + } + url=this.httpip+url; + console.log("请求地址:"+url); + xhr.open("POST", url, true); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhr.onreadystatechange = function () { + if (xhr.readyState === 4) { + let response:any = xhr.responseText; + if (xhr.status >= 200 && xhr.status < 300) { + let httpStatus = xhr.statusText; + // callback(true, JSON.parse(response)); + try{ + response=JSON.parse(response); + }catch{ + response={data:"数据有误",code:0}; + } + callback(response); + + } else { + callback({data:"网络请求失败,请检查网络连接",code:0}); + } + } + }; + xhr.send(dataStr); + } + + static http_sendRequest(path,data,handler,extraUrl=null){ + let xhr=cc.loader.getXMLHttpRequest(); + xhr.timeout=5000;//超时时间 + if(data==null){ + data={}; + } + // if(exports.token){//数据中加入安全密匙 + // data.token=exports.token; + // } + if(extraUrl==null){ + //@ts-ignore + if(Utils.isDebug){ + extraUrl="http://localhost:9003"; + }else{ + extraUrl=this.httpip; + } + + } + //解析请求路由以及格式化请求参数 + let sendtext="?"; + for(let k in data){ + if(sendtext !="?"){ + sendtext +="&"; + } + sendtext +=(k +'='+data[k]); + } + //组装完整的URL + let requestURL=extraUrl+"/"+path+encodeURI(sendtext); + // console.log("发送请求:"); + // console.log(requestURL); + //发送请求 Get形式发送请求 + xhr.open("GET",requestURL,true); + if (cc.sys.isNative) {//如果是手机就设置请求头 + xhr.setRequestHeader("Accept-Encoding", "gzip,deflate"); + } + //计时判断是否超时 + let hasRetried=false; + let timer=setTimeout(function(){ + //xhr.hasRetried=true; + hasRetried=true; + xhr.abort(); + console.log("http timeOut......"); + retryFun(); + },5000); + //重新发送请求 + let retryFun=function(){ + Utils.http_sendRequest(path,data,handler,extraUrl); + } + //监听反馈 + xhr.onreadystatechange=function(){ + clearTimeout(timer); + if(xhr.readyState===4&&(xhr.status >= 200 && xhr.status < 300)){ + let ret=null; + let respText=xhr.responseText; + try{ + ret=JSON.parse(respText); + }catch(e){ + console.log("http error:" +e); + ret={ + code:1, + msg:e + } + } + if(handler){ + handler(ret); + } + handler=null; + }else if(xhr.readyState===4){ + // if(xhr.hasRetried){ + // return; + // } + if(hasRetried){ + return; + } + console.log('other readystate =='+xhr.readyState + ', status:' + xhr.status); + if(xhr.readyState==4&&xhr.status==0){ + handler({ + err:1, + msg:"网络连接失败,请稍后再试" + }); + return; + } + setTimeout(function(){ + retryFun(); + },5000); + }else{ + //console.log('other readystate:' + xhr.readyState + ', status:' + xhr.status); + } + } + try{ + xhr.send(); + }catch(e){ + retryFun(); + } + return xhr; + } +} diff --git a/assets/Script/module/Pay/Utils.ts.meta b/assets/Script/module/Pay/Utils.ts.meta new file mode 100644 index 0000000..81d471f --- /dev/null +++ b/assets/Script/module/Pay/Utils.ts.meta @@ -0,0 +1,10 @@ +{ + "ver": "1.1.0", + "uuid": "974132c6-ae8f-4a31-95c8-d525a2310abb", + "importer": "typescript", + "isPlugin": false, + "loadPluginInWeb": true, + "loadPluginInNative": true, + "loadPluginInEditor": false, + "subMetas": {} +} \ No newline at end of file