oauth.js 4.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
import {
  invoke
} from '../../bridge'
Q
qiang 已提交
4 5 6 7
import {
  warpPlusSuccessCallback,
  warpPlusErrorCallback
} from '../util'
fxy060608's avatar
fxy060608 已提交
8

Q
qiang 已提交
9 10 11 12 13 14 15
function getService (provider) {
  return new Promise((resolve, reject) => {
    plus.oauth.getServices(services => {
      const service = services.find(({ id }) => id === provider)
      service ? resolve(service) : reject(new Error('provider not find'))
    }, reject)
  })
fxy060608's avatar
fxy060608 已提交
16
}
Q
qiang 已提交
17

fxy060608's avatar
fxy060608 已提交
18 19 20 21 22
/**
 * 微信登录
 */
export function login (params, callbackId) {
  const provider = params.provider || 'weixin'
23 24
  const errorCallback = warpPlusErrorCallback(callbackId, 'login')

Q
qiang 已提交
25 26 27 28
  getService(provider).then(service => {
    function login () {
      service.login(res => {
        const authResult = res.target.authResult
fxy060608's avatar
fxy060608 已提交
29
        invoke(callbackId, {
Q
qiang 已提交
30 31 32
          code: authResult.code,
          authResult: authResult,
          errMsg: 'login:ok'
fxy060608's avatar
fxy060608 已提交
33
        })
34
      }, errorCallback, provider === 'apple' ? { scope: 'email' } : { univerifyStyle: params.univerifyStyle } || {})
Q
qiang 已提交
35 36
    }
    // 先注销再登录
Q
qiang 已提交
37 38
    // apple登录logout之后无法重新触发获取email,fullname;一键登录无logout
    if (provider === 'apple' || provider === 'univerify') {
Q
qiang 已提交
39 40 41 42
      login()
    } else {
      service.logout(login, login)
    }
43
  }).catch(errorCallback)
fxy060608's avatar
fxy060608 已提交
44 45
}

Q
qiang 已提交
46
export function getUserInfo (params, callbackId) {
fxy060608's avatar
fxy060608 已提交
47
  const provider = params.provider || 'weixin'
Q
qiang 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  const errorCallback = warpPlusErrorCallback(callbackId, 'operateWXData')
  getService(provider).then(loginService => {
    loginService.getUserInfo(res => {
      let userInfo
      if (provider === 'weixin') {
        const wechatUserInfo = loginService.userInfo
        userInfo = {
          openId: wechatUserInfo.openid,
          nickName: wechatUserInfo.nickname,
          gender: wechatUserInfo.sex,
          city: wechatUserInfo.city,
          province: wechatUserInfo.province,
          country: wechatUserInfo.country,
          avatarUrl: wechatUserInfo.headimgurl,
          unionId: wechatUserInfo.unionid
        }
      } else if (provider === 'apple') {
        const appleInfo = loginService.appleInfo
        userInfo = {
          openId: appleInfo.user,
          fullName: appleInfo.fullName,
          email: appleInfo.email,
          authorizationCode: appleInfo.authorizationCode,
          identityToken: appleInfo.identityToken,
          realUserStatus: appleInfo.realUserStatus
        }
      } else {
        userInfo = loginService.userInfo
        userInfo.openId = userInfo.openId || userInfo.openid || loginService.authResult.openid
        userInfo.nickName = userInfo.nickName || userInfo.nickname
        userInfo.avatarUrl = userInfo.avatarUrl || userInfo.headimgurl
fxy060608's avatar
fxy060608 已提交
79
      }
Q
qiang 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
      const result = {
        errMsg: 'operateWXData:ok'
      }
      if (params.data && params.data.api_name === 'webapi_getuserinfo') {
        result.data = {
          data: JSON.stringify(userInfo),
          rawData: '',
          signature: '',
          encryptedData: '',
          iv: ''
        }
      } else {
        result.userInfo = userInfo
      }
      invoke(callbackId, result)
    }, errorCallback)
  }).catch(() => {
fxy060608's avatar
fxy060608 已提交
97
    invoke(callbackId, {
98
      errMsg: 'operateWXData:fail 请先调用 uni.login'
fxy060608's avatar
fxy060608 已提交
99 100 101
    })
  })
}
102 103 104 105 106 107
/**
 * 获取用户信息-兼容
 */
export function getUserProfile (params, callbackId) {
  return getUserInfo(params, callbackId)
}
fxy060608's avatar
fxy060608 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

/**
 * 获取用户信息
 */
export function operateWXData (params, callbackId) {
  switch (params.data.api_name) {
    case 'webapi_getuserinfo':
      getUserInfo(params, callbackId)
      break
    default:
      return {
        errMsg: 'operateWXData:fail'
      }
  }
}
Q
qiang 已提交
123 124 125 126 127 128 129 130 131

export function preLogin (params, callbackId) {
  const successCallback = warpPlusSuccessCallback(callbackId, 'preLogin')
  const errorCallback = warpPlusErrorCallback(callbackId, 'preLogin')
  getService(params.provider).then(service => service.preLogin(successCallback, errorCallback)).catch(errorCallback)
}

export function closeAuthView () {
  getService('univerify').then(service => service.closeAuthView())
Q
qiang 已提交
132
}