camera-scan-code.uvue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. if(this.imgArrLen == 0){
  249. updateImgs = this.path
  250. updateNames = newImgName;
  251. }else{
  252. updateImgs = this.joinRecord.urlpdt + "," + this.path
  253. updateNames = this.joinRecord.imgname + "," + newImgName;
  254. }
  255. let updatedData = "imgname='" + updateNames + "',urlpdt='" + updateImgs + "'"
  256. if(this.joinRecord.num === this.imgArrLen + 1 ){
  257. updateStatus = "status='3'"
  258. updatedData = updatedData + "," + updateStatus
  259. }else if(this.joinRecord.status === 1){
  260. updateStatus = "status='2'"
  261. updatedData = updatedData + "," + updateStatus
  262. }
  263. console.log(updatedData)
  264. updateData('app_media_record', updatedData, 'sxid', this.recordId.toString()).then((res : UTSJSONObject) => {
  265. let dataList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
  266. if (dataList != null && dataList.length > 0) {
  267. console.log(dataList[0])
  268. uni.showToast({
  269. title: "保存成功!",
  270. });
  271. /*
  272. uni.reLaunch({
  273. url: '/pages/record/camera-scan-code?path='+this.path+'&recordId='+this.recordId+'&num='+this.num
  274. }); */
  275. }
  276. });
  277. },
  278. previewSingleImage(imageUrl : string) {
  279. uni.previewImage({
  280. urls: [imageUrl], // 需要预览的图片链接列表
  281. current: 0, // 当前显示图片的索引
  282. indicator: 'number', // 图片指示器样式
  283. loop: false // 是否可循环预览
  284. });
  285. },
  286. handleScanCode(ev : UniCameraScanCodeEvent) {
  287. const deatil = ev.detail;
  288. this.result = {
  289. type: deatil.type,
  290. result: deatil.result
  291. } as CameraScanCodeResult
  292. },
  293. // 计算最小的可用图片编号
  294. calculateMinAvailableNumber() {
  295. if(this.imgArrLen === 0) {
  296. this.minAvailableNumber = 1;
  297. return;
  298. }
  299. // 从已有的图片名称中提取编号
  300. const existingNumbers = new Set<number>();
  301. const imgNames = this.joinRecord.imgname.split(',');
  302. imgNames.forEach((name : string) => {
  303. if(name != '') {
  304. // 提取图片名称中的数字部分
  305. const match = name.match(/-(\d+)\.jpg$/);
  306. if(match != null && match[1] != null) {
  307. const numStr = match[1] as string;
  308. existingNumbers.add(parseInt(numStr));
  309. }
  310. }
  311. });
  312. // 查找1到所需图片数量之间最小的未使用编号
  313. for(let i = 1; i <= this.joinRecord.num; i++) {
  314. if(!existingNumbers.has(i)) {
  315. this.minAvailableNumber = i;
  316. return;
  317. }
  318. }
  319. // 如果1到所需数量都被使用了,则使用当前数量+1
  320. this.minAvailableNumber = this.imgArrLen + 1;
  321. },
  322. }
  323. }
  324. </script>
  325. <style>
  326. .camera-scan-code-back-wrap {
  327. display: flex;
  328. justify-content: center;
  329. align-items: center;
  330. }
  331. .camera-scan-code-table {
  332. background-color: white;
  333. margin-top: 20px;
  334. }
  335. .camera-scan-code-table-pair {
  336. height: 150px;
  337. flex-direction: column;
  338. justify-content: space-between;
  339. align-items: center;
  340. }
  341. .camera-scan-code-table-pair-label {
  342. flex-grow: 1;
  343. margin-left: 15px;
  344. }
  345. .camera-scan-code-table-pair-value {
  346. flex-grow: 2;
  347. }
  348. .camera-scan-code-table-pair-value .txt {
  349. font-size: 14px;
  350. }
  351. .camera-scan-code-table-top-line {
  352. border-top: 1px solid #eee;
  353. }
  354. .swiper {
  355. height: 300rpx;
  356. }
  357. .swiper-box {
  358. height: 150px;
  359. }
  360. .swiper-item {
  361. /* #ifndef APP-NVUE */
  362. display: flex;
  363. /* #endif */
  364. flex-direction: column;
  365. justify-content: center;
  366. align-items: center;
  367. color: #fff;
  368. height: 300rpx;
  369. line-height: 300rpx;
  370. }
  371. .btn {
  372. margin-top: 30px;
  373. height: 45px;
  374. }
  375. </style>