edit.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. __name: "edit",
  7. setup(__props) {
  8. const avatarUrl = common_vendor.ref("/static/images/head.png");
  9. const nickname = common_vendor.ref("");
  10. const phone = common_vendor.ref("");
  11. const originalAvatar = common_vendor.ref("");
  12. const originalNickname = common_vendor.ref("");
  13. const saving = common_vendor.ref(false);
  14. const handleBack = () => {
  15. const pages = getCurrentPages();
  16. pages.length > 1 ? common_vendor.index.navigateBack() : common_vendor.index.switchTab({ url: "/pages/mine/mine" });
  17. };
  18. common_vendor.onMounted(() => {
  19. loadUserInfo();
  20. });
  21. common_vendor.onShow(() => {
  22. loadUserInfo();
  23. });
  24. const loadUserInfo = async () => {
  25. const userInfo = utils_auth.getUserInfo();
  26. if (userInfo) {
  27. avatarUrl.value = userInfo.avatar ? utils_api.getImageUrl(userInfo.avatar) : "/static/images/head.png";
  28. nickname.value = userInfo.nickname || "";
  29. phone.value = userInfo.phone || "";
  30. originalAvatar.value = userInfo.avatar || "";
  31. originalNickname.value = nickname.value;
  32. }
  33. const latestInfo = await utils_auth.refreshUserInfo();
  34. if (latestInfo) {
  35. avatarUrl.value = latestInfo.avatar ? utils_api.getImageUrl(latestInfo.avatar) : "/static/images/head.png";
  36. nickname.value = latestInfo.nickname || "";
  37. phone.value = latestInfo.phone || "";
  38. originalAvatar.value = latestInfo.avatar || "";
  39. originalNickname.value = nickname.value;
  40. }
  41. };
  42. const formatPhone = (p) => {
  43. if (!p || p.length !== 11)
  44. return p;
  45. return p.substring(0, 3) + "****" + p.substring(7);
  46. };
  47. const onChooseAvatar = (e) => {
  48. avatarUrl.value = e.detail.avatarUrl;
  49. };
  50. const uploadAvatar = async (tempPath) => {
  51. return new Promise((resolve, reject) => {
  52. const token = common_vendor.index.getStorageSync("user_token");
  53. common_vendor.index.uploadFile({
  54. url: `${utils_api.BASE_URL}/v1/file/upload`,
  55. filePath: tempPath,
  56. name: "file",
  57. header: {
  58. "Authorization": `Bearer ${token}`
  59. },
  60. success: (res) => {
  61. var _a;
  62. if (res.statusCode === 200) {
  63. const data = JSON.parse(res.data);
  64. if (data.code === 200 && ((_a = data.data) == null ? void 0 : _a.url)) {
  65. resolve(data.data.url);
  66. } else {
  67. reject(new Error(data.message || "上传失败"));
  68. }
  69. } else {
  70. reject(new Error("上传失败"));
  71. }
  72. },
  73. fail: (err) => {
  74. reject(new Error("网络错误"));
  75. }
  76. });
  77. });
  78. };
  79. const handleSave = async () => {
  80. if (!nickname.value || nickname.value.trim() === "") {
  81. common_vendor.index.showToast({ title: "请输入昵称", icon: "none" });
  82. return;
  83. }
  84. let currentAvatarPath = avatarUrl.value;
  85. if (currentAvatarPath.startsWith(utils_api.BASE_URL)) {
  86. currentAvatarPath = currentAvatarPath.replace(utils_api.BASE_URL, "");
  87. }
  88. if (currentAvatarPath === originalAvatar.value && nickname.value === originalNickname.value) {
  89. common_vendor.index.showToast({ title: "没有修改", icon: "none" });
  90. return;
  91. }
  92. saving.value = true;
  93. common_vendor.index.showLoading({ title: "保存中..." });
  94. try {
  95. let uploadedAvatarUrl = currentAvatarPath;
  96. if (currentAvatarPath !== originalAvatar.value && !avatarUrl.value.startsWith("/static/") && !avatarUrl.value.startsWith("http")) {
  97. try {
  98. uploadedAvatarUrl = await uploadAvatar(avatarUrl.value);
  99. } catch (e) {
  100. console.warn("头像上传失败,使用默认头像:", e.message);
  101. uploadedAvatarUrl = "/static/images/head.png";
  102. }
  103. }
  104. await utils_api.updateUserProfile({
  105. nickname: nickname.value.trim(),
  106. avatar: uploadedAvatarUrl
  107. });
  108. const userInfo = utils_auth.getUserInfo();
  109. userInfo.nickname = nickname.value.trim();
  110. userInfo.avatar = uploadedAvatarUrl;
  111. utils_auth.setUserInfo(userInfo);
  112. common_vendor.index.hideLoading();
  113. common_vendor.index.showToast({ title: "保存成功", icon: "success" });
  114. setTimeout(() => {
  115. common_vendor.index.navigateBack();
  116. }, 1500);
  117. } catch (error) {
  118. console.error("保存失败:", error);
  119. common_vendor.index.hideLoading();
  120. common_vendor.index.showToast({ title: error.message || "保存失败", icon: "none" });
  121. } finally {
  122. saving.value = false;
  123. }
  124. };
  125. return (_ctx, _cache) => {
  126. return common_vendor.e({
  127. a: common_vendor.o(handleBack),
  128. b: avatarUrl.value,
  129. c: common_vendor.o(onChooseAvatar),
  130. d: nickname.value,
  131. e: common_vendor.o(($event) => nickname.value = $event.detail.value),
  132. f: phone.value
  133. }, phone.value ? {
  134. g: common_vendor.t(formatPhone(phone.value))
  135. } : {}, {
  136. h: common_vendor.t(saving.value ? "保存中..." : "保存"),
  137. i: common_vendor.o(handleSave),
  138. j: saving.value
  139. });
  140. };
  141. }
  142. };
  143. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-7e5a80f3"], ["__file", "D:/program/gupiao-wx/src/pages/profile/edit.vue"]]);
  144. wx.createPage(MiniProgramPage);