您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {
  2. setItem,
  3. removeItem,
  4. StorageKeys,
  5. getItem
  6. } from "@/utils/storage";
  7. import {
  8. defineStore
  9. } from "pinia";
  10. import {
  11. request
  12. } from "@/utils/network/request";
  13. import {
  14. appId,
  15. getToken,
  16. wechatAppID
  17. } from "@/utils/network/api";
  18. import {
  19. stringToJson
  20. } from "@/utils/network/encryption";
  21. import {
  22. confirm,
  23. msg,
  24. navTo
  25. } from "@/utils/utils";
  26. export const useUserStore = defineStore("userStore", {
  27. state: () => ({
  28. //访问令牌
  29. access_token: "",
  30. //openId
  31. openId: "",
  32. }),
  33. getters: {},
  34. actions: {
  35. //设置用户token
  36. saveToken(token) {
  37. this.access_token = token;
  38. setItem(StorageKeys.Token, token);
  39. },
  40. //设置openId
  41. saveOpenId(openId) {
  42. this.openId = openId;
  43. setItem(StorageKeys.OpenId, openId);
  44. },
  45. /*根据登录code获取token信息*/
  46. fetchToken(code) {
  47. console.log(code);
  48. const options = {
  49. type: 2,
  50. data: {
  51. deviceId: "",
  52. code: code,
  53. loginSource: "69af303ba2eb4608a099163f0d2a5dbd",
  54. 'wxOpenid':wechatAppID,
  55. },
  56. method: "POST",
  57. showLoading: false,
  58. };
  59. return new Promise(async (resolve, reject) => {
  60. const res = await request(getToken, options);
  61. const data = stringToJson(res.bizContent);
  62. console.log("data",data)
  63. this.saveToken(data.accessToken);
  64. this.saveOpenId(data.openId);
  65. resolve(data);
  66. }).catch((error) => {
  67. reject(error);
  68. });
  69. },
  70. /*退出登录*/
  71. loginOutNoConfirm() {
  72. msg("退出登录成功!");
  73. // //把state中的值恢复成默认值
  74. this.$reset();
  75. // //移除本地缓存
  76. removeItem(StorageKeys.Token);
  77. removeItem(StorageKeys.OpenId);
  78. uni.$emit('loginOut');
  79. uni.navigateTo({
  80. url: "/login/login"
  81. });
  82. },
  83. /*退出登录*/
  84. loginOut() {
  85. confirm(
  86. "确定要退出登录吗?",
  87. () => {
  88. this.loginOutNoConfirm();
  89. },
  90. "提示",
  91. true
  92. );
  93. },
  94. },
  95. });