index.vue 9.6 KB

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