第一次提交
This commit is contained in:
123
uni_modules/uni-pay/js_sdk/appleiap.js
Normal file
123
uni_modules/uni-pay/js_sdk/appleiap.js
Normal file
@@ -0,0 +1,123 @@
|
||||
// uni iap
|
||||
const IapTransactionState = {
|
||||
purchasing: "0", // A transaction that is being processed by the App Store.
|
||||
purchased: "1", // A successfully processed transaction.
|
||||
failed: "2", // A failed transaction.
|
||||
restored: "3", // A transaction that restores content previously purchased by the user.
|
||||
deferred: "4" // A transaction that is in the queue, but its final status is pending external action such as Ask to Buy.
|
||||
};
|
||||
|
||||
class Iap {
|
||||
|
||||
constructor(data={}) {
|
||||
this._productIds = data.products || [];
|
||||
this._channel = null;
|
||||
this._channelError = null;
|
||||
this.ready = false;
|
||||
}
|
||||
|
||||
init() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.getChannels((channel) => {
|
||||
this.ready = true;
|
||||
resolve(channel);
|
||||
}, (err) => {
|
||||
reject(err);
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
getProduct(productIds) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._channel.requestProduct(productIds || this._productIds, (res) => {
|
||||
resolve(res);
|
||||
}, (err) => {
|
||||
reject(err);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
requestPayment(orderInfo) {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.requestPayment({
|
||||
provider: "appleiap",
|
||||
orderInfo: {
|
||||
quantity: 1,
|
||||
manualFinishTransaction: true,
|
||||
...orderInfo
|
||||
},
|
||||
success: (res) => {
|
||||
resolve(res);
|
||||
},
|
||||
fail: (err) => {
|
||||
//console.log('requestPayment-err: ', err)
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
restoreCompletedTransactions(username) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._channel.restoreCompletedTransactions({
|
||||
manualFinishTransaction: true,
|
||||
username
|
||||
}, (res) => {
|
||||
resolve(res);
|
||||
}, (err) => {
|
||||
console.log('restoreCompletedTransactions-err: ', err)
|
||||
reject(err);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
finishTransaction(transaction) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._channel.finishTransaction(transaction, (res) => {
|
||||
resolve(res);
|
||||
}, (err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getChannels(success, fail) {
|
||||
if (this._channel !== null) {
|
||||
success(this._channel)
|
||||
return
|
||||
}
|
||||
|
||||
if (this._channelError !== null) {
|
||||
fail(this._channelError)
|
||||
return
|
||||
}
|
||||
|
||||
uni.getProvider({
|
||||
service: 'payment',
|
||||
success: (res) => {
|
||||
this._channel = res.providers.find((channel) => {
|
||||
return (channel.id === 'appleiap')
|
||||
})
|
||||
|
||||
if (this._channel) {
|
||||
success(this._channel)
|
||||
} else {
|
||||
this._channelError = {
|
||||
errMsg: 'paymentContext:fail iap service not found'
|
||||
}
|
||||
fail(this._channelError)
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
get channel() {
|
||||
return this._channel;
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
Iap,
|
||||
IapTransactionState
|
||||
};
|
||||
158
uni_modules/uni-pay/js_sdk/js_sdk.js
Normal file
158
uni_modules/uni-pay/js_sdk/js_sdk.js
Normal file
@@ -0,0 +1,158 @@
|
||||
var util = {};
|
||||
/*
|
||||
* 此方法不支持微信公众号
|
||||
util.getWeixinCode().then((code) => {
|
||||
|
||||
});
|
||||
*/
|
||||
util.getWeixinCode = function() {
|
||||
return new Promise((resolve, reject) => {
|
||||
// #ifdef MP-WEIXIN
|
||||
uni.login({
|
||||
provider: 'weixin',
|
||||
success(res) {
|
||||
resolve(res.code)
|
||||
},
|
||||
fail(err) {
|
||||
reject(new Error('获取微信code失败'))
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
// #ifdef APP-PLUS
|
||||
plus.oauth.getServices((services) => {
|
||||
let weixinAuthService = services.find((service) => {
|
||||
return service.id === 'weixin';
|
||||
});
|
||||
if (weixinAuthService) {
|
||||
weixinAuthService.authorize(function(res) {
|
||||
resolve(res.code);
|
||||
}, function(err) {
|
||||
console.log(err);
|
||||
reject(new Error('获取微信code失败'));
|
||||
});
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
})
|
||||
};
|
||||
|
||||
util.getAlipayCode = function() {
|
||||
// #ifdef APP-PLUS || MP-ALIPAY
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.login({
|
||||
provider: 'alipay',
|
||||
success(res) {
|
||||
resolve(res.code);
|
||||
},
|
||||
fail(err) {
|
||||
reject(new Error('获取支付宝code失败,可能是没有关联appid或你的支付宝开发者工具还没有登录'));
|
||||
}
|
||||
});
|
||||
});
|
||||
// #endif
|
||||
};
|
||||
|
||||
util.checkPlatform = function() {
|
||||
// #ifdef H5
|
||||
let system = {
|
||||
win: false,
|
||||
mac: false,
|
||||
xll: false
|
||||
};
|
||||
let p = navigator.platform;
|
||||
system.win = p.indexOf("Win") == 0;
|
||||
system.mac = p.indexOf("Mac") == 0;
|
||||
system.x11 = p == "X11" || p.indexOf("Linux") == 0;
|
||||
if (system.win || system.mac || system.xll) {
|
||||
let ua = navigator.userAgent.toLowerCase();
|
||||
if (ua.indexOf("micromessenger") > -1) {
|
||||
// 微信开发者工具下访问(注意微信开发者工具下无法唤起微信公众号支付)
|
||||
return "pc-weixin";
|
||||
} else {
|
||||
return "pc";
|
||||
}
|
||||
} else {
|
||||
if (p.indexOf("iPhone") > -1 || p.indexOf("iPad") > -1) {
|
||||
return "ios";
|
||||
} else {
|
||||
return "android";
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取当前H5所在的环境
|
||||
*/
|
||||
util.getH5Env = function() {
|
||||
let ua = window.navigator.userAgent.toLowerCase();
|
||||
if (ua.match(/MicroMessenger/i) == 'micromessenger' && (ua.match(/miniprogram/i) == 'miniprogram')) {
|
||||
// 微信小程序
|
||||
return "mp-weixin";
|
||||
}
|
||||
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
|
||||
// 微信公众号
|
||||
return "h5-weixin";
|
||||
}
|
||||
if (ua.match(/alipay/i) == 'alipay' && ua.match(/miniprogram/i) == 'miniprogram') {
|
||||
return "mp-alipay";
|
||||
}
|
||||
if (ua.match(/alipay/i) == 'alipay') {
|
||||
return "h5-alipay";
|
||||
}
|
||||
// 外部 H5
|
||||
return "h5";
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 日期格式化
|
||||
* @params {Date || Number} date 需要格式化的时间
|
||||
* timeFormat(new Date(),"yyyy-MM-dd hh:mm:ss");
|
||||
*/
|
||||
util.timeFormat = function(time, fmt = 'yyyy-MM-dd hh:mm:ss', targetTimezone = 8) {
|
||||
try {
|
||||
if (!time) {
|
||||
return "";
|
||||
}
|
||||
if (typeof time === "string" && !isNaN(time)) time = Number(time);
|
||||
// 其他更多是格式化有如下:
|
||||
// yyyy-MM-dd hh:mm:ss|yyyy年MM月dd日 hh时MM分等,可自定义组合
|
||||
let date;
|
||||
if (typeof time === "number") {
|
||||
if (time.toString().length == 10) time *= 1000;
|
||||
date = new Date(time);
|
||||
} else {
|
||||
date = time;
|
||||
}
|
||||
|
||||
const dif = date.getTimezoneOffset();
|
||||
const timeDif = dif * 60 * 1000 + (targetTimezone * 60 * 60 * 1000);
|
||||
const east8time = date.getTime() + timeDif;
|
||||
|
||||
date = new Date(east8time);
|
||||
let opt = {
|
||||
"M+": date.getMonth() + 1, //月份
|
||||
"d+": date.getDate(), //日
|
||||
"h+": date.getHours(), //小时
|
||||
"m+": date.getMinutes(), //分
|
||||
"s+": date.getSeconds(), //秒
|
||||
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
|
||||
"S": date.getMilliseconds() //毫秒
|
||||
};
|
||||
if (/(y+)/.test(fmt)) {
|
||||
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
||||
}
|
||||
for (let k in opt) {
|
||||
if (new RegExp("(" + k + ")").test(fmt)) {
|
||||
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (opt[k]) : (("00" + opt[k]).substr(("" + opt[k]).length)));
|
||||
}
|
||||
}
|
||||
return fmt;
|
||||
} catch (err) {
|
||||
// 若格式错误,则原值显示
|
||||
return time;
|
||||
}
|
||||
};
|
||||
|
||||
export default util;
|
||||
Reference in New Issue
Block a user