edit.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_auth = require("../../utils/auth.js");
  4. const utils_api = require("../../utils/api.js");
  5. const _sfc_main = {
  6. data() {
  7. return {
  8. avatarUrl: "/static/images/head.png",
  9. nickname: "",
  10. phone: "",
  11. originalAvatar: "",
  12. originalNickname: ""
  13. };
  14. },
  15. onLoad() {
  16. const loginStatus = utils_auth.isLoggedIn();
  17. console.log("[编辑资料] 登录状态:", loginStatus);
  18. this.loadUserInfo();
  19. },
  20. onShow() {
  21. common_vendor.index.setNavigationBarTitle({ title: "量化交易大师" });
  22. },
  23. methods: {
  24. /**
  25. * 加载用户信息
  26. */
  27. loadUserInfo() {
  28. const userInfo = utils_auth.getUserInfo();
  29. console.log("[编辑资料] 加载用户信息:", userInfo);
  30. if (userInfo) {
  31. this.avatarUrl = userInfo.avatar || "/static/images/head.png";
  32. this.nickname = userInfo.nickname || "";
  33. this.phone = userInfo.phone || "";
  34. this.originalAvatar = this.avatarUrl;
  35. this.originalNickname = this.nickname;
  36. }
  37. },
  38. /**
  39. * 选择头像
  40. */
  41. onChooseAvatar(e) {
  42. const { avatarUrl } = e.detail;
  43. this.avatarUrl = avatarUrl;
  44. console.log("选择头像:", avatarUrl);
  45. },
  46. /**
  47. * 保存资料
  48. */
  49. async handleSave() {
  50. if (!this.nickname || this.nickname.trim() === "") {
  51. common_vendor.index.showToast({
  52. title: "请输入昵称",
  53. icon: "none"
  54. });
  55. return;
  56. }
  57. if (this.avatarUrl === this.originalAvatar && this.nickname === this.originalNickname) {
  58. common_vendor.index.showToast({
  59. title: "没有修改",
  60. icon: "none"
  61. });
  62. return;
  63. }
  64. try {
  65. common_vendor.index.showLoading({ title: "保存中..." });
  66. let uploadedAvatarUrl = this.avatarUrl;
  67. if (this.avatarUrl !== this.originalAvatar && !this.avatarUrl.startsWith("/static/")) {
  68. uploadedAvatarUrl = this.avatarUrl;
  69. }
  70. const result = await utils_api.updateUserProfile({
  71. nickname: this.nickname,
  72. avatar: uploadedAvatarUrl
  73. });
  74. console.log("更新成功:", result);
  75. const userInfo = utils_auth.getUserInfo();
  76. userInfo.nickname = this.nickname;
  77. userInfo.avatar = uploadedAvatarUrl;
  78. utils_auth.setUserInfo(userInfo);
  79. common_vendor.index.hideLoading();
  80. common_vendor.index.showToast({
  81. title: "保存成功",
  82. icon: "success"
  83. });
  84. setTimeout(() => {
  85. common_vendor.index.navigateBack();
  86. }, 1500);
  87. } catch (error) {
  88. console.error("保存失败:", error);
  89. common_vendor.index.hideLoading();
  90. common_vendor.index.showToast({
  91. title: error.message || "保存失败",
  92. icon: "none"
  93. });
  94. }
  95. }
  96. }
  97. };
  98. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  99. return common_vendor.e({
  100. a: $data.avatarUrl,
  101. b: common_vendor.o((...args) => $options.onChooseAvatar && $options.onChooseAvatar(...args)),
  102. c: $data.nickname,
  103. d: common_vendor.o(($event) => $data.nickname = $event.detail.value),
  104. e: $data.phone
  105. }, $data.phone ? {
  106. f: common_vendor.t($data.phone)
  107. } : {}, {
  108. g: common_vendor.o((...args) => $options.handleSave && $options.handleSave(...args))
  109. });
  110. }
  111. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-7e5a80f3"], ["__file", "D:/program/gupiao-wx/src/pages/profile/edit.vue"]]);
  112. wx.createPage(MiniProgramPage);