edit.js 3.4 KB

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