form.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. <view class="hc-flex pb-3 mb-5" un-border-b="1 solid gray-2" v-if="formData.type === 1">
  24. <template v-for="(item, index) in fileList" :key="index">
  25. <view class="hc-flex h-120 w-full b-rounded">
  26. <video class="w-full h-full b-rounded" :src="item"/>
  27. <view class="hc-tr bg-red-5 text-white text-center w-38 h-38 b-rounded-lb-1 b-rounded-tr-1 z-24" @click.stop="delFileClick(index)">
  28. <text class="relative cuIcon-close top--2 text-24"/>
  29. </view>
  30. </view>
  31. </template>
  32. <template v-if="fileList.length < 1">
  33. <view class="hc-flex-center h-120 w-full b-rounded" un-border="2 dashed gray-2" @click="addVideoFileClick">
  34. <text class="i-iconoir-plus text-70 text-gray-4"/>
  35. </view>
  36. </template>
  37. </view>
  38. <!-- 表单 -->
  39. <uni-forms ref="formRef" :rules="formRules" :modelValue="formData" :label-width="82">
  40. <uni-forms-item label="题名" required name="title">
  41. <uni-easyinput v-model="formData.title" placeholder="请输入题名" />
  42. </uni-forms-item>
  43. <uni-forms-item label="文字说明" required name="textContent">
  44. <uni-easyinput type="textarea" v-model="formData.textContent" placeholder="请输入文字说明" />
  45. </uni-forms-item>
  46. <uni-forms-item label="拍摄时间" required name="shootingTimeStr">
  47. <uni-datetime-picker type="date" v-model="formData.shootingTimeStr" :end="dateEnd"/>
  48. </uni-forms-item>
  49. <uni-forms-item label="上传日期" required name="uploadTime">
  50. <uni-datetime-picker type="date" v-model="formData.uploadTime" :end="dateEnd"/>
  51. </uni-forms-item>
  52. <uni-forms-item label="拍摄者" required name="shootingUser">
  53. <uni-easyinput v-model="formData.shootingUser" placeholder="请输入拍摄者" />
  54. </uni-forms-item>
  55. <template v-if="formData.type === 2">
  56. <uni-forms-item label="照片号">
  57. <uni-easyinput v-model="formData.photoCode" placeholder="请输入照片号" />
  58. </uni-forms-item>
  59. <uni-forms-item label="底片号">
  60. <uni-easyinput v-model="formData.filmCode" placeholder="请输入底片号" />
  61. </uni-forms-item>
  62. <uni-forms-item label="参见号">
  63. <uni-easyinput v-model="formData.seeAlsoCode" placeholder="请输入参见号" />
  64. </uni-forms-item>
  65. </template>
  66. </uni-forms>
  67. </view>
  68. <!-- 底部操作栏 -->
  69. <HcTabbarBlock :height="70"/>
  70. <hc-tabbars class="flex items-center">
  71. <template v-if="!dataId">
  72. <view class="flex-1 mr-2">
  73. <button hover-class="none" class="cu-btn block bg-orange text-white" @click="continueAdd">继续新增</button>
  74. </view>
  75. <view class="flex-1 ml-2">
  76. <button hover-class="none" class="cu-btn block bg-purple-8 text-white" @click="saveAdd">提交保存</button>
  77. </view>
  78. </template>
  79. <template v-else>
  80. <button hover-class="none" class="cu-btn flex-1 bg-purple-8 text-white" @click="saveClick">提交保存</button>
  81. </template>
  82. </hc-tabbars>
  83. </hc-sys>
  84. </template>
  85. <script setup>
  86. import {ref} from "vue";
  87. import mainApi from '~api/image/index';
  88. import {uploadApi} from '~api/upload';
  89. import {getObjValue, isString} from "js-fast-way";
  90. import {errorToast, formValidate, successToast} from "@/utils/tools";
  91. import {onLoad, onReady} from '@dcloudio/uni-app'
  92. import {chooseImage, chooseVideo} from "@/utils/utils";
  93. import {useAppStore} from "@/store";
  94. import dayjs from "dayjs";
  95. //初始变量
  96. const store = useAppStore()
  97. const userInfo = ref(store.userInfo);
  98. const projectId = ref(store.projectId);
  99. const contractId = ref(store.contractId);
  100. //基础变量
  101. const dataId = ref('');
  102. const fileList = ref([])
  103. const pdfList = ref([])
  104. const dateEnd = ref(dayjs().format("YYYY-MM-DD"));
  105. //页面启动
  106. onLoad((options) => {
  107. const {id, node} = getObjValue(options)
  108. dataId.value = id ?? '';
  109. if (id) {
  110. uni.setNavigationBarTitle({title: '编辑声像文件'})
  111. getInfoApi()
  112. } else {
  113. const nodes = node ? JSON.parse(decodeURIComponent(node)) : {};
  114. const {real_name} = userInfo.value
  115. formData.value = {
  116. projectId: projectId.value,
  117. contractId: contractId.value,
  118. classifyId: nodes.id,
  119. type: nodes.fileType,
  120. wbsId: nodes.treeId,
  121. uploadTime: dateEnd.value,
  122. shootingTimeStr: dateEnd.value,
  123. shootingUser: real_name
  124. }
  125. getFileTitleNamedata(nodes.treeId)
  126. uni.setNavigationBarTitle({title: '新增声像文件'})
  127. }
  128. })
  129. onReady(() => {
  130. // 设置自定义表单校验规则,必须在节点渲染完毕后执行
  131. formRef.value?.setRules(formRules)
  132. })
  133. //获取题名
  134. const getFileTitleNamedata = async (wbsId) => {
  135. const { data } = await mainApi.getFileTitleName({
  136. pKeyId: wbsId
  137. })
  138. formData.value.title = isString(data) ? data: ''
  139. }
  140. //获取详情
  141. const getInfoApi = async () => {
  142. formData.value = {}
  143. if (!dataId.value) {
  144. errorToast('参数异常,请退出重试')
  145. return false;
  146. }
  147. uni.showLoading({title: '获取数据中...', mask: true});
  148. const { data } = await mainApi.queryById({
  149. id: dataId.value
  150. })
  151. const res = getObjValue(data)
  152. formData.value = res
  153. const {imageUrl, pdfUrl} = res
  154. fileList.value = imageUrl.toString().split(',')
  155. pdfList.value = pdfUrl.toString().split(',')
  156. uni.hideLoading();
  157. }
  158. //添加文件
  159. const addFileClick = async () => {
  160. const tempFiles = await chooseImage(10 - fileList.value.length)
  161. if (tempFiles.length > 0) {
  162. uni.showLoading({title: '上传文件中...', mask: true});
  163. }
  164. for (let i = 0; i < tempFiles.length; i++) {
  165. await uploadFile(tempFiles[i].path);
  166. }
  167. uni.hideLoading();
  168. }
  169. //选择视频文件
  170. const addVideoFileClick = async () => {
  171. const {tempFilePath} = await chooseVideo()
  172. if (tempFilePath) {
  173. uni.showLoading({title: '上传文件中...', mask: true});
  174. await uploadFile(tempFilePath);
  175. uni.hideLoading();
  176. }
  177. }
  178. //上传文件
  179. const uploadFile = async (file) => {
  180. const {error, msg, data} = await uploadApi(file)
  181. if (!error) {
  182. const { link, pdfUrl } = getObjValue(data)
  183. fileList.value.push(link)
  184. if (pdfUrl) {
  185. pdfList.value.push(pdfUrl)
  186. }
  187. } else {
  188. errorToast(msg)
  189. }
  190. }
  191. //删除文件
  192. const delFileClick = (index) => {
  193. fileList.value.splice(index, 1)
  194. if (pdfList.value.length > 0) {
  195. pdfList.value.splice(index, 1)
  196. }
  197. }
  198. //预览图片
  199. const previewImg = (index) => {
  200. uni.previewImage({
  201. current: index,
  202. urls: fileList.value,
  203. });
  204. }
  205. //表单验证
  206. const formRef = ref(null)
  207. const formData = ref({})
  208. const formRules = {
  209. title: {
  210. rules: [{
  211. required: true,
  212. errorMessage: '请输入题名'
  213. }]
  214. },
  215. textContent: {
  216. rules: [{
  217. required: true,
  218. errorMessage: '请输入文字说明'
  219. }]
  220. },
  221. shootingTimeStr: {
  222. rules: [{
  223. required: true,
  224. errorMessage: '请选择拍摄时间'
  225. }]
  226. },
  227. uploadTime: {
  228. rules: [{
  229. required: true,
  230. errorMessage: '请选择上传日期'
  231. }]
  232. },
  233. shootingUser: {
  234. rules: [{
  235. required: true,
  236. errorMessage: '请输入拍摄者'
  237. }]
  238. },
  239. }
  240. const isFormValidate = async () => {
  241. if (fileList.value.length <= 0) {
  242. errorToast('请先上传文件')
  243. return false
  244. }
  245. const isForm = await formValidate(formRef.value)
  246. if (!isForm) {
  247. errorToast('请先完善表单')
  248. return false
  249. }
  250. return true
  251. }
  252. //提交保存
  253. const saveClick = async () => {
  254. const isForm = await isFormValidate()
  255. if (!isForm) return false;
  256. //更新数据
  257. if (dataId.value) {
  258. await updateImageData()
  259. } else {
  260. await addImageData()
  261. }
  262. }
  263. //更新数据
  264. const updateImageData = async () => {
  265. uni.showLoading({title: '保存数据中...', mask: true});
  266. const {error, code, msg} = await mainApi.updateImageclassifyFile({
  267. ...formData.value,
  268. imageUrl: fileList.value.join(','),
  269. pdfUrl: pdfList.value.join(','),
  270. })
  271. uni.hideLoading();
  272. if (!error && code === 200) {
  273. successToast('保存成功')
  274. } else {
  275. errorToast('保存失败: ' + msg)
  276. }
  277. }
  278. //新增数据
  279. const addImageData = async () => {
  280. uni.showLoading({title: '新增数据中...', mask: true});
  281. const {error, code, msg} = await mainApi.addImageclassifyFile({
  282. ...formData.value,
  283. imageUrl: fileList.value.join(','),
  284. pdfUrl: pdfList.value.join(','),
  285. })
  286. uni.hideLoading();
  287. if (!error && code === 200) {
  288. successToast('新增成功')
  289. return true
  290. } else {
  291. errorToast('新增失败: ' + msg)
  292. return false
  293. }
  294. }
  295. //继续新增
  296. const continueAdd = async () => {
  297. const isForm = await isFormValidate()
  298. if (!isForm) return false;
  299. const res = await addImageData()
  300. if(!res) return false;
  301. //清空表单
  302. const {real_name} = userInfo.value
  303. const { wbsId } = formData.value;
  304. formData.value.uploadTime = dateEnd.value
  305. formData.value.shootingTimeStr = dateEnd.value
  306. formData.value.shootingUser = real_name
  307. formData.value.title = ''
  308. formData.value.textContent = ''
  309. formData.value.photoCode = ''
  310. formData.value.filmCode = ''
  311. formData.value.seeAlsoCode = ''
  312. fileList.value = []
  313. pdfList.value = []
  314. getFileTitleNamedata(wbsId).then()
  315. }
  316. //新增保存
  317. const saveAdd = async () => {
  318. const isForm = await isFormValidate()
  319. if (!isForm) return false;
  320. const res = await addImageData()
  321. if(!res) return false;
  322. //返回上一页
  323. setTimeout(() => {
  324. uni.navigateBack()
  325. }, 1500)
  326. }
  327. </script>