登录页的业务逻辑_学习记录

//logs.js
const util = require('../../utils/util.js')
//引入鉴权工具方法
const { register, checkHasLogin, userloginOut } = require('../../utils/auth.js')
//读取后台api接口
const { getUserDetail, getUserBalanceDetail, getOrdersStatus } = require('../../http/api.js')

Page({
  data: {
    logs: [],
    wxlogin: true
  },
  onLoad: function () {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(log => {
        return util.formatTime(new Date(log))
      })
    })
  },
  //进入页面就触发
  async onShow() {
    //console.log('onShow')
    const token = wx.getStorageSync('token')

    //Promise版本
    // checkHasLogin(token).then(isLogin=>{

    //   console.log('isLogin::',isLogin)
    // })

    //assync,await版本的
    const isLogin = await checkHasLogin(token);
    //console.log('await-isLogin::', isLogin)

    this.setData({
      wxlogin: !isLogin
    })

    if (isLogin) {
      //读取用户信息接口
      this.getUserInfo();
      //读取个人资产接口
      this.getUserBalance()
      //读取订单统计接口
      this.getUserOrders();
    }
  },

  //读取用户信息
  async getUserInfo() {
    const token = wx.getStorageSync('token')
    //读取后台接口
    const result = await getUserDetail(token)
    //console.log('用户详情全在这里::', result)

    let _data = {}
    _data.userinfo = {}
    _data.userinfo.avatarUrl = result.data.base.avatarUrl;
    _data.userinfo.nick = result.data.base.nick;
    this.setData(_data);
  },
  //读取个人资产接口
  async getUserBalance() {
    const token = wx.getStorageSync('token')
    const result = await getUserBalanceDetail(token)
    //console.log('获取个人资产:', result)
    let _data = {}
    _data.userBalanceInfo = {}
    _data.userBalanceInfo.balance = result.data.balance
    _data.userBalanceInfo.freeze = result.data.freeze
    _data.userBalanceInfo.growth = result.data.growth
    _data.userBalanceInfo.score = result.data.score

    this.setData(_data)
  },
  /**
  * 读取订单统计接口
  * 参数说明如下:
  * count_id_no_pay: 待支付订单数量--待付款
  * count_id_no_transfer: 已支付等待商家发货订单数量--待发货
  * count_id_no_confirm: 商家已发货,等待确认收货订单数量--待收货
  * count_id_no_reputation: 交易成功,等待评价的订单数量--待评价
  */
  async getUserOrders() {
    const token = wx.getStorageSync('token')
    const result = await getOrdersStatus(token);
    //console.log('读取订单统计接口:', result)
    const _data = {}
    const { count_id_no_pay,
      count_id_no_transfer,
      count_id_no_confirm,
      count_id_no_reputation } = result.data

    _data.orderInfo = {
      count_id_no_pay,
      count_id_no_transfer,
      count_id_no_confirm,
      count_id_no_reputation
    }
    this.setData(_data)
  },

  //处理登录
  processLogin(e) {
    //console.log('处理登录:', e)
    //如果没有userinfo则直接返回并提示取消
    if (!e.detail.userInfo) {
      wx.showToast({
        title: '取消',
      })
      return;
    }
    //如果有userinfo,则注册用户
    register(this)
  },
  //退出登录
  loginOut() {
    userloginOut()
    wx.reLaunch({
      url: '/pages/logs/logs',
    })
  },
  //清除缓存
  clearCache() {
    wx.clearStorageSync();
    wx.reLaunch({
      url: '/pages/logs/logs',
    })
  },
  onReachBottom() {
    //console.log('到底页面底部')
  },
  onPullDownRefresh() {
    //console.log('onPullDownRefresh')
  },
  onShareAppMessage() {
	//console.log('onShareAppMessage')
  }
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值