edit-profile.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const defaultAvatar = "https://picsum.photos/seed/avatar/200/200";
  5. const _sfc_main = {
  6. data() {
  7. return {
  8. userInfo: {
  9. id: null,
  10. username: "",
  11. nickname: "",
  12. avatar: defaultAvatar,
  13. gender: "",
  14. birthday: "",
  15. bio: "",
  16. phone: "",
  17. email: ""
  18. },
  19. isLogin: false,
  20. isSaving: false
  21. };
  22. },
  23. onLoad() {
  24. this.loadUserInfo();
  25. },
  26. onShow() {
  27. const tempBio = common_vendor.index.getStorageSync("temp_text_bio");
  28. if (typeof tempBio === "string") {
  29. this.userInfo.bio = tempBio;
  30. common_vendor.index.removeStorageSync("temp_text_bio");
  31. const storedUser = common_vendor.index.getStorageSync("userInfo");
  32. if (storedUser) {
  33. storedUser.bio = tempBio;
  34. common_vendor.index.setStorageSync("userInfo", storedUser);
  35. }
  36. return;
  37. }
  38. this.loadUserInfo(true);
  39. },
  40. methods: {
  41. async loadUserInfo(forceRemote = false) {
  42. try {
  43. const storedUser = common_vendor.index.getStorageSync("userInfo") || null;
  44. const isLogin = common_vendor.index.getStorageSync("isLogin");
  45. if (!storedUser || !storedUser.id || !isLogin) {
  46. this.isLogin = false;
  47. this.userInfo = {
  48. ...this.userInfo,
  49. id: null,
  50. username: "",
  51. nickname: "",
  52. avatar: defaultAvatar,
  53. gender: "",
  54. birthday: "",
  55. bio: "",
  56. phone: "",
  57. email: ""
  58. };
  59. return;
  60. }
  61. this.isLogin = true;
  62. this.userInfo = {
  63. ...this.userInfo,
  64. ...storedUser,
  65. avatar: storedUser.avatar || defaultAvatar
  66. };
  67. if (!forceRemote && this.userInfo.nickname) {
  68. return;
  69. }
  70. const res = await utils_api.getUserProfile(storedUser.id);
  71. if (res && res.code === 200 && res.data) {
  72. this.userInfo = {
  73. ...this.userInfo,
  74. ...res.data,
  75. avatar: res.data.avatar || defaultAvatar
  76. };
  77. common_vendor.index.setStorageSync("userInfo", {
  78. ...storedUser,
  79. ...res.data
  80. });
  81. }
  82. } catch (e) {
  83. common_vendor.index.__f__("error", "at pages/edit-profile/edit-profile.vue:154", "加载用户资料失败:", e);
  84. common_vendor.index.showToast({
  85. title: "加载失败,请重试",
  86. icon: "none"
  87. });
  88. }
  89. },
  90. goBack() {
  91. common_vendor.index.navigateBack();
  92. },
  93. async handleSave() {
  94. try {
  95. if (!this.isLogin || !this.userInfo.id) {
  96. common_vendor.index.showToast({ title: "请先登录", icon: "none" });
  97. return;
  98. }
  99. if (this.isSaving)
  100. return;
  101. this.isSaving = true;
  102. common_vendor.index.showLoading({ title: "保存中..." });
  103. const payload = {
  104. userId: this.userInfo.id,
  105. nickname: this.userInfo.nickname || "",
  106. avatar: this.userInfo.avatar === defaultAvatar ? "" : this.userInfo.avatar || "",
  107. gender: this.userInfo.gender || "",
  108. birthday: this.userInfo.birthday || "",
  109. bio: this.userInfo.bio || "",
  110. phone: this.userInfo.phone || "",
  111. email: this.userInfo.email || ""
  112. };
  113. const res = await utils_api.updateUserProfile(payload);
  114. common_vendor.index.hideLoading();
  115. this.isSaving = false;
  116. if (res && res.code === 200) {
  117. const merged = {
  118. ...this.userInfo,
  119. ...res.data,
  120. avatar: res.data.avatar || this.userInfo.avatar || defaultAvatar
  121. };
  122. this.userInfo = merged;
  123. common_vendor.index.setStorageSync("userInfo", merged);
  124. common_vendor.index.showToast({ title: "保存成功", icon: "success" });
  125. setTimeout(() => common_vendor.index.navigateBack(), 800);
  126. } else {
  127. common_vendor.index.showToast({ title: res && res.message || "保存失败", icon: "none" });
  128. }
  129. } catch (e) {
  130. common_vendor.index.hideLoading();
  131. this.isSaving = false;
  132. common_vendor.index.__f__("error", "at pages/edit-profile/edit-profile.vue:202", "保存用户资料失败:", e);
  133. common_vendor.index.showToast({ title: "网络错误,请稍后再试", icon: "none" });
  134. }
  135. },
  136. handleAvatarClick() {
  137. common_vendor.index.chooseImage({
  138. count: 1,
  139. sizeType: ["compressed"],
  140. sourceType: ["album", "camera"],
  141. success: (res) => {
  142. const tempFilePath = res.tempFilePaths[0];
  143. this.userInfo.avatar = tempFilePath;
  144. common_vendor.index.showToast({
  145. title: "头像已更新",
  146. icon: "success"
  147. });
  148. }
  149. });
  150. },
  151. handleNicknameClick() {
  152. common_vendor.index.showModal({
  153. title: "修改昵称",
  154. editable: true,
  155. placeholderText: "请输入昵称",
  156. content: this.userInfo.nickname,
  157. success: (res) => {
  158. if (res.confirm && res.content) {
  159. this.userInfo.nickname = res.content.trim();
  160. }
  161. }
  162. });
  163. },
  164. handleGenderClick() {
  165. if (!this.isLogin) {
  166. this.goToLogin();
  167. return;
  168. }
  169. common_vendor.index.showActionSheet({
  170. itemList: ["男", "女", "保密"],
  171. success: (res) => {
  172. const genders = ["男", "女", "保密"];
  173. this.userInfo.gender = genders[res.tapIndex];
  174. }
  175. });
  176. },
  177. handleBirthdayClick() {
  178. if (!this.isLogin) {
  179. this.goToLogin();
  180. return;
  181. }
  182. common_vendor.index.showModal({
  183. title: "设置生日",
  184. editable: true,
  185. placeholderText: "格式:YYYY-MM-DD",
  186. content: this.userInfo.birthday || "",
  187. success: (modalRes) => {
  188. if (modalRes.confirm) {
  189. const value = (modalRes.content || "").trim();
  190. const ok = /^\d{4}-\d{2}-\d{2}$/.test(value);
  191. if (!value || ok) {
  192. this.userInfo.birthday = value;
  193. } else {
  194. common_vendor.index.showToast({ title: "日期格式应为YYYY-MM-DD", icon: "none" });
  195. }
  196. }
  197. }
  198. });
  199. },
  200. handleBioClick() {
  201. if (!this.isLogin) {
  202. this.goToLogin();
  203. return;
  204. }
  205. const bioValue = this.userInfo.bio || "";
  206. common_vendor.index.navigateTo({
  207. url: `/pages/text-edit/text-edit?key=bio&value=${encodeURIComponent(bioValue)}&placeholder=${encodeURIComponent("请输入个人简介")}&title=${encodeURIComponent("个人简介")}`
  208. });
  209. },
  210. goToLogin() {
  211. common_vendor.index.navigateTo({
  212. url: "/pages/login/login"
  213. });
  214. }
  215. }
  216. };
  217. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  218. return common_vendor.e({
  219. a: common_vendor.o((...args) => $options.goBack && $options.goBack(...args)),
  220. b: common_vendor.o((...args) => $options.handleSave && $options.handleSave(...args)),
  221. c: $data.userInfo.avatar,
  222. d: common_vendor.o((...args) => $options.handleAvatarClick && $options.handleAvatarClick(...args)),
  223. e: common_vendor.t($data.userInfo.nickname),
  224. f: common_vendor.o((...args) => $options.handleNicknameClick && $options.handleNicknameClick(...args)),
  225. g: common_vendor.t($data.userInfo.gender || "未设置"),
  226. h: common_vendor.o((...args) => $options.handleGenderClick && $options.handleGenderClick(...args)),
  227. i: common_vendor.t($data.userInfo.birthday || "未设置"),
  228. j: common_vendor.o((...args) => $options.handleBirthdayClick && $options.handleBirthdayClick(...args)),
  229. k: common_vendor.t($data.userInfo.bio || "未设置"),
  230. l: common_vendor.o((...args) => $options.handleBioClick && $options.handleBioClick(...args)),
  231. m: !$data.isLogin
  232. }, !$data.isLogin ? {
  233. n: common_vendor.o((...args) => $options.goToLogin && $options.goToLogin(...args))
  234. } : {});
  235. }
  236. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-c0f45e44"]]);
  237. wx.createPage(MiniProgramPage);
  238. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/edit-profile/edit-profile.js.map