提交 7e159890 编写于 作者: Q qiang

feat: univerify

上级 ac1c9ca4
...@@ -204,6 +204,9 @@ const third = [ ...@@ -204,6 +204,9 @@ const third = [
'login', 'login',
'checkSession', 'checkSession',
'getUserInfo', 'getUserInfo',
'preLogin',
'onAuthViewOtherLoginButtonClick',
'closeAuthView',
'share', 'share',
'shareWithSystem', 'shareWithSystem',
'showShareMenu', 'showShareMenu',
......
const provider = {
UNIVERIFY: 'univerify'
}
export const preLogin = {
provider: {
type: String,
required: true,
default: provider.UNIVERIFY,
validator (value, params) {
if (Object.values(provider).indexOf(value) < 0) {
return 'provider error'
}
}
}
}
import { import {
invoke invoke
} from '../../bridge' } from '../../bridge'
import {
warpPlusSuccessCallback,
warpPlusErrorCallback
} from '../util'
const loginServices = {} function getService (provider) {
return new Promise((resolve, reject) => {
const loginByService = (provider, callbackId) => { plus.oauth.getServices(services => {
function login () { const service = services.find(({ id }) => id === provider)
loginServices[provider].login(res => { service ? resolve(service) : reject(new Error('provider not find'))
const authResult = res.target.authResult }, reject)
invoke(callbackId, { })
code: authResult.code,
authResult: authResult,
errMsg: 'login:ok'
})
}, err => {
invoke(callbackId, {
code: err.code,
errMsg: 'login:fail:' + err.message
})
}, provider === 'apple' ? { scope: 'email' } : {})
}
// 先注销再登录
// apple登录logout之后无法重新触发获取email,fullname
if (provider === 'apple') {
login()
} else {
loginServices[provider].logout(login, login)
}
} }
/** /**
* 微信登录 * 微信登录
*/ */
export function login (params, callbackId) { export function login (params, callbackId) {
const provider = params.provider || 'weixin' const provider = params.provider || 'weixin'
if (loginServices[provider]) { getService(provider).then(service => {
loginByService(provider, callbackId) function login () {
} else { service.login(res => {
plus.oauth.getServices(services => { const authResult = res.target.authResult
loginServices[provider] = services.find(({
id
}) => id === provider)
if (!loginServices[provider]) {
invoke(callbackId, { invoke(callbackId, {
code: '', code: authResult.code,
errMsg: 'login:fail:登录服务[' + provider + ']不存在' authResult: authResult,
errMsg: 'login:ok'
}) })
} else { }, err => {
loginByService(provider, callbackId) invoke(callbackId, {
} code: err.code,
}, err => { errMsg: 'login:fail:' + err.message
invoke(callbackId, { })
code: err.code, }, provider === 'apple' ? { scope: 'email' } : params.univerifyStyle || {})
errMsg: 'login:fail:' + err.message }
}) // 先注销再登录
// apple登录logout之后无法重新触发获取email,fullname
if (provider === 'apple') {
login()
} else {
service.logout(login, login)
}
}).catch(err => {
invoke(callbackId, {
code: err.code || '',
errMsg: 'login:fail:' + err.message
}) })
} })
} }
export function getUserInfo (params, callbackId) { export function getUserInfo (params, callbackId) {
const provider = params.provider || 'weixin' const provider = params.provider || 'weixin'
const loginService = loginServices[provider] const errorCallback = warpPlusErrorCallback(callbackId, 'operateWXData')
if (!loginService || !loginService.authResult) { getService(provider).then(loginService => {
return invoke(callbackId, { loginService.getUserInfo(res => {
errMsg: 'operateWXData:fail:请先调用 uni.login' let userInfo
}) if (provider === 'weixin') {
} const wechatUserInfo = loginService.userInfo
loginService.getUserInfo(res => { userInfo = {
let userInfo openId: wechatUserInfo.openid,
if (provider === 'weixin') { nickName: wechatUserInfo.nickname,
const wechatUserInfo = loginService.userInfo gender: wechatUserInfo.sex,
userInfo = { city: wechatUserInfo.city,
openId: wechatUserInfo.openid, province: wechatUserInfo.province,
nickName: wechatUserInfo.nickname, country: wechatUserInfo.country,
gender: wechatUserInfo.sex, avatarUrl: wechatUserInfo.headimgurl,
city: wechatUserInfo.city, unionId: wechatUserInfo.unionid
province: wechatUserInfo.province, }
country: wechatUserInfo.country, } else if (provider === 'apple') {
avatarUrl: wechatUserInfo.headimgurl, const appleInfo = loginService.appleInfo
unionId: wechatUserInfo.unionid 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
} }
} else if (provider === 'apple') { const result = {
const appleInfo = loginService.appleInfo errMsg: 'operateWXData:ok'
userInfo = { }
openId: appleInfo.user, if (params.data && params.data.api_name === 'webapi_getuserinfo') {
fullName: appleInfo.fullName, result.data = {
email: appleInfo.email, data: JSON.stringify(userInfo),
authorizationCode: appleInfo.authorizationCode, rawData: '',
identityToken: appleInfo.identityToken, signature: '',
realUserStatus: appleInfo.realUserStatus encryptedData: '',
} iv: ''
} else { }
loginService.userInfo.openId = loginService.userInfo.openId || loginService.userInfo.openid || } else {
loginService.authResult.openid result.userInfo = userInfo
loginService.userInfo.nickName = loginService.userInfo.nickName || loginService.userInfo.nickname }
loginService.userInfo.avatarUrl = loginService.userInfo.avatarUrl || loginService.userInfo.avatarUrl || invoke(callbackId, result)
loginService.userInfo.headimgurl }, errorCallback)
userInfo = loginService.userInfo }).catch(() => {
}
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)
}, err => {
invoke(callbackId, { invoke(callbackId, {
errMsg: 'operateWXData:fail:' + err.message errMsg: 'operateWXData:fail:请先调用 uni.login'
}) })
}) })
} }
...@@ -133,3 +122,19 @@ export function operateWXData (params, callbackId) { ...@@ -133,3 +122,19 @@ export function operateWXData (params, callbackId) {
} }
} }
} }
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 onAuthViewOtherLoginButtonClick (callbackId) {
getService('univerify').then(service => service.onOtherLoginButtonClick(() => {
invoke(callbackId, {})
}))
}
export function closeAuthView () {
getService('univerify').then(service => service.closeAuthView())
}
\ No newline at end of file
...@@ -178,6 +178,15 @@ export function warpPlusEvent (module, name) { ...@@ -178,6 +178,15 @@ export function warpPlusEvent (module, name) {
} }
} }
export function warpPlusSuccessCallback (callbackId, neme) {
return function errorCallback (result) {
result = result || {}
invoke(callbackId, Object.assign({}, result, {
errMsg: `${neme}:ok`
}))
}
}
export function warpPlusErrorCallback (callbackId, neme, errMsg) { export function warpPlusErrorCallback (callbackId, neme, errMsg) {
return function errorCallback (error) { return function errorCallback (error) {
error = error || {} error = error || {}
...@@ -214,4 +223,4 @@ export function getFileName (path) { ...@@ -214,4 +223,4 @@ export function getFileName (path) {
export function getExtName (path) { export function getExtName (path) {
const array = path.split('.') const array = path.split('.')
return array.length > 1 ? '.' + array[array.length - 1] : '' return array.length > 1 ? '.' + array[array.length - 1] : ''
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册