提交 7bb0cb54 编写于 作者: fxy060608's avatar fxy060608

refactor service api

上级 c75decad
......@@ -42,8 +42,8 @@ module.exports = {
}),
new webpack.ProvidePlugin({
'console': [resolve('src/core/helpers/console'), 'default'],
'UniViewJSBridge': [resolve('src/core/view/bridge')],
'UniServiceJSBridge': [resolve('src/core/service/bridge')]
'UniViewJSBridge': [resolve('src/core/view/bridge/index')],
'UniServiceJSBridge': [resolve('src/core/service/bridge/index')]
})
]
}
......@@ -41,8 +41,8 @@ config.plugins = config.plugins.concat([
}),
new webpack.ProvidePlugin({
'console': [resolve('src/core/helpers/console'), 'default'],
'UniViewJSBridge': [resolve('src/core/view/bridge')],
'UniServiceJSBridge': [resolve('src/core/service/bridge')]
'UniViewJSBridge': [resolve('src/core/view/bridge/index')],
'UniServiceJSBridge': [resolve('src/core/service/bridge/index')]
})
])
module.exports = config
......@@ -17,7 +17,7 @@ const {
default: uni,
getApp,
getCurrentPages
} = require('uni-service')
} = require('uni-platform/service/index')
global.uni = uni
......@@ -36,4 +36,4 @@ Vue.use(require('uni-view/plugins').default, {
require('uni-core/vue')
require('uni-platform/components')
// require('uni-components')
require('uni-components')
......@@ -83,6 +83,7 @@
"wx": true,
"my": true,
"swan": true,
"weex": true,
"__id__": true,
"__uniConfig": true,
"__uniRoutes": true,
......
<meta charset="utf-8">
<title>index demo</title>
<script src="./index.umd.js"></script>
<link rel="stylesheet" href="./index.css">
<script>
console.log(index)
</script>
此差异已折叠。
因为 它太大了无法显示 source diff 。你可以改为 查看blob
export function pageScrollTo (args) {
const pages = getCurrentPages()
if (pages.length) {
UniServiceJSBridge.publishHandler('pageScrollTo', args, pages[pages.length - 1].$page.id)
}
return {}
}
import api from 'uni-platform/service/api'
export function unpack (args) {
return args
}
export function invoke (...args) {
return UniServiceJSBridge.invokeCallbackHandler(...args)
}
/**
* 执行内部平台方法
*/
export function invokeMethod (name, ...args) {
return api[name].apply(null, args)
}
/**
* 监听 service 层内部平台方法回调,与 publish 对应
* @param {Object} name
* @param {Object} callback
*/
export function onMethod (name, callback) {
return UniServiceJSBridge.on('api.' + name, callback)
}
......@@ -5,7 +5,7 @@ import {
import {
setPullDownRefreshPageId
} from '../api/page-event'
} from 'uni-platform/service/api/ui/pull-down-refresh'
export default function initOn (on, {
getApp,
......
import * as appApi from '../../app-plus/service/api/index'
import * as nvueApi from './api/index'
import * as api from './api/index'
export default Object.assign({}, appApi, nvueApi)
export default api
......@@ -2,6 +2,8 @@ export * from './context/live-pusher'
export * from './context/map'
export * from './context/video'
export * from './plugin/require-native-plugin'
export * from './route/navigate-back'
export * from './route/navigate-to'
export * from './route/re-launch'
......
export function requireNativePlugin (name) {
return weex.requireModule(name)
}
export {
setPullDownRefreshPageId
}
from '../../../../app-plus/service/api/ui/pull-down-refresh'
export function publishHandler (event, args, pageId) {
// TODO
}
export * from '../../app-plus/service/bridge'
export function publishHandler (event, args, pageId) {
// TODO
}
import * as api from './api/index'
export default api
import {
callApiSync,
isTabBarPage,
getLastWebview
} from '../util'
......@@ -11,7 +12,7 @@ import {
import tabbar from '../../framework/tabbar'
export function getSystemInfoSync () {
return getSystemInfo()
return callApiSync(getSystemInfo, Object.create(null), 'getSystemInfo', 'getSystemInfoSync')
}
export function getSystemInfo () {
......
import {
callApiSync
} from '../util'
import {
invoke
} from '../../bridge'
......@@ -8,19 +12,11 @@ let toast = false
let toastTimeout
export function showLoading (args) {
const ret = showToast(args)
if (ret && ret.errMsg) {
ret.errMsg = ret.errMsg.replace('showToast', 'showLoading')
}
return ret
return callApiSync(showToast, args, 'showToast', 'showLoading')
}
export function hideLoading () {
const ret = hideToast()
if (ret && ret.errMsg) {
ret.errMsg = ret.errMsg.replace('hideToast', 'hideLoading')
}
return ret
return callApiSync(hideToast, Object.create(null), 'hideToast', 'hideLoading')
}
export function showToast ({
......
......@@ -4,7 +4,7 @@ import {
let webview
export function setPullDownRefreshWebview (pullDownRefreshWebview) {
export function setPullDownRefreshPageId (pullDownRefreshWebview) {
webview = pullDownRefreshWebview
}
......@@ -25,6 +25,9 @@ export function startPullDownRefresh () {
}
export function stopPullDownRefresh () {
if (!webview) {
webview = getLastWebview()
}
if (webview) {
webview.endPullToRefresh()
webview = null
......
export function callApiSync (api, args, name, alias) {
const ret = api(args)
if (ret && ret.errMsg) {
ret.errMsg = ret.errMsg.replace(name, alias)
}
return ret
}
export function getLastWebview () {
try {
const pages = getCurrentPages()
......
export function requireNativePlugin (name) {
return uni.requireNativePlugin(name)
}
export function unpack (args) {
return args
export {
unpack,
invoke
}
from 'uni-core/service/bridge'
export function invoke (...args) {
return UniServiceJSBridge.invokeCallbackHandler(...args)
}
export function publish (...args) {
return UniServiceJSBridge.publish(...args)
export function requireNativePlugin (name) {
return uni.requireNativePlugin(name)
}
export function publishHandler (event, args, pageId) {
// TODO
/**
* 触发 service 层,与 onMethod 对应
*/
export function publish (name, res) {
return UniServiceJSBridge.emit('api.' + name, res)
}
export function publishHandler (event, args, pageId) {
// TODO
}
......@@ -12,13 +12,9 @@ import {
} from 'uni-helpers/promise'
import baseApi from 'uni-core/service/api'
import platformApi from 'uni-platform/service/api'
const api = Object.create(null)
Object.assign(api, baseApi)
Object.assign(api, platformApi)
const api = Object.assign(Object.create(null), baseApi, platformApi)
const uni = Object.create(null)
......
const api = Object.create(null)
const modules = require.context('./api', true, /\.js$/)
modules.keys().forEach(function (key) {
Object.assign(api, modules(key))
})
export default api
import { getJSONP } from '../../helpers/get-jsonp'
/**
* wgs84坐标转Gcj02坐标
* @param {object} coords
* @param {Function} success
* @param {Function} error
*/
function wgs84ToGcj02 (coords, success, error) {
/**
* uniapp 内置key
*/
var key = __uniConfig.qqMapKey
var url = `https://apis.map.qq.com/ws/coord/v1/translate?locations=${coords.latitude},${coords.longitude}&type=1&key=${key}&output=jsonp`
getJSONP(url, {}, (res) => {
if ('locations' in res && res.locations.length) {
success({
longitude: res.locations[0].lng,
latitude: res.locations[0].lat
})
} else {
error(res)
}
}, error)
}
/**
* 获取定位信息
* @param {*} param0
* @param {*} callbackId
*/
export function getLocation ({
type,
altitude
}, callbackId) {
const {
invokeCallbackHandler: invoke
} = UniServiceJSBridge
function callback (coords) {
invoke(callbackId, Object.assign(coords, {
errMsg: 'getLocation:ok',
verticalAccuracy: coords.altitudeAccuracy || 0,
// 无专门水平精度,使用位置精度替代
horizontalAccuracy: coords.accuracy
}))
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
var coords = position.coords
if (type === 'WGS84') {
callback(coords)
} else {
wgs84ToGcj02(coords, callback, (err) => {
invoke(callbackId, {
errMsg: 'getLocation:fail ' + JSON.stringify(err)
})
})
}
}, () => {
invoke(callbackId, {
errMsg: 'getLocation:fail'
})
}, {
enableHighAccuracy: altitude,
timeout: 1000 * 60 * 5
})
} else {
invoke(callbackId, {
errMsg: 'getLocation:fail device nonsupport geolocation'
})
}
}
export function pageScrollTo (args) {
const pages = getCurrentPages()
if (pages.length) {
UniServiceJSBridge.publishHandler('pageScrollTo', args, pages[pages.length - 1].$page.id)
}
return {}
}
let pageId
export function setPullDownRefreshPageId (pullDownRefreshPageId) {
......
import initOn from './bridge/on'
import initSubscribe from './bridge/subscribe'
import initOn from 'uni-core/service/bridge/on'
import initSubscribe from 'uni-core/service/bridge/subscribe'
import {
uni
......@@ -9,7 +9,7 @@ import {
getApp,
getCurrentPages
}
from './plugins/app'
from 'uni-core/service/plugins/app'
initOn(UniServiceJSBridge.on, {
getApp,
......@@ -26,4 +26,4 @@ export {
getApp,
getCurrentPages
}
from './plugins/app'
from 'uni-core/service/plugins/app'
......@@ -7,31 +7,12 @@ import {
promisify
} from 'uni-helpers/promise'
const api = Object.create(null)
import baseApi from 'uni-core/service/api'
import platformApi from 'uni-platform/service/api'
export const uni = Object.create(null)
const baseApis = require.context(
'./api',
true,
/\.js$/
)
baseApis.keys().forEach(function (key) {
Object.assign(api, baseApis(key))
})
const api = Object.assign(Object.create(null), baseApi, platformApi)
const platformApis = require.context(
'../../platforms/' + __PLATFORM__ + '/service/api',
true,
/\.js$/
)
platformApis.keys().forEach(function (key) {
Object.assign(api, platformApis(key))
})
/* eslint-disable no-undef */
uni.version = __VERSION__
export const uni = Object.create(null)
apis.forEach(name => {
if (api[name]) {
......@@ -40,7 +21,3 @@ apis.forEach(name => {
uni[name] = wrapperUnimplemented(name)
}
})
export function invokeMethod (name, ...args) {
return api[name](...args)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册