getPhone.js 2.4 KB
// pages/getPhone/getPhone.js
const baseUrl = getApp().globalData.baseUrl;
let Authorization = '';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  getPhoneNumber: function (e) {
    const _this = this;
    const iv = e.detail.iv;
    const encryptedData = e.detail.encryptedData;
    console.log('getPhoneNumber' ,e)
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未授权',
        success: function (res) { }
      })
    } else {
      this.login(iv, encryptedData);
    }
  },
  login: function (iv, phone) {
    var that = this;
    var Authorization = getApp().globalData.Authorization;
    const userInfo = app.globalData.userInfo;
    const { opt, salary_unit,id } = that.data;
    wx.request({
      url: baseUrl + "uaa/v1/users/op/bind-mobile",
      data: {
        "mobile": '13691224349',//必传,随便写一个
        "sms_code": '1234',//必传,随便写一个
        "wx_open_id": app.globalData.wx_open_id,  //可选,如果传了,绑定到对应的微信号
        "tenant_id": app.globalData.tenant_id,
        "source": "wx_app::" + phone + ":" + iv,
        "scope": "expert"
      },
      method: "POST",
      header: {
        'content-type': 'application/json', // 默认值
        "Authorization": Authorization
      },
      success: function (res) {

        console.log("succ", res);
        if (200 == res.statusCode) {
          app.globalData.Authorization = "Bearer " + res.data.access_token;
          app.globalData.refresh_token = res.data.refresh_token;
          var user = res.data.user;
          var tenant = res.data.tenant;
          app.globalData.tenant_id = tenant.id;
          app.globalData.userInfo = user;
          app.globalData.wx_open_id = user.wx_open_id;

          wx.reLaunch({
            url: '../tabbar/home/home'
          })

        } else {
          wx.showToast({
            title: '登录失败,请重新登录',
          })
        }
      },
      fail: function (res) {
        console.log("fail ", res.data)
      },
      complete:function (res){
        wx.reLaunch({
          url: '../tabbar/home/home'
        })
      }
    })
  },
  phoneLogin: function () {
    wx.navigateTo({
      url: '../login/login',
    })
  },

})