camera-scan-code.uvue 12 KB

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