index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. reset();
  292. setFocus();
  293. st && clearTimeout(st);
  294. }, 1000);
  295. }
  296. });
  297. };
  298. const getHistory = () => {
  299. lotnoLogHistory.value = uni.getStorageSync('lotnoLogHistory') || [];
  300. };
  301. const keypress = (e : any) => {
  302. console.log(e, '按键码');
  303. // 102 左侧 103 右侧 104 中间按键
  304. if (e.keyCode === 102 || e.keyCode === 103 || e.keyCode === 104) {
  305. //这里按键成功
  306. }
  307. if (e.keyCode == 66) {
  308. //enter按键
  309. //这里input已经拿到数据了,在这里把拿到的数据,通过接口数据联调起来
  310. onsubmit();
  311. }
  312. };
  313. onLoad(() => {
  314. // #ifdef APP-PLUS
  315. plus.key.addEventListener('keyup', keypress);
  316. // #endif
  317. // #ifdef H5
  318. document.addEventListener('keyup', keypress);
  319. // #endif
  320. });
  321. onUnload(() => {
  322. // #ifdef APP-PLUS
  323. plus.key.removeEventListener('keyup', keypress);
  324. // #endif
  325. // #ifdef H5
  326. document.removeEventListener('keyup', keypress);
  327. // #endif
  328. });
  329. onHide(() => {
  330. hidePage.value = true;
  331. // #ifdef APP-PLUS
  332. plus.key.removeEventListener('keyup', keypress);
  333. // #endif
  334. // #ifdef H5
  335. document.removeEventListener('keyup', keypress);
  336. // #endif
  337. });
  338. onBackPress(() => {
  339. // #ifdef APP-PLUS
  340. plus.key.removeEventListener('keyup', keypress);
  341. // #endif
  342. // #ifdef H5
  343. document.removeEventListener('keyup', keypress);
  344. // #endif
  345. });
  346. onShow(() => {
  347. hidePage.value = false;
  348. token.value = uni.getStorageSync('token');
  349. getBindParams();
  350. getHistory();
  351. });
  352. const setFocus = () => {
  353. if (hidePage.value) {
  354. return;
  355. }
  356. focusType.value = false;
  357. nextTick(() => {
  358. focusType.value = true;
  359. });
  360. };
  361. const getBindParams = () => {
  362. uni.request({
  363. url: getBindParamsURL,
  364. method: 'GET',
  365. header: {
  366. batoken: token.value
  367. },
  368. success: (res : any) => {
  369. if (res.data.code === 1) {
  370. options.batch = res.data.data.batch_number.map((item : any) => {
  371. return { text: item.name, value: item.id };
  372. });
  373. function transformData(data : any[]) {
  374. return data.map((item) => {
  375. const { id, value, label, ...rest } = item;
  376. return {
  377. id: id || value,
  378. value: value,
  379. text: label,
  380. ...rest,
  381. children: item.children ? transformData(item.children) : []
  382. };
  383. });
  384. }
  385. const shipping = transformData(res.data.data.shipping);
  386. options.pallet = shipping;
  387. }
  388. },
  389. fail(e) {
  390. console.log('fail--', e);
  391. }
  392. });
  393. };
  394. onNavigationBarButtonTap((event) => {
  395. if (event.index === 0) {
  396. uni.navigateTo({
  397. url: '/pages/scanLotno/lotnoLog'
  398. });
  399. }
  400. });
  401. </script>
  402. <style lang="scss">
  403. .container {
  404. padding: 15px;
  405. background-color: #fff;
  406. }
  407. .weight-right {
  408. padding-right: 10rpx;
  409. font-size: 14rpx;
  410. }
  411. .weight-tip {
  412. color: gray;
  413. font-size: 12rpx;
  414. }
  415. .select-item {
  416. :deep(.uni-forms-item__content) {
  417. display: flex !important;
  418. }
  419. :deep(.uni-forms-item__label) {
  420. width: auto !important;
  421. }
  422. button {
  423. height: 35px;
  424. font-size: 16rpx;
  425. margin-left: 10rpx;
  426. }
  427. }
  428. .button-group {
  429. margin-top: 15px;
  430. display: flex;
  431. flex-direction: row;
  432. justify-content: space-around;
  433. button {
  434. display: flex;
  435. align-items: center;
  436. justify-content: center;
  437. height: 35px;
  438. width: 50%;
  439. margin-left: 10px;
  440. font-size: 16rpx;
  441. }
  442. .uni-icons {
  443. margin-right: 10px;
  444. }
  445. }
  446. .history {
  447. display: flex;
  448. width: 100%;
  449. flex-direction: column;
  450. justify-items: start;
  451. .title {
  452. padding: 20rpx;
  453. font-size: 24rpx;
  454. font-weight: 600;
  455. }
  456. .type {
  457. padding-right: 20rpx;
  458. font-size: 24rpx;
  459. }
  460. .code {
  461. font-weight: 600;
  462. }
  463. .item {
  464. padding: 20rpx;
  465. font-size: 20rpx;
  466. color: #666;
  467. .status {
  468. padding-left: 20rpx;
  469. }
  470. .fail {
  471. font-weight: 600;
  472. color: #f00;
  473. }
  474. }
  475. .is-empty {
  476. text-align: center;
  477. margin: 40px 0;
  478. color: #999;
  479. }
  480. }
  481. </style>