index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. if (!Math) {
  4. (teacher + student)();
  5. }
  6. const teacher = () => "./path/teacher.js";
  7. const student = () => "./path/student.js";
  8. const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  9. __name: "index",
  10. setup(__props) {
  11. const $api = common_vendor.inject("$api");
  12. const $apifile = common_vendor.inject("$apifile");
  13. const config = common_vendor.ref({ logo: [], file: [] });
  14. const form = common_vendor.ref({ icon: [], card: [] });
  15. const errors = common_vendor.ref({});
  16. const user = common_vendor.computed(() => {
  17. return common_vendor.index.getStorageSync("user");
  18. });
  19. const genderList = common_vendor.ref([]);
  20. const educationList = common_vendor.ref([]);
  21. const learnStatusList = common_vendor.ref([]);
  22. const showList = common_vendor.ref([]);
  23. common_vendor.onLoad(async () => {
  24. await searchConfig();
  25. await searchOther();
  26. await search();
  27. });
  28. const searchConfig = async () => {
  29. config.value = common_vendor.index.getStorageSync("config");
  30. };
  31. const searchOther = async () => {
  32. let res;
  33. res = await $api(`dictData`, "GET", { code: "gender", is_use: "0" });
  34. if (res.errcode === 0)
  35. genderList.value = res.data;
  36. res = await $api(`dictData`, "GET", { code: "education", is_use: "0" });
  37. if (res.errcode === 0)
  38. educationList.value = res.data;
  39. res = await $api(`dictData`, "GET", { code: "learnStatus", is_use: "0" });
  40. if (res.errcode === 0)
  41. learnStatusList.value = res.data;
  42. res = await $api(`dictData`, "GET", { code: "show", is_use: "0" });
  43. if (res.errcode === 0)
  44. showList.value = res.data;
  45. };
  46. const search = async () => {
  47. if (user && user.value._id) {
  48. let res;
  49. if (user.value.role_type == "Teacher")
  50. res = await $api(`teacher/${user.value._id}`, "GET", {});
  51. else
  52. res = await $api(`student/${user.value._id}`, "GET", {});
  53. if (res.errcode == "0") {
  54. if (res.data.education) {
  55. const data = educationList.value.find((i) => i.value === res.data.education);
  56. if (data)
  57. res.data.education_name = data.label;
  58. }
  59. if (res.data.learnStatus) {
  60. const data = learnStatusList.value.find((i) => i.value === res.data.learnStatus);
  61. if (data)
  62. res.data.learnStatus_name = data.label;
  63. }
  64. form.value = res.data;
  65. common_vendor.index.setStorageSync("user", { ...res.data, role_type: user.value.role_type });
  66. }
  67. }
  68. };
  69. const deletePic = (event) => {
  70. const file = form.value[event.name].filter((i) => i.url !== event.file.url);
  71. form.value[event.name] = file;
  72. };
  73. const afterRead = async (event) => {
  74. const url = event.file[0].url;
  75. const result = await $apifile(`/web/learn_user/upload`, "file", url, "file");
  76. if (result.errcode === 0)
  77. form.value[event.name] = [...form.value[event.name], ...[result]];
  78. };
  79. const educationChange = (e) => {
  80. const data = educationList.value[e.detail.value];
  81. if (data) {
  82. form.value.education = data.value;
  83. form.value.education_name = data.label;
  84. }
  85. };
  86. const learnStatusChange = (e) => {
  87. const data = learnStatusList.value[e.detail.value];
  88. if (data) {
  89. form.value.learnStatus = data.value;
  90. form.value.learnStatus_name = data.label;
  91. }
  92. };
  93. const bindDateChange = (e) => {
  94. form.value.birth = e.detail.value;
  95. };
  96. const validateObject = (obj) => {
  97. const errors2 = {};
  98. if (!obj.nick_name || obj.nick_name.trim() === "") {
  99. errors2.nick_name = "请填写昵称!";
  100. }
  101. if (!obj.birth || obj.birth.trim() === "") {
  102. errors2.birth = "请选择出生日期!";
  103. }
  104. if (!obj.phone || obj.phone.trim() === "") {
  105. errors2.phone = "请填写手机号!";
  106. } else {
  107. const regex = /^1[3456789]\d{9}$/;
  108. if (!regex.test(obj.phone))
  109. errors2.phone = "请填写正确的手机号码!";
  110. }
  111. if (user.value.role_type == "Teacher") {
  112. if (!obj.is_show || obj.is_show.trim() === "") {
  113. errors2.is_show = "请选择是否公开!";
  114. }
  115. if (!obj.education || obj.education.trim() === "") {
  116. errors2.education = "请选择学历!";
  117. }
  118. if (!obj.college || obj.college.trim() === "") {
  119. errors2.college = "请输入院校!";
  120. }
  121. if (!obj.card || !obj.card.some((value) => value !== void 0)) {
  122. errors2.card = "请上传身份证照片!";
  123. }
  124. }
  125. if (Object.keys(errors2).length > 0) {
  126. return errors2;
  127. }
  128. return null;
  129. };
  130. const mergeObjectsWithoutDuplicates = async (obj1, obj2) => {
  131. return Object.keys({ ...obj1, ...obj2 }).reduce((acc, key) => {
  132. if (!acc.hasOwnProperty(key)) {
  133. if (key == "education_name" || key == "learnStatus_name") {
  134. delete acc[key];
  135. } else {
  136. acc[key] = obj1.hasOwnProperty(key) ? obj1[key] : obj2[key];
  137. }
  138. }
  139. return acc;
  140. }, {});
  141. };
  142. const formSubmit = async (e) => {
  143. const data = await mergeObjectsWithoutDuplicates(e.detail.value, form.value);
  144. const errorsInfo = await validateObject(data);
  145. if (errorsInfo) {
  146. errors.value = errorsInfo;
  147. for (const key in errorsInfo) {
  148. if (errorsInfo.hasOwnProperty(key)) {
  149. console.error(`${key} 错误: ${errorsInfo[key]}`);
  150. }
  151. }
  152. } else {
  153. errors.value = {};
  154. let res;
  155. data.status = "0";
  156. if (user.value.role_type == "Teacher")
  157. res = await $api(`teacher/${form.value._id}`, "POST", data);
  158. else
  159. res = await $api(`student/${form.value._id}`, "POST", data);
  160. if (res.errcode == "0") {
  161. await search();
  162. if (user.value.role_type == "Student") {
  163. common_vendor.index.showToast({
  164. title: "保存成功",
  165. icon: "success"
  166. });
  167. } else {
  168. common_vendor.index.showToast({
  169. title: "保存成功,等待管理员审核方可展示!",
  170. icon: "none"
  171. });
  172. }
  173. }
  174. }
  175. };
  176. common_vendor.provide("form", form);
  177. common_vendor.provide("errors", errors);
  178. common_vendor.provide("deletePic", deletePic);
  179. common_vendor.provide("afterRead", afterRead);
  180. common_vendor.provide("formSubmit", formSubmit);
  181. common_vendor.provide("educationChange", educationChange);
  182. common_vendor.provide("learnStatusChange", learnStatusChange);
  183. common_vendor.provide("bindDateChange", bindDateChange);
  184. common_vendor.provide("showList", showList);
  185. common_vendor.provide("genderList", genderList);
  186. common_vendor.provide("learnStatusList", learnStatusList);
  187. common_vendor.provide("educationList", educationList);
  188. return (_ctx, _cache) => {
  189. return {
  190. a: common_vendor.unref(user).role_type == "Teacher",
  191. b: common_vendor.unref(user).role_type == "Student"
  192. };
  193. };
  194. }
  195. });
  196. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-e33b06dd"], ["__file", "D:/project/学吧/learn_applet/pagesMy/account/index.vue"]]);
  197. wx.createPage(MiniProgramPage);