oauth.js 4.9 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'
Q
qiang 已提交
8
import { isPlainObject } from 'uni-shared'
fxy060608's avatar
fxy060608 已提交
9

Q
qiang 已提交
10
function getService (provider) {
Q
qiang 已提交
11 12 13 14 15 16
  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 已提交
17
}
Q
qiang 已提交
18

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

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

Q
qiang 已提交
47
export function getUserInfo (params, callbackId) {
fxy060608's avatar
fxy060608 已提交
48
  const provider = params.provider || 'weixin'
Q
qiang 已提交
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 79
  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 已提交
80
      }
Q
qiang 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
      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 已提交
98
    invoke(callbackId, {
99
      errMsg: 'operateWXData:fail 请先调用 uni.login'
fxy060608's avatar
fxy060608 已提交
100 101 102
    })
  })
}
103 104 105
/**
 * 获取用户信息-兼容
 */
Q
qiang 已提交
106
export function getUserProfile (params, callbackId) {
107 108
  return getUserInfo(params, callbackId)
}
fxy060608's avatar
fxy060608 已提交
109 110 111 112

/**
 * 获取用户信息
 */
Q
qiang 已提交
113
export function operateWXData (params, callbackId) {
fxy060608's avatar
fxy060608 已提交
114 115 116 117 118 119 120 121 122 123
  switch (params.data.api_name) {
    case 'webapi_getuserinfo':
      getUserInfo(params, callbackId)
      break
    default:
      return {
        errMsg: 'operateWXData:fail'
      }
  }
}
Q
qiang 已提交
124

Q
qiang 已提交
125
export function preLogin (params, callbackId) {
Q
qiang 已提交
126 127 128 129 130
  const successCallback = warpPlusSuccessCallback(callbackId, 'preLogin')
  const errorCallback = warpPlusErrorCallback(callbackId, 'preLogin')
  getService(params.provider).then(service => service.preLogin(successCallback, errorCallback)).catch(errorCallback)
}

Q
qiang 已提交
131
export function closeAuthView () {
132 133 134 135 136 137
  return getService('univerify').then(service => service.closeAuthView())
}

/**
 * 一键登录自定义登陆按钮点击处理
 */
Q
qiang 已提交
138
function univerifyButtonsClickHandling (univerifyStyle, errorCallback) {
D
DCloud_LXH 已提交
139
  if (univerifyStyle && isPlainObject(univerifyStyle) && univerifyStyle.buttons &&
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    Object.prototype.toString.call(univerifyStyle.buttons.list) === '[object Array]' &&
    univerifyStyle.buttons.list.length > 0
  ) {
    univerifyStyle.buttons.list.forEach((button, index) => {
      univerifyStyle.buttons.list[index].onclick = function () {
        closeAuthView().then(() => {
          errorCallback({
            code: '30008',
            message: '用户点击了自定义按钮',
            index
          })
        })
      }
    })
  }
  return univerifyStyle
Q
qiang 已提交
156
}