<!-- 结算 开始 -->
<view class="order_pay_wrap" bindtap="handleOrderPay">
去付款
</view>
//导入request请求工具类
import {
getBaseUrl,
getWxLogin,
getUserProfile,
requestUtil
} from '../../utils/requestUtil';
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
/**
* 页面的初始数据
*/
data: {
address:{},
baseUrl:'',
cart:[],
totalPrice:0,
totalNum:0
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
const baseUrl = getBaseUrl();
this.setData({
// swiperList:result.message,
baseUrl
})
},
//处理订单支付
async handleOrderPay(){
// wx.login({
// timeout:5000,
// success: (res) => {
// console.log(res.code)
// },
// })
// let res=await getWxLogin();
// console.log(res.code)
// wx.getUserProfile({
// desc:'获取用户信息',
// success:(res)=>{
// console.log(res.userInfo.nickName,res.userInfo.avatarUrl)
// }
// })
// let res2 = await getUserProfile();
// console.log(res2.userInfo.nickName,res2.userInfo.avatarUrl)
Promise.all([getWxLogin(),getUserProfile()]).then((res)=>{
console.log(res[0].code);
console.log(res[1].userInfo.nickName,res[1].userInfo.avatarUrl)
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function() {
console.log("show");
const address=wx.getStorageSync('address');
let cart=wx.getStorageSync('cart')||[];
cart=cart.filter(v=>v.checked);
let totalPrice=0;
let totalNum=0;
cart.forEach(v=>{
totalPrice+=v.price*v.num;
totalNum+=v.num;
})
this.setData({
cart,
totalNum,
address,
totalPrice
})
//cart设置到缓存中
wx.setStorageSync('cart', cart);
},
})
//定义请求根路径baseUrl
const baseUrl="http://localhost:8080";
//同时并发请求的次数
let ajaxTimes=0;
/**
* 返回请求根路径baseUrl
*
*/
export const getBaseUrl=()=>{
return baseUrl;
}
/**
* wx login封装
*
*/
export const getWxLogin=()=>{
return new Promise((resolve,reject)=>{
wx.login({
timeout:5000,
success: (res) => {
resolve(res)
},
fail:(err)=>{
reject(err)
}
})
});
}
/**
* wx getUserProfile封装
*
*/
export const getUserProfile=()=>{
return new Promise((resolve,reject)=>{
wx.getUserProfile({
desc:'获取用户信息',
success: (res) => {
resolve(res)
},
fail:(err)=>{
reject(err)
}
})
});
}
/**
*
* @param {后端请求工具类} params
*/
export const requestUtil=(params)=>{
var start=new Date().getTime();
console.log(start)
ajaxTimes++;
wx.showLoading({
title: '加载中...',
mask:true
})
//模拟网络延迟加载...
while(true){
if(new Date().getTime()-start>1*1000)break;
}
return new Promise((resolve,reject)=>{
wx.request({
...params,
url:baseUrl+params.url,
success:(result)=>{
resolve(result.data)
},
fail:(err)=>{
reject(err)
},
complete:()=>{
ajaxTimes--;
if(ajaxTimes==0){
wx.hideLoading(); //关闭加载图标
}
}
})
});
}