初始化仓库
This commit is contained in:
127
store/modules/app.js
Normal file
127
store/modules/app.js
Normal file
@ -0,0 +1,127 @@
|
||||
import { getUser } from '@/api/user'
|
||||
import { getCartNum } from '@/api/store'
|
||||
import { USER_INFO, TOKEN, CONFIG } from '@/config/cachekey'
|
||||
import Cache from '@/utils/cache'
|
||||
import wechath5 from '@/utils/wechath5'
|
||||
import { isWeixinClient } from '@/utils/tools'
|
||||
import { baseURL, basePath } from '@/config/app'
|
||||
|
||||
const state = {
|
||||
config: Cache.get(CONFIG) || {
|
||||
app_agreement: 1,
|
||||
center_setting: {},
|
||||
index_setting: {},
|
||||
navigation_menu: [],
|
||||
navigation_setting: {},
|
||||
share: {},
|
||||
site_statistic: {}
|
||||
},
|
||||
userInfo: {
|
||||
user_money: 0,
|
||||
user_integral: 0,
|
||||
coupon: 0
|
||||
},
|
||||
token: Cache.get(TOKEN) || null,
|
||||
cartNum: '',
|
||||
sysInfo: {}
|
||||
}
|
||||
|
||||
const mutations = {
|
||||
login(state, opt) {
|
||||
state.token = opt.token
|
||||
Cache.set(TOKEN, opt.token, 59 * 24 * 60 * 60)
|
||||
this.dispatch('getUser')
|
||||
},
|
||||
logout(state) {
|
||||
state.token = undefined
|
||||
state.userInfo = {
|
||||
user_money: 0,
|
||||
user_integral: 0,
|
||||
coupon: 0
|
||||
}
|
||||
Cache.remove(TOKEN)
|
||||
},
|
||||
setCartNum(state, num) {
|
||||
state.cartNum = num
|
||||
},
|
||||
setUserInfo(state, user) {
|
||||
state.userInfo = user
|
||||
},
|
||||
setConfig(state, data) {
|
||||
state.config = Object.assign(state.config, data)
|
||||
Cache.set(CONFIG, state.config)
|
||||
},
|
||||
setSystemInfo(state, data) {
|
||||
state.sysInfo = data
|
||||
}
|
||||
}
|
||||
|
||||
const actions = {
|
||||
getCartNum({ state, commit }) {
|
||||
return new Promise((resolve) => {
|
||||
if (!state.token)
|
||||
return uni.removeTabBarBadge({
|
||||
index: 3
|
||||
})
|
||||
getCartNum().then((res) => {
|
||||
if (res.code == 1) {
|
||||
commit('setCartNum', res.data.num)
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
wxShare({ state }, opt) {
|
||||
// #ifdef H5
|
||||
const shareInfo = state.config.share
|
||||
const inviteCode = state.userInfo.distribution_code
|
||||
const href = window.location.href
|
||||
const sym = href.includes('?') ? '&' : '?'
|
||||
const option = {
|
||||
shareTitle: shareInfo.h5_share_title,
|
||||
shareLink: inviteCode ? `${href}${sym}invite_code=${inviteCode}` : href,
|
||||
shareImage: shareInfo.h5_share_image,
|
||||
shareDesc: shareInfo.h5_share_intro
|
||||
}
|
||||
wechath5.share(Object.assign(option, opt))
|
||||
// #endif
|
||||
},
|
||||
getUser({ state, commit }) {
|
||||
return new Promise((resolve) => {
|
||||
getUser().then((res) => {
|
||||
if (res.code == 1) {
|
||||
commit('setUserInfo', res.data)
|
||||
}
|
||||
this.dispatch('wxShare')
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
},
|
||||
getSystemInfo({ state, commit }) {
|
||||
uni.getSystemInfo({
|
||||
success: (res) => {
|
||||
let { statusBarHeight, platform } = res
|
||||
let navHeight
|
||||
if (platform == 'ios' || platform == 'devtools') {
|
||||
navHeight = statusBarHeight + 44
|
||||
} else {
|
||||
navHeight = statusBarHeight + 48
|
||||
}
|
||||
commit('setSystemInfo', {
|
||||
...res,
|
||||
navHeight
|
||||
})
|
||||
console.log(state)
|
||||
},
|
||||
fail(err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
state,
|
||||
mutations,
|
||||
actions
|
||||
}
|
||||
104
store/modules/city.js
Normal file
104
store/modules/city.js
Normal file
@ -0,0 +1,104 @@
|
||||
import wechath5 from "@/utils/wechath5";
|
||||
import { isWeixinClient, toast } from "@/utils/tools";
|
||||
import { baseURL, basePath } from "@/config/app";
|
||||
import { getGeocoder } from "@/api/store.js";
|
||||
|
||||
const state = {
|
||||
cityInfo: {
|
||||
// id: city_id,
|
||||
// name: result.ad_info.city,
|
||||
// gcj02_lat: result.location.lat,
|
||||
// gcj02_lng: result.location.lng
|
||||
},
|
||||
};
|
||||
|
||||
const mutations = {
|
||||
setCityInfo(state, data) {
|
||||
state.cityInfo = data;
|
||||
uni.$emit("refreshhome");
|
||||
},
|
||||
};
|
||||
|
||||
const actions = {
|
||||
// 位置授权
|
||||
async getAuthorize({ dispatch }) {
|
||||
const [error, data] = await uni.showModal({
|
||||
title: "您已拒绝地理位置权限",
|
||||
content: "是否进入权限管理,调整授权?",
|
||||
});
|
||||
if (data.confirm) {
|
||||
const [error, data] = await uni.openSetting();
|
||||
if (data.authSetting["scope.userLocation"]) dispatch("initLocationFunc");
|
||||
}
|
||||
},
|
||||
// 获取当前定位
|
||||
async initLocationFunc({ dispatch, rootState }) {
|
||||
console.log("获取地址");
|
||||
try {
|
||||
const [error, res] = await uni.getLocation({
|
||||
// #ifndef APP
|
||||
type: "gcj02",
|
||||
// #endif
|
||||
});
|
||||
console.log(error, res, "----");
|
||||
// if(error?.errMsg == 'getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化') return toast({ title: '频繁定位,请稍后' })
|
||||
// #ifdef MP
|
||||
dispatch("getSystemInfo");
|
||||
if (!rootState.app.sysInfo.locationEnabled) {
|
||||
uni.showModal({
|
||||
title: "温馨提示",
|
||||
content: "您的手机定位还未开启",
|
||||
});
|
||||
return;
|
||||
}
|
||||
// #endif
|
||||
// #ifdef MP
|
||||
if (!res) return dispatch("getAuthorize");
|
||||
// #endif
|
||||
if (error) {
|
||||
uni.showModal({
|
||||
title: "温馨提示",
|
||||
// content: JSON.stringify(error.errMsg)
|
||||
content: "获取位置失败,请检查是否开启定位!",
|
||||
});
|
||||
return;
|
||||
}
|
||||
// #ifdef APP-PLUS
|
||||
|
||||
if (!res) return toast({ title: "获取位置失败" });
|
||||
// #endif
|
||||
dispatch("getGeocoderFunc", {
|
||||
location: `${res.latitude},${res.longitude}`,
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
// toast({ title: err.errMsg })
|
||||
// throw new Error(err + '获取位置')
|
||||
}
|
||||
},
|
||||
// 逆解析地址
|
||||
getGeocoderFunc({ commit }, location) {
|
||||
console.log(location);
|
||||
getGeocoder({ ...location }).then((res) => {
|
||||
console.log(res);
|
||||
if (res.code == 1) {
|
||||
const result = res.data.result;
|
||||
const city_id = res.data.city_id;
|
||||
commit("setCityInfo", {
|
||||
id: city_id,
|
||||
name: result.ad_info.city,
|
||||
gcj02_lat: result.location.lat,
|
||||
gcj02_lng: result.location.lng,
|
||||
});
|
||||
} else {
|
||||
toast({ title: res.msg });
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export default {
|
||||
state,
|
||||
mutations,
|
||||
actions,
|
||||
};
|
||||
67
store/modules/community.js
Normal file
67
store/modules/community.js
Normal file
@ -0,0 +1,67 @@
|
||||
import wechath5 from '@/utils/wechath5'
|
||||
import {
|
||||
isWeixinClient
|
||||
} from '@/utils/tools'
|
||||
import {
|
||||
baseURL,
|
||||
basePath
|
||||
} from '@/config/app'
|
||||
|
||||
const state = {
|
||||
communityItem: {}
|
||||
};
|
||||
|
||||
const mutations = {
|
||||
setCommunity(state, data) {
|
||||
state.communityItem = data
|
||||
}
|
||||
};
|
||||
|
||||
const actions = {
|
||||
// 分享种草文章
|
||||
communityShare({
|
||||
state,
|
||||
commit
|
||||
}) {
|
||||
const item = state.communityItem;
|
||||
console.log(item)
|
||||
// #ifdef H5
|
||||
if (isWeixinClient()) {
|
||||
const option = {
|
||||
shareTitle: `${item.user.nickname},TA的内容超级棒`,
|
||||
shareLink: `${baseURL}${basePath}/${item.url}?id=${item.id}`,
|
||||
shareImage: item.image,
|
||||
shareDesc: item.content
|
||||
}
|
||||
wechath5.share(option)
|
||||
}
|
||||
// #endif
|
||||
// #ifdef APP-PLUS
|
||||
uni.share({
|
||||
provider: "weixin",
|
||||
scene: "WXSceneSession",
|
||||
type: 0,
|
||||
href: `${baseURL}${basePath}/${item.url}?id=${item.id}`,
|
||||
title: `${item.user.nickname},TA的内容超级棒`,
|
||||
summary: item.content,
|
||||
imageUrl: item.image,
|
||||
success: (res) => {
|
||||
console.log('分享成功');
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log(err)
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: err.errMsg
|
||||
})
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
}
|
||||
};
|
||||
|
||||
export default {
|
||||
state,
|
||||
mutations,
|
||||
actions
|
||||
};
|
||||
9
store/modules/index.js
Normal file
9
store/modules/index.js
Normal file
@ -0,0 +1,9 @@
|
||||
import app from "./app";
|
||||
import city from "./city";
|
||||
import community from "./community";
|
||||
|
||||
export default {
|
||||
app,
|
||||
city,
|
||||
community
|
||||
};
|
||||
Reference in New Issue
Block a user