213 lines
4.9 KiB
JavaScript
213 lines
4.9 KiB
JavaScript
import Vue from 'vue'
|
|
import App from './App'
|
|
import store from './store'
|
|
import router from './router'
|
|
import { RouterMount } from './js_sdk/hhyang-uni-simple-router/index.js';
|
|
import ElementUI from 'element-ui';
|
|
import 'element-ui/lib/theme-chalk/index.css';
|
|
Vue.use(ElementUI);
|
|
|
|
import $mUtils from './common/utils.js';
|
|
import config from './config.js'
|
|
Vue.prototype.$mUtils = $mUtils;
|
|
Vue.prototype.config = config;
|
|
|
|
|
|
Vue.config.productionTip = false
|
|
|
|
App.mpType = 'app'
|
|
Vue.prototype.$store = store
|
|
|
|
const app = new Vue({
|
|
store,
|
|
...App
|
|
})
|
|
//v1.3.5起 H5端 你应该去除原有的app.$mount();使用路由自带的渲染方式
|
|
// #ifdef H5
|
|
RouterMount(app,'#app'); //使用router配置的路由
|
|
// #endif
|
|
// #ifndef H5
|
|
app.$mount(); //为了兼容小程序及app端必须这样写才有效果 使用pages.json的path路由
|
|
// #endif
|
|
|
|
|
|
Vue.prototype.websiteUrl = config.app_url;
|
|
Vue.prototype.app_id = config.app_id;
|
|
//h5发布路径
|
|
Vue.prototype.h5_addr = config.h5_addr;
|
|
|
|
//#ifdef H5
|
|
app.$router.beforeResolve((to,from,next)=>{
|
|
let mode = uni.getSystemInfoSync().model;
|
|
if(mode!='PC'){
|
|
// location.assign('/h5/#'+to.fullPath);
|
|
next();
|
|
}else{
|
|
next();
|
|
}
|
|
})
|
|
// app.$router.afterEach((to, from) => {
|
|
// const u = navigator.userAgent.toLowerCase()
|
|
// if (u.indexOf("like mac os x") < 0 || u.match(/MicroMessenger/i) != 'micromessenger') return
|
|
// if (to.path !== global.location.pathname) {
|
|
// location.assign(config.h5_addr + to.fullPath);
|
|
// }
|
|
// })
|
|
//#endif
|
|
|
|
//get请求
|
|
Vue.prototype._get = function(path, data, success, fail, complete) {
|
|
data = data || {};
|
|
data.token = uni.getStorageSync('token') || '';
|
|
data.app_id = this.getAppId();
|
|
uni.request({
|
|
url: this.websiteUrl + '/api/' + path,
|
|
data: data,
|
|
dataType: 'json',
|
|
method: 'GET',
|
|
success: (res) => {
|
|
if (res.statusCode !== 200 || typeof res.data !== 'object') {
|
|
return false;
|
|
}
|
|
if (res.data.code === -1) {
|
|
// 登录态失效, 重新登录
|
|
console.log('登录态失效, 重新登录');
|
|
this.doLogin();
|
|
} else if (res.data.code === 0) {
|
|
fail && fail(res);
|
|
// this.showError(res.data.msg, function() {
|
|
// fail && fail(res);
|
|
// });
|
|
return false;
|
|
} else {
|
|
success && success(res.data);
|
|
}
|
|
},
|
|
fail: (res) => {
|
|
fail && fail(res);
|
|
},
|
|
complete: (res) => {
|
|
uni.hideLoading();
|
|
complete && complete(res);
|
|
},
|
|
});
|
|
};
|
|
|
|
//post请求
|
|
Vue.prototype._post = function(path, data, success, fail, complete) {
|
|
data = data || {};
|
|
data.token = uni.getStorageSync('token') || '';
|
|
data.app_id = this.getAppId();
|
|
uni.request({
|
|
url: this.websiteUrl + '/api/' + path,
|
|
data: data,
|
|
dataType: 'json',
|
|
method: 'POST',
|
|
header: {
|
|
'content-type': 'application/x-www-form-urlencoded',
|
|
},
|
|
success: (res) => {
|
|
if (res.statusCode !== 200 || typeof res.data !== 'object') {
|
|
return false;
|
|
}
|
|
if (res.data.code === -1) {
|
|
// 登录态失效, 重新登录
|
|
console.log('登录态失效, 重新登录');
|
|
|
|
this.doLogin();
|
|
} else if (res.data.code === 0) {
|
|
fail && fail(res);
|
|
// this.showError(res.data.msg, function() {
|
|
// fail && fail(res);
|
|
// });
|
|
return false;
|
|
} else {
|
|
success && success(res.data);
|
|
}
|
|
},
|
|
fail: (res) => {
|
|
console.log(res,333)
|
|
fail && fail(res);
|
|
},
|
|
complete: (res) => {
|
|
uni.hideLoading();
|
|
complete && complete(res);
|
|
},
|
|
});
|
|
};
|
|
Vue.prototype.doLogin = function() {
|
|
uni.showToast({
|
|
icon:'none',
|
|
title: '登录态失效,请重新登录',
|
|
duration:1500
|
|
})
|
|
setTimeout(() => {
|
|
uni.reLaunch({
|
|
url: '/pages/login' // 替换为你实际的首页路径
|
|
});
|
|
}, 1600);
|
|
return ;
|
|
};
|
|
/**
|
|
* 获取应用ID
|
|
*/
|
|
Vue.prototype.getAppId = function() {
|
|
return this.app_id || 10001;
|
|
};
|
|
|
|
Vue.prototype.isWeixin = function(){
|
|
var ua = navigator.userAgent.toLowerCase();
|
|
if(ua.match(/MicroMessenger/i)=="micromessenger") {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
|
|
Vue.prototype.goPath = function(e){
|
|
if(/^http(s)?:.+/.test(e)){
|
|
let detail = {
|
|
url: e
|
|
}
|
|
let str = JSON.stringify(detail);
|
|
str = str.replace(/%/g, '%25');
|
|
uni.navigateTo({
|
|
url: "/pages/index/xiazai?detailData=" + encodeURIComponent(str)
|
|
});
|
|
}else{
|
|
uni.navigateTo({
|
|
url: e
|
|
});
|
|
}
|
|
};
|
|
Vue.prototype.goBack = function(delta=1){
|
|
if(typeof delta == 'object'){
|
|
delta = 1;
|
|
}
|
|
uni.navigateBack({
|
|
delta: delta
|
|
});
|
|
}
|
|
Vue.prototype.router_push = (router)=>{
|
|
Vue.prototype.$Router.push(router);
|
|
}
|
|
Vue.prototype.router_replace = (router)=>{
|
|
Vue.prototype.$Router.replace(router);
|
|
}
|
|
Vue.prototype.router_replaceAll = (data)=>{
|
|
Vue.prototype.$Router.replaceAll(data);
|
|
}
|
|
Vue.prototype.router_pushTab = (router)=>{
|
|
Vue.prototype.$Router.pushTab(router);
|
|
}
|
|
Vue.prototype.router_back = (router,obj)=>{
|
|
Vue.prototype.$Router.back(router,obj);
|
|
}
|
|
if(process.env.NODE_ENV === 'development'){
|
|
Vue.prototype.directory = '/';
|
|
}
|
|
if(process.env.NODE_ENV === 'production'){
|
|
Vue.prototype.directory = '/';
|
|
}
|