| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- /**
- * 用户状态管理
- */
- import {
- defineStore
- } from 'pinia'
- import {
- getCardInfo
- } from '@/api/card.js'
- import {
- formatRichText
- } from '@/utils/index.js'
- import {
- generateCardPoster,
- } from '@/utils/poster.js'
- // 空名片模板
- const createEmptyCard = () => ({
- "userId": "",
- "nickName": "",
- "phonenumber": "",
- "jobTitle": "",
- "companyName": "",
- "companyAddress": "",
- "email": "",
- "sex": null,
- "avatar": "",
- "companyDTO": {
- "id": "",
- "name": "",
- "email": "",
- "address": "",
- "introduce": ""
- }
- })
- export const useUserStore = defineStore('user', {
- state: () => ({
- cardList: [], // 名片数组(接口返回)
- currentIndex: 0, // 当前选中的名片索引(全局统一)
- cardImages: [],
- }),
- getters: {
- // 当前展示的名片对象
- cardInfo(state) {
- console.log(state.currentIndex,"state.cardList[state.currentIndex]state.cardList[state.currentIndex]state.cardList[state.currentIndex]");
- return state.cardList.length > 0 && state.cardList[state.currentIndex] ?
- state.cardList[state.currentIndex] :
- createEmptyCard()
- },
- cardImage(state) {
- return state.cardImages[state.currentIndex] || ''
- },
- // 名片总数
- cardCount(state) {
- return state.cardList.length
- }
- },
- actions: {
- // 设置当前名片索引
- setCurrentIndex(index) {
- console.log("indexindexindexindexindexindexindex", index);
- this.currentIndex = index
- },
- // 获取用户卡片信息
- queryCardInfo(userId = null) {
- return new Promise(async (resolve, reject) => {
- let parmas = userId ? {
- userId
- } : {}
- let res = await getCardInfo(parmas)
- console.log(res, "resresresresresres");
- if (res.code == 200) {
- let dataList = Array.isArray(res.data) ? res.data : [res.data]
- // 处理每个数据的头像 https
- this.cardList = dataList.map(item => {
- if (item.avatar) {
- item.avatar = item.avatar.replace("http", "https")
- }
- return item
- })
- this.currentIndex = 0
- } else if (res.code == 401) {
- // token 失效,清除登录态并跳转登录页
- uni.removeStorageSync('token')
- this.$reset()
- uni.reLaunch({
- url: '/pages/login/login'
- })
- }
- resolve(res.data)
- })
- },
- queryCardPoster() {
- return new Promise(async (resolve, reject) => {
- let cardImages = []
- for (let i = 0; i < this.cardList.length; i++) {
- let item = this.cardList[i];
- let cardImage = await generateCardPoster(item)
- cardImages.push(cardImage)
- }
- this.cardImages = cardImages || ''
- resolve(cardImages)
- })
- },
- // 退出登录,重置状态
- logout() {
- this.$reset()
- }
- }
- })
|