authorization.js 3.8 KB
import {
  request
} from "../../request/index.js"
const a = getApp()
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    code: '',
    encryptedData: '',
    iv: '',
    msg: '',
    num: 0
  },


  onReady: function () {

  },
  onLoad: function (options) {
    console.log(options);
    let that = this
    that.setData({
      num: options.num || ''
    })

  },
  onShow: function () {},
  // 立即授权
  bindGetUserInfo(e) {
    let that = this;
    console.log(e);
    if (e.detail.errMsg == "getUserInfo:ok") {
      that.setData({
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      })
    }
    wx.login({
      success(res) {
        console.log(res);
        that.setData({
          code: res.code
        })
        request({
            url: 'api/common/login',
            method: "POST",
            data: {
              rawData: e.detail.rawData,
              code: that.data.code,
              iv: that.data.iv,
              encryptedData: that.data.encryptedData,
            }
          })
          .then(res => {
            console.log(res);
            if (res.data.code == 1) {
              wx.setStorageSync('token', res.data.data.userInfo.token);
              wx.setStorageSync('avatar', res.data.data.userInfo.avatar);
              wx.setStorageSync('name', res.data.data.userInfo.nickname);
              wx.setStorageSync('user_id', res.data.data.userInfo.user_id);
              that.showModal();
            }
          }).catch(err => {
            console.log(err);

          })
      }
    })
  },

  getPhoneNumber(e) { //手机号授权
    let that = this
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      that.setData({
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      })
      that.getPhoneCall()
    } else {
      that.setData({
        msg: '重新进行手机号授权'
      })
      that.popTest()
    }
  },
  async getPhoneCall() {
    let that = this
    const {
      data
    } = await request({
      url: 'api/common/savePhone',
      data: {
        encryptedData: that.data.encryptedData,
        iv: that.data.iv
      }
    })
    that.setData({
      msg: data.msg
    })
    that.popSuccessTest();
    setTimeout(function () {

      if (that.data.num == 1) {
        wx.navigateBack()
      } else {
        wx.navigateTo({
          url: '/pages/already/already'
        })
      }


    }, 1100)
  },
  //显示对话框
  showModal() {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 100)
  },
  //隐藏对话框
  hideModal() {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 100)
  },
  popTest() {
    wx.showToast({
      title: this.data.msg,
      icon: 'none', //如果要纯文本,不要icon,将值设为'none'
      duration: 1300
    })
  },
  popSuccessTest() {
    wx.showToast({
      title: this.data.msg,
      icon: '', //默认值是success,就算没有icon这个值,就算有其他值最终也显示success
      duration: 1300, //停留时间
    })
  },


})