index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 } 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. reset();
  247. st && clearTimeout(st);
  248. }, 1000);
  249. }
  250. });
  251. };
  252. const getHistory = () => {
  253. lotnoLogHistory.value = uni.getStorageSync('lotnoLogHistory') || [];
  254. };
  255. const keypress = (e: any) => {
  256. console.log(e, '按键码');
  257. // 102 左侧 103 右侧 104 中间按键
  258. if (e.keyCode === 102 || e.keyCode === 103 || e.keyCode === 104) {
  259. //这里按键成功
  260. }
  261. if (e.keyCode == 66) {
  262. //enter按键
  263. //这里input已经拿到数据了,在这里把拿到的数据,通过接口数据联调起来
  264. onsubmit();
  265. }
  266. };
  267. onLoad(() => {
  268. // #ifdef APP-PLUS
  269. plus.key.addEventListener('keyup', keypress);
  270. // #endif
  271. // #ifdef H5
  272. document.addEventListener('keyup', keypress);
  273. // #endif
  274. });
  275. onUnload(() => {
  276. // #ifdef APP-PLUS
  277. plus.key.removeEventListener('keyup', keypress);
  278. // #endif
  279. // #ifdef H5
  280. document.removeEventListener('keyup', keypress);
  281. // #endif
  282. });
  283. onHide(() => {
  284. hidePage.value = true;
  285. // #ifdef APP-PLUS
  286. plus.key.removeEventListener('keyup', keypress);
  287. // #endif
  288. // #ifdef H5
  289. document.removeEventListener('keyup', keypress);
  290. // #endif
  291. });
  292. onBackPress(() => {
  293. // #ifdef APP-PLUS
  294. plus.key.removeEventListener('keyup', keypress);
  295. // #endif
  296. // #ifdef H5
  297. document.removeEventListener('keyup', keypress);
  298. // #endif
  299. });
  300. onShow(() => {
  301. token.value = uni.getStorageSync('token');
  302. getBindParams();
  303. getHistory();
  304. });
  305. const getBindParams = () => {
  306. uni.request({
  307. url: getBindParamsURL,
  308. method: 'GET',
  309. header: {
  310. batoken: token.value
  311. },
  312. success: (res: any) => {
  313. if (res.data.code === 1) {
  314. options.batch = res.data.data.batch_number.map((item: any) => {
  315. return { text: item.name, value: item.id };
  316. });
  317. function transformData(data: any[]) {
  318. return data.map((item) => {
  319. const { id, value, label, ...rest } = item;
  320. return {
  321. id: id || value,
  322. value: value,
  323. text: label,
  324. ...rest,
  325. children: item.children ? transformData(item.children) : []
  326. };
  327. });
  328. }
  329. const shipping = transformData(res.data.data.shipping);
  330. options.pallet = shipping;
  331. }
  332. },
  333. fail(e) {
  334. console.log('fail--', e);
  335. }
  336. });
  337. };
  338. onNavigationBarButtonTap((event) => {
  339. if (event.index === 0) {
  340. uni.navigateTo({
  341. url: '/pages/scanLotno/lotnoLog'
  342. });
  343. }
  344. });
  345. </script>
  346. <style lang="scss">
  347. .container {
  348. padding: 15px;
  349. background-color: #fff;
  350. }
  351. .weight-right {
  352. padding-right: 10rpx;
  353. font-size: 14rpx;
  354. }
  355. .weight-tip {
  356. color: gray;
  357. font-size: 12rpx;
  358. }
  359. .button-group {
  360. margin-top: 15px;
  361. display: flex;
  362. flex-direction: row;
  363. justify-content: space-around;
  364. button {
  365. display: flex;
  366. align-items: center;
  367. justify-content: center;
  368. height: 35px;
  369. width: 50%;
  370. margin-left: 10px;
  371. font-size: 16rpx;
  372. }
  373. .uni-icons {
  374. margin-right: 10px;
  375. }
  376. }
  377. .history {
  378. display: flex;
  379. width: 100%;
  380. flex-direction: column;
  381. justify-items: start;
  382. .title {
  383. padding: 20rpx;
  384. font-size: 24rpx;
  385. font-weight: 600;
  386. }
  387. .type {
  388. padding-right: 20rpx;
  389. font-size: 24rpx;
  390. }
  391. .code {
  392. font-weight: 600;
  393. }
  394. .item {
  395. padding: 20rpx;
  396. font-size: 20rpx;
  397. color: #666;
  398. .status {
  399. padding-left: 20rpx;
  400. }
  401. .fail {
  402. font-weight: 600;
  403. color: #f00;
  404. }
  405. }
  406. .is-empty {
  407. text-align: center;
  408. margin: 40px 0;
  409. color: #999;
  410. }
  411. }
  412. </style>