131 lines
2.9 KiB
JavaScript
131 lines
2.9 KiB
JavaScript
import Vue from 'vue'
|
||
import Vuex from 'vuex'
|
||
Vue.use(Vuex)
|
||
|
||
|
||
const store = new Vuex.Store({
|
||
state: {
|
||
userInfo: uni.getStorageSync('userInfo'),
|
||
customInfo: uni.getStorageSync('customInfo'),
|
||
customer: uni.getStorageSync('customer')
|
||
},
|
||
mutations: {
|
||
set_user_info(state, info) {
|
||
state.userInfo = info ? info : uni.getStorageSync('userInfo');
|
||
},
|
||
set_customer_info(state, info) {
|
||
state.customInfo = info ? info : uni.getStorageSync('customer');
|
||
},
|
||
|
||
},
|
||
actions: {
|
||
goout(state, data) {
|
||
if (data.code == '401') {
|
||
if (uni.getStorageSync('token')) {
|
||
uni.showToast({
|
||
title: '当前登录已失效,请重新登录后再试',
|
||
icon: "none",
|
||
duration: 3000
|
||
});
|
||
} else {
|
||
uni.showToast({
|
||
title: '您暂未登录,请先登录',
|
||
icon: "none",
|
||
duration: 3000
|
||
});
|
||
}
|
||
|
||
} else {
|
||
uni.showToast({
|
||
title: data.msg,
|
||
icon: "none",
|
||
duration: 3000
|
||
});
|
||
}
|
||
},
|
||
majax(state, {
|
||
url,
|
||
params,
|
||
method
|
||
}) {
|
||
if (uni.getStorageSync('networkType') == 'none') {
|
||
uni.showToast({
|
||
title: '检查网络是否连接',
|
||
icon: "none"
|
||
});
|
||
return false;
|
||
}
|
||
//返回异步加载结果
|
||
return new Promise((resolve, reject) => {
|
||
try {
|
||
uni.request({
|
||
url: url,
|
||
method: method,
|
||
data: params,
|
||
header: {
|
||
// 'token': uni.getStorageSync('token'),
|
||
}
|
||
}).then(data => { //data为一个数组,数组第一项为错误信息,第二项为返回数据
|
||
let [error, res] = data;
|
||
if (res) {
|
||
// let jsRes = JSON.parse(res.data.replace(/'/g,"\""))
|
||
let jsRes = res.data
|
||
resolve(jsRes);
|
||
} else {
|
||
resolve({
|
||
"msg": "请求失败!"
|
||
});
|
||
}
|
||
})
|
||
} catch (error) {
|
||
reject(error)
|
||
}
|
||
})
|
||
},
|
||
najax(state, {
|
||
url,
|
||
params,
|
||
method
|
||
}) {
|
||
if (uni.getStorageSync('networkType') == 'none') {
|
||
uni.showToast({
|
||
title: '检查网络是否连接',
|
||
icon: "none"
|
||
});
|
||
return false;
|
||
}
|
||
//返回异步加载结果
|
||
return new Promise((resolve, reject) => {
|
||
try {
|
||
uni.request({
|
||
url: url,
|
||
method: method,
|
||
data: params,
|
||
header: {
|
||
// 'token': uni.getStorageSync('token'),
|
||
'content-type': 'application/x-www-form-urlencoded',
|
||
// 'content-type': 'multipart/form-data'
|
||
// 'content-type': 'application/json'
|
||
}
|
||
}).then(data => { //data为一个数组,数组第一项为错误信息,第二项为返回数据
|
||
let [error, res] = data;
|
||
if (res) {
|
||
// let jsRes = JSON.parse(res.data.replace(/'/g,"\""))
|
||
let jsRes = res.data
|
||
resolve(jsRes);
|
||
} else {
|
||
resolve({
|
||
"msg": "请求失败!"
|
||
});
|
||
}
|
||
})
|
||
} catch (error) {
|
||
reject(error)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
})
|
||
|
||
export default store
|