123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import { T_STORAGE_KEY, USER_INFO } from '../constant'
- import { getAnotherTFTokenApi } from '../api/anotherTFInterface'
- import Vue from 'vue'
- import Vuex from 'vuex'
- Vue.use(Vuex)
- const store = new Vuex.Store({
- state: {
- userInfo: uni.getStorageSync(USER_INFO)
- },
- getters: {
- userId(state) {
- return state.userInfo.id || null
- }
- },
- mutations: {
-
- setStateAttr(state, param) {
- if (param instanceof Array) {
- for (const item of param) {
- state[item.key] = item.val
- }
- } else {
- state[param.key] = param.val
- }
- },
- 'CHNAGE_USER_INFO'(state, userInfo) {
- state.userInfo = userInfo
- uni.setStorageSync(USER_INFO, userInfo)
- }
- },
- actions: {
- setUserData({ state, commit }, data) {
- commit('setStateAttr', {
- key: 'userInfo',
- val: data
- })
- uni.setStorageSync(USER_INFO, data)
- },
-
- loginAction({ commit, dispatch }, loginData) {
- return new Promise((resolve, reject) => {
- loginApi({ ...loginData })
- .then(async ({ data }) => {
- commit('CHNAGE_USER_INFO', data.userInfo)
- uni.showToast({
- title: '登录成功'
- })
- console.log(data)
- await dispatch('updateStorageKeyToken')
- resolve(data)
- })
- .catch((err) => {
- reject(err)
- })
- })
- },
-
- codeLoginAction({ commit, dispatch }, loginData) {
- return new Promise((resolve, reject) => {
- verificationCodeApi({ ...loginData })
- .then(async ({ data }) => {
- commit('CHNAGE_USER_INFO', data.userInfo)
- uni.showToast({
- title: '登录成功'
- })
- console.log(data)
- await dispatch('updateStorageKeyToken')
- resolve(data)
- })
- .catch((err) => {
- reject(err)
- })
- })
- },
-
- wxLogin({ commit, dispatch }, code) {
- return new Promise((resolve, reject) => {
- wxLoginApi({ code })
- .then(async ({ data }) => {
- commit('CHNAGE_USER_INFO', data.userInfo)
- uni.showToast({
- title: '登录成功'
- })
- await dispatch('updateStorageKeyToken')
- resolve(data)
- })
- .catch((err) => {
- reject(err)
- })
- })
- },
- logout({ commit }, isQuiet) {
- uni.removeStorageSync(USER_INFO)
- commit(CHNAGE_USER_INFO, '')
- clearAllCache()
- if (isQuiet) {
- uni.showToast({
- title: '退出成功'
- })
- setTimeout(() => {
- uni.switchTab({
- url: '/pages/community-center/community-centerr'
- })
- }, 2000)
- }
- },
-
- updateStorageKeyToken({ state, dispatch, commit }) {
- return new Promise((resolve, reject) => {
- const userInfo = uni.getStorageSync(USER_INFO)
- if (userInfo && userInfo.phone) {
- uni.showLoading({ mask: true })
- getAnotherTFTokenApi({ phone: userInfo.phone })
- .then((res) => {
- uni.setStorageSync(T_STORAGE_KEY, res.data)
- uni.hideLoading()
- resolve(res.data)
- })
- .catch((err) => {
- uni.hideLoading()
- resolve(err)
- })
- dispatch('updateIdentityInfo')
- } else {
- uni.showToast({
- title: '缺少用户手机号码'
- })
- resolve('缺少用户手机号码')
- }
- })
- }
- }
- })
- export default store
|