123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import {
- getInfoByToken,
- authLogin,
- getConfig
- } from '@/api/app';
- import {
- USER_INFO,
- TOKEN,
- CONFIG
- } from '@/config/cachekey';
- import Cache from '@/utils/cache'
- import wechath5 from '@/utils/wechath5'
- import {
- baseURL,
- basePath
- } from '@/config/app'
- import tabBar from '@/utils/tabBar.js'
- const state = {
- config: Cache.get(CONFIG) || {
- center_setting: {},
- index_setting: {},
- navigation_menu: [],
- navigation_setting: {},
- share: {}
- },
- userInfo: Cache.get('USERINFO') || {},
- token: Cache.get(TOKEN) || null,
- sysInfo: {},
- tabarList: tabBar['map'], // 动态底部导航栏
- };
- const mutations = {
- setTabarList(state, opt) {
- state.tabarList = tabBar[opt.type] // 动态底部导航栏
- },
- login(state, opt) {
- console.log('登录成功')
- console.log(opt)
- state.token = opt.token;
- Cache.set(TOKEN, opt.token, 2 * 60 * 60); //保存2小时
- this.dispatch('getUser')
- },
- logout(state) {
- state.token = undefined;
- state.userInfo = undefined;
- Cache.remove(TOKEN);
- Cache.remove('USERINFO');
- Cache.remove('USERINFOLIST');
- },
- setUserInfo(state, userInfo) {
- state.userInfo = userInfo;
- Cache.set('USERINFO', userInfo, 2 * 60 * 60);
- },
- setConfig(state, data) {
- state.config = Object.assign(state.config, data)
- Cache.set(CONFIG, state.config);
- },
- setSystemInfo(state, data) {
- state.sysInfo = data
- }
- };
- const actions = {
- getUser({
- state,
- commit
- }) {
- return new Promise(resolve => {
- getInfoByToken().then(res => {
- //token 过期,重新登录
- if (res.code == 2) {
- // uni.showModal({
- // title: '提示',
- // content: '请使用企业身份登录',
- // mask: false,
- // success: function(res) {
- // if (res.confirm) {
- // uni.navigateTo({
- // url: '/pages/login/login'
- // })
- // }
- // }
- // })
- return false;
- }
- if (res.code == 1) {
- //个人用户,拒绝访问
- if (res.data.type == 1) {
- uni.showToast({
- icon: 'none',
- title: '仅对企业开放',
- complete: () => {
- uni.navigateTo({
- url: '/pages/login/login'
- })
- }
- })
- return false;
- }
- //企业用户
- if (res.data.type == 2) {
- authLogin(res.data).then(auth => {
- commit('setUserInfo', auth.data)
- });
- }
- }
- resolve()
- })
- })
- },
- getConfig({
- state,
- commit
- }) {
- return new Promise(resolve => {
- getConfig({}).then(res => {
- if (res.status == 1) {
- commit('setConfig', res.data)
- }
- 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,
- })
- },
- fail(err) {
- console.log(err);
- }
- });
- },
- 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
- }
- };
- export default {
- state,
- mutations,
- actions
- };
|