1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import store from './store'
- const baseUrl = process.env.NODE_ENV === 'development' ? "http://192.168.1.101:8000":"http://sxzg.ngrok.ssly.site";
- const baseApiUrl = baseUrl + "/api";
- function initPramas() {
- arguments[0].url = baseApiUrl + arguments[0].url;
- arguments[0].timeout = 10000
- // console.log(typeof arguments[0].fail)
- const fail = arguments[0].fail
- arguments[0].fail = (res) => {
- console.log(res)
- let msg = '请求失败'
- if (res.data && res.data.msg) {
- msg = res.data.msg
- }
- uni.showToast({
- title: msg,
- icon: "none"
- })
- fail(res)
- }
- const success = arguments[0].success
- arguments[0].success = (res) => {
- console.log(res)
- if (typeof res.data === "string") {
- try {
- res.data = JSON.parse(res.data)
- } catch (err) {}
- }
- if (res.data.code === 0) {
- uni.showToast({
- title: res.data.msg,
- icon: "none"
- })
- arguments[0].fail(res)
- }
- if (res.data.code === 1) {
- success(res)
- }
- if (res.data.code === 401 || res.statusCode === 401) {
- uni.navigateTo({
- url: "/pages/user/login"
- })
- }
- }
- if (typeof arguments[0].header === "undefined") {
- arguments[0].header = {}
- }
- if (store.state.user.token) {
- arguments[0].header["token"] = store.state.user.token;
- }
- return arguments[0];
- }
- export default {
- baseUrl: baseUrl,
- get: function() {
- let obj = initPramas(arguments[0]);
- obj.method = "GET";
- uni.request(obj);
- },
- post: function() {
- let obj = initPramas(arguments[0]);
- obj.method = "POST";
- obj.header["Content-type"] = "application/x-www-form-urlencoded"
- console.log(obj);
- uni.request(obj);
- },
- upload: function() {
- let obj = initPramas(arguments[0]);
- obj.url = baseApiUrl + '/common/upload';
- uni.uploadFile(obj)
- }
- }
|