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

feat: getLocation add highAccuracyExpireTime、getUniverifyManager

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