index.vue 10 KB

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