uniapp封装的request请求和时间格式处理

以下内容直接放入main.js中,方法都是挂载在vue原型上的,使用this直接调用

import Vue from 'vue'

import App from './App'

Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({

...App,

})

app.$mount()

Vue.prototype.URL = 'http://x x x x x x x x/'     公共请求头部分

Vue.prototype.success = function(statusCode, data, user, success) {

if (statusCode === 200) {

this.Show.hide()

if (data.code === -4001) {

let page = getCurrentPages()[getCurrentPages().length - 1]

let url = '/' + page.route + Object.entries(page.options).map(i => '&' + i.join('=')).join('').replace('&', '?')

if (user) {

this.Show.toast('登录过期,重新授权中', 'none', true)

uni.login({

success: ({

code

}) => {

this.post('wxapp/login', {

login_code: code

}, ({

data

}) => {

this.Storage.set('X-User', data, this.Route[url.split('/')[3] === 'index' ? 're' : 'direct'](url))

})

}

})

} else {

this.Storage.set('url', url, this.Route.direct('/pages/login/login'))

}

} else {

success && success(data)

}

} else {

this.Show.toast('请求失败')

}

}

Vue.prototype.get = function(url, data, success) {

if (arguments.length === 2) {

data = {}

success = arguments[1]

}

let header = {}

let user = this.Storage.get('userinfo')

if (user) {

// console.log(user)

// 设置默认的data中的token,作为参数传递

data.__bearerToken = user.BearerToken;

// 请求头中设置token

// header['Authorization'] = 'BearerToken '+ user.BearerToken;

// console.log(user.BearerToken)

} else {

console.log('数据请求中没有token')

}

this.Show.loading('加载中')

uni.request({

url: this.URL + url,

data,

header,

success: ({

statusCode,

data

}) => this.success(statusCode, data, user, success),

fail: (e) => {

console.log(e)

this.Show.toast('网络异常1')

}

})

}

Vue.prototype.post = function(url, data, success) {

let header = {

'Content-Type': 'application/x-www-form-urlencoded'

}

let user = this.Storage.get('userinfo')

if (user) {

// header['X-Sign'] = user.sign

header['SessionId'] = user.SessionId

}

this.Show.loading('提交中')

uni.request({

url: this.URL + url,

method: 'POST',

data,

header,

success: ({

statusCode,

data

}) => this.success(statusCode, data, user, success),

fail: () => {

this.Show.toast('网络异常')

}

})

}

Vue.prototype.upload = function(fileurl, success) {

this.Show.loading('上传中')

uni.uploadFile({

url: this.URL + 'upload',

fileurl,

name: 'Filedata',

success: ({

statusCode,

data

}) => {

if (statusCode === 200) {

this.Show.hide()

success && success(JSON.parse(data).data[0])

} else {

this.Show.toast('上传失败')

}

},

fail: () => {

this.Show.toast('网络异常')

}

})

}

Vue.prototype.Route = {

to(url) {

uni.navigateTo({

url

})

},

back() {

uni.navigateBack()

},

direct(url) {

uni.redirectTo({

url

})

},

tab(url) {

uni.switchTab({

url

})

},

re(url) {

uni.reLaunch({

url

})

}

}

Vue.prototype.Show = {

loading(title) {

uni.showLoading({

title,

mask: true

})

},

hide() {

uni.hideLoading()

},

toast(title, icon, mask) {

uni.showToast({

title,

icon: icon || 'none',

mask

})

}

}

Vue.prototype.Storage = {

set(key, data, success) {

uni.setStorage({

key,

data,

success

})

},

get(key) {

return uni.getStorageSync(key)

},

remove(key) {

uni.removeStorageSync(key)

}

}

Vue.prototype.pay = function(id, success) {

this.get('pay/payment?gateway=miniapp&type=wechat', {

id

}, ({

data

}) => {

uni.requestPayment(Object.assign(data.payment, {

success,

fail: () => {

this.Show.toast('支付未完成')

}

}))

})

}

// 时间格式处理

Date.prototype.formater = function(fmt) {

var o = {

"M+": this.getMonth() + 1, //月份       

"d+": this.getDate(), //日       

"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时       

"H+": this.getHours(), //小时       

"m+": this.getMinutes(), //分       

"s+": this.getSeconds(), //秒       

"q+": Math.floor((this.getMonth() + 3) / 3), //季度       

"S": this.getMilliseconds() //毫秒       

};

var week = {

"0": "/u65e5",

"1": "/u4e00",

"2": "/u4e8c",

"3": "/u4e09",

"4": "/u56db",

"5": "/u4e94",

"6": "/u516d"

};

if (/(y+)/.test(fmt)) {

fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));

}

if (/(E+)/.test(fmt)) {

fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") +

week[this.getDay() + ""]);

}

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;

}

你可能感兴趣的:(uniapp封装的request请求和时间格式处理)