index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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">
  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">
  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. const res = options.pallet.find(item => item.value === valiFormData.value.palletNum)
  173. return res?.text
  174. })
  175. const checkOrder = () => {
  176. loading.value = true;
  177. uni.setStorageSync('waybills', []);
  178. uni.request({
  179. url: getWaybillsURL,
  180. method: 'POST',
  181. header: {
  182. batoken: token.value
  183. },
  184. data: {
  185. order_no: valiFormData.value.orderNum,
  186. batch_number: valiFormData.value.batchNum,
  187. pallet_number: valiFormData.value.palletNum ? findPalletNumIdByValue(options.pallet, valiFormData.value.palletNum) : ''
  188. },
  189. success: (res : any) => {
  190. loading.value = false;
  191. if (res.data.code == 1) {
  192. messageType.value = 'success';
  193. messageText.value = res.data.msg;
  194. messageRef.value.open();
  195. uni.setStorageSync('waybills', res.data.data.waybills);
  196. uni.navigateTo({
  197. url: '/pages/scanLotno/waybillsList'
  198. });
  199. } else {
  200. messageType.value = 'error';
  201. messageText.value = res.data.msg;
  202. messageRef.value.open();
  203. }
  204. }
  205. });
  206. };
  207. const findPalletNumIdByValue = (data : any, targetValue : any) => {
  208. for (let i = 0; i < data.length; i++) {
  209. const item = data[i];
  210. // 检查当前项的 label
  211. if (item.id === targetValue || item.value === targetValue) {
  212. return [item.id];
  213. } else if (item.children && item.children.length > 0) {
  214. const childId = findPalletNumIdByValue(item.children, targetValue) as any;
  215. if (childId !== null) {
  216. return childId;
  217. }
  218. }
  219. }
  220. return null;
  221. };
  222. const onsubmit = () => {
  223. st && clearTimeout(st);
  224. loading.value = true;
  225. uni.request({
  226. url: bindShippingURL,
  227. method: 'POST',
  228. header: {
  229. batoken: token.value
  230. },
  231. data: {
  232. order_no: valiFormData.value.orderNum,
  233. reweighting: valiFormData.value.weight,
  234. batch_number: valiFormData.value.batchNum,
  235. pallet_number: valiFormData.value.palletNum ? findPalletNumIdByValue(options.pallet, valiFormData.value.palletNum) : ''
  236. },
  237. success: (res : any) => {
  238. loading.value = false;
  239. if (res.data.code == 1) {
  240. messageType.value = 'success';
  241. messageText.value = res.data.msg;
  242. messageRef.value.open();
  243. if (res.data.data.labels && res.data.data.labels.length > 0) {
  244. console.log('有打印面单');
  245. // res.data.data.labels.map((url: string) => {
  246. // let path = fullUrl(url);
  247. // window.open(path, '_blank');
  248. // });
  249. }
  250. const historyItem = {
  251. orderNum: valiFormData.value.orderNum,
  252. createTime: new Date(),
  253. type: valiFormData.value.batchNum ? '绑定批次' : '绑定托盘',
  254. status: true
  255. };
  256. console.log('lotnoLogHistory--', lotnoLogHistory);
  257. lotnoLogHistory.value.unshift(historyItem);
  258. uni.setStorageSync('lotnoLogHistory', lotnoLogHistory.value);
  259. getHistory();
  260. } else {
  261. messageType.value = 'error';
  262. messageText.value = res.data.msg;
  263. messageRef.value.open();
  264. const historyItem = {
  265. orderNum: valiFormData.value.orderNum,
  266. createTime: new Date(),
  267. type: valiFormData.value.batchNum ? '绑定批次' : '绑定托盘',
  268. status: false
  269. };
  270. lotnoLogHistory.value.unshift(historyItem);
  271. uni.setStorageSync('lotnoLogHistory', lotnoLogHistory.value);
  272. getHistory();
  273. }
  274. st = setTimeout(() => {
  275. setFocus();
  276. reset();
  277. setFocus();
  278. st && clearTimeout(st);
  279. }, 1000);
  280. }
  281. });
  282. };
  283. const getHistory = () => {
  284. lotnoLogHistory.value = uni.getStorageSync('lotnoLogHistory') || [];
  285. };
  286. const keypress = (e : any) => {
  287. console.log(e, '按键码');
  288. // 102 左侧 103 右侧 104 中间按键
  289. if (e.keyCode === 102 || e.keyCode === 103 || e.keyCode === 104) {
  290. //这里按键成功
  291. }
  292. if (e.keyCode == 66) {
  293. //enter按键
  294. //这里input已经拿到数据了,在这里把拿到的数据,通过接口数据联调起来
  295. onsubmit();
  296. }
  297. };
  298. onLoad(() => {
  299. // #ifdef APP-PLUS
  300. plus.key.addEventListener('keyup', keypress);
  301. // #endif
  302. // #ifdef H5
  303. document.addEventListener('keyup', keypress);
  304. // #endif
  305. });
  306. onUnload(() => {
  307. // #ifdef APP-PLUS
  308. plus.key.removeEventListener('keyup', keypress);
  309. // #endif
  310. // #ifdef H5
  311. document.removeEventListener('keyup', keypress);
  312. // #endif
  313. });
  314. onHide(() => {
  315. hidePage.value = true;
  316. // #ifdef APP-PLUS
  317. plus.key.removeEventListener('keyup', keypress);
  318. // #endif
  319. // #ifdef H5
  320. document.removeEventListener('keyup', keypress);
  321. // #endif
  322. });
  323. onBackPress(() => {
  324. // #ifdef APP-PLUS
  325. plus.key.removeEventListener('keyup', keypress);
  326. // #endif
  327. // #ifdef H5
  328. document.removeEventListener('keyup', keypress);
  329. // #endif
  330. });
  331. onShow(() => {
  332. hidePage.value = false;
  333. token.value = uni.getStorageSync('token');
  334. getBindParams();
  335. getHistory();
  336. });
  337. const setFocus = () => {
  338. if (hidePage.value) {
  339. return;
  340. }
  341. focusType.value = false;
  342. nextTick(() => {
  343. focusType.value = true;
  344. });
  345. };
  346. const getBindParams = () => {
  347. uni.request({
  348. url: getBindParamsURL,
  349. method: 'GET',
  350. header: {
  351. batoken: token.value
  352. },
  353. success: (res : any) => {
  354. if (res.data.code === 1) {
  355. options.batch = res.data.data.batch_number.map((item : any) => {
  356. return { text: item.name, value: item.id };
  357. });
  358. function transformData(data : any[]) {
  359. return data.map((item) => {
  360. const { id, value, label, ...rest } = item;
  361. return {
  362. id: id || value,
  363. value: value,
  364. text: label,
  365. ...rest,
  366. children: item.children ? transformData(item.children) : []
  367. };
  368. });
  369. }
  370. const shipping = transformData(res.data.data.shipping);
  371. options.pallet = shipping;
  372. }
  373. },
  374. fail(e) {
  375. console.log('fail--', e);
  376. }
  377. });
  378. };
  379. onNavigationBarButtonTap((event) => {
  380. if (event.index === 0) {
  381. uni.navigateTo({
  382. url: '/pages/scanLotno/lotnoLog'
  383. });
  384. }
  385. });
  386. </script>
  387. <style lang="scss">
  388. .container {
  389. padding: 15px;
  390. background-color: #fff;
  391. }
  392. .weight-right {
  393. padding-right: 10rpx;
  394. font-size: 14rpx;
  395. }
  396. .weight-tip {
  397. color: gray;
  398. font-size: 12rpx;
  399. }
  400. .button-group {
  401. margin-top: 15px;
  402. display: flex;
  403. flex-direction: row;
  404. justify-content: space-around;
  405. button {
  406. display: flex;
  407. align-items: center;
  408. justify-content: center;
  409. height: 35px;
  410. width: 50%;
  411. margin-left: 10px;
  412. font-size: 16rpx;
  413. }
  414. .uni-icons {
  415. margin-right: 10px;
  416. }
  417. }
  418. .history {
  419. display: flex;
  420. width: 100%;
  421. flex-direction: column;
  422. justify-items: start;
  423. .title {
  424. padding: 20rpx;
  425. font-size: 24rpx;
  426. font-weight: 600;
  427. }
  428. .type {
  429. padding-right: 20rpx;
  430. font-size: 24rpx;
  431. }
  432. .code {
  433. font-weight: 600;
  434. }
  435. .item {
  436. padding: 20rpx;
  437. font-size: 20rpx;
  438. color: #666;
  439. .status {
  440. padding-left: 20rpx;
  441. }
  442. .fail {
  443. font-weight: 600;
  444. color: #f00;
  445. }
  446. }
  447. .is-empty {
  448. text-align: center;
  449. margin: 40px 0;
  450. color: #999;
  451. }
  452. }
  453. </style>