index.nvue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <view class="tiaoxingma" :style="[{minHeight:windowHeight + 'px'}]" >
  3. <view class="header" >
  4. <text>将二维码对准摄像头</text>
  5. </view>
  6. <view class="center border">
  7. <Ba-ScanViewS ref="scanViewS" style="height: 200; width: 100%" :load="loadData" @onScan="onScan"> </Ba-ScanViewS>
  8. </view>
  9. <view class="" style="margin-bottom: 20rpx;" >
  10. <text>成功录入:{{cgLength}}条</text>
  11. </view>
  12. <scroll-view scroll-y="true" class="footer msg-center">
  13. <view class="msg-list" :class="item.type === 'res'?'cg':''" v-for="(item,index) in msgList">
  14. <text style="color: #999; text-align: center; font-size: 20rpx;" >{{item.time}}</text>
  15. <view class="msg-li" >
  16. <text >MAC码{{item.MAC}} - </text>
  17. <text v-if="item.type === 'res'" class="cg" >{{item.title}}</text>
  18. <text v-else-if="item.type === 'err'" class="sb" >{{item.title}}</text>
  19. <text v-else >{{item.title}}</text>
  20. </view>
  21. </view>
  22. </scroll-view>
  23. <view class="butScan" style="margin-bottom: 20rpx;" >
  24. <text class="textScan" @click="openScan"> 打开扫描</text>
  25. <text class="textScan" @click="closeScan" style="border: 1px solid red; color: red;" >关闭扫描</text>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. export default {
  31. data() {
  32. return {
  33. loadData: {
  34. //配置
  35. scanTimeSpace: 1000, //时间间隔
  36. //isDirScanMulti: true,//是否直接返回多码,默认fasle(弹窗多码标识,用户自己选择)
  37. //rpCancelBtnText: 'cancel',
  38. isShowToast: false,
  39. },
  40. msgList: [],
  41. newList: {},
  42. luType: 'zhengchang',
  43. cgLength :0,
  44. windowHeight: 0
  45. }
  46. },
  47. onReady() {
  48. // 获取屏幕高度
  49. uni.getSystemInfo({
  50. success: (res) => {
  51. this.windowHeight = res.windowHeight
  52. }
  53. })
  54. },
  55. onLoad() {
  56. let that = this
  57. //监听扫码结果(vue2和vue3都支持)
  58. var globalEvent = uni.requireNativePlugin('globalEvent')
  59. globalEvent.addEventListener('baScanViewEvent', function (e) {
  60. if (that.luType === 'zhengchang') {
  61. let newMac = e.detail.result.split(',')
  62. let objMac = newMac.reduce((acc, value, index) => {
  63. let newMac = value.split('=')
  64. acc[newMac[0]] = newMac[1];
  65. return acc;
  66. }, {});
  67. objMac.title='准备录入中...'
  68. objMac.time= that.formatDate(new Date())
  69. that.luType = 'loading'
  70. // console.log(objMac, 'objMac--------')
  71. // console.log('onScan:' + e.detail.result)
  72. that.msgList.unshift(objMac)
  73. that.newList = e.detail.result
  74. that.onRequest(objMac)
  75. // 请求接口录入路由器信息
  76. // console.log('baScanViewEvent' + JSON.stringify(e))
  77. } else if (that.luType === 'loading') {
  78. uni.showToast({
  79. title: '有数据正在录入...',
  80. icon: 'none'
  81. })
  82. }
  83. })
  84. },
  85. methods: {
  86. onScan(e) {
  87. //扫码监听(仅支持vue2,vue3请使用globalEvent方式)
  88. console.log('onScan:' + e.detail.result)
  89. console.log('onScan:' + e.detail.results) //多个
  90. this.msgList.unshift(JSON.stringify(e.detail))
  91. },
  92. // 定义格式化函数
  93. formatDate(date) {
  94. // 获取年份、月份和日期
  95. const year = date.getFullYear();
  96. const month = String(date.getMonth() + 1).padStart(2, '0'); // +1因为getMonth()返回的是0-11
  97. const day = String(date.getDate()).padStart(2, '0');
  98. // 获取小时、分钟和秒
  99. const hours = String(date.getHours()).padStart(2, '0');
  100. const minutes = String(date.getMinutes()).padStart(2, '0');
  101. const seconds = String(date.getSeconds()).padStart(2, '0');
  102. // 返回格式化后的日期字符串
  103. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  104. },
  105. onRequest(data) {
  106. if (!data.MAC) {
  107. uni.showToast({
  108. title: '二维码数据异常,没有MAC数据',
  109. icon: 'none'
  110. })
  111. return
  112. }
  113. let that = this
  114. that.luType === 'loading'
  115. uni.request({
  116. url: 'https://api.hninpop.com/api/addbox?routerboxmac='+data.MAC, // 你的API接口
  117. method: 'GET', // 请求方法,根据需要可能是 'GET', 'POST' 等
  118. data: {
  119. routerboxmac: data.MAC
  120. // 这里放置需要传递给服务器的数据
  121. },
  122. header: {
  123. // 设置请求的 header,例如:'Content-Type': 'application/json'
  124. },
  125. success: (res) => {
  126. // 请求成功的回调函数
  127. if (res.data.Status === 0) {
  128. that.cgLength++
  129. let objMac = {
  130. title: '录入成功',
  131. type: 'res',
  132. MAC: data.MAC,
  133. }
  134. objMac.time= that.formatDate(new Date())
  135. that.msgList.unshift(objMac)
  136. } else {
  137. let objMac = {
  138. title: '录入失败 ' + res.data.Message,
  139. type: 'err',
  140. MAC: data.MAC,
  141. }
  142. objMac.time= that.formatDate(new Date())
  143. that.msgList.unshift(objMac)
  144. }
  145. console.log(res.data.Status, 'success');
  146. },
  147. fail: (err) => {
  148. let objMac = {
  149. title: '录入失败',
  150. type: 'err',
  151. MAC: data.MAC,
  152. }
  153. objMac.time= that.formatDate(new Date())
  154. that.msgList.unshift(objMac)
  155. // 请求失败的回调函数
  156. console.error(err, 'fail');
  157. },
  158. complete: (value) => {
  159. console.log(value, 'value');
  160. that.luType = 'zhengchang'
  161. // 请求结束的回调函数(无论成功或失败都会执行)
  162. }
  163. });
  164. },
  165. openScanLight() {
  166. //打开闪光灯
  167. this.$refs.scanViewS.openScanLight()
  168. },
  169. closeScanLight() {
  170. //关闭闪光灯
  171. this.$refs.scanViewS.closeScanLight()
  172. },
  173. isLightOn() {
  174. //是否打开闪光灯
  175. this.$refs.scanViewS.isLightOn((res) => {
  176. uni.showToast({
  177. title: 'isLightOn:' + res.isLightOn,
  178. icon: 'none'
  179. })
  180. })
  181. },
  182. openAlbumPage() {
  183. //相册识别
  184. this.$refs.scanViewS.openAlbumPage()
  185. },
  186. openScan() {
  187. //打开扫描
  188. this.$refs.scanViewS.openScan()
  189. this.isScanOpen()
  190. },
  191. closeScan() {
  192. //关闭扫描
  193. this.$refs.scanViewS.closeScan()
  194. this.isScanOpen()
  195. },
  196. isScanOpen() {
  197. //是否打开扫描
  198. this.$refs.scanViewS.isScanOpen((res) => {
  199. uni.showToast({
  200. title: res.isScanOpen? '已打开扫描':'已关闭扫描',
  201. icon: 'none'
  202. })
  203. })
  204. }
  205. }
  206. }
  207. </script>
  208. <style lang="scss" scoped>
  209. .tiaoxingma {
  210. height: 100vh;
  211. padding: 40rpx 40rpx;
  212. border: 1px solid #eee;
  213. overflow: auto;
  214. padding-top: 100rpx;
  215. .border{
  216. border: 1px solid #333;
  217. }
  218. .center{
  219. height: 380rpx;
  220. margin: 20rpx 0rpx;
  221. }
  222. .footer{
  223. height: 800rpx;
  224. overflow-y: auto; /* 在垂直方向上启用滚动 */
  225. .msg-list{
  226. background: #eee;
  227. padding: 8rpx 20rpx;
  228. border-radius: 10rpx;
  229. margin-bottom: 10rpx;
  230. }
  231. .msg-li{
  232. display: flex;
  233. flex-direction: row;
  234. }
  235. }
  236. .cg{
  237. color: green;
  238. }
  239. .sb{
  240. color: red;
  241. }
  242. .butScan{
  243. margin-top: 20rpx;
  244. display: flex;
  245. flex-direction: row;
  246. justify-content: space-between;
  247. padding: 0rpx 70rpx;
  248. .textScan{
  249. border: 1px solid green;
  250. color: green;
  251. border-radius: 10rpx;
  252. padding: 20rpx 40rpx;
  253. }
  254. }
  255. }
  256. </style>