443 lines
16 KiB
TypeScript
443 lines
16 KiB
TypeScript
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("请求结果:", res);
|
|
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(id, count, price, callBack) {
|
|
console.log("请求uid:" + Utils.uid, "请求id:" + id, "请求数量:" + count, "请求价格:" + price);
|
|
|
|
Utils.POST("wx/orderPaySig", { uid: Utils.uid, itemid: id, itemCount: count, itemPrice: price }, 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(null);
|
|
}
|
|
})
|
|
}
|
|
|
|
});
|
|
|
|
|
|
}
|
|
static getPayInfo(callBack) {
|
|
const delays = [1000, 10000, 30000, 60000, 60000]; // 延迟时间数组
|
|
let attempt = 0; // 轮询次数
|
|
|
|
const poll = () => {
|
|
if (attempt >= delays.length) {
|
|
callBack({ code: 0, data: { pay_state: -1 }, message: '轮询超时' });
|
|
return;
|
|
}
|
|
|
|
console.log("请求uid:", Utils.uid);
|
|
console.log("outTradeNo:", this.outTradeNo);
|
|
Utils.POST("wx/getPayInfo", { uid: Utils.uid, outTradeNo: Utils.outTradeNo }, res => {
|
|
console.log("查询字符结果:", res);
|
|
if (res.code === 1 && (res.data.pay_state === 1 || res.data.pay_state === 2)) {
|
|
callBack(res);
|
|
} else {
|
|
attempt++;
|
|
setTimeout(poll, delays[attempt - 1]);
|
|
}
|
|
});
|
|
};
|
|
|
|
poll();
|
|
}
|
|
|
|
static setPayInfo(callBack) {
|
|
const delays = [1000, 5000, 10000, 20000, 30000]; // 延迟时间数组
|
|
let attempt = 0; // 重试次数
|
|
const sendRequest = () => {
|
|
if (attempt > delays.length) {
|
|
// 达到最大重试次数,调用回调并返回错误信息
|
|
callBack({ code: 0, message: '请求失败,已达到最大重试次数' });
|
|
return;
|
|
}
|
|
Utils.POST("wx/getOrderReward", { outTradeNo: Utils.outTradeNo }, res => {
|
|
console.log("告知服务器发货:", res);
|
|
if (res.code === 1) {
|
|
// 请求成功,调用回调并返回结果
|
|
callBack(res);
|
|
} else {
|
|
// 请求失败,增加重试次数并设置下一次请求的延迟
|
|
attempt++;
|
|
if (attempt <= delays.length) {
|
|
setTimeout(sendRequest, delays[attempt - 1]);
|
|
} else {
|
|
callBack({ code: 0, message: '请求失败,已达到最大重试次数' });
|
|
}
|
|
}
|
|
});
|
|
};
|
|
sendRequest();
|
|
}
|
|
|
|
|
|
//#region ios支付
|
|
|
|
/**跳转客服*/
|
|
static GoKEFu() {
|
|
cc.fx.GameConfig.GM_INFO.iosOutTradeNo = null;
|
|
cc.fx.GameConfig.GM_INFO.iosOutTradeNo = `wcx_` + Math.round(Math.random() * 10 ** 13) + Date.now();
|
|
const data = {
|
|
tpye: "ios",
|
|
outTradeNo: cc.fx.GameConfig.GM_INFO.iosOutTradeNo,
|
|
propName: '金币',
|
|
count: 1,
|
|
price: 100,//价格单位是分
|
|
}
|
|
//@ts-ignore
|
|
wx.openCustomerServiceConversation({
|
|
sessionFrom: JSON.stringify(data), // 会话来源(可选)
|
|
showMessageCard: false, // 是否展示消息卡片
|
|
success() {
|
|
console.log('客服会话已打开');
|
|
}
|
|
});
|
|
}
|
|
static getIosPayInfo(callBack) {
|
|
const delays = [1000, 10000, 30000, 60000, 60000]; // 延迟时间数组
|
|
let attempt = 0; // 轮询次数
|
|
|
|
const poll = () => {
|
|
if (attempt >= delays.length) {
|
|
callBack({ code: 0, data: null, message: '轮询超时' });
|
|
return;
|
|
}
|
|
|
|
console.log("请求uid:" + Utils.uid);
|
|
console.log("outTradeNo:" + cc.fx.GameConfig.GM_INFO.iosOutTradeNo);
|
|
Utils.POST("wx/iosgetPayInfo", { outTradeNo: cc.fx.GameConfig.GM_INFO.iosOutTradeNo }, res => {
|
|
console.log("查询字符结果IOS");
|
|
console.log(res);
|
|
if (res.code === 1) {
|
|
callBack(res);
|
|
cc.fx.GameConfig.GM_INFO.iosOutTradeNo = null;
|
|
} else if (res.code === 0) {
|
|
callBack(res);
|
|
cc.fx.GameConfig.GM_INFO.iosOutTradeNo = null;
|
|
}
|
|
else {
|
|
attempt++;
|
|
setTimeout(poll, delays[attempt - 1]);
|
|
}
|
|
});
|
|
};
|
|
|
|
poll();
|
|
}
|
|
|
|
|
|
|
|
//#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;
|
|
}
|
|
}
|