| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- import Vue from 'vue'
- import Vuex from 'vuex'
- import * as storageKeys from './storageKeys'
- import { bindWxCode, wxLogin } from '@/api/app'
- Vue.use(Vuex)
- const store = new Vuex.Store({
- state: {
- imgPrefix: 'https://image.qcbwc.cn/',
- ossImgResize: {},
- enable_commission: false,
- min_apply_amount: 2000,
- commission_banner: 'https://image.qcbwc.cn/banner/commission-banner.png',
- openid: uni.getStorageSync(storageKeys.OPENIDID) || '', // 微信code换取openid
- userInfo: uni.getStorageSync(storageKeys.USER_INFO) || {}, // 用户信息
- cateId: '',
- INVITE_ID: uni.getStorageSync(storageKeys.INVITE_ID) || '',
- lat: uni.getStorageSync(storageKeys.LAT) || '',
- lng: uni.getStorageSync(storageKeys.LNG) || ''
- },
- mutations: {
- changeOssImgResize (state, data) {
- state.ossImgResize = data
- },
- saveInviteId (state, data) {
- if (data) {
- if (!state.INVITE_ID) {
- state.INVITE_ID = data
- uni.setStorageSync(storageKeys.INVITE_ID, data)
- }
- }
- },
- changeLocation (state, data) {
- if (data) {
- state.lat = data.lat
- state.lng = data.lng
- uni.setStorageSync(storageKeys.LAT, data.lat)
- uni.setStorageSync(storageKeys.LNG, data.lng)
- }
- },
- changeOpenId (state, data) {
- if (!data) data = ''
- state.openid = data
- uni.setStorageSync(storageKeys.OPENIDID, data)
- },
- updateUserInfo (state, info) {
- state.userInfo = info
- if (!state.userInfo.token) {
- uni.setStorageSync(storageKeys.USER_TOKEN, state.userInfo.token)
- }
- uni.setStorageSync(storageKeys.USER_INFO, state.userInfo)
- },
- changeHeadOrNickName (state, data) {
- console.log(data)
- state.userInfo.head_url = data.head_url
- state.userInfo.nick_name = data.nick_name
- state.userInfo.alipay_account = data.alipay_account
- state.userInfo.alipay_username = data.alipay_username
- uni.setStorageSync(storageKeys.USER_INFO, state.userInfo)
- },
- changeCateId (state, data) {
- state.cateId = data
- }
- },
- actions: {
- async getUserInfo ({ commit, state, dispatch }) {
- return new Promise(async resolve => {
- await new Promise(resolve => {
- uni.checkSession({
- success: res => resolve(res),
- fail: err => {
- // console.error('检查登陆状态失败', err)
- resolve(err)
- }
- })
- })
- let userData = {}
- let params = {}
- // if (state.openid === '' || sessionInfo.errMsg !== 'checkSession:ok')
- {
- const openid = await dispatch('getUserCode')
- if (!openid) {
- return
- }
- userData = await uni.getUserInfo({ withCredentials: true })
- }
- if (!Array.isArray(userData) && !userData.userInfo) {
- resolve(false)
- return
- }
- if (Array.isArray(userData)) {
- if (userData[1].errMsg !== 'getUserInfo:ok') {
- resolve(false)
- return
- }
- params = {
- openid: state.openid,
- iv: userData[1].iv,
- encryptedData: userData[1].encryptedData
- }
- } else {
- if (userData.errMsg !== 'getUserInfo:ok') {
- resolve(false)
- return
- }
- params = {
- openid: state.openid,
- iv: userData.iv,
- encryptedData: userData.encryptedData
- }
- }
- const userInfo = await wxLogin(params)
- console.log(userInfo)
- if (userInfo.data) {
- userInfo.data.openid = state.openid
- uni.setStorageSync(storageKeys.USER_TOKEN, userInfo.data.token)
- commit('updateUserInfo', userInfo.data)
- resolve(true)
- } else {
- resolve(false)
- }
- })
- },
- getUserCode ({ commit }) {
- return new Promise(async (resolve, reject) => {
- try {
- const loginData = await new Promise(resolve => {
- uni.login({
- success: res => resolve(res),
- fail: err => {
- console.error('登录失败', err)
- resolve(false)
- }
- })
- })
- const { code, data, msg } = await bindWxCode({ code: loginData.code })
- if (code === 200) {
- if (Object.prototype.hasOwnProperty.call(data, 'openid')) {
- commit('changeOpenId', data.openid)
- resolve(data.openid)
- } else {
- resolve(data)
- }
- } else {
- uni.showToast({
- icon: 'none',
- title: msg
- })
- resolve(false)
- }
- } catch (err) {
- console.error('获取用户信息失败', err)
- resolve(false)
- }
- })
- },
- toOtherApplte ({ commit, state, dispatch }, platType) {
- let appId = ''
- if (platType * 1 === 8) {
- appId = 'wxece3a9a4c82f58c9'
- } else if (platType * 1 === 9) {
- appId = 'wx2c348cf579062e56'
- }
- uni.navigateToMiniProgram({
- appId: appId,
- path: ''
- })
- }
- }
- })
- export default store
|