camera-scan-code.uvue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. onLoad(options) {
  111. this.path = options?.['path'] ?? ''
  112. let recordId = options?.['recordId'] ?? ''
  113. let num = options?.['num'] ?? ''
  114. console.log(this.path)
  115. if (this.path != '') {
  116. this.data = []
  117. this.data.push({ image: this.path, name: '', checked: true })
  118. }
  119. if (recordId != '') {
  120. this.recordId = parseInt(recordId)
  121. 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) => {
  122. let dataList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
  123. if (dataList != null && dataList.length > 0) {
  124. dataList.forEach(item => {
  125. if (item != null) {
  126. let record = JSON.parse<JoinRecord>(item.toJSONString());
  127. if (record != null) {
  128. this.num = record.num;
  129. this.dyImgName = record.workorder + record.invname + record.part + record.photoitem + record.partno;
  130. this.joinRecord = record
  131. this.imgArrLen = record.imgname.indexOf(",")>-1? record.imgname.split(",").length : (record.imgname!=''?1:0)
  132. // 计算最小可用编号
  133. this.calculateMinAvailableNumber();
  134. }
  135. }
  136. });
  137. }
  138. });
  139. }
  140. if (num != '') {
  141. this.num = parseInt(num)
  142. }
  143. },
  144. methods: {
  145. navigateBack() {
  146. //uni.navigateBack()
  147. uni.navigateTo({
  148. url: `/pages/work/record/Camera?id=${this.recordId}&num=${this.num}`
  149. })
  150. },
  151. navigateExit(){
  152. uni.navigateTo({
  153. url: `/pages/work/record/InfoDetail?id=${this.joinRecord.pid}`
  154. })
  155. },
  156. checkboxChange() {
  157. console.log("选择图片名称")
  158. },
  159. chooseImage() {
  160. uni.chooseImage({
  161. sourceType: ['album'],
  162. success: (res) => {
  163. //this.data = res.tempFilePaths; // 获取选中的图片路径
  164. this.data = []
  165. for (let i = 0; i < res.tempFilePaths.length; i++) {
  166. this.data.push({ image: res.tempFilePaths[i], name: '', checked: true })
  167. }
  168. },
  169. fail: (err) => {
  170. console.error('选择图片失败', err);
  171. }
  172. });
  173. },
  174. saveImage() {
  175. //保存图片进入相册文件
  176. if(this.path==''){
  177. uni.showToast({
  178. title: '没有拍照文件',
  179. icon: 'error',
  180. duration: 2000
  181. });
  182. return;
  183. }
  184. if(this.imgArrLen == this.joinRecord.num){
  185. uni.showToast({
  186. title: '拍照图片已满',
  187. icon: 'error',
  188. duration: 2000
  189. });
  190. return;
  191. }
  192. /*
  193. uni.saveImageToPhotosAlbum({
  194. filePath: this.path,
  195. success: () => {
  196. uni.showToast({
  197. title: "保存成功!",
  198. });
  199. },
  200. fail: () => {
  201. uni.showToast({
  202. title: "保存失败",
  203. });
  204. },
  205. }); */
  206. let updateImgs = ''
  207. let updateNames = ''
  208. let updateStatus = ''
  209. // 动态生成最小的可用图片编号
  210. const getMinAvailableNumber = () => {
  211. if(this.imgArrLen === 0) return 1;
  212. // 从已有的图片名称中提取编号
  213. const existingNumbers = new Set<number>();
  214. const imgNames = this.joinRecord.imgname.split(',');
  215. imgNames.forEach((name : string) => {
  216. if(name != '') {
  217. // 提取图片名称中的数字部分
  218. const match = name.match(/-(\d+)\.jpg$/);
  219. if(match != null && match[1] != null) {
  220. const numStr = match[1] as string;
  221. existingNumbers.add(parseInt(numStr));
  222. }
  223. }
  224. });
  225. // 查找1到所需图片数量之间最小的未使用编号
  226. for(let i = 1; i <= this.joinRecord.num; i++) {
  227. if(!existingNumbers.has(i)) {
  228. return i;
  229. }
  230. }
  231. // 如果1到所需数量都被使用了,则使用当前数量+1
  232. return this.imgArrLen + 1;
  233. };
  234. const minAvailableNumber = getMinAvailableNumber();
  235. const newImgName = this.dyImgName + "-" + minAvailableNumber + ".jpg";
  236. if(this.imgArrLen == 0){
  237. updateImgs = this.path
  238. updateNames = newImgName;
  239. }else{
  240. updateImgs = this.joinRecord.urlpdt + "," + this.path
  241. updateNames = this.joinRecord.imgname + "," + newImgName;
  242. }
  243. let updatedData = "imgname='" + updateNames + "',urlpdt='" + updateImgs + "'"
  244. if(this.joinRecord.num === this.imgArrLen + 1 ){
  245. updateStatus = "status='3'"
  246. updatedData = updatedData + "," + updateStatus
  247. }else if(this.joinRecord.status === 1){
  248. updateStatus = "status='2'"
  249. updatedData = updatedData + "," + updateStatus
  250. }
  251. console.log(updatedData)
  252. updateData('app_media_record', updatedData, 'sxid', this.recordId.toString()).then((res : UTSJSONObject) => {
  253. let dataList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
  254. if (dataList != null && dataList.length > 0) {
  255. console.log(dataList[0])
  256. uni.showToast({
  257. title: "保存成功!",
  258. });
  259. /*
  260. uni.reLaunch({
  261. url: '/pages/record/camera-scan-code?path='+this.path+'&recordId='+this.recordId+'&num='+this.num
  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. handleScanCode(ev : UniCameraScanCodeEvent) {
  275. const deatil = ev.detail;
  276. this.result = {
  277. type: deatil.type,
  278. result: deatil.result
  279. } as CameraScanCodeResult
  280. },
  281. // 计算最小的可用图片编号
  282. calculateMinAvailableNumber() {
  283. if(this.imgArrLen === 0) {
  284. this.minAvailableNumber = 1;
  285. return;
  286. }
  287. // 从已有的图片名称中提取编号
  288. const existingNumbers = new Set<number>();
  289. const imgNames = this.joinRecord.imgname.split(',');
  290. imgNames.forEach((name : string) => {
  291. if(name != '') {
  292. // 提取图片名称中的数字部分
  293. const match = name.match(/-(\d+)\.jpg$/);
  294. if(match != null && match[1] != null) {
  295. const numStr = match[1] as string;
  296. existingNumbers.add(parseInt(numStr));
  297. }
  298. }
  299. });
  300. // 查找1到所需图片数量之间最小的未使用编号
  301. for(let i = 1; i <= this.joinRecord.num; i++) {
  302. if(!existingNumbers.has(i)) {
  303. this.minAvailableNumber = i;
  304. return;
  305. }
  306. }
  307. // 如果1到所需数量都被使用了,则使用当前数量+1
  308. this.minAvailableNumber = this.imgArrLen + 1;
  309. },
  310. }
  311. }
  312. </script>
  313. <style>
  314. .camera-scan-code-back-wrap {
  315. display: flex;
  316. justify-content: center;
  317. align-items: center;
  318. }
  319. .camera-scan-code-table {
  320. background-color: white;
  321. margin-top: 20px;
  322. }
  323. .camera-scan-code-table-pair {
  324. height: 150px;
  325. flex-direction: column;
  326. justify-content: space-between;
  327. align-items: center;
  328. }
  329. .camera-scan-code-table-pair-label {
  330. flex-grow: 1;
  331. margin-left: 15px;
  332. }
  333. .camera-scan-code-table-pair-value {
  334. flex-grow: 2;
  335. }
  336. .camera-scan-code-table-pair-value .txt {
  337. font-size: 14px;
  338. }
  339. .camera-scan-code-table-top-line {
  340. border-top: 1px solid #eee;
  341. }
  342. .swiper {
  343. height: 300rpx;
  344. }
  345. .swiper-box {
  346. height: 150px;
  347. }
  348. .swiper-item {
  349. /* #ifndef APP-NVUE */
  350. display: flex;
  351. /* #endif */
  352. flex-direction: column;
  353. justify-content: center;
  354. align-items: center;
  355. color: #fff;
  356. height: 300rpx;
  357. line-height: 300rpx;
  358. }
  359. .btn {
  360. margin-top: 30px;
  361. height: 45px;
  362. }
  363. </style>