import gloablConfig from '@/common/config.js' import { getUrlQuery, objToUrlQuery } from '@/util/squ.js' const globalKeyPrefix = gloablConfig.key ? gloablConfig.key + '-' : 'squni-' /** * 缓存全局APP实例对象(vm) */ let APP = null // ====== 存储 // uni.getStorageInfo uni.getStorageInfoSync uni.clearStorageSync 略 export const setStorage = config => uni.setStorage({ key: globalKeyPrefix + config.key, data: config.data, success: config.success, fail: config.fail, complete: config.complete }) export const setStorageSync = (key, value) => uni.setStorageSync(globalKeyPrefix + key, value) export const getStorage = config => uni.getStorage({ key: globalKeyPrefix + config.key, success: config.success, fail: config.fail, complete: config.complete }) export const getStorageSync = key => uni.getStorageSync(globalKeyPrefix + key) export const removeStorage = config => uni.removeStorage({ key: globalKeyPrefix + config.key, success: config.success, fail: config.fail, complete: config.complete }) export const removeStorageSync = key => uni.removeStorageSync(globalKeyPrefix + key) // ====== 路由 export const setApp = (vm) => { APP = vm } export const getApp = () => { return APP } export const switchTab = (path, config) => { uni.switchTab({ url: path, success: config && config.success, fail: config && config.fail, complete: config && config.complete }) } export const navigateTo = (path, config) => { uni.navigateTo({ url: path, animationType: config && config.animationType, animationDuration: config && config.animationDuration, events: config && config.events, success: config && config.success, fail: config && config.fail, complete: config && config.complete }) } export const navigateBack = (vm) => { // 从分享打开的,返回首页. 可引入wx-share.js if (vm.share == true) { redirectHome() } else { // 不是从分享打开的,返回上一页 uni.navigateBack({ delta: 1, }) } } export const redirectTo = (path, config) => { uni.redirectTo({ url: path, success: config && config.success, fail: config && config.fail, complete: config && config.complete }) } export const redirectHome = () => { if (gloablConfig.indexType === 'Tab') { uni.switchTab({ url: gloablConfig.indexPath }) } else { uni.redirectTo({ url: gloablConfig.indexPath }) } } export const routeWithParams = (path, method) => { method = method || 'redirectTo' const params = getCurQueryAll() const _originHref = params['_originHref'] || window.location.href params['_originHref'] = _originHref uni[method]({ url: path + '?' + objToUrlQuery(params) }) } /** * 获取当前页面请求路径 * 返回: { $vm, route, options, ... } */ export const getCurPage = () => { // uni-app内置函数: https://uniapp.dcloud.net.cn/api/window/window.html#getcurrentpages const pages = getCurrentPages() return (pages && pages.length > 0) ? pages[pages.length - 1] : {} } /** * 获取上个页面 * 返回: { $vm, route, options, ... } | null */ export const getPrePage = () => { const pages = getCurrentPages() return pages && pages.length > 1 ? pages[pages.length - 2] : null } /** * 获取当前页面请求路径所有参数 */ export const getCurQueryAll = () => { const curPage = getCurPage() // 在微信小程序或是app中,通过curPage.options;如果是H5,则需要curPage.$route.query let params = curPage.options || (curPage.$route && curPage.$route.query) // 有时候H5会出现 curPage.options = {} if (params == null // #ifdef H5 || Object.getOwnPropertyNames(params).length === 0 // #endif ) { params = getUrlQuery() } return params } /** * 获取当前页面请求路径参数. 可参考 squ.js 中的 getUrlQuery */ export const getCurQuery = (name) => { const query = getCurQueryAll() return query ? query[name] : null } // ====== 其他 export const copy = (value, msg) => { uni.setClipboardData({ data: value, success: () => { uni.showToast({ title: msg || '复制成功', duration: 800 }) } }) } /** * 信息提示更简洁 */ export const toast = (message, icon, config) => { config = config || {} config.title = message config.icon = icon || 'none' uni.showToast(config) } /** * 滚动页面到底部。如聊天时 */ export const scrollToBottom = () => { // 要加点延迟, 不然有可能不生效 setTimeout(() => { uni.pageScrollTo({ scrollTop: 999999, duration: 0 }) }, 50) } export const shareWeb = () => { let url = window.location.protocol + '//' + window.location.host + window.location.pathname const query = getCurQueryAll() query.inviterUserId = getStorageSync('userId') url = url + '?' + objToUrlQuery(query) copy(url, '已复制分享链接,快去分享吧~') } export default { globalKeyPrefix, setStorageSync, getStorageSync, removeStorageSync, setApp, getApp, switchTab, navigateTo, navigateBack, redirectTo, redirectHome, routeWithParams, getCurPage, getPrePage, getCurQuery, getCurQueryAll, copy, toast, shareWeb }