Login.js 3.27 KB
// components/Login/Login.js
import {
  ssoApi
} from '../../api/api'
import {
  componentsMixin
} from '../../mixins/index'
Component({
  behaviors: [componentsMixin],
  /**
   * 组件的属性列表
   */
  properties: {
    show: Boolean,
    content: {
      type: String,
      value: '您还没有登录,请您登录后查看!'
    },
    returnResponse: {
      type: Boolean,
      value: true
    }
  },
  observers: {
    'show': function (newVal, oldVal) {
      let {
        returnResponse
      } = this.data
      if (newVal) {
        wx.$util.showLoading()
        getApp().login(returnResponse).then(({
          code,
          data
        }) => {
          this.triggerEvent('loginInfo', {
            code,
            data
          })
          wx.hideLoading()
          if (data) {
            let {
              sessionId
            } = data
            this.setData({
              sessionId,
              openSet: code != 0
            })
            this.sessionId = sessionId
            if (data.isLogin == 'Y'&& code == '0') {
              let thisPage = wx.$router.getPageInstance()
              if (thisPage.loginCallBack) thisPage.loginCallBack(data)
			  if (thisPage.onRoute) thisPage.onRoute()
            }
          }
        })
      } else {
        this.hide()
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    hide() {
      this.setData({
        openSet: false
      })
    },
    cancel(flag) {

      this.hide()
      if (flag) this.triggerEvent('cancel')
    },
    noScroll: wx.$util.noScroll(),
    getPhoneNumber({
      detail
    }) {
      if (detail.errMsg && detail.errMsg == 'getPhoneNumber:fail user deny') {
        this.cancel(true)
        return
      }
      delete detail.errMsg
      if (detail.iv && detail.encryptedData) {
        const {
          sessionId
        } = this.data
        wx.$util.showLoading()
        detail.sessionId = sessionId
        console.log(detail);
        ssoApi.authUserPhone(detail).then(({
          data,
          code
        }) => {
          wx.$Track.setTrack({}, 3)
          wx.hideLoading()
          let thisPage = wx.$router.getPageInstance()
          if (code == '0') {
            thisPage.setData({
              showLogin: false,
              userInfo: data
            })
            wx.$authStatus = false
            this.setData({
              sessionId: ''
            })
            wx.$util.setUserInfo(data)
            wx.$util.setSessionId(data.sessionId)
            thisPage.onLoad()
            thisPage.onShow()
            if (thisPage.init) thisPage.init()
            if (thisPage.onRoute) thisPage.onRoute()
            if (thisPage.loginCallBack) thisPage.loginCallBack(data)
            let customerId = wx.$util.getStorageSync('srcm_shopmall_customer_id')
            let merchantId = wx.$util.getStorageSync('srcm_shopmall_merchant_id')
            if (customerId) {
              ssoApi.updataCustomerPhone({
                customerId,
                merchantId
              }).then(() => {
                wx.removeStorageSync('srcm_shopmall_customer_id')
              })
            }
            wx.$util.customerbehavior()
            wx.$eventbus.emit('EQUITY')
          }
        })
      }
    },
  }
})