camera-scan-code.uvue 12 KB

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