// 用户数据模块 import api from '@/common/request/index' import store from '@/store' import { setStorage, getStorage, removeStorage } from '@/util/index.js' import mpWx from '@/common/mpWX.js' import config from '@/config.js' import { USER_INFO, SET_TOKEN, OUT_LOGIN, SET_LOGINSTATE, USER_ADULTHOOD, SET_OPENID } from '../types.js' const state = { adultSet: getStorage('adultSet') || false, adulthoodStatus: false, // 18岁询问状态 userInfo: getStorage('userInfo') ? getStorage('userInfo') : {}, // 用户信息 openid: getStorage('openid') ? getStorage('openid') : '', // token sessionKey: '', // token token: getStorage('token') ? getStorage('token') : '', // token userList: getStorage('userList') ? getStorage('userList') : [], phoneStatus: false, // 请求获取手机号 phone: '', // 请求获取手机号 } const actions = { // 获取用户openId async getOpenId({ state, commit }, {code}) { try{ const obj = await api("app.getOpenId", {code: code}) const { openid, sessionKey } = obj.data; console.log(openid) // 存储用户信息 // if(userinfo){ // commit(USER_INFO, userinfo); // } // 判断是否设计过年龄 // if(!is18){ // commit(USER_ADULTHOOD, true) // } // 储存openId commit(SET_OPENID, openid) // 设置token commit(SET_TOKEN, openid); // 储存sessionKey state.sessionKey = sessionKey if(obj.code == 200){ // 登陆状态设为已经登陆 store.commit('SET_LOGINSTATE',true); } else{ } }catch(e){ //TODO handle the exception } }, // 关闭 async closePhone({ state, commit }, phoneCode) { // 关闭手机号请求窗口 state.phoneStatus = false }, // 获取用户手机号 async disPhone({ state, commit }, phoneCode) { // 获取code // const obj = await api("app.getPhone", {code: phoneCode}) // return obj }, // 判断是否成年 async disAdulthood({ state, commit }, info) { const adultType = info.detail; const pos = info.pos; const rawData = { city: pos.city, country: pos.country, province: pos.province, district: pos.district} if(adultType){ try{ const obj = await api("app.adultUpdate", {is18: true, ...rawData}) console.log("success:","设置成年,请求成功") commit(USER_ADULTHOOD, false) }catch(e){ console.log("error: ","设置成年,请求失败") //TODO handle the exception // uni.showModal({ // title: '提示', // content: "设置成年,请求失败" // }); } }else{ console.log("未满18岁") // const obj = await api("app.adultUpdate", {is18: false, ...rawData}) wx.exitMiniProgram({ success(e) { console.log("退出") }, fail(e) { console.log("退出失败") }, }) } }, async cacheUserInfo({ state, commit, dispatch}, info){ const userInfo = info.detail.userInfo; // 存储用户信息 commit(USER_INFO, userInfo); // 弹出询问获取手机号 if(config['getPhone'] && state.sessionKey){ commit('USER_GETPHONE', true); }else { await dispatch('login', {openid: state.openid, phone: ''}) return 2 } }, // 登陆 async login({ state, commit }, info) { const openid = info.openid; const phone = info.phone; const { nickName, gender, avatarUrl} = state.userInfo try{ const res = await api("app.getOpenId", { "openid": openid, "phone": phone, "name": nickName }) // 存储用户信息 commit(USER_INFO, { ...state.userInfo, name: res.data.name}); // 设置token commit(SET_TOKEN, openid); // 登陆状态设为已经登陆 store.commit('SET_LOGINSTATE',true); // 存储用户信息 // store.commit('SET_USERLIST',{mobile: form.mobile, token: token}); // 获取客服电话 // store.dispatch('getAdministratorTel') // 刷新地图 // await store.dispatch('initMap')10.190.35.204 return res.data }catch(e){ console.log(e) //TODO handle the exception return e } }, // 根据 token登录 async loginRefresh({ commit }, token ) { try{ // const res = await api('h5.refreshToken', {token: token}) // if(res.code == 1){ // // 更新token // let token = res.data.token // commit(SET_TOKEN, token); // // 登陆状态设为已经登陆 // store.commit('SET_LOGINSTATE',true); // return token // } else { // commit(OUT_LOGIN); // store.commit('SET_LOGINSTATE',false); // throw new Error(res.msg); // } }catch(e){ //TODO handle the exception return e } }, // 退出登陆 loginOut({ commit }) { commit(OUT_LOGIN); uni.$u.route({ url: '/pages/view/public/login', type: 'redirectTo' }) }, } const mutations = { // 保存18岁成年状态 [USER_ADULTHOOD](state, status) { state.adulthoodStatus = status state.adultSet = true // #ifdef MP-WEIXIN if(status){ wx.hideTabBar() }else{ wx.showTabBar() } // #endif setStorage('adultSet', true) }, ['USER_GETPHONE'](state, status) { state.phoneStatus = status }, // 保存用户信息 [USER_INFO](state, userInfo) { state.userInfo = userInfo setStorage('userInfo', userInfo) }, // 保存Token [SET_TOKEN](state, token) { state.token = token setStorage('token', token) }, // 保存 sessionKey ["SET_SESSIONKEY"](state, sessionKey) { state.sessionKey = sessionKey setStorage('sessionKey', sessionKey) }, // 保存openid [SET_OPENID](state, openid) { state.openid = openid setStorage('openid', openid) }, // 保存手机号 和 token 'SET_USERLIST'(state, userObj) { const list = state.userList; // 之前没有存储过 if(list.findIndex(obj => {return obj.mobile == userObj.mobile}) < 0){ list.push(userObj) } state.userList = list; setStorage('userList', list) }, // 退出登录 [OUT_LOGIN](state, data) { removeStorage('token'); removeStorage('userInfo'); store.commit('SET_LOGINSTATE',false); store.commit('USER_INFO', {}); store.commit('SET_TOKEN', ''); }, } const getters = { } export default { state, mutations, actions, getters }