index.vue 11 KB

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