123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import Vue from 'vue'
- import App from './App'
- import store from './store'
- import * as config from './config'
- const baseUrl = config.def().baseUrl
- Vue.config.productionTip = false
- Vue.prototype.request = function(method, endpoint, token, data = {}) {
- let netWork = ''
- uni.getNetworkType({
- success: function(res) {
- netWork = res.networkType
- }
- })
- if (netWork === 'none') {
- uni.showToast({
- title: '当前无网络',
- icon: 'none',
- duration: 1500
- })
- return;
- }
- return new Promise((resolve, reject) => {
- let content = 'application/x-www-form-urlencoded; charset=UTF-8';
- if (method === 'post') {
- content = 'application/json; charset=UTF-8';
- }
- const header = {
- 'Content-Type': content
- }
- if (token) {
- header['Authorization'] = 'Bearer ' + token
- }
- uni.request({
- url: baseUrl + "/" + endpoint,
- method: method,
- data: data,
- header,
- success: function(res) {
- if (res.data.code === 200) {
- resolve(res.data);
- } else if (res.data.code === 401) {
- let pages = getCurrentPages();
- let curPath = `/${pages[pages.length - 1].route}`; // 当前页
- let loginPath = '/pages/login/index'; // 登录页
- store.commit('logout')
- if (curPath === loginPath) {
- resolve(res.data)
- } else {
- uni.redirectTo({
- url: loginPath
- })
- }
- } else if (res.data.code === 500 && res.data.msg === '用户类型不正确') {
- // 特殊处理用户类型不正确的情况,不弹窗,直接返回错误
- reject(res.data)
- } else {
- // 其他错误情况仍然弹窗提示
- uni.showToast({
- title: res.data.msg,
- icon: 'none',
- duration: 1500
- })
- reject(res.data)
- }
- },
- fail: function(err) {
- uni.showToast({
- title: '请稍后再试',
- icon: 'none'
- })
- reject(err)
- }
- })
- })
- }
- Vue.prototype.dateFtt = function(fmt, date) {
- var o = {
- "M+": date.getMonth() + 1, //月份
- "d+": date.getDate(), //日
- "h+": date.getHours(), //小时
- "m+": date.getMinutes(), //分
- "s+": date.getSeconds(), //秒
- "q+": Math.floor((date.getMonth() + 3) / 3), //季度
- "S": date.getMilliseconds() //毫秒
- };
- if (/(y+)/.test(fmt))
- fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
- for (var k in o)
- if (new RegExp("(" + k + ")").test(fmt))
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
- return fmt;
- }
- App.mpType = 'app'
- const app = new Vue({
- store,
- ...App
- })
- app.$mount()
|