TaskCamera-scan-code.uvue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex:1">
  4. <!-- #endif -->
  5. <!--
  6. <camera style="width: 100%; height: 300px;" :resolution="'high'" :mode="'scanCode'" @scancode="handleScanCode">
  7. </camera> -->
  8. <view class="my-page">
  9. <swiper class="swiper-box my-swiper" :current="swiperDotIndex" :indicator-dots="true">
  10. <swiper-item v-for="item in data">
  11. <view class="swiper-item" @tap="previewSingleImage(item.image)">
  12. <image class="my-image" :src="item.image" mode="aspectFill" />
  13. </view>
  14. <view>
  15. <text class="demo-view-label">{{ item?.name ?? ''}}</text>
  16. </view>
  17. </swiper-item>
  18. </swiper>
  19. <view class="camera-scan-code-back-wrap" v-show="false">
  20. <button class="btn block bg-blue lg round" @click="chooseImage">
  21. 浏览相册
  22. </button>
  23. </view>
  24. <view class="camera-scan-code-table">
  25. <view class="camera-scan-code-table-pair" v-if="joinRecord.imgname!=''">
  26. <view class="camera-scan-code-table-pair-label">
  27. <text>已存图片</text>
  28. </view>
  29. <view class="camera-scan-code-table-pair-value" v-for="itemName in joinRecord.imgname.split(',')" >
  30. <text class="txt">{{ itemName }}</text>
  31. </view>
  32. </view>
  33. <view class="camera-scan-code-table-pair">
  34. <view class="camera-scan-code-table-pair-label">
  35. <text>待存图片</text>
  36. </view>
  37. <view class="camera-scan-code-table-pair-value">
  38. <text class="txt">{{ dyImgName+"-"+minAvailableNumber+".jpg"}}</text>
  39. </view>
  40. </view>
  41. </view>
  42. <view class="camera-scan-code-back-wrap">
  43. <button class="btn block bg-blue lg round" @click="saveImage" style="min-width: 250px;">
  44. 保存图片
  45. </button>
  46. </view>
  47. <view class="camera-scan-code-back-wrap">
  48. <button type="default" class="btn bg-blue round" @click="navigateBack">返回拍照</button>
  49. <button type="default" class="btn bg-blue round" @click="navigateExit">退出拍照</button>
  50. </view>
  51. </view>
  52. <!-- #ifdef APP -->
  53. </scroll-view>
  54. <!-- #endif -->
  55. </template>
  56. <script lang="uts">
  57. type CameraScanCodeResult = {
  58. type : string | null;
  59. result : string | null;
  60. }
  61. type ImageItem = {
  62. image : string,
  63. name : string,
  64. checked : boolean
  65. }
  66. import { getJoinList, TaskJoinRecord, updateData } from '@/api/work';
  67. export default {
  68. data() {
  69. return {
  70. result: null as CameraScanCodeResult | null,
  71. swiperDotIndex: 0 as number,
  72. path: '',
  73. recordId: 0,
  74. imgArrLen: 0,
  75. joinRecord: {
  76. sxid : 0,
  77. pdid : 0,
  78. photographpoint : '',
  79. photographdescription : '',
  80. photourl : '',
  81. photourlSpl : [],
  82. imgname : '',
  83. exampleurl : '',
  84. exampleurlSpl : [],
  85. photoname : '',
  86. fk_qcRecord : '',
  87. fk_prodcode : '',
  88. prodno : '',
  89. processStep : '',
  90. workorder:'',
  91. invname: '',
  92. } as TaskJoinRecord,
  93. num: 0,
  94. maxcount: 0,
  95. dyImgName: '',
  96. minAvailableNumber: 1,
  97. data: [{
  98. image: '/static/images/banner/banner01.png',
  99. name: 'banner01.png',
  100. checked: true
  101. }] as ImageItem[]
  102. }
  103. },
  104. onBackPress() {
  105. console.log(this.recordId, this.num, this.joinRecord.pdid);
  106. // 覆盖系统默认的返回行为,返回到指定页面
  107. //PhotoRecord?pdid=
  108. uni.navigateTo({
  109. url: `/pages/work/download/PhotoRecord?pdid=${this.joinRecord.pdid}&senum=${this.num}`,
  110. // 修改动画方向为从左到右退回
  111. animationType: 'slide-in-left', // 使用从左到右滑出的动画效果
  112. animationDuration: 300 // 动画持续时间,单位ms
  113. })
  114. // 返回true表示拦截默认返回行为
  115. return true
  116. },
  117. onLoad(options) {
  118. this.path = options?.['path'] ?? ''
  119. let recordId = options?.['recordId'] ?? ''
  120. let num = options?.['num'] ?? ''
  121. if (num != '') {
  122. this.num = parseInt(num);
  123. }
  124. let maxcount = options?.['maxcount'] ?? ''
  125. if (this.path != '') {
  126. this.data = []
  127. this.data.push({ image: this.path, name: '', checked: true })
  128. }
  129. if (recordId != '') {
  130. this.recordId = parseInt(recordId)
  131. getJoinList('app_task_photo as r', 'app_task_info as i', 'r.*,i.*', 'r.pdid=i.pdid', 'sxid', recordId, null).then((res : UTSJSONObject) => {
  132. let dataList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
  133. if (dataList != null && dataList.length > 0) {
  134. dataList.forEach(item => {
  135. if (item != null) {
  136. let record = JSON.parse<TaskJoinRecord>(item.toJSONString());
  137. console.log(record);
  138. if (record != null) {
  139. this.dyImgName = record.workorder + record.invname;
  140. this.joinRecord = record
  141. this.imgArrLen = record.imgname.indexOf(",") > -1 ? record.imgname.split(",").length : (record.imgname != '' ? 1 : 0)
  142. // 计算最小可用编号
  143. this.calculateMinAvailableNumber();
  144. }
  145. }
  146. });
  147. }
  148. });
  149. }
  150. if (num != '') {
  151. this.num = parseInt(num)
  152. }
  153. if (maxcount != '') {
  154. this.maxcount = parseInt(maxcount)
  155. }
  156. },
  157. methods: {
  158. navigateBack() {
  159. //uni.navigateBack()
  160. uni.navigateTo({
  161. url: `/pages/work/download/TaskCamera?id=${this.recordId}&num=${this.num}`
  162. })
  163. },
  164. navigateExit() {
  165. uni.navigateTo({
  166. url: `/pages/work/download/DownloadDetail?id=${this.joinRecord.pdid}`
  167. })
  168. },
  169. checkboxChange() {
  170. console.log("选择图片名称")
  171. },
  172. chooseImage() {
  173. uni.chooseImage({
  174. sourceType: ['album'],
  175. success: (res) => {
  176. //this.data = res.tempFilePaths; // 获取选中的图片路径
  177. this.data = []
  178. for (let i = 0; i < res.tempFilePaths.length; i++) {
  179. this.data.push({ image: res.tempFilePaths[i], name: '', checked: true })
  180. }
  181. },
  182. fail: (err) => {
  183. console.error('选择图片失败', err);
  184. }
  185. });
  186. },
  187. saveImage() {
  188. //保存图片进入相册文件
  189. if (this.path == '') {
  190. uni.showToast({
  191. title: '没有拍照文件',
  192. icon: 'error',
  193. duration: 2000
  194. });
  195. return;
  196. }
  197. let updateImgs = ''
  198. let updateNames = ''
  199. // 动态生成最小的可用图片编号
  200. const getMinAvailableNumber = () => {
  201. if (this.imgArrLen === 0) return 1;
  202. // 从已有的图片名称中提取编号
  203. const existingNumbers = new Set<number>();
  204. const imgNames = this.joinRecord.imgname.split(',');
  205. imgNames.forEach((name : string) => {
  206. if (name != '') {
  207. // 提取图片名称中的数字部分
  208. const match = name.match(/-(\d+)\.jpg$/);
  209. if (match != null && match[1] != null) {
  210. const numStr = match[1] as string;
  211. existingNumbers.add(parseInt(numStr));
  212. }
  213. }
  214. });
  215. // 查找1到所需图片数量之间最小的未使用编号
  216. for (let i = 1; i <= 10; i++) {
  217. if (!existingNumbers.has(i)) {
  218. return i;
  219. }
  220. }
  221. // 如果1到所需数量都被使用了,则使用当前数量+1
  222. return this.imgArrLen + 1;
  223. };
  224. const minAvailableNumber = getMinAvailableNumber();
  225. const newImgName = this.dyImgName + "-" + minAvailableNumber + ".jpg";
  226. const relativePath = `/uploadImgs/${newImgName}`
  227. if (this.imgArrLen == 0) {
  228. updateImgs = relativePath
  229. updateNames = newImgName;
  230. } else {
  231. updateImgs = this.joinRecord.photourl + "," + relativePath
  232. updateNames = this.joinRecord.imgname + "," + newImgName;
  233. }
  234. let updatedData = "imgname='" + updateNames + "',photourl='" + updateImgs + "'"
  235. updateData('app_task_photo', updatedData, 'sxid', this.recordId.toString()).then((res : UTSJSONObject) => {
  236. let data = res?.['data'] as boolean ?? false
  237. if (data != null && data== true) {
  238. uni.showToast({
  239. title: "保存成功!",
  240. });
  241. const newPath = `${uni.env.USER_DATA_PATH}/uploadImgs/${newImgName}`
  242. this.renameFile(this.path, newPath)
  243. uni.navigateTo({
  244. url: `/pages/work/download/PhotoRecord?pdid=${this.joinRecord.pdid}&senum=${this.num}`,
  245. // 修改动画方向为从左到右退回
  246. animationType: 'slide-in-left', // 使用从左到右滑出的动画效果
  247. animationDuration: 300 // 动画持续时间,单位ms
  248. })
  249. }
  250. });
  251. },
  252. previewSingleImage(imageUrl : string) {
  253. uni.previewImage({
  254. urls: [imageUrl], // 需要预览的图片链接列表
  255. current: 0, // 当前显示图片的索引
  256. indicator: 'number', // 图片指示器样式
  257. loop: false // 是否可循环预览
  258. });
  259. },
  260. renameFile(oldPath:string, newPath:string) {
  261. // 先拷贝文件到新路径
  262. uni.getFileSystemManager().copyFile({
  263. srcPath: oldPath,
  264. destPath: newPath,
  265. success: function () {
  266. // 可选:删除原文件,如果你需要释放空间的话
  267. uni.getFileSystemManager().unlink({
  268. filePath: oldPath,
  269. success: function () {
  270. console.log('原文件已删除');
  271. },
  272. fail: function (unlinkErr) {
  273. console.error('删除原文件失败', unlinkErr);
  274. }
  275. });
  276. console.log('文件重命名成功');
  277. },
  278. fail: function (copyErr) {
  279. console.error('拷贝文件失败', copyErr);
  280. }
  281. });
  282. },
  283. handleScanCode(ev : UniCameraScanCodeEvent) {
  284. const deatil = ev.detail;
  285. this.result = {
  286. type: deatil.type,
  287. result: deatil.result
  288. } as CameraScanCodeResult
  289. },
  290. // 计算最小的可用图片编号
  291. calculateMinAvailableNumber() {
  292. if (this.imgArrLen === 0) {
  293. this.minAvailableNumber = 1;
  294. return;
  295. }
  296. // 从已有的图片名称中提取编号
  297. const existingNumbers = new Set<number>();
  298. const imgNames = this.joinRecord.imgname.split(',');
  299. imgNames.forEach((name : string) => {
  300. if (name != '') {
  301. // 提取图片名称中的数字部分
  302. const match = name.match(/-(\d+)\.jpg$/);
  303. if (match != null && match[1] != null) {
  304. const numStr = match[1] as string;
  305. existingNumbers.add(parseInt(numStr));
  306. }
  307. }
  308. });
  309. // 查找1到所需图片数量之间最小的未使用编号
  310. for (let i = 1; i <= 10; i++) {
  311. if (!existingNumbers.has(i)) {
  312. this.minAvailableNumber = i;
  313. return;
  314. }
  315. }
  316. // 如果1到所需数量都被使用了,则使用当前数量+1
  317. this.minAvailableNumber = this.imgArrLen + 1;
  318. },
  319. }
  320. }
  321. </script>
  322. <style>
  323. .camera-scan-code-back-wrap {
  324. display: flex;
  325. justify-content: center;
  326. align-items: center;
  327. flex-direction: row;
  328. }
  329. .camera-scan-code-table {
  330. background-color: white;
  331. margin-top: 20px;
  332. }
  333. .camera-scan-code-table-pair {
  334. height: 100px;
  335. flex-direction: column;
  336. justify-content: space-between;
  337. align-items: center;
  338. }
  339. .camera-scan-code-table-pair-label {
  340. flex-grow: 1;
  341. margin-left: 15px;
  342. }
  343. .camera-scan-code-table-pair-value {
  344. flex-grow: 2;
  345. flex-direction: column;
  346. }
  347. .camera-scan-code-table-pair-value .txt {
  348. font-size: 14px;
  349. }
  350. .camera-scan-code-table-top-line {
  351. border-top: 1px solid #eee;
  352. }
  353. .my-page {
  354. display: flex;
  355. flex-direction: column;
  356. box-sizing: border-box;
  357. background-color: #fff;
  358. /* #ifndef APP-ANDROID */
  359. min-height: 100%;
  360. /* #endif */
  361. height: 100%;
  362. }
  363. .swiper {
  364. height: 400px;
  365. }
  366. .swiper-box {
  367. height: 300px;
  368. }
  369. .swiper-item {
  370. /* #ifndef APP-NVUE */
  371. display: flex;
  372. /* #endif */
  373. flex-direction: column;
  374. justify-content: center;
  375. align-items: center;
  376. color: #fff;
  377. height: 400px;
  378. line-height: 400px;
  379. }
  380. .btn {
  381. /*margin-top: 30px; */
  382. height: 45px;
  383. margin: 20px 20px;
  384. }
  385. </style>