|
|
-
- import Vue from 'vue'
-
-
- function http(uri, data, callback, method = 'GET', showLoading, title) {
-
- if(showLoading){
- uni.showLoading({
- title: title || '正在提交...'
- });
- }
-
- uni.request({
- url: Vue.prototype.$config.baseUrl + uri,
- data: enhanceData(data),
- method: method,
- header: {
- 'X-Access-Token': uni.getStorageSync('token'),
- 'Content-Type' : method == 'POST' ? 'application/x-www-form-urlencoded' : 'application/json'
- },
- success: (res) => {
-
- if(showLoading){
- uni.hideLoading();
- }
-
- if(res.statusCode == 401){
- uni.removeStorageSync('token');
- uni.removeStorageSync('userInfo');
- console.error('登录过期');
- uni.navigateTo({
- url: '/pages/login/studentLogin'
- })
- }
-
- if(res.statusCode == 200 && res.data.code != 200){
- uni.showToast({
- mask: true,
- duration: 1000,
- title: res.data.message,
- });
- }
-
- callback(res.data)
- },
-
- fail: () => {
- uni.showLoading({})
- setTimeout(()=>{
- uni.hideLoading()
- uni.showToast({icon:"none", title:"网络异常"})
- }, 3000)
-
- if(showLoading){
- uni.hideLoading();
- }
- }
- });
- }
-
- function deleted(uri, data, callback) {
- http(uri, data, callback, 'DELETE')
- }
-
- function post(uri, data, callback) {
- http(uri, data, callback, 'POST')
- }
-
- function get(uri, data, callback) {
- http(uri, data, callback, 'GET')
- }
-
- function enhanceData(data) {
- const userid = uni.getStorageSync("userid")
- if (!data) {
- data = {}
- }
- if (userid) {
- data.userid = userid
- }
- return data
- }
-
-
-
-
-
- function sync(method, uri, data) {
- return new Promise((resolve, reject) => {
- uni.request({
- url: uri,
- data: data,
- method: method,
- header: {
- 'auth': '1AS9F1HPC4FBC9EN00J7KX2L5RJ99XHZ'
- },
- success: (res) => {
- resolve(res.data)
- },
- fail: (err) => {
- reject(err);
- }
- })
- })
- }
-
-
- let cache = null
-
- function async (method, uri, data) {
- const promise = sync(method, uri, data).then(res => {
- cache = res
- }).catch(err => {
-
- })
- }
-
-
- function syncHttp(uri, data, method = 'GET') {
- async (method, uri, data)
- }
-
-
-
-
-
-
- export default {
- http: http,
- delete: deleted,
- post: post,
- get: get,
- syncHttp: syncHttp
- }
|