export function getRecommend() {
let recommend = uni.getStorageSync('recommend');
return recommend;
}
如果你需要用来存储项目内使用的第三方api key,可以创建keys.js文件:
let txMapKey = 'xxxx' // 腾讯地图的key
export {
txMapKey
}
封装必不可少的支付功能:
创建文件pay.js,代码如下:
微信公众号支付
// description:微信公众号支付
// data 微信支付请求成功之后返回的支付参数
// payParams {object} 微信支付需要的参数
function onBridgeReady(data, payParams) {
window.WeixinJSBridge.invoke('getBrandWCPayRequest', {
appId: data.appId,
timeStamp: data.timeStamp,
nonceStr: data.nonceStr,
package: data.package,
signType: data.signType,
paySign: data.paySign // 微信签名
}, function(res) {
// console.log(res);
if(res.err_msg === 'get_brand_wcpay_request:ok') {
// 支付成功页面
if(payParams.type === 'SHYY') {
toastMsg('correct','支付成功');
setTimeout(()=>{
router.push('/home');
},500);
}else{
router.push({
path: '/success',
query: {
orderId: payParams.orderId,
totalFree: payParams.totalFree
}
});
}
}else if(res.err_msg === 'get_brand_wcpay_request:fail') {
// 支付失败
return alert(res.err_desc);
}else{
// 跳转订单详情
toastMsg('warn', '支付取消');
}
});
}
支付宝支付
/**
支付宝支付
*/
function alipay(res) {
console.log('调起支付alipay',res);
uni.requestPayment({
provider: 'alipay',
orderInfo: {
"dealId": res.dealId,
"appKey": res.appKey,
"totalAmount": res.totalAmount,
"tpOrderId": res.tpOrderId,
"dealTitle": res.dealTitle,
"rsaSign": res.rsaSign,
"bizInfo": res.bizInfo
},
//订单数据
success: function(res) {
console.log('success:' + JSON.stringify(res));
},
fail: function(err) {
console.log('fail:' + JSON.stringify(err));
}
});
}
微信小程序支付
function wxpay(ret, orderid) {
console.log('调起支付wxpay', ret);
let res = ret;
let status = false;
uni.requestPayment({
timeStamp: res.timeStamp,
nonceStr: res.nonceStr,
package: res.package,
signType: res.signType,
paySign: res.paySign,
appId: res.appId,
success: function (res) {
console.log('success:' + JSON.stringify(res), res);
uni.showToast({
title: '支付成功!'
});
},
fail: function (err) {
console.log('fail:' + JSON.stringify(err));
}
});
}
封装的异步请求处理函数代码封装,创建文件request.js
request('接口名称', {key:value}, '请求方式(默认为GET)').then(res=>{console.log(res)})
import { getToken, removeToken } from './auth';
let baseUrl = '';
async function request(method,params,type,callBack){
// 创建一个名为request请求的方法函数
if (!type) {
type = 'GET';
}
let header = {
//设置请求头信息
'Authorization': getToken(),
'X-Requested-With': 'XMLHttpRequest',
"Accept": "application/json",
"Content-Type": "application/json; charset=UTF-8"
};
let http = {
url: baseUrl + mehtod,
data: params,
method: type,
header: header
};
let promise = new Promise((resolve, reject) => {
uni.request(http).then(res => {
let newdata = res[1].data; // if (newdata.code == 403) {
if (newdata == -1) {
uni.showToast({
title: res[1].data.msg,
icon: 'none'
});
}
resolve(res[1].data);
}).catch(err => {
reject(err);
console.log(err);
});
});
return promise;
}
export default {
request
};
创建util.js文件:(时间工具)
const formatTime = date => {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
const second = date.getSeconds();
return [year, month, day].map(formatNumber).join('http://www.likecs.com/') + ' ' + [hour, minute, second].map(formatNumber).join('