form.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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, node} = getObjValue(options)
  86. dataId.value = id ?? '';
  87. if (id) {
  88. uni.setNavigationBarTitle({title: '编辑声像文件'})
  89. getInfoApi()
  90. } else {
  91. const nodes = node ? JSON.parse(decodeURIComponent(node)) : {};
  92. console.log(nodes)
  93. uni.setNavigationBarTitle({title: '新增声像文件'})
  94. }
  95. })
  96. onReady(() => {
  97. // 设置自定义表单校验规则,必须在节点渲染完毕后执行
  98. formRef.value?.setRules(formRules)
  99. })
  100. //获取详情
  101. const getInfoApi = async () => {
  102. formData.value = {}
  103. if (!dataId.value) {
  104. errorToast('参数异常,请退出重试')
  105. return false;
  106. }
  107. uni.showLoading({title: '获取数据中...', mask: true});
  108. const { data } = await mainApi.queryById({
  109. id: dataId.value
  110. })
  111. const res = getObjValue(data)
  112. formData.value = res
  113. console.log(res)
  114. const {imageUrl, pdfUrl} = res
  115. fileList.value = imageUrl.toString().split(',')
  116. pdfList.value = pdfUrl.toString().split(',')
  117. uni.hideLoading();
  118. }
  119. //添加文件
  120. const addFileClick = async () => {
  121. const tempFiles = await chooseImage(10 - fileList.value.length)
  122. if (tempFiles.length > 0) {
  123. uni.showLoading({title: '上传文件中...', mask: true});
  124. }
  125. for (let i = 0; i < tempFiles.length; i++) {
  126. await uploadFile(tempFiles[i]);
  127. }
  128. uni.hideLoading();
  129. }
  130. //上传文件
  131. const uploadFile = async (item) => {
  132. const {error, msg, data} = await uploadApi(item.path || item.tempFilePath)
  133. if (!error) {
  134. const { link, pdfUrl } = getObjValue(data)
  135. fileList.value.push(link)
  136. pdfList.value.push(pdfUrl)
  137. } else {
  138. errorToast(msg)
  139. }
  140. }
  141. //删除文件
  142. const delFileClick = (index) => {
  143. fileList.value.splice(index, 1)
  144. pdfList.value.splice(index, 1)
  145. }
  146. //预览图片
  147. const previewImg = (index) => {
  148. uni.previewImage({
  149. current: index,
  150. urls: fileList.value,
  151. });
  152. }
  153. //表单验证
  154. const formRef = ref(null)
  155. const formData = ref({})
  156. const formRules = {
  157. title: {
  158. rules: [{
  159. required: true,
  160. errorMessage: '请输入题名'
  161. }]
  162. },
  163. textContent: {
  164. rules: [{
  165. required: true,
  166. errorMessage: '请输入文字说明'
  167. }]
  168. },
  169. shootingTimeStr: {
  170. rules: [{
  171. required: true,
  172. errorMessage: '请选择拍摄时间'
  173. }]
  174. },
  175. uploadTime: {
  176. rules: [{
  177. required: true,
  178. errorMessage: '请选择上传日期'
  179. }]
  180. },
  181. shootingUser: {
  182. rules: [{
  183. required: true,
  184. errorMessage: '请输入拍摄者'
  185. }]
  186. },
  187. }
  188. //提交保存
  189. const saveClick = async () => {
  190. if (fileList.value.length <= 0) {
  191. errorToast('请先上传文件')
  192. return false
  193. }
  194. const isForm = formValidate(formRef.value)
  195. if (!isForm) {
  196. errorToast('请先完善表单')
  197. return false
  198. }
  199. //更新数据
  200. if (dataId.value) {
  201. await updateImageData()
  202. } else {
  203. await addImageData()
  204. }
  205. }
  206. //更新数据
  207. const updateImageData = async () => {
  208. uni.showLoading({title: '保存数据中...', mask: true});
  209. const {error, code, msg} = await mainApi.updateImageclassifyFile({
  210. ...formData.value,
  211. imageUrl: fileList.value.join(','),
  212. pdfUrl: pdfList.value.join(','),
  213. })
  214. uni.hideLoading();
  215. if (!error && code === 200) {
  216. successToast('保存成功')
  217. } else {
  218. errorToast('保存失败: ' + msg)
  219. }
  220. }
  221. //新增数据
  222. const addImageData = async () => {
  223. uni.showLoading({title: '新增数据中...', mask: true});
  224. const {error, code, msg} = await mainApi.addImageclassifyFile({
  225. ...formData.value,
  226. imageUrl: fileList.value.join(','),
  227. pdfUrl: pdfList.value.join(','),
  228. })
  229. uni.hideLoading();
  230. if (!error && code === 200) {
  231. successToast('新增成功')
  232. } else {
  233. errorToast('新增失败: ' + msg)
  234. }
  235. }
  236. </script>