Init project
This commit is contained in:
+162
@@ -0,0 +1,162 @@
|
||||
import Vue from "vue";
|
||||
import Vuex from "vuex";
|
||||
import city from './modules/switchCity.js';
|
||||
|
||||
Vue.use(Vuex);
|
||||
const debug = process.env.NODE_ENV !== "production";
|
||||
|
||||
import cookie from "@/utils/store/cookie";
|
||||
import {
|
||||
getUserInfo,
|
||||
getUser
|
||||
} from "@/api/user";
|
||||
import dialog from "@/utils/dialog";
|
||||
|
||||
const loginKey = "login_status";
|
||||
|
||||
const vuexStore = new Vuex.Store({
|
||||
state: {
|
||||
// 是否已经在授权页面
|
||||
isAuthorizationPage: false,
|
||||
// 是否授权
|
||||
isAuthorization: false,
|
||||
// 不建议从这里取 token,但是删除掉会影响其他的页面
|
||||
token: cookie.get(loginKey) || null,
|
||||
userInfo: cookie.get('userInfo'),
|
||||
$deviceType: null,
|
||||
location: {
|
||||
latitude: '',
|
||||
longitude: ''
|
||||
},
|
||||
storeItems: cookie.get("storeItems") || null,
|
||||
goName: cookie.get("goName") || ""
|
||||
},
|
||||
mutations: {
|
||||
login(state, token, expires_time) {
|
||||
state.token = token;
|
||||
cookie.set(loginKey, token, expires_time); // 授权过期时间
|
||||
},
|
||||
logout(state) {
|
||||
state.token = null;
|
||||
state.userInfo = null
|
||||
let spread = cookie.get('spread')
|
||||
cookie.clearAll()
|
||||
cookie.set('spread', spread)
|
||||
},
|
||||
backgroundColor(state, color) {
|
||||
state.color = color;
|
||||
// document.body.style.backgroundColor = color;
|
||||
},
|
||||
updateUserInfo(state, userInfo) {
|
||||
state.userInfo = userInfo;
|
||||
if (userInfo) {
|
||||
cookie.set('userInfo', userInfo)
|
||||
} else {
|
||||
cookie.set('userInfo', null)
|
||||
}
|
||||
},
|
||||
updateAuthorizationPage(state, isAuthorizationPage) {
|
||||
state.isAuthorizationPage = isAuthorizationPage;
|
||||
},
|
||||
updateAuthorization(state, isAuthorization) {
|
||||
state.isAuthorization = isAuthorization;
|
||||
},
|
||||
updateDevicetype(state, $deviceType) {
|
||||
state.$deviceType = $deviceType;
|
||||
},
|
||||
setLocation(state, location) {
|
||||
state.location = location
|
||||
},
|
||||
get_store(state, storeItems) {
|
||||
state.storeItems = storeItems;
|
||||
cookie.set("storeItems", storeItems);
|
||||
},
|
||||
get_to(state, goName) {
|
||||
state.goName = goName;
|
||||
cookie.set("goName", goName);
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
getLocation({ state, commit }, force) {
|
||||
uni.getLocation({
|
||||
type: 'wgs84',
|
||||
success: function (res) {
|
||||
commit("setLocation", {
|
||||
longitude: res.longitude,
|
||||
latitude: res.latitude,
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
setLocation({
|
||||
state,
|
||||
commit
|
||||
}, location) {
|
||||
commit("setLocation", location);
|
||||
},
|
||||
userInfo({ state, commit }, force) {
|
||||
if (state.userInfo !== null && !force) {
|
||||
return Promise.resolve(state.userInfo);
|
||||
} else {
|
||||
return new Promise(reslove => {
|
||||
getUserInfo().then(res => {
|
||||
commit("updateUserInfo", res.data);
|
||||
reslove(res.data);
|
||||
});
|
||||
}).catch(() => {
|
||||
dialog.error("获取信息失败!");
|
||||
});
|
||||
}
|
||||
},
|
||||
getUser({ state, commit }) {
|
||||
return new Promise(reslove => {
|
||||
getUserInfo().then(res => {
|
||||
commit("updateUserInfo", res.data);
|
||||
reslove(res.data);
|
||||
});
|
||||
}).catch((error) => {
|
||||
dialog.error("获取信息失败!");
|
||||
});
|
||||
},
|
||||
changeLogin({
|
||||
state,
|
||||
commit
|
||||
}, data, date) {
|
||||
commit("login", data, date);
|
||||
},
|
||||
setUserInfo({
|
||||
state,
|
||||
commit
|
||||
}, user) {
|
||||
commit("updateUserInfo", user);
|
||||
},
|
||||
changeAuthorizationPage({
|
||||
state,
|
||||
commit
|
||||
}, index) {
|
||||
commit("updateAuthorizationPage", index);
|
||||
},
|
||||
changeAuthorization({
|
||||
state,
|
||||
commit
|
||||
}, index) {
|
||||
commit("updateAuthorization", index);
|
||||
},
|
||||
},
|
||||
getters: {
|
||||
isAuthorizationPage: state => state.isAuthorizationPage,
|
||||
isAuthorization: state => state.isAuthorization,
|
||||
token: state => state.token,
|
||||
isLogin: state => !!state.token,
|
||||
userInfo: state => state.userInfo || {},
|
||||
location: state => state.location,
|
||||
storeItems: state => state.storeItems,
|
||||
goName: state => state.goName,
|
||||
},
|
||||
modules: {
|
||||
city
|
||||
},
|
||||
strict: debug
|
||||
});
|
||||
|
||||
export default vuexStore
|
||||
@@ -0,0 +1,239 @@
|
||||
import config from '@/utils/mapConfig';
|
||||
import { CITY_GET_LOCATION, CITY_SELECT_COUNTY, CITY_CHANGE_CODE, CITY_CHANGE_COUNTY } from '../mutation-types';
|
||||
import Vue from 'vue'
|
||||
import VueJsonp from '@/utils/vue-jsonp.umd.js'
|
||||
Vue.use(VueJsonp)
|
||||
|
||||
const state = {
|
||||
city: '定位中',
|
||||
county: '',
|
||||
province:'',
|
||||
detailAddress:'',
|
||||
countyList: [],
|
||||
currentCityCode: '',
|
||||
defaultCity: '',
|
||||
defaultCounty: '',
|
||||
latitude:'',
|
||||
longitude:''
|
||||
};
|
||||
|
||||
const getters = {
|
||||
city: state => state.city,
|
||||
county: state => state.county,
|
||||
detailAddress: state=> state.detailAddress,
|
||||
province: state => state.province,
|
||||
currentCityCode: state => state.currentCityCode,
|
||||
defaultCity: state => state.defaultCity,
|
||||
defaultCounty: state => state.defaultCounty,
|
||||
countyList: state => state.countyList,
|
||||
latitude: state => state.latitude,
|
||||
longitude: state => state.longitude
|
||||
};
|
||||
|
||||
const mutations = {
|
||||
// 设置当前城市信息
|
||||
[CITY_GET_LOCATION](state, payload) {
|
||||
state.city = payload.city;
|
||||
state.county = payload.county;
|
||||
state.province = payload.province;
|
||||
state.detailAddress = payload.detailAddress,
|
||||
state.currentCityCode = payload.currentCityCode;
|
||||
state.defaultCity = payload.city;
|
||||
state.defaultCounty = payload.county;
|
||||
state.countyList = [];
|
||||
state.latitude = payload.latitude;
|
||||
state.longitude = payload.longitude;
|
||||
},
|
||||
[CITY_SELECT_COUNTY](state, payload) {
|
||||
state.countyList = payload.list;
|
||||
},
|
||||
[CITY_CHANGE_CODE](state, payload) {
|
||||
state.city = payload.city;
|
||||
state.currentCityCode = payload.code;
|
||||
state.county = '';
|
||||
},
|
||||
// 在更新区级信息后,同时更新默认信息
|
||||
[CITY_CHANGE_COUNTY](state, payload) {
|
||||
state.county = payload.county;
|
||||
state.defaultCity = state.city;
|
||||
state.defaultCounty = state.county;
|
||||
}
|
||||
};
|
||||
|
||||
const actions = {
|
||||
|
||||
// 获取当前定位 城市 区
|
||||
[CITY_GET_LOCATION]({commit, dispatch}) {
|
||||
|
||||
// Vue.jsonp(
|
||||
// 'https://apis.map.qq.com/ws/geocoder/v1/?location=25.288289676896564,110.31579578706705&key=UGMBZ-S5AKU-YQGV3-47M5J-BAQ62-ZBBJW',
|
||||
// {
|
||||
// //callbackName: 'QQmap',
|
||||
// output:'jsonp',
|
||||
// })
|
||||
// .then(json => {
|
||||
//
|
||||
// console.log('json:'+json)
|
||||
// let latitude = 25.288289676896564;
|
||||
// let longitude = 110.31579578706705;
|
||||
// // Success.
|
||||
// commit({
|
||||
// type: CITY_GET_LOCATION,
|
||||
// city: json.result.ad_info.city,
|
||||
// currentCityCode: json.result.ad_info.adcode,
|
||||
// county: json.result.ad_info.district,
|
||||
// latitude:latitude,
|
||||
// longitude:longitude
|
||||
// });
|
||||
//
|
||||
// }).catch(err => {
|
||||
// // Failed.
|
||||
// console.log('地址解析失败:'+JSON.stringify(err))
|
||||
// })
|
||||
|
||||
uni.getLocation({
|
||||
type: 'wgs84',
|
||||
success: function(res) {
|
||||
let latitude = res.latitude;
|
||||
let longitude = res.longitude;
|
||||
|
||||
console.log('latitude:'+latitude+'longitude:'+longitude)
|
||||
|
||||
// #ifdef H5
|
||||
Vue.jsonp(
|
||||
'https://apis.map.qq.com/ws/geocoder/v1/?location='+latitude+','+longitude+'&key='+config.key,
|
||||
{
|
||||
//callbackName: 'QQmap',
|
||||
output:'jsonp',
|
||||
}).then(json => {
|
||||
// Success.
|
||||
// console.log('json:'+JSON.stringify(json))
|
||||
// let latitude = 25.288289676896564;
|
||||
// let longitude = 110.31579578706705;
|
||||
// Success.
|
||||
commit({
|
||||
type: CITY_GET_LOCATION,
|
||||
city: json.result.ad_info.city,
|
||||
detailAddress:json.result.formatted_addresses.recommend,
|
||||
province:json.result.ad_info.province,
|
||||
currentCityCode: json.result.ad_info.adcode,
|
||||
county: json.result.ad_info.district,
|
||||
latitude:latitude,
|
||||
longitude:longitude
|
||||
});
|
||||
|
||||
}).catch(err => {
|
||||
// Failed.
|
||||
console.log('地址解析失败:'+JSON.stringify(err))
|
||||
})
|
||||
// #endif
|
||||
|
||||
// #ifndef H5
|
||||
uni.request({
|
||||
url: 'https://apis.map.qq.com/ws/geocoder/v1/?location='+latitude+','+longitude+'&key='+config.key,
|
||||
success: function(res) {
|
||||
// console.log('res:'+JSON.stringify(res.data));
|
||||
commit({
|
||||
type: CITY_GET_LOCATION,
|
||||
city: res.data.result.ad_info.city,
|
||||
province:res.data.result.ad_info.province,
|
||||
detailAddress:res.data.result.formatted_addresses.recommend,
|
||||
currentCityCode: res.data.result.ad_info.adcode,
|
||||
county: res.data.result.ad_info.district,
|
||||
latitude:latitude,
|
||||
longitude:longitude
|
||||
});
|
||||
},
|
||||
fail:function(res){
|
||||
console.log('地址解析失败')
|
||||
},
|
||||
complete:function(){
|
||||
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
},
|
||||
fail:function(res){
|
||||
|
||||
// uni.showToast({
|
||||
// title:'城市定位失败:'+JSON.stringify(res),
|
||||
// icon:'none',
|
||||
// duration:2000
|
||||
// })
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
},
|
||||
// 根据城市代码 定位区县
|
||||
[CITY_SELECT_COUNTY]({ commit, state }) {
|
||||
console.log('正在定位区县');
|
||||
let code = state.currentCityCode;
|
||||
|
||||
// #ifdef H5
|
||||
Vue.jsonp(
|
||||
'https://apis.map.qq.com/ws/district/v1/getchildren?&id='+code+'&key='+config.key,
|
||||
{
|
||||
output:'jsonp',
|
||||
})
|
||||
.then(json => {
|
||||
|
||||
commit({
|
||||
type: CITY_SELECT_COUNTY,
|
||||
list: json.result[0]
|
||||
});
|
||||
|
||||
}).catch(err => {
|
||||
// Failed.
|
||||
console.log('地址解析失败:'+JSON.stringify(err))
|
||||
})
|
||||
// #endif
|
||||
|
||||
// #ifndef H5
|
||||
uni.request({
|
||||
url: 'https://apis.map.qq.com/ws/district/v1/getchildren?&id='+code+'&key='+config.key,
|
||||
success: function(res) {
|
||||
commit({
|
||||
type: CITY_SELECT_COUNTY,
|
||||
list: res.data.result[0]
|
||||
});
|
||||
//console.log(res.data);
|
||||
console.log('请求区县成功' + 'https://apis.map.qq.com/ws/district/v1/getchildren?&id='+code+'&key='+config.key);
|
||||
},
|
||||
fail: function() {
|
||||
console.log('请求区县失败,请重试');
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
},
|
||||
// 更新城市名和城市代码
|
||||
[CITY_CHANGE_CODE]({commit}, data) {
|
||||
commit({
|
||||
type: CITY_CHANGE_CODE,
|
||||
city: data.city,
|
||||
code: data.code
|
||||
});
|
||||
},
|
||||
// 更新选择区县
|
||||
[CITY_CHANGE_COUNTY]({commit}, data) {
|
||||
commit({
|
||||
type: CITY_CHANGE_COUNTY,
|
||||
county: data.county
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state,
|
||||
getters,
|
||||
mutations,
|
||||
actions
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
|--------------------------------------------------
|
||||
| Switch City 模块
|
||||
|--------------------------------------------------
|
||||
*/
|
||||
export const CITY_GET_LOCATION = 'CITY_GET_LOCATION';
|
||||
export const CITY_SELECT_COUNTY = 'CITY_SELECT_COUNTY';
|
||||
export const CITY_CHANGE_CODE = 'CITY_CHANGE_CODE';
|
||||
export const CITY_CHANGE_COUNTY = 'CITY_CHANGE_COUNTY';
|
||||
|
||||
/**
|
||||
|--------------------------------------------------
|
||||
| 购物车模块
|
||||
|--------------------------------------------------
|
||||
*/
|
||||
export const ADD_CART = 'ADD_CART'
|
||||
export const REDUCE_CART = 'REDUCE_CART'
|
||||
export const INIT_BUYCART = 'INIT_BUYCART'
|
||||
export const CLEAR_CART = 'CLEAR_CART'
|
||||
Reference in New Issue
Block a user