login-button.js 2.56 KB

import { GET, POST } from '../../libs/request';
import { API_HOST } from '../../libs/config';
Component({

  // externalClasses: ['login-class'],
  /**
   * 组件的属性列表
   */
  properties: {
    openType: {
      type: String,
      value: '',
      observer(newVal) {
        if (newVal === 'getPhoneNumber') {
          let app = getApp()
          let param = {
            method: 'app.passport.signinByOpenID',
            openId: app.globalData.WXUnion_ID,
            nickname: app.globalData.userInfo.nickName || '',
          }
          POST(API_HOST, param)
            .then(data => {
              if (data && data.data.is_bind === 'Y') {
                this.setData({
                  myOpenType: '',
                  goBind: false
                })
              } else {
                this.setData({
                  myOpenType: newVal,
                  goBind: true
                })
              }
            }).catch(error => {
              this.setData({
                myOpenType: newVal,
                goBind: true
              });
            });
        } else {
          this.setData({
            myOpenType: newVal,
            goBind: true
          })
        }
      }
    },
    plain: {
      type: Boolean,
      value: false
    }  
  },

  /**
   * 组件的初始数据
   */
  data: {
    myOpenType: '',
    goBind: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getUserInfo(e) {
      console.log(e.detail);
      this.triggerEvent('getuserinfo', e.detail);
    },

    wechatBindUnionId() {
      if (this.data.goBind) return;
      let app = getApp()
      let param = {
        method: 'app.passport.signinByOpenID',
        openId: app.globalData.WXUnion_ID,
        nickname: app.globalData.userInfo.nickName || '',
      }
      POST(API_HOST, param)
        .then(data => {
          if (data && data.data.is_bind === 'Y') {
            let resData = data.data;
            let userInfo = app.globalData.userInfo;
            userInfo.is_bind = resData.is_bind;
            userInfo.mobile = resData.mobile;
            userInfo.ssouid = resData.ssouid;
            userInfo.uid = resData.uid;
            app.setUserInfo(userInfo);
            app.setSessionkey(resData.session_key);
            this.triggerEvent('updateuserinfo', userInfo);
          } else {
            this.setData({
              myOpenType: 'getPhoneNumber',
              goBind: true
            });
          }
        });
    },

    getPhoneNumber(e) {
      console.log(e.detail);
      this.triggerEvent('getphonenumber', e.detail);
    }
  }
})