forked from gzzfw/frontEnd/gzDyh

liuwh
2024-09-13 6613c11627b5e0be1629f3784acf0245a8040ea1
gz-wxparty/components/common-page/index.js
@@ -3,7 +3,12 @@
// 获取手机号码
function getPhoneNumber(submitData) {
   return $$.request({ url: 'paAccount/getUserPhone', type: 'post', service: 'cust', submitData });
  return $$.request({
    url: 'paAccount/getUserPhone',
    type: 'post',
    service: 'cust',
    submitData
  });
}
Component({
@@ -15,7 +20,9 @@
   properties: {
      popup: {
         type: Object,
         value: { visible: false }, // default: { visible: false, title: '', selectData: [] }
      value: {
        visible: false
      }, // default: { visible: false, title: '', selectData: [] }
      },
      safeBottom: {
         type: Boolean,
@@ -41,9 +48,10 @@
                        content: '抱歉您未登录,是否前往登录?',
                        success: (res) => {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '../../pages/login/index',
                                });
                // wx.redirectTo({
                //     url: '../../pages/login/index',
                // });
                this.handleGetUserInfo()
                            } else {
                                wx.navigateBack({
                                    delta: 1,
@@ -55,15 +63,70 @@
                }
         }
         if (app.globalData.token && !this.data.loginVisible) {
            this.setData({ loginVisible: true });
        this.setData({
          loginVisible: true
        });
         }
      },
  },
  // 登录,获取用户信息
  async handleGetUserInfo() {
    $$.showLoading();
    wx.getUserProfile({
      desc: '完善用户信息',
      complete(res) {
        if (res.errMsg === 'getUserProfile:ok') {
          wx.login({
            async success(res2) {
              if (res2.code) {
                const accountInfo = wx.getAccountInfoSync();
                const submitData = {
                  appid: accountInfo.miniProgram.appId,
                  code: res2.code,
                  avatar: res?.userInfo.avatarUrl,
                  encryptedData: res.encryptedData,
                  ivStr: res.iv,
                };
                const res3 = await loginApi(submitData);
                $$.hideLoading();
                if (res3.type) {
                  wx.setStorage({
                    key: 'userInfo',
                    data: res3.data
                  });
                  app.globalData.token = res3.data.token;
                  $$.showToast({
                    title: '登录成功',
                    icon: 'success'
                  });
                  await $$.sleep();
                  wx.reLaunch({
                    url: '../../pages/homePage/index',
                  });
                }
              } else {
                $$.hideLoading();
                $$.showToast('登录失败,请稍后重试');
              }
            },
          });
        } else {
          $$.hideLoading();
          $$.showToast({
            title: '抱歉!授权失败'
          });
        }
      },
    });
   },
   observers: {
      'popup.visible,popup.noPicker': function (data1, data2) {
         if ((data1, data2)) {
            this.setData({ popupIndex: this.data.popup.activeIndex });
        this.setData({
          popupIndex: this.data.popup.activeIndex
        });
         }
      },
   },
@@ -74,16 +137,23 @@
   methods: {
      // 退出登录
      loginOut() {
         this.setData({ loginVisible: false });
      this.setData({
        loginVisible: false
      });
      },
      // 获取手机号码
      async handleGetPhoneNumber(code) {
         $$.showLoading();
         const accountInfo = wx.getAccountInfoSync();
         const res = await getPhoneNumber({ appid: accountInfo.miniProgram.appId, code });
      const res = await getPhoneNumber({
        appid: accountInfo.miniProgram.appId,
        code
      });
         $$.hideLoading();
         if (res.type) {
            $$.showToast({ title: '获取成功' });
        $$.showToast({
          title: '获取成功'
        });
            return res.data;
         }
      },
@@ -92,17 +162,29 @@
         this.triggerEvent('onClosePopup');
      },
      _handleChangePicker(e) {
         this.triggerEvent('onChangePicker', { dataset: e.currentTarget.dataset, detail: e.detail });
      this.triggerEvent('onChangePicker', {
        dataset: e.currentTarget.dataset,
        detail: e.detail
      });
      },
      _handleConfirmPicker(e) {
         if (this.data.popup.noPicker) {
            // 当组件不是Picker时
            let index = e.currentTarget.dataset.index;
            let value = e.currentTarget.dataset.value;
            this.triggerEvent('onConfirmPicker', { dataset: e.currentTarget.dataset, detail: { index, value } });
        this.triggerEvent('onConfirmPicker', {
          dataset: e.currentTarget.dataset,
          detail: {
            index,
            value
          }
        });
            return;
         }
         this.triggerEvent('onConfirmPicker', { dataset: e.currentTarget.dataset, detail: e.detail });
      this.triggerEvent('onConfirmPicker', {
        dataset: e.currentTarget.dataset,
        detail: e.detail
      });
      },
   },
});