|
|
import Vue from 'vue'
|
|
|
import Vuex from 'vuex'
|
|
|
Vue.use(Vuex)
|
|
|
|
|
|
let lifeData = {};
|
|
|
|
|
|
try{
|
|
|
// 尝试获取本地是否存在lifeData变量,第一次启动APP时是不存在的
|
|
|
lifeData = uni.getStorageSync('lifeData');
|
|
|
} catch(e) {
|
|
|
|
|
|
}
|
|
|
|
|
|
// 需要永久存储,且下次APP启动需要取出的,在state中的变量名
|
|
|
let saveStateKeys = ['vuex_token', 'vuex_user'];
|
|
|
|
|
|
// 保存变量到本地存储中
|
|
|
const saveLifeData = function(key, value){
|
|
|
// 判断变量名是否在需要存储的数组中
|
|
|
if(saveStateKeys.indexOf(key) != -1) {
|
|
|
// 获取本地存储的lifeData对象,将变量添加到对象中
|
|
|
let tmp = uni.getStorageSync('lifeData');
|
|
|
// 第一次打开APP,不存在lifeData变量,故放一个{}空对象
|
|
|
tmp = tmp ? tmp : {};
|
|
|
tmp[key] = value;
|
|
|
// 执行这一步后,所有需要存储的变量,都挂载在本地的lifeData对象中
|
|
|
uni.setStorageSync('lifeData', tmp);
|
|
|
}
|
|
|
}
|
|
|
const store = new Vuex.Store({
|
|
|
// 下面这些值仅为示例,使用过程中请删除
|
|
|
state: {
|
|
|
// 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
|
|
|
// 加上vuex_前缀,是防止变量名冲突,也让人一目了然
|
|
|
// 如果vuex_version无需保存到本地永久存储,无需lifeData.vuex_version方式
|
|
|
vuex_version: '1.0.0',
|
|
|
vuex_default_icon: '/static/logo-mini.png',
|
|
|
vuex_token: '',
|
|
|
vuex_user: {},
|
|
|
|
|
|
vuex_site: {
|
|
|
name: '',
|
|
|
id: ''
|
|
|
},
|
|
|
vuex_location: {
|
|
|
latitude: '',
|
|
|
longitude: '',
|
|
|
city: '',
|
|
|
cityCode: '',
|
|
|
status: 0, // 0失败 1成功 2手动选择
|
|
|
},
|
|
|
vuex_order_status: [
|
|
|
{
|
|
|
name: "全部",
|
|
|
value: "",
|
|
|
},
|
|
|
{
|
|
|
name: "待支付",
|
|
|
value: 0,
|
|
|
},
|
|
|
{
|
|
|
name: "已支付",
|
|
|
value: 1,
|
|
|
},
|
|
|
{
|
|
|
name: "已退款",
|
|
|
value: 2,
|
|
|
}
|
|
|
// {
|
|
|
// name: '已取消'
|
|
|
// }
|
|
|
]
|
|
|
},
|
|
|
mutations: {
|
|
|
$uStore(state, payload) {
|
|
|
// 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
|
|
|
let nameArr = payload.name.split('.');
|
|
|
let saveKey = '';
|
|
|
let len = nameArr.length;
|
|
|
if(nameArr.length >= 2) {
|
|
|
let obj = state[nameArr[0]];
|
|
|
for(let i = 1; i < len - 1; i ++) {
|
|
|
obj = obj[nameArr[i]];
|
|
|
}
|
|
|
obj[nameArr[len - 1]] = payload.value;
|
|
|
saveKey = nameArr[0];
|
|
|
} else {
|
|
|
// 单层级变量,在state就是一个普通变量的情况
|
|
|
state[payload.name] = payload.value;
|
|
|
saveKey = payload.name;
|
|
|
}
|
|
|
// 保存变量到本地,见顶部函数定义
|
|
|
saveLifeData(saveKey, state[saveKey])
|
|
|
},
|
|
|
|
|
|
SET_CITY(state, { cityName, cityCode }) {
|
|
|
state.vuex_location.city = cityName
|
|
|
state.vuex_location.cityCode = cityCode
|
|
|
state.vuex_location.status = 2
|
|
|
}
|
|
|
},
|
|
|
actions: {
|
|
|
async getLocation({ state }) {
|
|
|
try {
|
|
|
const [err, res] = await uni.getLocation({
|
|
|
type: 'gcj02'
|
|
|
})
|
|
|
if (err) {
|
|
|
await Promise.reject(err)
|
|
|
}
|
|
|
console.log(state, res)
|
|
|
state.vuex_location.latitude = res.latitude;
|
|
|
state.vuex_location.longitude = res.longitude;
|
|
|
const address = await new Promise((resolve, reject) => {
|
|
|
Vue.prototype.$qqmapsdk.reverseGeocoder({
|
|
|
location: {
|
|
|
latitude: res.latitude,
|
|
|
longitude: res.longitude
|
|
|
},
|
|
|
success: address => resolve(address),
|
|
|
fail: error => reject(error)
|
|
|
})
|
|
|
})
|
|
|
console.log('address', address)
|
|
|
if (!!address.status) {
|
|
|
await Promise.reject(address.message)
|
|
|
}
|
|
|
state.vuex_location.city = address?.result?.ad_info.city
|
|
|
state.vuex_location.cityCode = address?.result?.ad_info?.city_code
|
|
|
state.vuex_location.status = 1
|
|
|
return {
|
|
|
address,
|
|
|
pos: {
|
|
|
latitude: res.latitude,
|
|
|
longitude: res.longitude
|
|
|
}
|
|
|
}
|
|
|
} catch (err) {
|
|
|
console.error(err)
|
|
|
state.vuex_location.status = 0
|
|
|
uni.showToast({
|
|
|
title: '获取定位失败',
|
|
|
icon: 'none',
|
|
|
duration: 2000
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
|
|
|
export default store
|