form.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <hc-sys :isNavBar="false">
  3. <uni-notice-bar text="请上传MP4、MOV格式的视频文件,文件大小不超过500M,只能上传1个视频文件" v-if="formData.type === 1"/>
  4. <uni-notice-bar text="请上传JPG、JPEG、PNG格式的图片文件,大小不超过30M,最多10张图片文件" v-if="formData.type === 2"/>
  5. <view class="relative bg-white mt-1 hc-p">
  6. <!-- 文件上传 -->
  7. <view class="hc-flex flex-wrap pb-3 mb-5" un-border-b="1 solid gray-2" v-if="formData.type === 2">
  8. <template v-for="(item, index) in fileList" :key="index">
  9. <view class="hc-flex h-120 w-120 b-rounded mr-2 mt-2" @click="previewImg(index)">
  10. <hc-img :src="item" width="60" height="60" class="b-rounded"/>
  11. <view class="hc-tr bg-red-5 text-white text-center w-38 h-38 b-rounded-lb-1 b-rounded-tr-1" @click.stop="delFileClick(index)">
  12. <text class="relative cuIcon-close top--2 text-24"/>
  13. </view>
  14. </view>
  15. </template>
  16. <template v-if="fileList.length < 10">
  17. <view class="hc-flex-center h-120 w-120 b-rounded mt-2" un-border="2 dashed gray-2" @click="addFileClick">
  18. <text class="i-iconoir-plus text-70 text-gray-4"/>
  19. </view>
  20. </template>
  21. </view>
  22. <!-- 表单 -->
  23. <uni-forms ref="formRef" :rules="formRules" :modelValue="formData" :label-width="82">
  24. <uni-forms-item label="题名" required name="title">
  25. <uni-easyinput v-model="formData.title" placeholder="请输入题名" />
  26. </uni-forms-item>
  27. <uni-forms-item label="文字说明" required name="textContent">
  28. <uni-easyinput type="textarea" v-model="formData.textContent" placeholder="请输入文字说明" />
  29. </uni-forms-item>
  30. <uni-forms-item label="拍摄时间" required name="shootingTimeStr">
  31. <uni-datetime-picker type="date" v-model="formData.shootingTimeStr" :end="dateEnd"/>
  32. </uni-forms-item>
  33. <uni-forms-item label="上传日期" required name="uploadTime">
  34. <uni-datetime-picker type="date" v-model="formData.uploadTime" :end="dateEnd"/>
  35. </uni-forms-item>
  36. <uni-forms-item label="拍摄者" required name="shootingUser">
  37. <uni-easyinput v-model="formData.shootingUser" placeholder="请输入拍摄者" />
  38. </uni-forms-item>
  39. <template v-if="formData.type === 2">
  40. <uni-forms-item label="照片号">
  41. <uni-easyinput v-model="formData.photoCode" placeholder="请输入照片号" />
  42. </uni-forms-item>
  43. <uni-forms-item label="底片号">
  44. <uni-easyinput v-model="formData.filmCode" placeholder="请输入底片号" />
  45. </uni-forms-item>
  46. <uni-forms-item label="参见号">
  47. <uni-easyinput v-model="formData.seeAlsoCode" placeholder="请输入参见号" />
  48. </uni-forms-item>
  49. </template>
  50. </uni-forms>
  51. </view>
  52. <!-- 底部操作栏 -->
  53. <HcTabbarBlock :height="70"/>
  54. <hc-tabbars class="flex items-center">
  55. <template v-if="!dataId">
  56. <view class="flex-1 mr-2">
  57. <button hover-class="none" class="cu-btn block bg-orange text-white">继续新增</button>
  58. </view>
  59. <view class="flex-1 ml-2">
  60. <button hover-class="none" class="cu-btn block bg-purple-8 text-white">提交保存</button>
  61. </view>
  62. </template>
  63. <template v-else>
  64. <button hover-class="none" class="cu-btn flex-1 bg-purple-8 text-white" @click="saveClick">提交保存</button>
  65. </template>
  66. </hc-tabbars>
  67. </hc-sys>
  68. </template>
  69. <script setup>
  70. import {ref} from "vue";
  71. import mainApi from '~api/image/index';
  72. import {uploadApi} from '~api/upload';
  73. import {getObjValue} from "js-fast-way";
  74. import {errorToast, formValidate, successToast} from "@/utils/tools";
  75. import {onLoad, onReady} from '@dcloudio/uni-app'
  76. import {chooseImage} from "@/utils/utils";
  77. import dayjs from "dayjs";
  78. //初始变量
  79. const dataId = ref('');
  80. const fileList = ref([])
  81. const pdfList = ref([])
  82. const dateEnd = ref(dayjs().format("YYYY-MM-DD"));
  83. //页面启动
  84. onLoad((options) => {
  85. const {id} = getObjValue(options)
  86. dataId.value = id ?? '';
  87. if (id) {
  88. uni.setNavigationBarTitle({title: '编辑声像文件'})
  89. getInfoApi()
  90. } else {
  91. uni.setNavigationBarTitle({title: '新增声像文件'})
  92. }
  93. })
  94. onReady(() => {
  95. // 设置自定义表单校验规则,必须在节点渲染完毕后执行
  96. formRef.value?.setRules(formRules)
  97. })
  98. //获取详情
  99. const getInfoApi = async () => {
  100. formData.value = {}
  101. if (!dataId.value) {
  102. errorToast('参数异常,请退出重试')
  103. return false;
  104. }
  105. uni.showLoading({title: '获取数据中...', mask: true});
  106. const { data } = await mainApi.queryById({
  107. id: dataId.value
  108. })
  109. const res = getObjValue(data)
  110. formData.value = res
  111. console.log(res)
  112. const {imageUrl, pdfUrl} = res
  113. fileList.value = imageUrl.toString().split(',')
  114. pdfList.value = pdfUrl.toString().split(',')
  115. uni.hideLoading();
  116. }
  117. //添加文件
  118. const addFileClick = async () => {
  119. const tempFiles = await chooseImage(10 - fileList.value.length)
  120. if (tempFiles.length > 0) {
  121. uni.showLoading({title: '上传文件中...', mask: true});
  122. }
  123. for (let i = 0; i < tempFiles.length; i++) {
  124. await uploadFile(tempFiles[i]);
  125. }
  126. uni.hideLoading();
  127. }
  128. //上传文件
  129. const uploadFile = async (item) => {
  130. const {error, msg, data} = await uploadApi(item.path || item.tempFilePath)
  131. if (!error) {
  132. const { link, pdfUrl } = getObjValue(data)
  133. fileList.value.push(link)
  134. pdfList.value.push(pdfUrl)
  135. } else {
  136. errorToast(msg)
  137. }
  138. }
  139. //删除文件
  140. const delFileClick = (index) => {
  141. fileList.value.splice(index, 1)
  142. pdfList.value.splice(index, 1)
  143. }
  144. //预览图片
  145. const previewImg = (index) => {
  146. uni.previewImage({
  147. current: index,
  148. urls: fileList.value,
  149. });
  150. }
  151. //表单验证
  152. const formRef = ref(null)
  153. const formData = ref({})
  154. const formRules = {
  155. title: {
  156. rules: [{
  157. required: true,
  158. errorMessage: '请输入题名'
  159. }]
  160. },
  161. textContent: {
  162. rules: [{
  163. required: true,
  164. errorMessage: '请输入文字说明'
  165. }]
  166. },
  167. shootingTimeStr: {
  168. rules: [{
  169. required: true,
  170. errorMessage: '请选择拍摄时间'
  171. }]
  172. },
  173. uploadTime: {
  174. rules: [{
  175. required: true,
  176. errorMessage: '请选择上传日期'
  177. }]
  178. },
  179. shootingUser: {
  180. rules: [{
  181. required: true,
  182. errorMessage: '请输入拍摄者'
  183. }]
  184. },
  185. }
  186. //提交保存
  187. const saveClick = async () => {
  188. if (fileList.value.length <= 0) {
  189. errorToast('请先上传文件')
  190. return false
  191. }
  192. const isForm = formValidate(formRef.value)
  193. if (!isForm) {
  194. errorToast('请先完善表单')
  195. return false
  196. }
  197. //更新数据
  198. if (dataId.value) {
  199. await updateImageData()
  200. } else {
  201. await addImageData()
  202. }
  203. }
  204. //更新数据
  205. const updateImageData = async () => {
  206. uni.showLoading({title: '保存数据中...', mask: true});
  207. const {error, code, msg} = await mainApi.updateImageclassifyFile({
  208. ...formData.value,
  209. imageUrl: fileList.value.join(','),
  210. pdfUrl: pdfList.value.join(','),
  211. })
  212. uni.hideLoading();
  213. if (!error && code === 200) {
  214. successToast('保存成功')
  215. } else {
  216. errorToast('保存失败: ' + msg)
  217. }
  218. }
  219. //新增数据
  220. const addImageData = async () => {
  221. uni.showLoading({title: '新增数据中...', mask: true});
  222. const {error, code, msg} = await mainApi.addImageclassifyFile({
  223. ...formData.value,
  224. imageUrl: fileList.value.join(','),
  225. pdfUrl: pdfList.value.join(','),
  226. })
  227. uni.hideLoading();
  228. if (!error && code === 200) {
  229. successToast('新增成功')
  230. } else {
  231. errorToast('新增失败: ' + msg)
  232. }
  233. }
  234. </script>