index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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') form.value = res.data
  78. }
  79. };
  80. // 删除图片
  81. const deletePic = (event) => {
  82. form.value.icon = []
  83. };
  84. // 新增图片
  85. const afterRead = async (event) => {
  86. const url = event.file[0].url
  87. const result = await $apifile(`/web/learn_user/upload`, 'file', url, 'file');
  88. if (result.errcode === 0) form.value.icon = [result]
  89. };
  90. // 身份类型选择
  91. const cardChange = (e) => {
  92. const data = cardTypeList.value[e.detail.value]
  93. if (data) {
  94. form.value.cardType = data.value
  95. form.value.cardType_name = data.label
  96. }
  97. };
  98. // 学历类型选择
  99. const educationChange = (e) => {
  100. const data = educationList.value[e.detail.value]
  101. if (data) {
  102. form.value.education = data.value
  103. form.value.education_name = data.label
  104. }
  105. };
  106. // 学业状态类型选择
  107. const learnStatusChange = (e) => {
  108. const data = learnStatusList.value[e.detail.value]
  109. if (data) {
  110. form.value.learnStatus = data.value
  111. form.value.learnStatus_name = data.label
  112. }
  113. };
  114. // 年级类型选择
  115. const gradeChange = (e) => {
  116. const data = gradeList.value[e.detail.value]
  117. if (data) {
  118. form.value.grade = data.value
  119. form.value.grade_name = data.label
  120. }
  121. };
  122. // 学科类型选择
  123. const subjectChange = (e) => {
  124. const data = subjectList.value[e.detail.value]
  125. if (data) {
  126. form.value.subject = data.value
  127. form.value.subject_name = data.label
  128. }
  129. };
  130. // 自定义的验证函数
  131. const validateObject = (obj : any) => {
  132. const errors : any = {};
  133. // 检查name属性是否填写
  134. if (!obj.nick_name || obj.nick_name.trim() === '') {
  135. errors.nick_name = '请填写昵称!';
  136. }
  137. // 检查email属性是否填写
  138. if (!obj.age || obj.age.trim() === '') {
  139. errors.age = '请填写年龄!';
  140. }
  141. // 检查email属性是否填写
  142. if (!obj.gender || obj.gender.trim() === '') {
  143. errors.gender = '请填写性别!';
  144. }
  145. // 检查email属性是否填写
  146. if (!obj.card || obj.card.trim() === '') {
  147. errors.card = '请填写身份证号码!';
  148. }
  149. // 检查email属性是否填写
  150. if (!obj.phone || obj.phone.trim() === '') {
  151. errors.phone = '请填写手机号!';
  152. }
  153. // 如果有错误,返回错误对象
  154. if (Object.keys(errors).length > 0) {
  155. return errors;
  156. }
  157. // 如果没有错误,返回null或undefined
  158. return null;
  159. }
  160. // 保存
  161. const formSubmit = async (e) => {
  162. // 调用验证函数
  163. const errorsInfo = await validateObject(form.value);
  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. console.log('所有字段都已填写');
  175. console.log(form.value);
  176. }
  177. };
  178. // provide
  179. provide('form', form)
  180. provide('errors', errors)
  181. provide('deletePic', deletePic)
  182. provide('afterRead', afterRead)
  183. provide('formSubmit', formSubmit)
  184. provide('cardChange', cardChange)
  185. provide('educationChange', educationChange)
  186. provide('learnStatusChange', learnStatusChange)
  187. provide('gradeChange', gradeChange)
  188. provide('subjectChange', subjectChange)
  189. // 字典
  190. provide('showList', showList)
  191. provide('genderList', genderList)
  192. provide('gradeList', gradeList)
  193. provide('subjectList', subjectList)
  194. provide('learnStatusList', learnStatusList)
  195. provide('cardTypeList', cardTypeList)
  196. provide('educationList', educationList)
  197. </script>
  198. <style lang="scss" scoped>
  199. .content {
  200. display: flex;
  201. flex-direction: column;
  202. background-color: var(--f1Color);
  203. }
  204. </style>