index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <template>
  2. <view class="container">
  3. <uni-forms ref="valiForm" label-align="right" :label-width="80" :rules="rules" :modelValue="valiFormData">
  4. <uni-forms-item label="运单号码" required name="orderNum">
  5. <uni-easyinput v-model="valiFormData.orderNum" placeholder="请输入单号" suffixIcon="scan" :focus="focusType"
  6. @iconClick="scanInput" />
  7. </uni-forms-item>
  8. <uni-forms-item label="复称重量" name="weight">
  9. <uni-easyinput v-model="valiFormData.weight" placeholder="请输入复称重量"
  10. oninput="value=value.replace(/[^\d.]/g,'').replace(/^\./g, '').replace(/\.{2,}/g, '').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').match(/^\d*(\.?\d{0,2})/g)[0] || null">
  11. <template #right>
  12. <view class="weight-right">KG</view>
  13. </template>
  14. </uni-easyinput>
  15. </uni-forms-item>
  16. <!-- <text class='weight-tip'>复称重量作用是确保句惠完整,防止工作人员计费称車后进行的其他操作引起的物品丢失问题</text> -->
  17. <!-- <uni-section title="绑定批托盘号" type="line" padding style="height: calc(100vh - 100px)"> -->
  18. <uni-forms-item label="绑定批次号" name="batchNum" class="select-item">
  19. <uni-data-select v-if="!editBatchNum" v-model="valiFormData.batchNum" placeholder="请选择批次号"
  20. :localdata="options.batch" :clear="false"></uni-data-select>
  21. <uni-easyinput v-if="editBatchNum" :value="batchNum" :disabled="editBatchNum" />
  22. <button v-if="!editBatchNum" type="primary" @click="submitBatchNum">确认</button>
  23. <button v-if="editBatchNum" type="primary" @click="editBatchNum = false">修改</button>
  24. </uni-forms-item>
  25. <uni-forms-item label="绑定托盘号" name="palletNum" class="select-item">
  26. <uni-data-picker v-if="!editPalletNum" placeholder="请选择托盘号" popup-title="绑定托盘号"
  27. :localdata="options.pallet" v-model="valiFormData.palletNum" @change="onchange"
  28. @nodeclick="onnodeclick" @popupopened="onpopupopened"
  29. @popupclosed="onpopupclosed"></uni-data-picker>
  30. <uni-easyinput v-if="editPalletNum" :value="palletNum" :disabled="editPalletNum" />
  31. <button v-if="!editPalletNum" type="primary" @click="submitPalletNum">确认</button>
  32. <button v-if="editPalletNum" type="primary" @click="editPalletNum = false">修改</button>
  33. </uni-forms-item>
  34. <!-- </uni-section> -->
  35. </uni-forms>
  36. <view class="button-group">
  37. <button type="info" @click="reset">重置</button>
  38. <button type="default" @click="checkOrder" :loading="loading">
  39. <uni-icons v-if="!loading" type="search" size="18"></uni-icons>
  40. 查看运单
  41. </button>
  42. <button type="primary" @click="onsubmit" :loading="loading">
  43. <uni-icons v-if="!loading" type="checkmarkempty" size="18" color="white"></uni-icons>
  44. 提交
  45. </button>
  46. </view>
  47. <view v-if="lotnoLogHistory.length > 0" class="history">
  48. <text class="title">记录(最近5条)</text>
  49. </view>
  50. <view class="history">
  51. <view class="item" v-for="(item, i) in lotnoLogHistory.slice(0, 5)" :key="i">
  52. <text class="type">{{ item.type }}</text>
  53. <text class="code" :style="{ color: item.status ? 'green' : '#666' }">{{ item.orderNum }}</text>
  54. <uni-icons v-if="item.status" type="checkmarkempty" class="status" size="16" color="green"></uni-icons>
  55. <text class="status fail" v-else>F</text>
  56. <text style="margin-left: 10rpx; font-weight: 300">
  57. {{ '\r\n' + item.createTime }}
  58. </text>
  59. </view>
  60. </view>
  61. <uni-popup ref="messageRef" type="message">
  62. <uni-popup-message :type="messageType" :message="messageText" :duration="2000"></uni-popup-message>
  63. </uni-popup>
  64. </view>
  65. </template>
  66. <script setup lang="ts">
  67. import { reactive, ref, nextTick, computed } from 'vue';
  68. import permision from '@/common/permission.js';
  69. import { onShow, onLoad, onUnload, onHide, onBackPress, onNavigationBarButtonTap } from '@dcloudio/uni-app';
  70. import { getBindParamsURL, bindShippingURL, getWaybillsURL } from '@/utils/api.js';
  71. const token = ref();
  72. const loading = ref(false);
  73. const hidePage = ref(false);
  74. const focusType = ref(true);
  75. const editBatchNum = ref(false);
  76. const editPalletNum = ref(false);
  77. const lotnoLogHistory = ref([]);
  78. const messageRef = ref();
  79. const messageType = ref();
  80. const messageText = ref();
  81. let st : NodeJS.Timeout;
  82. const valiFormData = ref({
  83. orderNum: '',
  84. weight: '',
  85. batchNum: '',
  86. palletNum: ''
  87. });
  88. const rules = reactive({
  89. orderNum: {
  90. rules: [
  91. {
  92. required: true,
  93. errorMessage: '单号不能为空'
  94. }
  95. ]
  96. }
  97. });
  98. const options = reactive({
  99. batch: [] as any,
  100. pallet: [] as any
  101. });
  102. const onnodeclick = () => { };
  103. const onpopupopened = () => { };
  104. const onpopupclosed = () => { };
  105. const onchange = () => { };
  106. const checkPermission = async () => {
  107. let status = permision.isIOS ? await permision.requestIOS('camera') : await permision.requestAndroid('android.permission.CAMERA');
  108. if (status === null || status === 1) {
  109. status = 1;
  110. } else {
  111. uni.showModal({
  112. content: 'Camera permission required',
  113. confirmText: 'Setting',
  114. success: function (res) {
  115. if (res.confirm) {
  116. permision.gotoAppSetting();
  117. }
  118. }
  119. });
  120. }
  121. return status;
  122. };
  123. const scanInput = async () => {
  124. // #ifdef APP-PLUS
  125. let status = await checkPermission();
  126. if (status !== 1) {
  127. return;
  128. }
  129. // #endif
  130. uni.scanCode({
  131. success: (res : any) => {
  132. valiFormData.value.orderNum = res.result;
  133. // onsubmit();
  134. },
  135. fail: (err) => {
  136. // 需要注意的是小程序扫码不需要申请相机权限
  137. }
  138. });
  139. };
  140. const reset = () => {
  141. loading.value = false;
  142. valiFormData.value.orderNum = '';
  143. valiFormData.value.weight = '';
  144. // valiFormData.value.batchNum = '';
  145. // valiFormData.value.palletNum = '';
  146. };
  147. const submitBatchNum = () => {
  148. if (valiFormData.value.batchNum) {
  149. editBatchNum.value = true;
  150. // setFocus();
  151. } else {
  152. messageType.value = 'error';
  153. messageText.value = '请选择批次号';
  154. messageRef.value.open();
  155. }
  156. };
  157. const batchNum = computed(() => {
  158. const res = options.batch.find(item => item.value === valiFormData.value.batchNum)
  159. return res?.text
  160. })
  161. const submitPalletNum = () => {
  162. if (valiFormData.value.palletNum) {
  163. editPalletNum.value = true;
  164. // setFocus();
  165. } else {
  166. messageType.value = 'error';
  167. messageText.value = '请选择托盘号';
  168. messageRef.value.open();
  169. }
  170. };
  171. const palletNum = computed(() => {
  172. function findTextByValue(arr, targetValue) {
  173. for (let item of arr) {
  174. // 检查当前项的 value 是否匹配
  175. if (item.value === targetValue) {
  176. return item.text; // 找到匹配,返回对应的 text
  177. }
  178. // 如果有子项,递归查找
  179. if (item.children && item.children.length > 0) {
  180. const result = findTextByValue(item.children, targetValue);
  181. if (result) {
  182. return `${item.text}/${result}`; // 返回路径
  183. }
  184. }
  185. }
  186. return null; // 如果没有找到,返回 null
  187. }
  188. const textPath = findTextByValue(options.pallet, valiFormData.value.palletNum);
  189. return textPath
  190. })
  191. const checkOrder = () => {
  192. loading.value = true;
  193. uni.setStorageSync('waybills', []);
  194. uni.request({
  195. url: getWaybillsURL,
  196. method: 'POST',
  197. header: {
  198. batoken: token.value
  199. },
  200. data: {
  201. order_no: valiFormData.value.orderNum,
  202. batch_number: valiFormData.value.batchNum,
  203. pallet_number: valiFormData.value.palletNum ? findPalletNumIdByValue(options.pallet, valiFormData.value.palletNum) : ''
  204. },
  205. success: (res : any) => {
  206. loading.value = false;
  207. if (res.data.code == 1) {
  208. messageType.value = 'success';
  209. messageText.value = res.data.msg;
  210. messageRef.value.open();
  211. uni.setStorageSync('waybills', res.data.data.waybills);
  212. uni.navigateTo({
  213. url: '/pages/scanLotno/waybillsList'
  214. });
  215. } else {
  216. messageType.value = 'error';
  217. messageText.value = res.data.msg;
  218. messageRef.value.open();
  219. }
  220. }
  221. });
  222. };
  223. const findPalletNumIdByValue = (data : any, targetValue : any) => {
  224. for (let i = 0; i < data.length; i++) {
  225. const item = data[i];
  226. // 检查当前项的 label
  227. if (item.id === targetValue || item.value === targetValue) {
  228. return [item.id];
  229. } else if (item.children && item.children.length > 0) {
  230. const childId = findPalletNumIdByValue(item.children, targetValue) as any;
  231. if (childId !== null) {
  232. return childId;
  233. }
  234. }
  235. }
  236. return null;
  237. };
  238. const onsubmit = () => {
  239. st && clearTimeout(st);
  240. loading.value = true;
  241. uni.request({
  242. url: bindShippingURL,
  243. method: 'POST',
  244. header: {
  245. batoken: token.value
  246. },
  247. data: {
  248. order_no: valiFormData.value.orderNum,
  249. reweighting: valiFormData.value.weight,
  250. batch_number: valiFormData.value.batchNum,
  251. pallet_number: valiFormData.value.palletNum ? findPalletNumIdByValue(options.pallet, valiFormData.value.palletNum) : ''
  252. },
  253. success: (res : any) => {
  254. loading.value = false;
  255. if (res.data.code == 1) {
  256. messageType.value = 'success';
  257. messageText.value = res.data.msg;
  258. messageRef.value.open();
  259. if (res.data.data.labels && res.data.data.labels.length > 0) {
  260. console.log('有打印面单');
  261. // res.data.data.labels.map((url: string) => {
  262. // let path = fullUrl(url);
  263. // window.open(path, '_blank');
  264. // });
  265. }
  266. const historyItem = {
  267. orderNum: valiFormData.value.orderNum,
  268. createTime: new Date(),
  269. type: valiFormData.value.batchNum ? '绑定批次' : '绑定托盘',
  270. status: true
  271. };
  272. console.log('lotnoLogHistory--', lotnoLogHistory);
  273. lotnoLogHistory.value.unshift(historyItem);
  274. uni.setStorageSync('lotnoLogHistory', lotnoLogHistory.value);
  275. getHistory();
  276. } else {
  277. messageType.value = 'error';
  278. messageText.value = res.data.msg;
  279. messageRef.value.open();
  280. const historyItem = {
  281. orderNum: valiFormData.value.orderNum,
  282. createTime: new Date(),
  283. type: valiFormData.value.batchNum ? '绑定批次' : '绑定托盘',
  284. status: false
  285. };
  286. lotnoLogHistory.value.unshift(historyItem);
  287. uni.setStorageSync('lotnoLogHistory', lotnoLogHistory.value);
  288. getHistory();
  289. }
  290. st = setTimeout(() => {
  291. setFocus();
  292. reset();
  293. setFocus();
  294. st && clearTimeout(st);
  295. }, 1000);
  296. }
  297. });
  298. };
  299. const getHistory = () => {
  300. lotnoLogHistory.value = uni.getStorageSync('lotnoLogHistory') || [];
  301. };
  302. const keypress = (e : any) => {
  303. console.log(e, '按键码');
  304. // 102 左侧 103 右侧 104 中间按键
  305. if (e.keyCode === 102 || e.keyCode === 103 || e.keyCode === 104) {
  306. //这里按键成功
  307. }
  308. if (e.keyCode == 66) {
  309. //enter按键
  310. //这里input已经拿到数据了,在这里把拿到的数据,通过接口数据联调起来
  311. onsubmit();
  312. }
  313. };
  314. onLoad(() => {
  315. // #ifdef APP-PLUS
  316. plus.key.addEventListener('keyup', keypress);
  317. // #endif
  318. // #ifdef H5
  319. document.addEventListener('keyup', keypress);
  320. // #endif
  321. });
  322. onUnload(() => {
  323. // #ifdef APP-PLUS
  324. plus.key.removeEventListener('keyup', keypress);
  325. // #endif
  326. // #ifdef H5
  327. document.removeEventListener('keyup', keypress);
  328. // #endif
  329. });
  330. onHide(() => {
  331. hidePage.value = true;
  332. // #ifdef APP-PLUS
  333. plus.key.removeEventListener('keyup', keypress);
  334. // #endif
  335. // #ifdef H5
  336. document.removeEventListener('keyup', keypress);
  337. // #endif
  338. });
  339. onBackPress(() => {
  340. // #ifdef APP-PLUS
  341. plus.key.removeEventListener('keyup', keypress);
  342. // #endif
  343. // #ifdef H5
  344. document.removeEventListener('keyup', keypress);
  345. // #endif
  346. });
  347. onShow(() => {
  348. hidePage.value = false;
  349. token.value = uni.getStorageSync('token');
  350. getBindParams();
  351. getHistory();
  352. });
  353. const setFocus = () => {
  354. if (hidePage.value) {
  355. return;
  356. }
  357. focusType.value = false;
  358. nextTick(() => {
  359. focusType.value = true;
  360. });
  361. };
  362. const getBindParams = () => {
  363. uni.request({
  364. url: getBindParamsURL,
  365. method: 'GET',
  366. header: {
  367. batoken: token.value
  368. },
  369. success: (res : any) => {
  370. if (res.data.code === 1) {
  371. options.batch = res.data.data.batch_number.map((item : any) => {
  372. return { text: item.name, value: item.id };
  373. });
  374. function transformData(data : any[]) {
  375. return data.map((item) => {
  376. const { id, value, label, ...rest } = item;
  377. return {
  378. id: id || value,
  379. value: value,
  380. text: label,
  381. ...rest,
  382. children: item.children ? transformData(item.children) : []
  383. };
  384. });
  385. }
  386. const shipping = transformData(res.data.data.shipping);
  387. options.pallet = shipping;
  388. }
  389. },
  390. fail(e) {
  391. console.log('fail--', e);
  392. }
  393. });
  394. };
  395. onNavigationBarButtonTap((event) => {
  396. if (event.index === 0) {
  397. uni.navigateTo({
  398. url: '/pages/scanLotno/lotnoLog'
  399. });
  400. }
  401. });
  402. </script>
  403. <style lang="scss">
  404. .container {
  405. padding: 15px;
  406. background-color: #fff;
  407. }
  408. .weight-right {
  409. padding-right: 10rpx;
  410. font-size: 14rpx;
  411. }
  412. .weight-tip {
  413. color: gray;
  414. font-size: 12rpx;
  415. }
  416. .select-item {
  417. .uni-forms-item__content {
  418. display: flex !important;
  419. }
  420. .uni-forms-item__label {
  421. width: auto !important;
  422. }
  423. button {
  424. height: 35px;
  425. font-size: 16rpx;
  426. margin-left: 10rpx;
  427. }
  428. }
  429. .button-group {
  430. margin-top: 15px;
  431. display: flex;
  432. flex-direction: row;
  433. justify-content: space-around;
  434. button {
  435. display: flex;
  436. align-items: center;
  437. justify-content: center;
  438. height: 35px;
  439. width: 50%;
  440. margin-left: 10px;
  441. font-size: 16rpx;
  442. }
  443. .uni-icons {
  444. margin-right: 10px;
  445. }
  446. }
  447. .history {
  448. display: flex;
  449. width: 100%;
  450. flex-direction: column;
  451. justify-items: start;
  452. .title {
  453. padding: 20rpx;
  454. font-size: 24rpx;
  455. font-weight: 600;
  456. }
  457. .type {
  458. padding-right: 20rpx;
  459. font-size: 24rpx;
  460. }
  461. .code {
  462. font-weight: 600;
  463. }
  464. .item {
  465. padding: 20rpx;
  466. font-size: 20rpx;
  467. color: #666;
  468. .status {
  469. padding-left: 20rpx;
  470. }
  471. .fail {
  472. font-weight: 600;
  473. color: #f00;
  474. }
  475. }
  476. .is-empty {
  477. text-align: center;
  478. margin: 40px 0;
  479. color: #999;
  480. }
  481. }
  482. </style>