user.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * 用户状态管理
  3. */
  4. import {
  5. defineStore
  6. } from 'pinia'
  7. import {
  8. getCardInfo,
  9. getCardQrcode,
  10. getCompanyInfo
  11. } from '@/api/card.js'
  12. import {
  13. formatRichText
  14. } from '@/utils/index.js'
  15. import {
  16. generateCardPoster,
  17. } from '@/utils/poster.js'
  18. export const useUserStore = defineStore('user', {
  19. state: () => ({
  20. cardInfo: {
  21. "userId": "",
  22. "nickName": "",
  23. "phonenumber": "",
  24. "postName": "",
  25. "companyName": "",
  26. "companyAddress": "",
  27. "email": "",
  28. "sex": null,
  29. "avatar": ""
  30. },
  31. companyInfo: {
  32. "name": "",
  33. "email": "",
  34. "address": "",
  35. "introduce": ""
  36. },
  37. qrInfo: {
  38. },
  39. cardImage: "",
  40. }),
  41. actions: {
  42. // 获取用户卡片信息
  43. queryCardInfo(userId = null) {
  44. return new Promise(async (resolve, reject) => {
  45. let parmas = userId ? {
  46. userId
  47. } : {}
  48. let res = await getCardInfo(parmas)
  49. console.log(res, "resresresresresres");
  50. if (res.code == 200) {
  51. this.cardInfo = res.data
  52. this.cardInfo.avatar = res.data.avatar.replace("http","https")
  53. } else if (res.code == 401) {
  54. // token 失效,清除登录态并跳转登录页
  55. uni.removeStorageSync('token')
  56. this.$reset()
  57. uni.reLaunch({ url: '/pages/login/login' })
  58. }
  59. resolve(res.data)
  60. })
  61. },
  62. queryCompanyInfo(userId = null) {
  63. return new Promise(async (resolve, reject) => {
  64. let parmas = userId ? {
  65. userId
  66. } : {}
  67. let res = await getCompanyInfo(parmas)
  68. if (res.code == 200) {
  69. this.companyInfo = res.data
  70. this.companyInfo.introduce = formatRichText(res.data.introduce)
  71. } else if (res.code == 401) {
  72. // token 失效,清除登录态并跳转登录页
  73. uni.removeStorageSync('token')
  74. this.$reset()
  75. uni.reLaunch({ url: '/pages/login/login' })
  76. }
  77. resolve(this.companyInfo)
  78. })
  79. },
  80. queryCardQrcode(userId = null) {
  81. return new Promise(async (resolve, reject) => {
  82. let parmas = userId ? {
  83. userId
  84. } : {
  85. userId: this.cardInfo.userId
  86. }
  87. let res = await getCardQrcode(parmas)
  88. this.qrInfo = res.data || {}
  89. resolve(res.data)
  90. })
  91. },
  92. queryCardPoster() {
  93. return new Promise(async (resolve, reject) => {
  94. const cardImage = await generateCardPoster(this.cardInfo)
  95. this.cardImage = cardImage || ''
  96. resolve(cardImage)
  97. })
  98. },
  99. // 退出登录,重置状态
  100. logout() {
  101. this.$reset()
  102. }
  103. }
  104. })