profile.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const api_student = require("../../api/student.js");
  4. const api_dict = require("../../api/dict.js");
  5. const utils_request = require("../../utils/request.js");
  6. if (!Array) {
  7. const _easycom_step_layout2 = common_vendor.resolveComponent("step-layout");
  8. _easycom_step_layout2();
  9. }
  10. const _easycom_step_layout = () => "../../components/step-layout/step-layout.js";
  11. if (!Math) {
  12. _easycom_step_layout();
  13. }
  14. const _sfc_main = {
  15. __name: "profile",
  16. setup(__props) {
  17. const isEditMode = common_vendor.ref(false);
  18. const genderOptions = common_vendor.ref([
  19. { dictLabel: "男", dictValue: "0" },
  20. { dictLabel: "女", dictValue: "1" }
  21. ]);
  22. const normalizeGenderValue = (value) => {
  23. if (value === void 0 || value === null || value === "") {
  24. return "0";
  25. }
  26. const gender = String(value).trim().toUpperCase();
  27. if (gender === "M")
  28. return "0";
  29. if (gender === "F")
  30. return "1";
  31. if (gender === "0" || gender === "1" || gender === "2")
  32. return gender;
  33. return "0";
  34. };
  35. const loadGenderDict = async () => {
  36. try {
  37. const res = await api_dict.getDicts("sys_user_sex");
  38. const data = (res == null ? void 0 : res.data) || [];
  39. if (Array.isArray(data) && data.length > 0) {
  40. genderOptions.value = data.filter((item) => item && item.dictValue !== "2").map((item) => ({
  41. dictLabel: item.dictLabel,
  42. dictValue: String(item.dictValue)
  43. }));
  44. }
  45. } catch (error) {
  46. common_vendor.index.__f__("error", "at pages/profile/profile.vue:99", "加载性别字典失败", error);
  47. }
  48. };
  49. const genderSliderClass = common_vendor.computed(() => {
  50. const currentIndex = genderOptions.value.findIndex((item) => item.dictValue === formData.gender);
  51. if (currentIndex <= 0) {
  52. return currentIndex === 0 ? "left" : "none";
  53. }
  54. return "right";
  55. });
  56. common_vendor.onLoad(async (options) => {
  57. await loadGenderDict();
  58. if (options.editMode === "1") {
  59. isEditMode.value = true;
  60. const userInfo = common_vendor.index.getStorageSync("userInfo");
  61. if (userInfo && userInfo.studentId) {
  62. try {
  63. common_vendor.index.showLoading({ title: "加载中..." });
  64. const res = await api_student.getStudent(userInfo.studentId);
  65. common_vendor.index.hideLoading();
  66. if (res && res.data) {
  67. const data = res.data;
  68. Object.assign(formData, {
  69. avatarUrl: data.avatarUrl || "/static/images/default-avatar.svg",
  70. avatarId: data.avatar || null,
  71. name: data.name || "",
  72. gender: normalizeGenderValue(data.gender),
  73. idCard: data.idCardNumber || "",
  74. email: data.email || ""
  75. });
  76. }
  77. } catch (err) {
  78. common_vendor.index.hideLoading();
  79. common_vendor.index.__f__("error", "at pages/profile/profile.vue:138", "获取用户信息失败", err);
  80. common_vendor.index.showToast({ title: "加载数据失败", icon: "none" });
  81. }
  82. }
  83. }
  84. });
  85. const formData = common_vendor.reactive({
  86. avatarUrl: "/static/images/default-avatar.svg",
  87. // 用于展示
  88. avatarId: null,
  89. // 用于传给后端
  90. name: "",
  91. gender: "0",
  92. idCard: "",
  93. email: ""
  94. });
  95. const chooseAvatar = () => {
  96. common_vendor.index.chooseImage({
  97. count: 1,
  98. success: (res) => {
  99. const tempFilePath = res.tempFilePaths[0];
  100. formData.avatarUrl = tempFilePath;
  101. common_vendor.index.showLoading({ title: "上传头像中..." });
  102. common_vendor.index.uploadFile({
  103. url: utils_request.UPLOAD_URL + "/portal/oss/upload",
  104. filePath: tempFilePath,
  105. name: "file",
  106. header: {
  107. "clientid": "e5cd7e4891bf95d1d19206ce24a7b32e",
  108. "PLATFORM_CODE": "PINGTAIDUAN",
  109. "Authorization": common_vendor.index.getStorageSync("token") ? `Bearer ${common_vendor.index.getStorageSync("token")}` : ""
  110. },
  111. success: (uploadFileRes) => {
  112. common_vendor.index.hideLoading();
  113. const data = JSON.parse(uploadFileRes.data);
  114. if (data.code === 200) {
  115. formData.avatarId = data.data.ossId;
  116. common_vendor.index.showToast({ title: "上传成功", icon: "success" });
  117. } else {
  118. common_vendor.index.showToast({ title: data.msg || "上传失败", icon: "none" });
  119. }
  120. },
  121. fail: (err) => {
  122. common_vendor.index.hideLoading();
  123. common_vendor.index.__f__("error", "at pages/profile/profile.vue:184", "上传失败", err);
  124. common_vendor.index.showToast({ title: "上传失败", icon: "none" });
  125. }
  126. });
  127. }
  128. });
  129. };
  130. const setGender = (val) => {
  131. formData.gender = val;
  132. };
  133. const goNext = async () => {
  134. if (!formData.name) {
  135. common_vendor.index.showToast({ title: "请输入您的真实姓名", icon: "none" });
  136. return;
  137. }
  138. if (formData.idCard) {
  139. const idCardReg = /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/;
  140. if (!idCardReg.test(formData.idCard)) {
  141. common_vendor.index.showToast({ title: "请输入正确的身份证号", icon: "none" });
  142. return;
  143. }
  144. }
  145. if (formData.email) {
  146. const emailReg = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  147. if (!emailReg.test(formData.email)) {
  148. common_vendor.index.showToast({ title: "请输入正确的邮箱地址", icon: "none" });
  149. return;
  150. }
  151. }
  152. common_vendor.index.showLoading({ title: "保存中..." });
  153. try {
  154. const userInfo = common_vendor.index.getStorageSync("userInfo");
  155. const studentId = userInfo ? userInfo.studentId : null;
  156. if (!studentId) {
  157. common_vendor.index.hideLoading();
  158. common_vendor.index.showToast({ title: "用户信息失效,请重新登录", icon: "none" });
  159. return;
  160. }
  161. const res = await api_student.updateStudent({
  162. id: studentId,
  163. name: formData.name,
  164. gender: formData.gender,
  165. email: formData.email,
  166. idCardNumber: formData.idCard,
  167. avatar: formData.avatarId
  168. // 传入上传后的 ossId, 是 Long 类型的
  169. });
  170. common_vendor.index.hideLoading();
  171. if (res.code === 200) {
  172. const url = isEditMode.value ? "/pages/experience/experience?editMode=1" : "/pages/experience/experience";
  173. common_vendor.index.navigateTo({ url });
  174. } else {
  175. common_vendor.index.showToast({
  176. title: res.msg || "保存失败",
  177. icon: "none"
  178. });
  179. }
  180. } catch (err) {
  181. common_vendor.index.hideLoading();
  182. common_vendor.index.__f__("error", "at pages/profile/profile.vue:254", "保存个人信息异常", err);
  183. common_vendor.index.showToast({ title: "网络异常,请重试", icon: "none" });
  184. }
  185. };
  186. const goSkip = () => {
  187. common_vendor.index.navigateTo({
  188. url: "/pages/experience/experience"
  189. });
  190. };
  191. return (_ctx, _cache) => {
  192. return {
  193. a: formData.avatarUrl,
  194. b: common_vendor.o(chooseAvatar),
  195. c: formData.name,
  196. d: common_vendor.o(($event) => formData.name = $event.detail.value),
  197. e: common_vendor.n(genderSliderClass.value),
  198. f: common_vendor.f(genderOptions.value, (item, k0, i0) => {
  199. return {
  200. a: common_vendor.t(item.dictLabel),
  201. b: item.dictValue,
  202. c: common_vendor.n(formData.gender === item.dictValue ? "active" : ""),
  203. d: common_vendor.o(($event) => setGender(item.dictValue), item.dictValue)
  204. };
  205. }),
  206. g: formData.idCard,
  207. h: common_vendor.o(($event) => formData.idCard = $event.detail.value),
  208. i: formData.email,
  209. j: common_vendor.o(($event) => formData.email = $event.detail.value),
  210. k: common_vendor.o(goNext),
  211. l: common_vendor.o(goSkip),
  212. m: common_vendor.p({
  213. title: "填写个人信息",
  214. showSkip: !isEditMode.value
  215. })
  216. };
  217. };
  218. }
  219. };
  220. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-dd383ca2"]]);
  221. wx.createPage(MiniProgramPage);
  222. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/profile/profile.js.map