提交 223a161b 编写于 作者: D DCloud_LXH

feat: getLocation add highAccuracyExpireTime、getUniverifyManager

上级 b47092df
...@@ -223,6 +223,7 @@ const third = [ ...@@ -223,6 +223,7 @@ const third = [
'preLogin', 'preLogin',
'closeAuthView', 'closeAuthView',
'getCheckBoxState', 'getCheckBoxState',
'getUniverifyManager',
'share', 'share',
'shareWithSystem', 'shareWithSystem',
'showShareMenu', 'showShareMenu',
......
...@@ -52,7 +52,8 @@ function getLocationSuccess (type, position, callbackId) { ...@@ -52,7 +52,8 @@ function getLocationSuccess (type, position, callbackId) {
export function getLocation ({ export function getLocation ({
type = 'wgs84', type = 'wgs84',
geocode = false, geocode = false,
altitude = false altitude = false,
highAccuracyExpireTime
} = {}, callbackId) { } = {}, callbackId) {
const errorCallback = warpPlusErrorCallback(callbackId, 'getLocation') const errorCallback = warpPlusErrorCallback(callbackId, 'getLocation')
plus.geolocation.getCurrentPosition( plus.geolocation.getCurrentPosition(
...@@ -68,7 +69,8 @@ export function getLocation ({ ...@@ -68,7 +69,8 @@ export function getLocation ({
errorCallback(e) errorCallback(e)
}, { }, {
geocode: geocode, geocode: geocode,
enableHighAccuracy: altitude enableHighAccuracy: altitude,
timeout: highAccuracyExpireTime
} }
) )
} }
...@@ -5,7 +5,9 @@ import { ...@@ -5,7 +5,9 @@ import {
warpPlusSuccessCallback, warpPlusSuccessCallback,
warpPlusErrorCallback warpPlusErrorCallback
} from '../util' } from '../util'
import { isPlainObject } from 'uni-shared' import { isPlainObject, toRawType, callback } from 'uni-shared'
let univerifyManager
function getService (provider) { function getService (provider) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
...@@ -19,20 +21,21 @@ function getService (provider) { ...@@ -19,20 +21,21 @@ function getService (provider) {
/** /**
* 微信登录 * 微信登录
*/ */
export function login (params, callbackId) { export function login (params, callbackId, plus = true) {
const provider = params.provider || 'weixin' const provider = params.provider || 'weixin'
const errorCallback = warpPlusErrorCallback(callbackId, 'login') const errorCallback = warpErrorCallback(callbackId, 'login', plus)
const authOptions = provider === 'apple' const authOptions = provider === 'apple'
? { scope: 'email' } ? { scope: 'email' }
: params.univerifyStyle : params.univerifyStyle
? { univerifyStyle: univerifyButtonsClickHandling(params.univerifyStyle, errorCallback) } ? { univerifyStyle: univerifyButtonsClickHandling(params.univerifyStyle, errorCallback) }
: {} : {}
const _invoke = plus ? invoke : callback.invoke
getService(provider).then(service => { getService(provider).then(service => {
function login () { function login () {
if (params.onlyAuthorize && provider === 'weixin') { if (params.onlyAuthorize && provider === 'weixin') {
service.authorize(({ code }) => { service.authorize(({ code }) => {
invoke(callbackId, { _invoke(callbackId, {
code, code,
authResult: '', authResult: '',
errMsg: 'login:ok' errMsg: 'login:ok'
...@@ -42,7 +45,7 @@ export function login (params, callbackId) { ...@@ -42,7 +45,7 @@ export function login (params, callbackId) {
} }
service.login(res => { service.login(res => {
const authResult = res.target.authResult const authResult = res.target.authResult
invoke(callbackId, { _invoke(callbackId, {
code: authResult.code, code: authResult.code,
authResult: authResult, authResult: authResult,
errMsg: 'login:ok' errMsg: 'login:ok'
...@@ -137,9 +140,9 @@ export function operateWXData (params, callbackId) { ...@@ -137,9 +140,9 @@ export function operateWXData (params, callbackId) {
} }
} }
export function preLogin (params, callbackId) { export function preLogin (params, callbackId, plus) {
const successCallback = warpPlusSuccessCallback(callbackId, 'preLogin') const successCallback = warpSuccessCallback(callbackId, 'preLogin', plus)
const errorCallback = warpPlusErrorCallback(callbackId, 'preLogin') const errorCallback = warpErrorCallback(callbackId, 'preLogin', plus)
getService(params.provider).then(service => service.preLogin(successCallback, errorCallback)).catch(errorCallback) getService(params.provider).then(service => service.preLogin(successCallback, errorCallback)).catch(errorCallback)
} }
...@@ -147,9 +150,9 @@ export function closeAuthView () { ...@@ -147,9 +150,9 @@ export function closeAuthView () {
return getService('univerify').then(service => service.closeAuthView()) return getService('univerify').then(service => service.closeAuthView())
} }
export function getCheckBoxState (params, callbackId) { export function getCheckBoxState (params, callbackId, plus) {
const successCallback = warpPlusSuccessCallback(callbackId, 'getCheckBoxState') const successCallback = warpSuccessCallback(callbackId, 'getCheckBoxState', plus)
const errorCallback = warpPlusErrorCallback(callbackId, 'getCheckBoxState') const errorCallback = warpErrorCallback(callbackId, 'getCheckBoxState', plus)
try { try {
getService('univerify').then(service => { getService('univerify').then(service => {
const state = service.getCheckBoxState() const state = service.getCheckBoxState()
...@@ -164,22 +167,92 @@ export function getCheckBoxState (params, callbackId) { ...@@ -164,22 +167,92 @@ export function getCheckBoxState (params, callbackId) {
* 一键登录自定义登陆按钮点击处理 * 一键登录自定义登陆按钮点击处理
*/ */
function univerifyButtonsClickHandling (univerifyStyle, errorCallback) { function univerifyButtonsClickHandling (univerifyStyle, errorCallback) {
if (univerifyStyle && isPlainObject(univerifyStyle) && univerifyStyle.buttons && if (isPlainObject(univerifyStyle) && isPlainObject(univerifyStyle.buttons) && toRawType(univerifyStyle.buttons.list) === 'Array') {
Object.prototype.toString.call(univerifyStyle.buttons.list) === '[object Array]' &&
univerifyStyle.buttons.list.length > 0
) {
univerifyStyle.buttons.list.forEach((button, index) => { univerifyStyle.buttons.list.forEach((button, index) => {
univerifyStyle.buttons.list[index].onclick = function () { univerifyStyle.buttons.list[index].onclick = function () {
closeAuthView().then(() => { const res = {
errorCallback({ code: '30008',
code: '30008', message: '用户点击了自定义按钮',
message: '用户点击了自定义按钮', index,
index, provider: button.provider
provider: button.provider }
isPlainObject(univerifyManager)
? univerifyManager._triggerUniverifyButtonsClick(res)
: closeAuthView().then(() => {
errorCallback(res)
}) })
})
} }
}) })
} }
return univerifyStyle return univerifyStyle
} }
class UniverifyManager {
constructor () {
this.provider = 'univerify'
this.eventName = 'api.univerifyButtonsClick'
}
close () {
closeAuthView()
}
login (options) {
this._warp((data, callbackId) => login(data, callbackId, false), this._getOptions(options))
}
getCheckBoxState (options) {
this._warp((_, callbackId) => getCheckBoxState(_, callbackId, false), options)
}
preLogin (options) {
this._warp((data, callbackId) => preLogin(data, callbackId, false), this._getOptions(options))
}
onButtonsClick (callback) {
UniServiceJSBridge.on(this.eventName, callback)
}
offButtonsClick (callback) {
UniServiceJSBridge.off(this.eventName, callback)
}
_triggerUniverifyButtonsClick (res) {
UniServiceJSBridge.emit(this.eventName, res)
}
_warp (fn, options) {
return callback.warp(fn)(this._getOptions(options))
}
_getOptions (options = {}) {
return Object.assign({}, options, { provider: this.provider })
}
}
export function getUniverifyManager () {
return univerifyManager || (univerifyManager = new UniverifyManager())
}
function warpSuccessCallback (callbackId, name, plus = true) {
return plus
? warpPlusSuccessCallback(callbackId, name)
: (options) => {
callback.invoke(callbackId, Object.assign({}, options, {
errMsg: `${name}:ok`
}))
}
}
function warpErrorCallback (callbackId, name, plus = true) {
return plus
? warpPlusErrorCallback(callbackId, name)
: (error) => {
const { code = 0, message: errorMessage } = error
callback.invoke(callbackId, {
errMsg: `${name}:fail ${errorMessage || ''}`,
errCode: code,
code
})
}
}
...@@ -13,7 +13,8 @@ import { ...@@ -13,7 +13,8 @@ import {
*/ */
export function getLocation ({ export function getLocation ({
type, type,
altitude altitude,
highAccuracyExpireTime
}, callbackId) { }, callbackId) {
const { const {
invokeCallbackHandler: invoke invokeCallbackHandler: invoke
...@@ -24,7 +25,7 @@ export function getLocation ({ ...@@ -24,7 +25,7 @@ export function getLocation ({
if (navigator.geolocation) { if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(res => resolve(res.coords), reject, { navigator.geolocation.getCurrentPosition(res => resolve(res.coords), reject, {
enableHighAccuracy: altitude, enableHighAccuracy: altitude,
timeout: 1000 * 100 timeout: highAccuracyExpireTime || 1000 * 100
}) })
} else { } else {
reject(new Error('device nonsupport geolocation')) reject(new Error('device nonsupport geolocation'))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册