edit.js 3.3 KB

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