diff --git a/lib/apis.js b/lib/apis.js index 7d1669424bd393f92a10ff9bf6716cbf502fc0f2..4983913fc0b7a26684965fbdd4300db732115c5b 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -204,6 +204,9 @@ const third = [ 'login', 'checkSession', 'getUserInfo', + 'preLogin', + 'onAuthViewOtherLoginButtonClick', + 'closeAuthView', 'share', 'shareWithSystem', 'showShareMenu', diff --git a/src/core/helpers/protocol/plugin/pre-login.js b/src/core/helpers/protocol/plugin/pre-login.js new file mode 100644 index 0000000000000000000000000000000000000000..34494a1670692bdd67b4d51344716cb5748b87d7 --- /dev/null +++ b/src/core/helpers/protocol/plugin/pre-login.js @@ -0,0 +1,16 @@ +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' + } + } + } +} diff --git a/src/platforms/app-plus/service/api/plugin/oauth.js b/src/platforms/app-plus/service/api/plugin/oauth.js index 14deb0086bc8ad7f98690e720842993a3e12ff27..1d2c6d45df5f4fcc5f0d817c5b3728369ca5cf7c 100644 --- a/src/platforms/app-plus/service/api/plugin/oauth.js +++ b/src/platforms/app-plus/service/api/plugin/oauth.js @@ -1,120 +1,109 @@ import { invoke } from '../../bridge' +import { + warpPlusSuccessCallback, + warpPlusErrorCallback +} from '../util' -const loginServices = {} - -const loginByService = (provider, callbackId) => { - function login () { - loginServices[provider].login(res => { - const authResult = res.target.authResult - 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) - } +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) + }) } + /** * 微信登录 */ export function login (params, callbackId) { const provider = params.provider || 'weixin' - if (loginServices[provider]) { - loginByService(provider, callbackId) - } else { - plus.oauth.getServices(services => { - loginServices[provider] = services.find(({ - id - }) => id === provider) - if (!loginServices[provider]) { + getService(provider).then(service => { + function login () { + service.login(res => { + const authResult = res.target.authResult invoke(callbackId, { - code: '', - errMsg: 'login:fail:登录服务[' + provider + ']不存在' + code: authResult.code, + authResult: authResult, + errMsg: 'login:ok' }) - } else { - loginByService(provider, callbackId) - } - }, err => { - invoke(callbackId, { - code: err.code, - errMsg: 'login:fail:' + err.message - }) + }, err => { + invoke(callbackId, { + code: err.code, + errMsg: 'login:fail:' + err.message + }) + }, provider === 'apple' ? { scope: 'email' } : params.univerifyStyle || {}) + } + // 先注销再登录 + // 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) { const provider = params.provider || 'weixin' - const loginService = loginServices[provider] - if (!loginService || !loginService.authResult) { - return invoke(callbackId, { - errMsg: 'operateWXData:fail:请先调用 uni.login' - }) - } - 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 + 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 } - } 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 { - loginService.userInfo.openId = loginService.userInfo.openId || loginService.userInfo.openid || - loginService.authResult.openid - loginService.userInfo.nickName = loginService.userInfo.nickName || loginService.userInfo.nickname - loginService.userInfo.avatarUrl = loginService.userInfo.avatarUrl || loginService.userInfo.avatarUrl || - loginService.userInfo.headimgurl - userInfo = loginService.userInfo - } - 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 => { + 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(() => { invoke(callbackId, { - errMsg: 'operateWXData:fail:' + err.message + errMsg: 'operateWXData:fail:请先调用 uni.login' }) }) } @@ -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 diff --git a/src/platforms/app-plus/service/api/util.js b/src/platforms/app-plus/service/api/util.js index 4fca9e143750ed6ba03ddda0cff954146766caf5..f52c6d20c944099f09789537df6609b57a164afc 100644 --- a/src/platforms/app-plus/service/api/util.js +++ b/src/platforms/app-plus/service/api/util.js @@ -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) { return function errorCallback (error) { error = error || {} @@ -214,4 +223,4 @@ export function getFileName (path) { export function getExtName (path) { const array = path.split('.') return array.length > 1 ? '.' + array[array.length - 1] : '' -} +}