123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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;
- }
- const uploadImgByPath = (filePath, token, successCallback, failCallback = undefined) => {
- 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 ' + token,
- },
- 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,
- })
- },
- })
- }
- Vue.prototype.$api = {
- uploadImgByPath
- };
- App.mpType = 'app'
- const app = new Vue({
- store,
- ...App
- })
- app.$mount()
|