form.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 {onLoad, onReady} from '@dcloudio/uni-app'
  88. import mainApi from '~api/image/index';
  89. import {uploadApi} from '~api/upload';
  90. import {getObjValue, isString} from "js-fast-way";
  91. import {errorToast, formValidate, successToast} from "@/utils/tools";
  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. console.log(nodes.type)
  126. if (nodes.type === 1) {
  127. getFileTitleNamedata(nodes.treeId)
  128. }
  129. uni.setNavigationBarTitle({title: '新增声像文件'})
  130. }
  131. })
  132. onReady(() => {
  133. // 设置自定义表单校验规则,必须在节点渲染完毕后执行
  134. formRef.value?.setRules(formRules)
  135. })
  136. //获取题名
  137. const getFileTitleNamedata = async (wbsId) => {
  138. const { data } = await mainApi.getFileTitleName({
  139. pKeyId: wbsId
  140. })
  141. formData.value.title = isString(data) ? data: ''
  142. }
  143. //获取详情
  144. const getInfoApi = async () => {
  145. formData.value = {}
  146. if (!dataId.value) {
  147. errorToast('参数异常,请退出重试')
  148. return false;
  149. }
  150. uni.showLoading({title: '获取数据中...', mask: true});
  151. const { data } = await mainApi.queryById({
  152. id: dataId.value
  153. })
  154. const res = getObjValue(data)
  155. formData.value = res
  156. const {imageUrl, pdfUrl} = res
  157. fileList.value = imageUrl.toString().split(',')
  158. pdfList.value = pdfUrl.toString().split(',')
  159. uni.hideLoading();
  160. }
  161. //添加文件
  162. const addFileClick = async () => {
  163. const tempFiles = await chooseImage(10 - fileList.value.length)
  164. if (tempFiles.length > 0) {
  165. uni.showLoading({title: '上传文件中...', mask: true});
  166. }
  167. for (let i = 0; i < tempFiles.length; i++) {
  168. await uploadFile(tempFiles[i].path);
  169. }
  170. uni.hideLoading();
  171. }
  172. //选择视频文件
  173. const addVideoFileClick = async () => {
  174. const {tempFilePath} = await chooseVideo()
  175. if (tempFilePath) {
  176. uni.showLoading({title: '上传文件中...', mask: true});
  177. await uploadFile(tempFilePath);
  178. uni.hideLoading();
  179. }
  180. }
  181. //上传文件
  182. const uploadFile = async (file) => {
  183. const {error, msg, data} = await uploadApi(file)
  184. if (!error) {
  185. const { link, pdfUrl } = getObjValue(data)
  186. fileList.value.push(link)
  187. if (pdfUrl) {
  188. pdfList.value.push(pdfUrl)
  189. }
  190. } else {
  191. errorToast(msg)
  192. }
  193. }
  194. //删除文件
  195. const delFileClick = (index) => {
  196. fileList.value.splice(index, 1)
  197. if (pdfList.value.length > 0) {
  198. pdfList.value.splice(index, 1)
  199. }
  200. }
  201. //预览图片
  202. const previewImg = (index) => {
  203. uni.previewImage({
  204. current: index,
  205. urls: fileList.value,
  206. });
  207. }
  208. //表单验证
  209. const formRef = ref(null)
  210. const formData = ref({})
  211. const formRules = {
  212. title: {
  213. rules: [{
  214. required: true,
  215. errorMessage: '请输入题名'
  216. }]
  217. },
  218. textContent: {
  219. rules: [{
  220. required: true,
  221. errorMessage: '请输入文字说明'
  222. }]
  223. },
  224. shootingTimeStr: {
  225. rules: [{
  226. required: true,
  227. errorMessage: '请选择拍摄时间'
  228. }]
  229. },
  230. uploadTime: {
  231. rules: [{
  232. required: true,
  233. errorMessage: '请选择上传日期'
  234. }]
  235. },
  236. shootingUser: {
  237. rules: [{
  238. required: true,
  239. errorMessage: '请输入拍摄者'
  240. }]
  241. },
  242. }
  243. const isFormValidate = async () => {
  244. if (fileList.value.length <= 0) {
  245. errorToast('请先上传文件')
  246. return false
  247. }
  248. const isForm = await formValidate(formRef.value)
  249. if (!isForm) {
  250. errorToast('请先完善表单')
  251. return false
  252. }
  253. return true
  254. }
  255. //提交保存
  256. const saveClick = async () => {
  257. const isForm = await isFormValidate()
  258. if (!isForm) return false;
  259. //更新数据
  260. if (dataId.value) {
  261. await updateImageData()
  262. } else {
  263. await addImageData()
  264. }
  265. }
  266. //更新数据
  267. const updateImageData = async () => {
  268. uni.showLoading({title: '保存数据中...', mask: true});
  269. const {error, code, msg} = await mainApi.updateImageclassifyFile({
  270. ...formData.value,
  271. imageUrl: fileList.value.join(','),
  272. pdfUrl: pdfList.value.join(','),
  273. })
  274. uni.hideLoading();
  275. if (!error && code === 200) {
  276. successToast('保存成功')
  277. } else {
  278. errorToast('保存失败: ' + msg)
  279. }
  280. }
  281. //新增数据
  282. const addImageData = async () => {
  283. uni.showLoading({title: '新增数据中...', mask: true});
  284. const {error, code, msg} = await mainApi.addImageclassifyFile({
  285. ...formData.value,
  286. imageUrl: fileList.value.join(','),
  287. pdfUrl: pdfList.value.join(','),
  288. })
  289. uni.hideLoading();
  290. if (!error && code === 200) {
  291. successToast('新增成功')
  292. return true
  293. } else {
  294. errorToast('新增失败: ' + msg)
  295. return false
  296. }
  297. }
  298. //继续新增
  299. const continueAdd = async () => {
  300. const isForm = await isFormValidate()
  301. if (!isForm) return false;
  302. const res = await addImageData()
  303. if(!res) return false;
  304. //清空表单
  305. const {real_name} = userInfo.value
  306. const { wbsId } = formData.value;
  307. formData.value.uploadTime = dateEnd.value
  308. formData.value.shootingTimeStr = dateEnd.value
  309. formData.value.shootingUser = real_name
  310. formData.value.title = ''
  311. formData.value.textContent = ''
  312. formData.value.photoCode = ''
  313. formData.value.filmCode = ''
  314. formData.value.seeAlsoCode = ''
  315. fileList.value = []
  316. pdfList.value = []
  317. getFileTitleNamedata(wbsId).then()
  318. }
  319. //新增保存
  320. const saveAdd = async () => {
  321. const isForm = await isFormValidate()
  322. if (!isForm) return false;
  323. const res = await addImageData()
  324. if(!res) return false;
  325. //返回上一页
  326. setTimeout(() => {
  327. uni.navigateBack()
  328. }, 1500)
  329. }
  330. </script>