import Vue from 'vue' import config from './config' import page from './page' import pic from './pic' const dever = { // 运行平台 platform: 'h5', // 运行环境标识 2是h5独立浏览器、3是jsapi、4是app、5是小程序、6是pc env: 1, // 当前主机域名 host: '', page, pic, // 获取当前时间戳 curTime: () => Math.floor(Date.now() / 1000), // 更新全局配置 upConfig(update) { if (!config.base.init) return return new Promise((resolve) => { const $config = this.data('dever_config') if (!$config || (update && $config.u != update)) { Vue.prototype.DeverApi.request('GET', config.base.init, { noloading: 1 }).then((res) => { this.data('dever_config', res); resolve(res) }) } else { resolve($config) } }) }, // 获取全局配置 config() { return new Promise((resolve) => { const config = this.data('dever_config') if (config) { resolve(config) } else { this.goHome(); } }) }, // 初始化来源 init() { //#ifdef H5 const ua = navigator.userAgent.toLowerCase() if (ua.includes('micromessenger')) { this.platform = 'weixin' this.env = 3 } else if (ua.includes('alipayclient')) { this.platform = 'alipay' this.env = 3 } else { this.platform = 'h5' this.env = 2 } this.host = `${window.location.protocol}//${window.location.host}/#` //#endif //#ifdef APP-PLUS //ios或者android this.platform = uni.getSystemInfoSync().platform this.env = 4 //#endif //#ifdef MP-WEIXIN this.platform = 'weixin' this.env = 5 //#endif //#ifdef MP-ALIPAY this.platform = 'alipay' this.env = 5 //#endif //#ifdef MP-BAIDU this.platform = 'baidu' this.env = 5 //#endif //#ifdef MP-TOUTIAO this.platform = 'toutiao' this.env = 5 //#endif }, // 页面跳转 location(path, go = '', title) { if (path.includes('http')) { this.debug('http:' + path) this.data('web_view', path) title && this.data('web_view_title', title) if ([4, 5].includes(this.env) || go === 'webview') { uni.navigateTo({ url: config.path.webView }) } else if (go.includes('/')) { window.location.href = path + (path.includes('?') ? '&' : '?') + 'refer=' + btoa(this.host + go) } else { window.location.href = path + (path.includes('?') ? '&' : '?') + 'refer=' + btoa(window.location .href + go) } } else { const route = 'pages/' + path const pages = getCurrentPages() const index = pages.findIndex(p => p.route == route) if (index !== -1) { const delta = pages.length - 1 - index console.info(route, pages, index, delta) uni.navigateBack({ delta }) } else { path = path.includes('/pages/') ? path : '/pages/' + path if (config.switchTab.list.includes(path)) { uni.switchTab({ url: path, success: (e) => config.switchTab.call[path]?.(this, e) }) } else if (go && go !== 'webview') { uni.redirectTo({ url: path }) } else { uni.navigateTo({ url: path }) } } } }, goHome() { this.location(config.path.home, 'go') }, goLogin(refer) { this.data('refer', page.refer(refer)) this.location(config.path.login) }, goUser(refer) { this.data('refer', page.refer(refer)) this.location(config.path.user) }, goBindMobile(refer) { this.data('refer', page.refer(refer)) this.location(config.path.bindMobile) }, goBindEmail(refer) { this.data('refer', page.refer(refer)) this.location(config.path.bindEmail) }, goTabbar(item) { config.tabbar.go(this, item) }, success(info, callback) { this.alert(info, 'success', callback) }, alert(info, icon = 'none', callback) { this.debug('alert:' + info) info && uni.showToast({ title: info, icon, duration: 1000, success: callback }) return false }, confirm(text, callback, errCallback) { uni.showModal({ content: text, cancelText: '取消', confirmText: '确定', success: res => { if (res.confirm) { callback() } else if(res.cancel && errCallback) { errCallback() } } }) }, debug(string) { config.base.debug && console.log(string) }, log: console.log.bind(console), data(key, value) { if (value !== undefined) { if (value === 'del') return uni.removeStorageSync(key) return uni.setStorageSync(key, value) } return uni.getStorageSync(key) }, jump() { let refer = this.data('refer') || config.path.home const id = this.data('invite') if (id && refer !== config.path.home) refer += `?id=${id}&type=${type}` this.data('refer', 'del') this.data('invite', 'del') this.location(refer, 'go') }, xing: s => s.replace(/./g, '*'), quit(page, call) { if (this.env == 4) { const main = plus.android.runtimeMainActivity() plus.nativeUI.toast = str => { if (str === '再按一次退出应用') { call ? call(main) : page.$mp.page.$getAppWebview().children()[0].back() return false } else this.alert(str) } } }, sleep: time => new Promise(resolve => setTimeout(resolve, time)), copy(content) { uni.setClipboardData({ data: content, success: () => uni.showToast({ title: '复制成功' }) }) }, formatDate: function(date) { var date = new Date(date); var YY = date.getFullYear() + '-'; var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()); var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'; var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'; var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()); return YY + MM + DD + " " + hh + mm + ss; }, } export default dever