edit.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 || "/static/images/head.png";
  28. nickname.value = userInfo.nickname || "";
  29. phone.value = userInfo.phone || "";
  30. originalAvatar.value = avatarUrl.value;
  31. originalNickname.value = nickname.value;
  32. }
  33. const latestInfo = await utils_auth.refreshUserInfo();
  34. if (latestInfo) {
  35. avatarUrl.value = latestInfo.avatar || "/static/images/head.png";
  36. nickname.value = latestInfo.nickname || "";
  37. phone.value = latestInfo.phone || "";
  38. originalAvatar.value = avatarUrl.value;
  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: "http://localhost:8081/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. if (avatarUrl.value === originalAvatar.value && nickname.value === originalNickname.value) {
  85. common_vendor.index.showToast({ title: "没有修改", icon: "none" });
  86. return;
  87. }
  88. saving.value = true;
  89. common_vendor.index.showLoading({ title: "保存中..." });
  90. try {
  91. let uploadedAvatarUrl = avatarUrl.value;
  92. if (avatarUrl.value !== originalAvatar.value && !avatarUrl.value.startsWith("/static/") && !avatarUrl.value.startsWith("http")) {
  93. try {
  94. uploadedAvatarUrl = await uploadAvatar(avatarUrl.value);
  95. } catch (e) {
  96. console.warn("头像上传失败,使用默认头像:", e.message);
  97. uploadedAvatarUrl = "/static/images/head.png";
  98. }
  99. }
  100. await utils_api.updateUserProfile({
  101. nickname: nickname.value.trim(),
  102. avatar: uploadedAvatarUrl
  103. });
  104. const userInfo = utils_auth.getUserInfo();
  105. userInfo.nickname = nickname.value.trim();
  106. userInfo.avatar = uploadedAvatarUrl;
  107. utils_auth.setUserInfo(userInfo);
  108. common_vendor.index.hideLoading();
  109. common_vendor.index.showToast({ title: "保存成功", icon: "success" });
  110. setTimeout(() => {
  111. common_vendor.index.navigateBack();
  112. }, 1500);
  113. } catch (error) {
  114. console.error("保存失败:", error);
  115. common_vendor.index.hideLoading();
  116. common_vendor.index.showToast({ title: error.message || "保存失败", icon: "none" });
  117. } finally {
  118. saving.value = false;
  119. }
  120. };
  121. return (_ctx, _cache) => {
  122. return common_vendor.e({
  123. a: common_vendor.o(handleBack),
  124. b: avatarUrl.value,
  125. c: common_vendor.o(onChooseAvatar),
  126. d: nickname.value,
  127. e: common_vendor.o(($event) => nickname.value = $event.detail.value),
  128. f: phone.value
  129. }, phone.value ? {
  130. g: common_vendor.t(formatPhone(phone.value))
  131. } : {}, {
  132. h: common_vendor.t(saving.value ? "保存中..." : "保存"),
  133. i: common_vendor.o(handleSave),
  134. j: saving.value
  135. });
  136. };
  137. }
  138. };
  139. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-7e5a80f3"], ["__file", "D:/program/gupiao-wx/src/pages/profile/edit.vue"]]);
  140. wx.createPage(MiniProgramPage);