1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- import config from "@/config";
- import storage from "./storage";
- import store from "../store/index"
- export default {
- console(options) {
- // if(config.debug){
- // console.log("====================【request start】===========================");
- // console.log("header: " + JSON.stringify(options.header));
- // console.log("method: " + options.method + " URL: " + options.url);
- // console.log(options.data);
- // console.log("====================【request end】===========================");
- // }
- },
- domain() {
- return config.uni_app_web_api_url.replace("api", "");
- },
- send(options = {}) {
- options.url = config.uni_app_web_api_url + '' + options.url;
- options.method = options.method || "GET";
- options.timeout=5000
- let users = storage.getJson("userId");
- let token = storage.getJson("token");
- if (users != null && token != null) {
- options.header = {
- "Authorization": token
- };
- }
- this.console(options);
- return new Promise((resolve, reject) => {
- // uni.showLoading({
- // title: '加载中...',
- // mask: true
- // });
-
- uni.request(options).then(data => {
- var [error, res] = data;
- // console.log(error,'data')
- if (error != null) {
- reject(error);
- if(error.errMsg.includes('timeout')){
- uni.showToast({
- title: '请求接口失败,请检查IP地址!',
- icon: 'none',
- })
- }else{
- uni.showToast({
- title: '网络错误!',
- icon: 'none',
- })
- }
- } else {
- if (res.data.code == -3) {
- storage.remove("users")
- storage.remove("token")
- store.commit("DELETEUSERS")
- uni.reLaunch({
- url: '/pages/public/login'
- })
- } else {
- resolve(res.data);
- }
- }
- // uni.hideLoading();
- });
-
- });
- },
- get(url = "", data = {}) {
- return this.send({
- url: url,
- data: data
- });
- },
- post(url = "", data = {}) {
- return this.send({
- url: url,
- data: data,
- method: "POST",
- });
- }
- };
|