index.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <view class="content">
  3. <view class="one" v-show="user.role_type=='Teacher'">
  4. <teacher></teacher>
  5. </view>
  6. <view class="two" v-show="user.role_type=='Student'">
  7. <student></student>
  8. </view>
  9. </view>
  10. </template>
  11. <script setup lang="ts">
  12. import teacher from "./path/teacher.vue"
  13. import student from "./path/student.vue"
  14. import { inject, provide, computed, ref } from 'vue';
  15. //该依赖已内置不需要单独安装
  16. import { onShow, onPullDownRefresh } from "@dcloudio/uni-app";
  17. // 请求接口
  18. const $api = inject('$api');
  19. const $config = inject('$config');
  20. const $apifile = inject('$apifile');
  21. // 基本信息
  22. const config = ref({ logo: [], file: [] });
  23. const form = ref({ icon: [] });
  24. const errors = ref({});
  25. // user
  26. const user = computed(() => {
  27. return uni.getStorageSync('user');
  28. })
  29. // 字典表
  30. const genderList = ref([])
  31. const educationList = ref([])
  32. const learnStatusList = ref([])
  33. const gradeList = ref([])
  34. const cardTypeList = ref([])
  35. const subjectList = ref([])
  36. const showList = ref([])
  37. onShow(async () => {
  38. await searchConfig();
  39. await searchOther();
  40. await search();
  41. })
  42. // config信息
  43. const searchConfig = async () => {
  44. config.value = uni.getStorageSync('config');
  45. };
  46. // 其他查询信息
  47. const searchOther = async () => {
  48. let res;
  49. // 性别
  50. res = await $api(`dictData`, 'GET', { code: 'gender', is_use: '0' });
  51. if (res.errcode === 0) genderList.value = res.data;
  52. // 学历
  53. res = await $api(`dictData`, 'GET', { code: 'education', is_use: '0' });
  54. if (res.errcode === 0) educationList.value = res.data;
  55. // 年级
  56. res = await $api(`dictData`, 'GET', { code: 'grade', is_use: '0' });
  57. if (res.errcode === 0) gradeList.value = res.data;
  58. // 学业状态
  59. res = await $api(`dictData`, 'GET', { code: 'learnStatus', is_use: '0' });
  60. if (res.errcode === 0) learnStatusList.value = res.data;
  61. // 身份证类型
  62. res = await $api(`dictData`, 'GET', { code: 'cardType', is_use: '0' });
  63. if (res.errcode === 0) cardTypeList.value = res.data;
  64. // 学科
  65. res = await $api(`dictData`, 'GET', { code: 'subject', is_use: '0' });
  66. if (res.errcode === 0) subjectList.value = res.data;
  67. // 是否公开
  68. res = await $api(`dictData`, 'GET', { code: 'show', is_use: '0' });
  69. if (res.errcode === 0) showList.value = res.data;
  70. };
  71. // 查询
  72. const search = async () => {
  73. if (user && user.value._id) {
  74. let res;
  75. if (user.value.role_type == 'Teacher') res = await $api(`teacher/${user.value._id}`, 'GET', {});
  76. else res = await $api(`student/${user.value._id}`, 'GET', {});
  77. if (res.errcode == '0') {
  78. if (res.data.cardType) {
  79. const data = cardTypeList.value.find(i => i.value === res.data.cardType)
  80. if (data) res.data.cardType_name = data.label
  81. }
  82. if (res.data.education) {
  83. const data = educationList.value.find(i => i.value === res.data.education)
  84. if (data) res.data.education_name = data.label
  85. }
  86. if (res.data.learnStatus) {
  87. const data = learnStatusList.value.find(i => i.value === res.data.learnStatus)
  88. if (data) res.data.learnStatus_name = data.label
  89. }
  90. if (res.data.grade) {
  91. const data = gradeList.value.find(i => i.value === res.data.grade)
  92. if (data) res.data.grade_name = data.label
  93. }
  94. if (res.data.subject) {
  95. const data = subjectList.value.find(i => i.value === res.data.subject)
  96. if (data) res.data.subject_name = data.label
  97. }
  98. form.value = res.data
  99. uni.setStorageSync('user', { ...res.data, role_type: user.value.role_type });
  100. }
  101. }
  102. };
  103. // 删除图片
  104. const deletePic = (event) => {
  105. form.value.icon = []
  106. };
  107. // 新增图片
  108. const afterRead = async (event) => {
  109. const url = event.file[0].url
  110. const result = await $apifile(`/web/learn_user/upload`, 'file', url, 'file');
  111. if (result.errcode === 0) form.value.icon = [result]
  112. };
  113. // 身份类型选择
  114. const cardChange = (e) => {
  115. const data = cardTypeList.value[e.detail.value]
  116. if (data) {
  117. form.value.cardType = data.value
  118. form.value.cardType_name = data.label
  119. }
  120. };
  121. // 学历类型选择
  122. const educationChange = (e) => {
  123. const data = educationList.value[e.detail.value]
  124. if (data) {
  125. form.value.education = data.value
  126. form.value.education_name = data.label
  127. }
  128. };
  129. // 学业状态类型选择
  130. const learnStatusChange = (e) => {
  131. const data = learnStatusList.value[e.detail.value]
  132. if (data) {
  133. form.value.learnStatus = data.value
  134. form.value.learnStatus_name = data.label
  135. }
  136. };
  137. // 年级类型选择
  138. const gradeChange = (e) => {
  139. const data = gradeList.value[e.detail.value]
  140. if (data) {
  141. form.value.grade = data.value
  142. form.value.grade_name = data.label
  143. }
  144. };
  145. // 学科类型选择
  146. const subjectChange = (e) => {
  147. const data = subjectList.value[e.detail.value]
  148. if (data) {
  149. form.value.subject = data.value
  150. form.value.subject_name = data.label
  151. }
  152. };
  153. // 自定义的验证函数
  154. const validateObject = (obj : any) => {
  155. const errors : any = {};
  156. // 检查name属性是否填写
  157. if (!obj.nick_name || obj.nick_name.trim() === '') {
  158. errors.nick_name = '请填写昵称!';
  159. }
  160. // 检查email属性是否填写
  161. if (!obj.age || obj.age.trim() === '') {
  162. errors.age = '请填写年龄!';
  163. }
  164. // 检查email属性是否填写
  165. if (!obj.gender || obj.gender.trim() === '') {
  166. errors.gender = '请填写性别!';
  167. }
  168. // 检查email属性是否填写
  169. if (!obj.card || obj.card.trim() === '') {
  170. errors.card = '请填写身份证号码!';
  171. }
  172. // 检查email属性是否填写
  173. if (!obj.phone || obj.phone.trim() === '') {
  174. errors.phone = '请填写手机号!';
  175. }
  176. // 检查email属性是否填写
  177. if (!obj.is_show || obj.is_show.trim() === '') {
  178. errors.is_show = '请选择是否公开!';
  179. }
  180. // 如果有错误,返回错误对象
  181. if (Object.keys(errors).length > 0) {
  182. return errors;
  183. }
  184. // 如果没有错误,返回null或undefined
  185. return null;
  186. }
  187. // 处理两个对象合并
  188. const mergeObjectsWithoutDuplicates = async (obj1, obj2) => {
  189. return Object.keys({ ...obj1, ...obj2 }).reduce((acc, key) => {
  190. if (!acc.hasOwnProperty(key)) {
  191. if (key == 'cardType_name' || key == 'education_name' || key == 'learnStatus_name' || key == 'grade_name' || key == 'subject_name') {
  192. delete acc[key]
  193. } else {
  194. // 如果累加器对象(acc)中不存在该键,则添加它
  195. acc[key] = obj1.hasOwnProperty(key) ? obj1[key] : obj2[key];
  196. }
  197. }
  198. return acc;
  199. }, {});
  200. }
  201. // 保存
  202. const formSubmit = async (e) => {
  203. // 调用验证函数
  204. const data = await mergeObjectsWithoutDuplicates(e.detail.value, form.value);
  205. const errorsInfo = await validateObject(data);
  206. // 检查是否有错误
  207. if (errorsInfo) {
  208. errors.value = errorsInfo
  209. // 遍历错误对象并显示错误信息
  210. for (const key in errorsInfo) {
  211. if (errorsInfo.hasOwnProperty(key)) {
  212. console.error(`${key} 错误: ${errorsInfo[key]}`);
  213. }
  214. }
  215. } else {
  216. errors.value = {}
  217. let res;
  218. if (user.value.role_type == 'Teacher') res = await $api(`teacher/${form.value._id}`, 'POST', data);
  219. else res = await $api(`student/${form.value._id}`, 'POST', data);
  220. if (res.errcode == '0') {
  221. await search();
  222. uni.showToast({
  223. title: '保存成功',
  224. icon: 'success'
  225. });
  226. }
  227. }
  228. };
  229. // provide
  230. provide('form', form)
  231. provide('errors', errors)
  232. provide('deletePic', deletePic)
  233. provide('afterRead', afterRead)
  234. provide('formSubmit', formSubmit)
  235. provide('cardChange', cardChange)
  236. provide('educationChange', educationChange)
  237. provide('learnStatusChange', learnStatusChange)
  238. provide('gradeChange', gradeChange)
  239. provide('subjectChange', subjectChange)
  240. // 字典
  241. provide('showList', showList)
  242. provide('genderList', genderList)
  243. provide('gradeList', gradeList)
  244. provide('subjectList', subjectList)
  245. provide('learnStatusList', learnStatusList)
  246. provide('cardTypeList', cardTypeList)
  247. provide('educationList', educationList)
  248. </script>
  249. <style lang="scss" scoped>
  250. .content {
  251. display: flex;
  252. flex-direction: column;
  253. background-color: var(--f1Color);
  254. }
  255. </style>