forked from gzzfw/frontEnd/gzDyh

zhangyongtian
2024-09-13 cf88306b1f6aade9f0c6c4fa5859872d3f177c4a
gz-wxparty/components/common-page/index.js
@@ -3,106 +3,188 @@
// 获取手机号码
function getPhoneNumber(submitData) {
   return $$.request({ url: 'paAccount/getUserPhone', type: 'post', service: 'cust', submitData });
  return $$.request({
    url: 'paAccount/getUserPhone',
    type: 'post',
    service: 'cust',
    submitData
  });
}
Component({
   /**
    * 组件的属性列表
    * popup: 下拉框的数据;visible:boolean 是否显示;title: string 标题;selectData: array;列数据;可拓展对象属性
    * safeBottom: iphoneX安全距离
    */
   properties: {
      popup: {
         type: Object,
         value: { visible: false }, // default: { visible: false, title: '', selectData: [] }
      },
      safeBottom: {
         type: Boolean,
         value: true,
      },
   },
  /**
   * 组件的属性列表
   * popup: 下拉框的数据;visible:boolean 是否显示;title: string 标题;selectData: array;列数据;可拓展对象属性
   * safeBottom: iphoneX安全距离
   */
  properties: {
    popup: {
      type: Object,
      value: {
        visible: false
      }, // default: { visible: false, title: '', selectData: [] }
    },
    safeBottom: {
      type: Boolean,
      value: true,
    },
  },
   /**
    * 组件的初始数据
    */
   columnsDefaultIndex: 0,
   data: {
      loginVisible: false,
      popupIndex: null,
   },
  /**
   * 组件的初始数据
   */
  columnsDefaultIndex: 0,
  data: {
    loginVisible: false,
    popupIndex: null,
  },
   pageLifetimes: {
      show: function () {
         if (!app.globalData.token) {
                console.log('测试')
                if (!app.globalData.access_token) {
                    $$.showModal({
                        content: '抱歉您未登录,是否前往登录?',
                        success: (res) => {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '../../pages/login/index',
                                });
                            } else {
                                wx.navigateBack({
                                    delta: 1,
                                });
                            }
                        },
                    });
  pageLifetimes: {
    show: function () {
      if (!app.globalData.token) {
        console.log('测试')
        if (!app.globalData.access_token) {
          $$.showModal({
            content: '抱歉您未登录,是否前往登录?',
            success: (res) => {
              if (res.confirm) {
                // wx.redirectTo({
                //     url: '../../pages/login/index',
                // });
                this.handleGetUserInfo()
              } else {
                wx.navigateBack({
                  delta: 1,
                });
              }
            },
          });
        }
      }
      if (app.globalData.token && !this.data.loginVisible) {
        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',
                  });
                }
         }
         if (app.globalData.token && !this.data.loginVisible) {
            this.setData({ loginVisible: true });
         }
      },
   },
              } 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 });
         }
      },
   },
  observers: {
    'popup.visible,popup.noPicker': function (data1, data2) {
      if ((data1, data2)) {
        this.setData({
          popupIndex: this.data.popup.activeIndex
        });
      }
    },
  },
   /**
    * 组件的方法列表
    */
   methods: {
      // 退出登录
      loginOut() {
         this.setData({ loginVisible: false });
      },
      // 获取手机号码
      async handleGetPhoneNumber(code) {
         $$.showLoading();
         const accountInfo = wx.getAccountInfoSync();
         const res = await getPhoneNumber({ appid: accountInfo.miniProgram.appId, code });
         $$.hideLoading();
         if (res.type) {
            $$.showToast({ title: '获取成功' });
            return res.data;
         }
      },
      // 下拉框底层弹出层方法
      _handleClosePopup() {
         this.triggerEvent('onClosePopup');
      },
      _handleChangePicker(e) {
         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 } });
            return;
         }
         this.triggerEvent('onConfirmPicker', { dataset: e.currentTarget.dataset, detail: e.detail });
      },
   },
});
  /**
   * 组件的方法列表
   */
  methods: {
    // 退出登录
    loginOut() {
      this.setData({
        loginVisible: false
      });
    },
    // 获取手机号码
    async handleGetPhoneNumber(code) {
      $$.showLoading();
      const accountInfo = wx.getAccountInfoSync();
      const res = await getPhoneNumber({
        appid: accountInfo.miniProgram.appId,
        code
      });
      $$.hideLoading();
      if (res.type) {
        $$.showToast({
          title: '获取成功'
        });
        return res.data;
      }
    },
    // 下拉框底层弹出层方法
    _handleClosePopup() {
      this.triggerEvent('onClosePopup');
    },
    _handleChangePicker(e) {
      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
          }
        });
        return;
      }
      this.triggerEvent('onConfirmPicker', {
        dataset: e.currentTarget.dataset,
        detail: e.detail
      });
    },
  },
});