import Taro from '@tarojs/taro';
import {
  getStore
} from '@tarojs/redux'
import {
  queryUserPhone
} from '@services/user'
import {
  UPDATE_USER_INFO
} from '@constants/user'

export default function(e, cb) {
  const {
    dispatch
  } = getStore()
  const sessionKey = Taro.getStorageSync('sessionKey')
  let payload = Object.assign(e.detail, {
    sessionKey
  })

  if (e.detail.encryptedData) {
    queryUserPhone(payload).then(({
      phoneNumber
    }) => {
      dispatch({
        type: UPDATE_USER_INFO,
        payload: {
          phone:phoneNumber
        }
      })
      cb && cb(phoneNumber)

    })
  } else {
    console.error(e.detail.errMsg)
    cb && cb()
  }
}
