微信小程序登录以及获取微信用户信息

    <!-- 结算 开始 --> 
    <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();   //关闭加载图标
                }
            }
        })
    });
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值