index.vue 12 KB

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