import Vue from 'vue' import store from './store' import App from './App' import * as filters from './filters' import * as config from './config' //自定义modal import initModal from "@/components/zhangxu-showModal/initModal.js"; import showModal from '@/components/zhangxu-showModal/show-modal'; initModal(Vue); Vue.component('show-modal', showModal); Object.keys(filters).forEach(key => { Vue.filter(key, filters[key]) }) //#ifdef H5 let jweixin = require('./components/jweixin-module') let jwx = require('./components/jweixin-module/jwx') Vue.mixin({ onShow() { // jwx.configWeiXin(jwx => { // }) } }) //#endif const defConfig = config.def const msg = (title, duration = 1500, mask = false, icon = 'none') => { //统一提示方便全局修改 if (Boolean(title) === false) { return; } uni.showToast({ title, duration, mask, icon }); } let userInfo = undefined const logout = () => { userInfo = undefined uni.removeStorage({ key: 'userInfo' }) } const setUserInfo = (i) => { userInfo = i } const isVip = () => { return userInfo && userInfo.level } let loginLock = false const request = (method, endpoint, data = {}, failCallback) => { //异步请求数据 return new Promise(resolve => { userInfo = uni.getStorageSync('userInfo') let accessToken = userInfo ? userInfo.accessToken : '' let addresses = uni.getStorageSync('addresses') || ''; let baseUrl = config.def().baseUrl let content = 'application/x-www-form-urlencoded; charset=UTF-8'; if (method === 'post') { content = 'application/json; charset=UTF-8'; } uni.request({ url: (endpoint.startsWith('http://') || endpoint.startsWith('https://')) ? endpoint : baseUrl + "/" + endpoint, method: method, data: data, header: { 'Content-Type': content, 'Authorization': 'Bearer ' + accessToken }, success: (res) => { if (endpoint.startsWith('http://') || endpoint.startsWith('https://')) { return resolve(res.data) } if (res.data.code === 200) { //#ifdef H5 if ((!addresses || !addresses.addressesName) && accessToken) { uni.showModal({ title: '地图提示', content: '您尚未选择,您的位置?', showCancel: true, confirmText: '选择', success: (e) => { if (e.confirm) { uni.chooseLocation({ success: (res1) => { console.log(res1) if (res1.name === "") { return } let addressesInfo = {}; addressesInfo.lng = res1.longitude; addressesInfo.lat = res1.latitude; addressesInfo.addressesName = res1.name; uni.setStorageSync('addresses', addressesInfo); uni.reLaunch({ url: '/pages/index/index?type=1' }); }, fail: (e) => { console.log(e) } }); } }, fail: () => {} }) } //#endif resolve(res.data); } else if (res.data.code === 401) { if (failCallback) { failCallback(res.data) } if (!loginLock) { loginLock = true uni.showModal({ title: '登录提示', content: '您尚未登录,是否立即登录?', showCancel: true, confirmText: '登录', success: (e) => { if (e.confirm) { uni.navigateTo({ url: '/pages/public/login' }) } }, fail: () => {}, complete: () => { loginLock = false } }) } } else { if (failCallback) { failCallback(res.data) } else { uni.showToast({ title: res.data.msg, icon: 'none' }) } } } }) }) } const uploadImg = (num = 9, successCallback, failCallback= undefined) => { userInfo = uni.getStorageSync('userInfo'); var accessToken = userInfo ? userInfo.accessToken : ''; let baseUrl = config.def().baseUrl uni.chooseImage({ count: 1, sourceType: ['album'], success: res => { uni.getImageInfo({ src: res.tempFilePaths[0], success: image => { console.log(image) uni.showLoading({ title: '图片上传中', mask: true }) uni.uploadFile({ url: baseUrl + `/oss/app/upload`, file: image, filePath: image.path, header: { Authorization: 'Bearer ' + accessToken, }, name: 'file', success: res => { if (successCallback) { successCallback(JSON.parse(res.data).data.url) } }, fail: err => { if (failCallback) { failCallback(err) } else { uni.showToast({ title: '上传图片失败', icon: 'none', duration: 2000, }) } }, complete: res => { uni.hideLoading() }, }) }, fail: err => { uni.showToast({ title: '获取图片信息失败', icon: 'none', duration: 2000, }) }, }) }, }) } // 新增:直接上传文件路径的方法(不再弹出选择图片对话框) const uploadImgByPath = (filePath, successCallback, failCallback = undefined) => { userInfo = uni.getStorageSync('userInfo'); var accessToken = userInfo ? userInfo.accessToken : ''; let baseUrl = config.def().baseUrl uni.getImageInfo({ src: filePath, success: image => { console.log(image) uni.showLoading({ title: '图片上传中', mask: true }) uni.uploadFile({ url: baseUrl + `/oss/app/upload`, file: image, filePath: image.path, header: { Authorization: 'Bearer ' + accessToken, }, name: 'file', success: res => { if (successCallback) { successCallback(JSON.parse(res.data).data.url) } }, fail: err => { if (failCallback) { failCallback(err) } else { uni.showToast({ title: '上传图片失败', icon: 'none', duration: 2000, }) } }, complete: res => { uni.hideLoading() }, }) }, fail: err => { uni.showToast({ title: '获取图片信息失败', icon: 'none', duration: 2000, }) }, }) } function get_suffix(filename) { var pos = filename.lastIndexOf('.') var suffix = '' if (pos != -1) { suffix = filename.substring(pos) } return suffix; } function random_string(len) { len = len || 32; var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; var maxPos = chars.length; var pwd = ''; for (var i = 0; i < len; i++) { pwd += chars.charAt(Math.floor(Math.random() * maxPos)); } return pwd; } const prePage = () => { let pages = getCurrentPages(); let prePage = pages[pages.length - 2]; // #ifdef H5 return prePage; // #endif return prePage.$vm; } const globalData = {} let deviceType = '' // #ifdef MP-WEIXIN // 微信小程序编译的代码 deviceType = 'routine' // #endif // #ifdef APP-PLUS // App平台编译的代码 deviceType = 'app' Vue.prototype.$platform = uni.getSystemInfoSync().platform // #endif // #ifdef H5 // H5编译的代码 // 判断是否是微信浏览器 async function init() { if (isWeixin()) { deviceType = 'weixin' let wechatInit = wechat() if (wechatInit) { await oAuth() } } else { deviceType = 'weixinh5' } } init() // #endif Vue.config.productionTip = false Vue.prototype.$fire = new Vue(); Vue.prototype.$store = store; Vue.prototype.$deviceType = deviceType Vue.prototype.$api = { msg, prePage, request, uploadImg, uploadImgByPath, logout, isVip, setUserInfo, defConfig, globalData }; //#ifdef H5 Vue.prototype.$jweixin = jweixin; //#endif //zj 2.17 统一获取地理信息 //isChoose 是否 chooseLocation 接口返回的经纬度 该接口返回的address没做省市区划分 Vue.prototype.$getLocation = async (isChoose = false, latitude = '', longitude = '') => { var ret = {} if (isChoose) { ret.latitude = res.latitude ret.longitude = res.longitude } else { var [err, res] = await uni.getLocation({ type: 'wgs84' }) if (res && res.errMsg === 'getLocation:ok') { ret.latitude = res.latitude ret.longitude = res.longitude } else { console.log(err.errMsg) return false } } var that = this; var [error, geocodeData] = await uni.request({ url: "https://restapi.amap.com/v3/geocode/regeo", method: "GET", data: { key: that.$api.defConfig().mapKey, location: ret.longitude + ',' + ret.latitude, extensions: 'all', // poitype:'120000|060000'返回poi限定范围 } }) if (geocodeData.statusCode === 200) { console.log(geocodeData) var data = geocodeData.data.regeocode.addressComponent for (var key in data) { ret[key] = data[key] } ret.pois = geocodeData.data.regeocode.pois return ret } else { console.log("高德地图报错信息error:"+error) console.log("高德地图报错信息geocodeData:"+geocodeData) return false } } App.mpType = 'app' const app = new Vue({ ...App }) store.commit('updateDevicetype', deviceType) app.$mount()