猫妈狗爸伴宠师小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.4 KiB

  1. <template>
  2. <view class="sort-answer">
  3. <uni-forms ref="baseForm" :modelValue="formData" label-position="top" label-width="auto">
  4. <uni-forms-item
  5. v-for="(item,index) in formData.answerList"
  6. :name="`answerList[${index}].answer`"
  7. :rules="[{
  8. required: true,
  9. errorMessage: item.question.includes('请上传')?'请上传照片':'请输入考试答题' ,
  10. }]"
  11. :label="item.order+'、'+item.question" required>
  12. <up-textarea v-if="!item.question.includes('请上传')" v-model="item.answer" :maxlength="1000"
  13. placeholder="请输入内容,600百字以内" :height="200"
  14. count></up-textarea>
  15. <uni-file-picker v-else v-model="item.answer" @select="(e)=>successUpload(e,item)" limit="6"
  16. title="最多选择6张图片"
  17. :image-styles="imageStyles"></uni-file-picker>
  18. </uni-forms-item>
  19. </uni-forms>
  20. </view>
  21. <submitBut text="提交" @click="handleClick"></submitBut>
  22. </template>
  23. <script setup>
  24. import submitBut from "../../../components/submitBut/index.vue"
  25. import {getTrainQuestion, submitTrainCheckAnswer} from "../../../api/work/work";
  26. import {ref} from "vue"
  27. import tab from "../../../plugins/tab";
  28. import {getStorage} from "../../../utils/auth";
  29. const formData = ref({
  30. answerList: [],
  31. staffId: getStorage("userInfo").id
  32. })
  33. const imageStyles = ref({
  34. width: 100,
  35. height: 100,
  36. })
  37. const baseForm = ref()
  38. const value = ref()
  39. const getTrainQuestions = async () => {
  40. const {data} = await getTrainQuestion()
  41. formData.value.answerList = data || []
  42. }
  43. getTrainQuestions()
  44. const handleClick = async () => {
  45. baseForm.value.validate((valida, err) => {
  46. console.log(!valida);
  47. })
  48. await submitTrainCheckAnswer(formData.value)
  49. tab.navigateTo("/otherPages/workbenchManage/queryResults/index")
  50. }
  51. // 新增图片
  52. const successUpload = async (event, item) => {
  53. console.log(event);
  54. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  55. let lists = [].concat(event.tempFiles)
  56. let fileList = item.answer || []
  57. let fileListLen = fileList.length
  58. lists.map((item) => {
  59. fileList.push({
  60. ...item
  61. })
  62. })
  63. for (let i = 0; i < lists.length; i++) {
  64. const result = await uploadFilePromise(lists[i].url)
  65. let item = fileList[fileListLen]
  66. fileList.splice(fileListLen, 1, Object.assign(item, {
  67. status: 'success',
  68. message: '',
  69. url: result
  70. }))
  71. fileListLen++
  72. }
  73. item.answer = fileList
  74. }
  75. const uploadFilePromise = (url) => {
  76. return new Promise((resolve, reject) => {
  77. uni.uploadFile({
  78. url: 'https://store-test.catmdogd.com/test-api/h5/oss/upload', // 仅为示例,非真实的接口地址
  79. filePath: url,
  80. name: 'file',
  81. formData: {
  82. user: 'test'
  83. },
  84. success: (res) => {
  85. setTimeout(() => {
  86. if (res && res.data) {
  87. let resData = JSON.parse(res.data);
  88. resolve(resData.url);
  89. }
  90. reject("上传失败");
  91. }, 1000)
  92. }
  93. });
  94. })
  95. }
  96. </script>
  97. <style scoped lang="scss">
  98. .sort-answer {
  99. padding: 40rpx 40rpx 140rpx;
  100. background: #FFFFFF;
  101. &-item {
  102. margin-bottom: 30rpx;
  103. }
  104. &-question {
  105. display: flex;
  106. margin-bottom: 20rpx;
  107. line-height: 50rpx;
  108. }
  109. :deep(.uni-forms-item__label) {
  110. height: auto !important;
  111. align-items: start !important;
  112. }
  113. }
  114. </style>