index.vue 9.8 KB

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