oauth.js 5.7 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
  const errorCallback = warpPlusErrorCallback(callbackId, 'login')
25 26 27 28 29
  const authOptions = provider === 'apple'
    ? { scope: 'email' }
    : params.univerifyStyle 
      ? { univerifyStyle: univerifyButtonsClickHandling(params.univerifyStyle, errorCallback) } 
      : {}
30

Q
qiang 已提交
31
  getService(provider).then(service => {
32 33 34 35 36 37 38 39 40 41 42
    function login() {
      if (params.onlyAuthorize && provider === 'weixin') {
        service.authorize(({ code }) => {
          invoke(callbackId, {
            code,
            authResult: '',
            errMsg: 'login:ok'
          })
        }, errorCallback)
        return
      }
Q
qiang 已提交
43 44
      service.login(res => {
        const authResult = res.target.authResult
fxy060608's avatar
fxy060608 已提交
45
        invoke(callbackId, {
Q
qiang 已提交
46 47 48
          code: authResult.code,
          authResult: authResult,
          errMsg: 'login:ok'
fxy060608's avatar
fxy060608 已提交
49
        })
50
      }, errorCallback, authOptions)
Q
qiang 已提交
51 52
    }
    // 先注销再登录
Q
qiang 已提交
53 54
    // apple登录logout之后无法重新触发获取email,fullname;一键登录无logout
    if (provider === 'apple' || provider === 'univerify') {
Q
qiang 已提交
55 56 57 58
      login()
    } else {
      service.logout(login, login)
    }
59
  }).catch(errorCallback)
fxy060608's avatar
fxy060608 已提交
60 61
}

Q
qiang 已提交
62
export function getUserInfo (params, callbackId) {
fxy060608's avatar
fxy060608 已提交
63
  const provider = params.provider || 'weixin'
Q
qiang 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  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 已提交
95
      }
Q
qiang 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      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 已提交
113
    invoke(callbackId, {
114
      errMsg: 'operateWXData:fail 请先调用 uni.login'
fxy060608's avatar
fxy060608 已提交
115 116 117
    })
  })
}
118 119 120
/**
 * 获取用户信息-兼容
 */
Q
qiang 已提交
121
export function getUserProfile (params, callbackId) {
122 123
  return getUserInfo(params, callbackId)
}
fxy060608's avatar
fxy060608 已提交
124 125 126 127

/**
 * 获取用户信息
 */
Q
qiang 已提交
128
export function operateWXData (params, callbackId) {
fxy060608's avatar
fxy060608 已提交
129 130 131 132 133 134 135 136 137 138
  switch (params.data.api_name) {
    case 'webapi_getuserinfo':
      getUserInfo(params, callbackId)
      break
    default:
      return {
        errMsg: 'operateWXData:fail'
      }
  }
}
Q
qiang 已提交
139

Q
qiang 已提交
140
export function preLogin (params, callbackId) {
Q
qiang 已提交
141 142 143 144 145
  const successCallback = warpPlusSuccessCallback(callbackId, 'preLogin')
  const errorCallback = warpPlusErrorCallback(callbackId, 'preLogin')
  getService(params.provider).then(service => service.preLogin(successCallback, errorCallback)).catch(errorCallback)
}

Q
qiang 已提交
146
export function closeAuthView () {
147 148 149
  return getService('univerify').then(service => service.closeAuthView())
}

150 151 152 153 154 155 156 157 158 159 160 161 162
export function getCheckBoxState(params, callbackId) {
  const successCallback = warpPlusSuccessCallback(callbackId, 'getCheckBoxState')
  const errorCallback = warpPlusErrorCallback(callbackId, 'getCheckBoxState')
  try {
    getService('univerify').then(service => {
      const state = service.getCheckBoxState()
      successCallback({ state })
    })
  } catch (error) {
    errorCallback(error)
  }
}

163 164 165
/**
 * 一键登录自定义登陆按钮点击处理
 */
Q
qiang 已提交
166
function univerifyButtonsClickHandling (univerifyStyle, errorCallback) {
D
DCloud_LXH 已提交
167
  if (univerifyStyle && isPlainObject(univerifyStyle) && univerifyStyle.buttons &&
168 169 170 171 172 173 174 175 176
    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: '用户点击了自定义按钮',
177 178
            index,
            provider: button.provider
179 180 181 182 183 184
          })
        })
      }
    })
  }
  return univerifyStyle
Q
qiang 已提交
185
}