user.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * 用户状态管理
  3. */
  4. import {
  5. defineStore
  6. } from 'pinia'
  7. import {
  8. getCardInfo
  9. } from '@/api/card.js'
  10. import {
  11. formatRichText
  12. } from '@/utils/index.js'
  13. import {
  14. generateCardPoster,
  15. } from '@/utils/poster.js'
  16. // 空名片模板
  17. const createEmptyCard = () => ({
  18. "userId": "",
  19. "nickName": "",
  20. "phonenumber": "",
  21. "jobTitle": "",
  22. "companyName": "",
  23. "companyAddress": "",
  24. "email": "",
  25. "sex": null,
  26. "avatar": "",
  27. "companyDTO": {
  28. "id": "",
  29. "name": "",
  30. "email": "",
  31. "address": "",
  32. "introduce": ""
  33. }
  34. })
  35. export const useUserStore = defineStore('user', {
  36. state: () => ({
  37. cardList: [], // 名片数组(接口返回)
  38. currentIndex: 0, // 当前选中的名片索引(全局统一)
  39. cardImages: [],
  40. }),
  41. getters: {
  42. // 当前展示的名片对象
  43. cardInfo(state) {
  44. console.log(state.currentIndex,"state.cardList[state.currentIndex]state.cardList[state.currentIndex]state.cardList[state.currentIndex]");
  45. return state.cardList.length > 0 && state.cardList[state.currentIndex] ?
  46. state.cardList[state.currentIndex] :
  47. createEmptyCard()
  48. },
  49. cardImage(state) {
  50. return state.cardImages[state.currentIndex] || ''
  51. },
  52. // 名片总数
  53. cardCount(state) {
  54. return state.cardList.length
  55. }
  56. },
  57. actions: {
  58. // 设置当前名片索引
  59. setCurrentIndex(index) {
  60. console.log("indexindexindexindexindexindexindex", index);
  61. this.currentIndex = index
  62. },
  63. // 获取用户卡片信息
  64. queryCardInfo(userId = null) {
  65. return new Promise(async (resolve, reject) => {
  66. let parmas = userId ? {
  67. userId
  68. } : {}
  69. let res = await getCardInfo(parmas)
  70. console.log(res, "resresresresresres");
  71. if (res.code == 200) {
  72. let dataList = Array.isArray(res.data) ? res.data : [res.data]
  73. // 处理每个数据的头像 https
  74. this.cardList = dataList.map(item => {
  75. if (item.avatar) {
  76. item.avatar = item.avatar.replace("http", "https")
  77. }
  78. return item
  79. })
  80. this.currentIndex = 0
  81. } else if (res.code == 401) {
  82. // token 失效,清除登录态并跳转登录页
  83. uni.removeStorageSync('token')
  84. this.$reset()
  85. uni.reLaunch({
  86. url: '/pages/login/login'
  87. })
  88. }
  89. resolve(res.data)
  90. })
  91. },
  92. queryCardPoster() {
  93. return new Promise(async (resolve, reject) => {
  94. let cardImages = []
  95. for (let i = 0; i < this.cardList.length; i++) {
  96. let item = this.cardList[i];
  97. let cardImage = await generateCardPoster(item)
  98. cardImages.push(cardImage)
  99. }
  100. this.cardImages = cardImages || ''
  101. resolve(cardImages)
  102. })
  103. },
  104. // 退出登录,重置状态
  105. logout() {
  106. this.$reset()
  107. }
  108. }
  109. })