addReport.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <scroll-view class="scrollBox" scroll-y="true">
  3. <view class="content">
  4. <uni-forms
  5. ref="infoRef"
  6. v-model="formData"
  7. :rules="formRules"
  8. validateTrigger="bind"
  9. label-width="86px"
  10. labelAlign="right"
  11. >
  12. <uni-forms-item name="enterpriseId" label="实习企业" required>
  13. <uni-data-picker v-model="formData.enterpriseId" :localdata="companyList" :clear-icon="false" popup-title="请选择实习企业" :clear="false" :map="{ text: 'enterpriseName', value: 'id' }"></uni-data-picker>
  14. </uni-forms-item>
  15. <uni-forms-item label="实习报告" name="report" class="f-straight">
  16. <view style="display: flex;flex-wrap: wrap;">
  17. <view v-for="(img, index) of reportList" :key="index">
  18. <view class="upload-img" v-if="img">
  19. <uni-icons size="35" type="clear" color="#fe574a" style="position: absolute;right: -15px; top: -15px; z-index: 9" @click="delReport(index)"></uni-icons>
  20. <image :src="img" mode="contain" style="width: 200rpx;height: 200rpx;" @click="handlePreviewImage(index)"></image>
  21. </view>
  22. </view>
  23. <view v-if="reportList?.length < 9" class="upload-file" @click="uploadPhotos">
  24. <uni-icons type="plusempty" size="50" color="#f1f1f1"></uni-icons>
  25. </view>
  26. </view>
  27. </uni-forms-item>
  28. <view class="uploadTip text-center">*请上传实习报告图片(最多可上传9张图片)</view>
  29. <view class="uploadTip text-center">*只支持JPG、JPEG、PNG类型的图片</view>
  30. </uni-forms>
  31. </view>
  32. <view class="bottom-sticky">
  33. <button type="primary" size="default" class="recomm-button" @click="submit">确认</button>
  34. </view>
  35. </scroll-view>
  36. </template>
  37. <script setup>
  38. import { ref, unref } from 'vue'
  39. import { onLoad } from '@dcloudio/uni-app'
  40. import { uploadFile } from '@/api/file'
  41. import { saveStudentReport } from '@/api/student.js'
  42. const companyList = ref([])
  43. onLoad((options) => {
  44. if (options.companyList) {
  45. companyList.value = JSON.parse(options.companyList)
  46. }
  47. })
  48. const formData = ref({
  49. enterpriseId: null,
  50. })
  51. const formRules = {
  52. enterpriseId: {
  53. rules: [{required: true, errorMessage: '请选择实习企业' }]
  54. },
  55. }
  56. const infoRef = ref()
  57. const submit = async () => {
  58. const validate = await unref(infoRef).validate()
  59. if (!validate) return uni.showToast({ title: '请选择企业', icon: 'none' })
  60. if (!reportList.value?.length) return uni.showToast({ title: '请上传实习报告', icon: 'none' })
  61. try {
  62. await saveStudentReport({
  63. ...formData.value,
  64. urlList: reportList.value
  65. })
  66. uni.showToast({ title: '保存成功', icon: 'none' })
  67. setTimeout(() => {
  68. uni.navigateBack({
  69. delta: 1
  70. })
  71. }, 500)
  72. } catch (err) {
  73. uni.showToast({ title: err?.msg || '保存失败', icon: 'none' })
  74. }
  75. }
  76. const reportList = ref([])
  77. const delReport = (index) => {
  78. reportList.value.splice(index, 1)
  79. }
  80. // 图片预览
  81. const handlePreviewImage = (index) => {
  82. uni.previewImage({
  83. current: 0,
  84. urls: [reportList.value[index]]
  85. })
  86. }
  87. // 上传
  88. const uploadPhotos = () => {
  89. wx.chooseImage({
  90. count: 1,
  91. sizeType: ['original', 'compressed'],
  92. sourceType: ['album', 'camera'],
  93. success: function(res){
  94. const size = res.tempFiles[0]?.size || 0
  95. if (size >= 31457280) {
  96. uni.showToast({
  97. icon: 'none',
  98. title: '头像上传大小不得超过 20MB !',
  99. duration: 2000
  100. })
  101. return
  102. }
  103. const path = res.tempFilePaths[0]
  104. uploadFile(path, 'img').then(res => {
  105. formData.value.avatar = res.data
  106. reportList.value.push(res.data)
  107. }).catch(error => {
  108. uni.showToast({
  109. icon: 'error',
  110. title: '图片上传失败!',
  111. duration: 2000
  112. })
  113. })
  114. }
  115. })
  116. }
  117. </script>
  118. <style lang="scss" scoped>
  119. .img{
  120. width: 28%;
  121. height: 200rpx;
  122. margin: 10rpx;
  123. border: 1px solid #f4f4f4;
  124. }
  125. .wrapper{
  126. height: 84vh;
  127. overflow: auto;
  128. }
  129. :deep(.uni-section .uni-section-header__decoration) {
  130. background-color: #00B760 !important;
  131. }
  132. .line {
  133. border-top: 1px solid #ccc;
  134. }
  135. .picker {
  136. flex: 1;
  137. overflow: hidden;
  138. margin-right: 12px;
  139. }
  140. .content {
  141. padding: 20rpx;
  142. padding-bottom: 50rpx;
  143. .uploadTip {
  144. color: #00B760;
  145. margin-bottom: 20px;
  146. font-size: 13px;
  147. }
  148. }
  149. .upload-img{
  150. position: relative;
  151. width: 200rpx;
  152. height: 200rpx;
  153. border: 1px solid #f1f1f1;
  154. margin: 10rpx;
  155. }
  156. .upload-file{
  157. width: 200rpx;
  158. height: 200rpx;
  159. border: 1px solid #f1f1f1;
  160. margin: 10rpx;
  161. display: flex;
  162. justify-content: center;
  163. align-items: center;
  164. border-radius: 10rpx;
  165. }
  166. </style>