camera-scan-code.uvue 12 KB

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