edit.js 3.5 KB

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