计算机网络/计算机科学与应用/系统/运维/开发

授权手机号时,同时获取微信昵称 / 头像,存储到后端

const app = getApp();
Page({
  data: {
    url: app.globalData.apiDomain.prod,
    agree: false,
    member: null,
    boundMobile: false,
    redirect: ''
  },

  onLoad(options) {
    if (options.redirect) {
      this.setData({ redirect: options.redirect });
    }
  },

  changeAgree(e) {
    this.setData({ agree: e.detail.value.length > 0 })
  },

  loginCheck() {
    if (!this.data.agree) {
      wx.showToast({ title: '请阅读并勾选协议', icon: 'none' });
    }
  },

  openPrivacyProtocol() {
    wx.navigateTo({ url: '/pages/agreement/agreement' });
  },
  openUserProtocol() {
    wx.navigateTo({ url: '/pages/user/user' });
  },

  // ————————————————————————————
  // 登录:获取手机号 + 昵称头像
  // ————————————————————————————
  getPhoneNumber(e) {
    const that = this;

    // 1. 协议校验
    if (!that.data.agree) {
      wx.showToast({ title: '请勾选协议', icon: 'none' });
      return;
    }

    // 2. 用户拒绝
    if (e.detail.errMsg === 'getPhoneNumber:fail user deny') {
      wx.showToast({ title: '需要授权手机号', icon: 'none' });
      return;
    }

    // 3. 获取用户信息(昵称+头像)
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (userInfo) => {
        const { nickName, avatarUrl } = userInfo;

        // 4. 登录拿 code
        wx.login({
          success: (res) => {
            if (!res.code) {
              wx.showToast({ title: 'code获取失败', icon: 'none' });
              return;
            }

            // 5. 传给后端:手机号加密信息 + 昵称 + 头像
            wx.request({
              url: that.data.url + '/v1/phoneLogin',
              method: 'POST',
              data: {
                code: res.code,
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv,
                nickname: nickName,     // 昵称
                avatar: avatarUrl       // 头像
              },
              success: (res) => {
                if (res.data.code === 1) {
                  const { phone, id, openid } = res.data.data;
                  wx.setStorageSync('member', { phone, id, openid });
                  app.storageMember(res.data.data);

                  // 跳转逻辑
                  if (that.data.redirect === 'recharge') {
                    wx.redirectTo({ url: '/pages/recharge/recharge' });
                  } else if (that.data.redirect === 'couponReceive') {
                    wx.redirectTo({ url: '/pages/activity/activity' });
                  } else {
                    wx.switchTab({ url: '/pages/index/index' });
                  }
                } else {
                  wx.showToast({ title: res.data.msg || '登录失败', icon: 'none' });
                }
              },
              fail: () => {
                wx.showToast({ title: '网络错误', icon: 'none' });
              }
            });
          }
        });
      },
      fail: () => {
        wx.showToast({ title: '需要获取昵称才能登录', icon: 'none' });
      }
    });
  }
});


const app = getApp();
Page({
  data: {
    url: app.globalData.apiDomain.prod,
    agree: false,
    member: null,
    boundMobile: false,
    redirect: ''
  },

  onLoad(options) {
    if (options.redirect) {
      this.setData({ redirect: options.redirect });
    }
  },

  changeAgree(e) {
    this.setData({ agree: e.detail.value.length > 0 })
  },

  loginCheck() {
    if (!this.data.agree) {
      wx.showToast({ title: '请阅读并勾选协议', icon: 'none' });
    }
  },

  openPrivacyProtocol() {
    wx.navigateTo({ url: '/pages/agreement/agreement' });
  },
  openUserProtocol() {
    wx.navigateTo({ url: '/pages/user/user' });
  },

  // ————————————————————————————
  // 登录:获取手机号 + 昵称头像
  // ————————————————————————————
  getPhoneNumber(e) {
    const that = this;

    // 1. 协议校验
    if (!that.data.agree) {
      wx.showToast({ title: '请勾选协议', icon: 'none' });
      return;
    }

    // 2. 用户拒绝
    if (e.detail.errMsg === 'getPhoneNumber:fail user deny') {
      wx.showToast({ title: '需要授权手机号', icon: 'none' });
      return;
    }

    // 3. 获取用户信息(昵称+头像)
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (userInfo) => {
        const { nickName, avatarUrl } = userInfo;

        // 4. 登录拿 code
        wx.login({
          success: (res) => {
            if (!res.code) {
              wx.showToast({ title: 'code获取失败', icon: 'none' });
              return;
            }

            // 5. 传给后端:手机号加密信息 + 昵称 + 头像
            wx.request({
              url: that.data.url + '/v1/phoneLogin',
              method: 'POST',
              data: {
                code: res.code,
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv,
                nickname: nickName,     // 昵称
                avatar: avatarUrl       // 头像
              },
              success: (res) => {
                if (res.data.code === 1) {
                  const { phone, id, openid } = res.data.data;
                  wx.setStorageSync('member', { phone, id, openid });
                  app.storageMember(res.data.data);

                  // 跳转逻辑
                  if (that.data.redirect === 'recharge') {
                    wx.redirectTo({ url: '/pages/recharge/recharge' });
                  } else if (that.data.redirect === 'couponReceive') {
                    wx.redirectTo({ url: '/pages/activity/activity' });
                  } else {
                    wx.switchTab({ url: '/pages/index/index' });
                  }
                } else {
                  wx.showToast({ title: res.data.msg || '登录失败', icon: 'none' });
                }
              },
              fail: () => {
                wx.showToast({ title: '网络错误', icon: 'none' });
              }
            });
          }
        });
      },
      fail: () => {
        wx.showToast({ title: '需要获取昵称才能登录', icon: 'none' });
      }
    });
  }
});


人生在世,不如意事十之八九;人生的滋味,哪怕是酸甜或苦辣,也要靠自己去品。人活一口气:气质看一个人的过去,气度看一个人的未来

评论

^