index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <view class="content">
  3. <view class="content scan-view">
  4. <view class="scan-btn-view">
  5. <view class="border-wrapper">
  6. <div class="scan-btn first"></div>
  7. <div class="scan-btn second"></div>
  8. </view>
  9. <button class="scan-btn" @click="scan">
  10. <uni-icons type="scan" size="40" color="white"></uni-icons>
  11. </button>
  12. </view>
  13. <view class="text-area">
  14. <text class="title">扫码</text>
  15. </view>
  16. <input
  17. class="inputSty"
  18. v-model="valiFormData.code"
  19. :focus="focusType"
  20. @blur="setfocus"
  21. @confirm="onsubmit"
  22. placeholder="请输入运单号"
  23. placeholder-style="font-size:12px"
  24. />
  25. <view class="input-view">
  26. <!-- <text v-if="valiFormData.code">
  27. Scan Result: {{valiFormData.code}}
  28. </text> -->
  29. <button :disabled="valiFormData.code.length === 0" class="submit-btn" @click="onsubmit" size="mini">
  30. <uni-icons type="checkmarkempty" size="12" :color="valiFormData.code.length === 0 ? 'gray' : 'white'"></uni-icons>
  31. <text :style="{ color: valiFormData.code.length === 0 ? 'gray' : 'white' }">提交</text>
  32. </button>
  33. </view>
  34. <view v-if="history.length > 0" class="history">
  35. <text class="title">记录</text>
  36. </view>
  37. </view>
  38. <view class="history">
  39. <view class="item" v-for="(item, i) in history">
  40. <text class="code" :style="{ color: item.status ? 'green' : '#666' }">{{ item.code }}</text>
  41. <uni-icons v-if="item.status" type="checkmarkempty" class="status" size="16" color="green"></uni-icons>
  42. <text class="status fail" v-else>F</text>
  43. <text style="margin-left: 10rpx; font-weight: 300">
  44. {{ '\r\n' + item.createTime }}
  45. </text>
  46. </view>
  47. </view>
  48. <uni-popup ref="message" type="message">
  49. <uni-popup-message :type="messageType" :message="messageText" :duration="2000"></uni-popup-message>
  50. </uni-popup>
  51. </view>
  52. </template>
  53. <script>
  54. import permision from '@/common/permission.js';
  55. import { ref, reactive, nextTick } from 'vue';
  56. import { warehouseScanURL } from '@/utils/api.js';
  57. export default {
  58. data() {
  59. const valiFormData = reactive({
  60. code: ''
  61. });
  62. const token = ref(null);
  63. return {
  64. messageType: '',
  65. messageText: '',
  66. valiFormData,
  67. token,
  68. submitFail: false,
  69. history: [],
  70. focusType: true,
  71. st: -1,
  72. hidePage: false
  73. };
  74. },
  75. onLoad() {
  76. // #ifdef APP-PLUS
  77. plus.key.addEventListener('keyup', this.keypress);
  78. // #endif
  79. // #ifdef H5
  80. document.addEventListener('keyup', this.keypress);
  81. // #endif
  82. },
  83. onUnload() {
  84. // #ifdef APP-PLUS
  85. plus.key.removeEventListener('keyup', this.keypress);
  86. // #endif
  87. // #ifdef H5
  88. document.removeEventListener('keyup', this.keypress);
  89. // #endif
  90. },
  91. onShow() {
  92. this.hidePage = false;
  93. this.token = uni.getStorageSync('token');
  94. },
  95. onHide() {
  96. this.hidePage = true;
  97. // #ifdef APP-PLUS
  98. plus.key.removeEventListener('keyup', this.keypress);
  99. // #endif
  100. // #ifdef H5
  101. document.removeEventListener('keyup', this.keypress);
  102. // #endif
  103. },
  104. onBackPress() {
  105. // #ifdef APP-PLUS
  106. plus.key.removeEventListener('keyup', this.keypress);
  107. // #endif
  108. // #ifdef H5
  109. document.removeEventListener('keyup', this.keypress);
  110. // #endif
  111. },
  112. methods: {
  113. async scan() {
  114. // #ifdef APP-PLUS
  115. let status = await this.checkPermission();
  116. if (status !== 1) {
  117. return;
  118. }
  119. // #endif
  120. uni.scanCode({
  121. success: (res) => {
  122. this.valiFormData.code = res.result;
  123. // this.$refs.alertDialog.open()
  124. this.onsubmit();
  125. },
  126. fail: (err) => {
  127. // 需要注意的是小程序扫码不需要申请相机权限
  128. }
  129. });
  130. },
  131. // #ifdef APP-PLUS
  132. async checkPermission(code) {
  133. let status = permision.isIOS ? await permision.requestIOS('camera') : await permision.requestAndroid('android.permission.CAMERA');
  134. if (status === null || status === 1) {
  135. status = 1;
  136. } else {
  137. uni.showModal({
  138. content: 'Camera permission required',
  139. confirmText: 'Setting',
  140. success: function (res) {
  141. if (res.confirm) {
  142. permision.gotoAppSetting();
  143. }
  144. }
  145. });
  146. }
  147. return status;
  148. },
  149. // #endif
  150. async warehouseScan(order_code) {
  151. this.st && clearTimeout(this.st);
  152. uni.request({
  153. url: warehouseScanURL,
  154. method: 'POST',
  155. header: {
  156. batoken: this.token
  157. },
  158. data: {
  159. order_code
  160. },
  161. success: (res) => {
  162. if (res.data.code == 1) {
  163. this.submitFail = false;
  164. this.messageType = 'success';
  165. this.messageText = 'Submit Successed!';
  166. this.$refs.message.open();
  167. const historyItem = {
  168. code: this.valiFormData.code,
  169. createTime: new Date(),
  170. status: true
  171. };
  172. this.history.unshift(historyItem);
  173. this.history.length > 10 && (this.history.length = 10);
  174. uni.setStorageSync('history', this.history);
  175. this.getHistory();
  176. } else {
  177. this.submitFail = true;
  178. this.messageType = 'error';
  179. this.messageText = res.data.msg;
  180. this.$refs.message.open();
  181. const historyItem = {
  182. code: this.valiFormData.code,
  183. createTime: new Date(),
  184. status: false
  185. };
  186. this.history.unshift(historyItem);
  187. this.history.length > 10 && (this.history.length = 10);
  188. uni.setStorageSync('history', this.history);
  189. this.getHistory();
  190. }
  191. this.st = setTimeout(() => {
  192. this.valiFormData.code = '';
  193. this.st && clearTimeout(this.st);
  194. }, 1000);
  195. }
  196. });
  197. },
  198. onsubmit() {
  199. this.warehouseScan(this.valiFormData.code);
  200. },
  201. getHistory() {
  202. this.history = uni.getStorageSync('history');
  203. },
  204. keypress(e) {
  205. console.log(e, '按键码');
  206. // 102 左侧 103 右侧 104 中间按键
  207. if (e.keyCode === 102 || e.keyCode === 103 || e.keyCode === 104) {
  208. //这里按键成功
  209. }
  210. if (e.keyCode == 66) {
  211. //enter按键
  212. //这里input已经拿到数据了,在这里把拿到的数据,通过接口数据联调起来
  213. this.onsubmit();
  214. }
  215. },
  216. setfocus() {
  217. if (this.hidePage) {
  218. return;
  219. }
  220. this.focusType = false;
  221. nextTick(() => {
  222. this.focusType = true;
  223. });
  224. }
  225. }
  226. };
  227. </script>
  228. <style lang="scss" scoped>
  229. .content {
  230. display: flex;
  231. flex-direction: column;
  232. align-items: center;
  233. justify-content: center;
  234. }
  235. .scan-view {
  236. background: white;
  237. width: 100%;
  238. position: fixed;
  239. position: sticky;
  240. top: 0px;
  241. }
  242. .scan-btn-view {
  243. margin-top: 200rpx;
  244. margin-bottom: 60rpx;
  245. display: flex;
  246. justify-content: center;
  247. align-items: center;
  248. position: relative;
  249. }
  250. .scan-btn {
  251. border-radius: 50%;
  252. width: 150rpx;
  253. height: 150rpx;
  254. display: flex;
  255. justify-content: center;
  256. align-items: center;
  257. background: rgba(0, 122, 255, 1);
  258. }
  259. .border-wrapper {
  260. position: absolute;
  261. left: 0rpx;
  262. top: 0rpx;
  263. width: 100%;
  264. height: 100%;
  265. display: block;
  266. }
  267. .scan-btn.first {
  268. position: absolute;
  269. left: 0px;
  270. top: 0px;
  271. width: 150rpx;
  272. height: 150rpx;
  273. background: rgba(0, 122, 255, 0.2);
  274. opacity: 0.1;
  275. border-radius: 50%;
  276. animation: first 3s infinite linear;
  277. }
  278. .scan-btn.second {
  279. position: absolute;
  280. left: 0px;
  281. top: 0px;
  282. width: 150rpx;
  283. height: 150rpx;
  284. background: rgba(0, 122, 255, 0.2);
  285. opacity: 0.1;
  286. border-radius: 50%;
  287. animation: first 3s infinite linear;
  288. animation-delay: 1s;
  289. }
  290. @keyframes first {
  291. 0% {
  292. transform: scale(1);
  293. opacity: 1;
  294. }
  295. 33% {
  296. opacity: 0.66;
  297. transform: scale(1.33);
  298. }
  299. 66% {
  300. opacity: 0.33;
  301. transform: scale(1.67);
  302. }
  303. 100% {
  304. opacity: 0;
  305. transform: scale(2);
  306. }
  307. }
  308. .text-area {
  309. display: flex;
  310. justify-content: center;
  311. }
  312. .inputSty {
  313. padding: 20rpx;
  314. width: 60%;
  315. border-bottom: #999 1rpx solid;
  316. }
  317. .input-view {
  318. margin-top: 50rpx;
  319. padding-left: 20rpx;
  320. padding-right: 20rpx;
  321. flex-direction: column;
  322. display: flex;
  323. justify-content: center;
  324. align-items: center;
  325. }
  326. .submit-btn {
  327. background: rgba(0, 170, 0, 1);
  328. }
  329. .history {
  330. display: flex;
  331. width: 100%;
  332. flex-direction: column;
  333. justify-items: start;
  334. .title {
  335. padding: 20rpx;
  336. font-size: 24rpx;
  337. font-weight: 600;
  338. }
  339. .code {
  340. font-weight: 600;
  341. }
  342. .item {
  343. padding: 20rpx;
  344. font-size: 20rpx;
  345. color: #666;
  346. .status {
  347. padding-left: 20rpx;
  348. }
  349. .fail {
  350. font-weight: 600;
  351. color: #f00;
  352. }
  353. }
  354. }
  355. </style>