import http from '../plugins/http'
|
import storage from '../plugins/storage.js'
|
import message from '../plugins/message.js'
|
// #ifndef VUE3
|
import Vue from 'vue'
|
import Vuex from 'vuex'
|
Vue.use(Vuex)
|
const store = new Vuex.Store({
|
// #endif
|
|
|
|
// #ifdef VUE3
|
import {
|
createStore
|
} from 'vuex'
|
const store = createStore({
|
// #endif
|
state: {
|
hasLogin: false,
|
isUniverifyLogin: false,
|
loginProvider: "",
|
openid: null,
|
testvuex: false,
|
colorIndex: 0,
|
colorList: ['#FF0000', '#00FF00', '#0000FF'],
|
noMatchLeftWindow: true,
|
active: 'componentPage',
|
leftWinActive: '/pages/component/view/view',
|
activeOpen: '',
|
menu: [],
|
univerifyErrorMsg: '',
|
currentInfo: {}, //个人用户信息
|
cache_address: {},
|
// remoteip:'',//服务器ip
|
sign: {
|
'enterprise': 0,
|
},
|
defaultaddress: {}
|
},
|
mutations: {
|
|
updateLogin(state, provider) {
|
console.log('updateLogin', provider)
|
state.hasLogin = provider && true || false;
|
if (!state.hasLogin) {
|
storage.removeItem('token')
|
}
|
},
|
login(state, provider) {
|
state.hasLogin = true;
|
state.loginProvider = provider;
|
},
|
logout(state) {
|
state.hasLogin = false
|
state.openid = null
|
state.currentInfo = {}
|
storage.removeItem('token')
|
message.showToast('退出登录成功')
|
// #ifdef APP
|
var KeepAliveModule = uni.requireNativePlugin("yh-nl") //保活组件
|
|
let ret = KeepAliveModule.stopLocation();
|
console.log('ret', ret)
|
// #endif
|
uni.redirectTo({
|
url: '/views/login/login'
|
})
|
},
|
setOpenid(state, openid) {
|
state.openid = openid
|
},
|
setDefaultAddress(state, defaultaddress) {
|
state.defaultaddress = defaultaddress
|
},
|
setTestTrue(state) {
|
state.testvuex = true
|
},
|
setTestFalse(state) {
|
state.testvuex = false
|
},
|
setColorIndex(state, index) {
|
state.colorIndex = index
|
},
|
setMatchLeftWindow(state, matchLeftWindow) {
|
state.noMatchLeftWindow = !matchLeftWindow
|
},
|
setActive(state, tabPage) {
|
state.active = tabPage
|
},
|
setLeftWinActive(state, leftWinActive) {
|
state.leftWinActive = leftWinActive
|
},
|
setActiveOpen(state, activeOpen) {
|
state.activeOpen = activeOpen
|
},
|
setMenu(state, menu) {
|
state.menu = menu
|
},
|
setUniverifyLogin(state, payload) {
|
typeof payload !== 'boolean' ? payload = !!payload : '';
|
state.isUniverifyLogin = payload;
|
},
|
setUniverifyErrorMsg(state, payload = '') {
|
state.univerifyErrorMsg = payload
|
}
|
},
|
getters: {
|
currentColor(state) {
|
return state.colorList[state.colorIndex]
|
}
|
},
|
actions: {
|
sign_add: async function({
|
commit,
|
state
|
}, key) {
|
console.log('sign_add', key)
|
state.sign[key] = 1
|
},
|
sign_clear: async function({
|
commit,
|
state
|
}, key) {
|
console.log('sign_clear', key)
|
state.sign[key] = 0
|
},
|
// lazy loading openid
|
logout: async function({
|
commit,
|
state
|
}) {
|
commit('logout')
|
},
|
getUserOpenId: async function({
|
commit,
|
state
|
}) {
|
return await new Promise((resolve, reject) => {
|
if (state.openid) {
|
resolve(state.openid)
|
} else {
|
uni.login({
|
success: (data) => {
|
commit('login')
|
setTimeout(function() { //模拟异步请求服务器获取 openid
|
const openid = '123456789'
|
console.log(
|
'uni.request mock openid[' +
|
openid + ']');
|
commit('setOpenid', openid)
|
resolve(openid)
|
}, 1000)
|
},
|
fail: (err) => {
|
console.log('uni.login 接口调用失败,将无法正常使用开放接口等服务',
|
err)
|
reject(err)
|
}
|
})
|
}
|
})
|
},
|
getPhoneNumber: function({
|
commit
|
}, univerifyInfo) {
|
return new Promise((resolve, reject) => {
|
uni.request({
|
url: 'https://97fca9f2-41f6-449f-a35e-3f135d4c3875.bspapp.com/http/univerify-login',
|
method: 'POST',
|
data: univerifyInfo,
|
success: (res) => {
|
const data = res.data
|
if (data.success) {
|
resolve(data.phoneNumber)
|
} else {
|
reject(res)
|
}
|
|
},
|
fail: (err) => {
|
reject(res)
|
}
|
})
|
})
|
},
|
getCurrentInfo: async function({
|
commit,
|
state
|
}) {
|
//把权限获取到
|
// console.log('getCurrentInfo,getCurrentInfo')
|
const currentInfo = await http.request('get', '/api/current/user', {
|
|
})
|
if (currentInfo && currentInfo.code == 0) {
|
// state.cMenu = cMenu
|
// state.roles = currentInfo.data.roles || []
|
state.currentInfo = currentInfo.data || {}
|
state.type = currentInfo.data.type || ''
|
state.spacecode = currentInfo.data.spacecode || ''
|
// if(reload){
|
// location.reload()
|
// }
|
commit("updateLogin", true)
|
} else {
|
commit("updateLogin", false)
|
//这种情况就是要跳转到登录页面
|
}
|
},
|
loginwx: async function({
|
dispatch,
|
commit,
|
state
|
}, data) {
|
var code = data.code
|
let inviter = data.inviter || ''
|
console.log('')
|
const resp = await http.request('post', '/api/login/wechat', {
|
data: {
|
code: code,
|
imgurl: data.imgurl || '',
|
nickname: data.nickname || '',
|
inviter: inviter
|
},
|
// params:{
|
// code:code
|
// }
|
})
|
if (resp && resp.code == 0) {
|
state.isBind = true
|
storage.setItem("token", resp.data.access_token || "")
|
state.hasLogin = true
|
if(inviter){
|
storage.removeItem('inviter');
|
storage.removeItem('inviterTime');
|
storage.removeItem('inviterName');
|
}
|
await dispatch('getCurrentInfo')
|
} else {
|
message.showToast('登录失败.' + (resp && resp.msg))
|
storage.removeItem('openid');
|
storage.removeItem('tel');
|
storage.removeItem('token');
|
}
|
return resp
|
},
|
loginSupplier: async function({
|
commit,
|
dispatch,
|
state
|
}, data) {
|
{
|
const resp = await http.request('post', '/api/login/supplier'+(data.phoneNumber?'/phone':''), {
|
data: {
|
username: data.username,
|
password: data.password,
|
tel:data.phoneNumber,
|
smsCode:data.smsCode || ''
|
},
|
params: {
|
clientType: data.clientType || 'app'
|
}
|
})
|
console.log('resp', resp)
|
if (resp && resp.code == 0) {
|
// await getCurrentInfo()
|
// // this.$message.showToast('登录成功')
|
// console.log(resp)
|
storage.setItem('token', resp.data.access_token)
|
commit("updateLogin", true)
|
await dispatch('getCurrentInfo')
|
|
console.log('login,reLaunch')
|
// #ifdef APP
|
var KeepAliveModule = uni.requireNativePlugin("yh-nl") //保活组件
|
|
KeepAliveModule && KeepAliveModule.startLocation({
|
intervalTime: 5000, //定位间隔时间
|
gaodeApiKey: "0eef1axxxxxx7964", //高德地图Key,已停用,引入插件配置
|
locationMode: 1, //定位模式
|
purpose: 0, //定位场景
|
ifUpload: true, //是否上报服务器
|
uploadUrl: "http://bio51-api.fixbug.fun/api/current/employee/location", //上报接口
|
params: "{'lat':'1','lng':'3'}", //其他参数
|
headers: "{'authorization1':'Bearer xOHeJhg'}",
|
}, function(res) {
|
console.log("启用结果:", res);
|
let ret = KeepAliveModule.getCurrentLocation();
|
console.log("启用结果2:", ret);
|
});
|
// #endif
|
// uni.redirectTo({
|
// url: '/views/app/app-home/app-home'
|
// })
|
uni.reLaunch({
|
url: '/pages/home/farmer-home'
|
})
|
} else {
|
// this.$message.showToast('登录失败')
|
|
}
|
return resp
|
}
|
|
}
|
}
|
})
|
|
export default store
|