index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 { onLoad } from "@dcloudio/uni-app";
  17. // 请求接口
  18. const $api = inject('$api');
  19. const $apifile = inject('$apifile');
  20. // 基本信息
  21. const config = ref({ logo: [], file: [] });
  22. const form = ref({ icon: [], card: [] });
  23. const errors = ref({});
  24. // user
  25. const user = computed(() => {
  26. return uni.getStorageSync('user');
  27. })
  28. // 字典表
  29. const genderList = ref([])
  30. const educationList = ref([])
  31. const learnStatusList = ref([])
  32. const showList = ref([])
  33. onLoad(async () => {
  34. await searchConfig();
  35. await searchOther();
  36. await search();
  37. })
  38. // config信息
  39. const searchConfig = async () => {
  40. config.value = uni.getStorageSync('config');
  41. };
  42. // 其他查询信息
  43. const searchOther = async () => {
  44. let res;
  45. // 性别
  46. res = await $api(`dictData`, 'GET', { code: 'gender', is_use: '0' });
  47. if (res.errcode === 0) genderList.value = res.data;
  48. // 学历
  49. res = await $api(`dictData`, 'GET', { code: 'education', is_use: '0' });
  50. if (res.errcode === 0) educationList.value = res.data;
  51. // 学业状态
  52. res = await $api(`dictData`, 'GET', { code: 'learnStatus', is_use: '0' });
  53. if (res.errcode === 0) learnStatusList.value = res.data;
  54. // 是否公开
  55. res = await $api(`dictData`, 'GET', { code: 'show', is_use: '0' });
  56. if (res.errcode === 0) showList.value = res.data;
  57. };
  58. // 查询
  59. const search = async () => {
  60. if (user && user.value._id) {
  61. let res;
  62. if (user.value.role_type == 'Teacher') res = await $api(`teacher/${user.value._id}`, 'GET', {});
  63. else res = await $api(`student/${user.value._id}`, 'GET', {});
  64. if (res.errcode == '0') {
  65. if (res.data.education) {
  66. const data = educationList.value.find(i => i.value === res.data.education)
  67. if (data) res.data.education_name = data.label
  68. }
  69. if (res.data.learnStatus) {
  70. const data = learnStatusList.value.find(i => i.value === res.data.learnStatus)
  71. if (data) res.data.learnStatus_name = data.label
  72. }
  73. form.value = res.data
  74. uni.setStorageSync('user', { ...res.data, role_type: user.value.role_type });
  75. }
  76. }
  77. };
  78. // 删除图片
  79. const deletePic = (event) => {
  80. const file = form.value[event.name].filter(i => i.url !== event.file.url)
  81. form.value[event.name] = file
  82. };
  83. // 新增图片
  84. const afterRead = async (event) => {
  85. const url = event.file[0].url
  86. const result = await $apifile(`/web/learn_user/upload`, 'file', url, 'file');
  87. if (result.errcode === 0) form.value[event.name] = [...form.value[event.name], ...[result]]
  88. };
  89. // 学历类型选择
  90. const educationChange = (e) => {
  91. const data = educationList.value[e.detail.value]
  92. if (data) {
  93. form.value.education = data.value
  94. form.value.education_name = data.label
  95. }
  96. };
  97. // 学业状态类型选择
  98. const learnStatusChange = (e) => {
  99. const data = learnStatusList.value[e.detail.value]
  100. if (data) {
  101. form.value.learnStatus = data.value
  102. form.value.learnStatus_name = data.label
  103. }
  104. };
  105. // 出生日期选择
  106. const bindDateChange = (e) => {
  107. form.value.birth = e.detail.value
  108. };
  109. // 自定义的验证函数
  110. const validateObject = (obj : any) => {
  111. const errors : any = {};
  112. if (!obj.nick_name || obj.nick_name.trim() === '') {
  113. errors.nick_name = '请填写昵称!';
  114. }
  115. if (!obj.birth || obj.birth.trim() === '') {
  116. errors.birth = '请选择出生日期!';
  117. }
  118. if (!obj.phone || obj.phone.trim() === '') {
  119. errors.phone = '请填写手机号!';
  120. } else {
  121. const regex = /^1[3456789]\d{9}$/;
  122. if (!regex.test(obj.phone)) errors.phone = '请填写正确的手机号码!';
  123. }
  124. if (user.value.role_type == 'Teacher') {
  125. if (!obj.is_show || obj.is_show.trim() === '') {
  126. errors.is_show = '请选择是否公开!';
  127. }
  128. if (!obj.education || obj.education.trim() === '') {
  129. errors.education = '请选择学历!';
  130. }
  131. if (!obj.college || obj.college.trim() === '') {
  132. errors.college = '请输入院校!';
  133. }
  134. if (!obj.card || !obj.card.some(value => value !== undefined)) {
  135. errors.card = '请上传身份证照片!';
  136. }
  137. }
  138. // 如果有错误,返回错误对象
  139. if (Object.keys(errors).length > 0) {
  140. return errors;
  141. }
  142. // 如果没有错误,返回null或undefined
  143. return null;
  144. }
  145. // 处理两个对象合并
  146. const mergeObjectsWithoutDuplicates = async (obj1, obj2) => {
  147. return Object.keys({ ...obj1, ...obj2 }).reduce((acc, key) => {
  148. if (!acc.hasOwnProperty(key)) {
  149. if (key == 'education_name' || key == 'learnStatus_name') {
  150. delete acc[key]
  151. } else {
  152. // 如果累加器对象(acc)中不存在该键,则添加它
  153. acc[key] = obj1.hasOwnProperty(key) ? obj1[key] : obj2[key];
  154. }
  155. }
  156. return acc;
  157. }, {});
  158. }
  159. // 保存
  160. const formSubmit = async (e) => {
  161. // 调用验证函数
  162. const data = await mergeObjectsWithoutDuplicates(e.detail.value, form.value);
  163. const errorsInfo = await validateObject(data);
  164. // 检查是否有错误
  165. if (errorsInfo) {
  166. errors.value = errorsInfo
  167. // 遍历错误对象并显示错误信息
  168. for (const key in errorsInfo) {
  169. if (errorsInfo.hasOwnProperty(key)) {
  170. console.error(`${key} 错误: ${errorsInfo[key]}`);
  171. }
  172. }
  173. } else {
  174. errors.value = {}
  175. let res;
  176. data.status = '0'
  177. if (user.value.role_type == 'Teacher') res = await $api(`teacher/${form.value._id}`, 'POST', data);
  178. else res = await $api(`student/${form.value._id}`, 'POST', data);
  179. if (res.errcode == '0') {
  180. await search();
  181. if (user.value.role_type == 'Student') {
  182. uni.showToast({
  183. title: '保存成功',
  184. icon: 'success'
  185. });
  186. }
  187. else {
  188. uni.showToast({
  189. title: '保存成功,等待管理员审核方可展示!',
  190. icon: 'none'
  191. });
  192. }
  193. }
  194. }
  195. };
  196. // provide
  197. provide('form', form)
  198. provide('errors', errors)
  199. provide('deletePic', deletePic)
  200. provide('afterRead', afterRead)
  201. provide('formSubmit', formSubmit)
  202. provide('educationChange', educationChange)
  203. provide('learnStatusChange', learnStatusChange)
  204. provide('bindDateChange', bindDateChange)
  205. // 字典
  206. provide('showList', showList)
  207. provide('genderList', genderList)
  208. provide('learnStatusList', learnStatusList)
  209. provide('educationList', educationList)
  210. </script>
  211. <style lang="scss" scoped>
  212. .content {
  213. display: flex;
  214. flex-direction: column;
  215. min-height: 100vh;
  216. background-color: var(--f1Color);
  217. }
  218. </style>