Index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template>
  2. <!-- 顶部弹出 -->
  3. <van-popup v-model:show="showSearch" position="top" :close-on-click-overlay="true">
  4. <div style="padding: 16px;">
  5. <div style="background-color: #eee; border-radius: 2.5rem; margin: 0px 5px;">
  6. <van-field style="padding: 6px 16px;" v-model="keyWord" size="normal" clearable />
  7. </div>
  8. <div style="margin-top: 16px; display: flex;">
  9. <div style="flex-grow: 1;">
  10. <van-button type="primary" size="normal" round block @click="onSearch">搜索</van-button>
  11. </div>
  12. <div style="width: 10px;"></div>
  13. <div style="flex-grow: 1;">
  14. <van-button type="warning" size="normal" round block @click="() => {showSearch = false}">取消</van-button>
  15. </div>
  16. </div>
  17. </div>
  18. </van-popup>
  19. <!-- 顶部弹出 -->
  20. <van-popup v-model:show="showMore" position="top">
  21. <div style="padding: 16px;">
  22. <div>
  23. <van-button type="primary" size="normal" icon="scan" round block @click="onScan">扫码添加设备</van-button>
  24. </div>
  25. </div>
  26. </van-popup>
  27. <!-- 相机界面 -->
  28. <!-- <van-popup v-model:show="showScan" position="top">
  29. <div style="width: 100vw; height: 100vh; background-color: #ccc;">
  30. <QRScreen ref="scanQR" @success="scanSuccess"></QRScreen>
  31. </div>
  32. </van-popup> -->
  33. <!-- 扫描二维码的界面-相机界面 -->
  34. <div v-show="showScan" style="position: absolute;
  35. width: 100vw; height: 100vh; top: 0; left: 0;
  36. z-index: 99; background-color: #ccc;">
  37. <QRScreen ref="scanQR" @success="scanSuccess"></QRScreen>
  38. </div>
  39. <!-- 页面内容 -->
  40. <div style="display: flex; flex-direction:column;height: 100vh;">
  41. <!-- 店铺名称 -->
  42. <div style="padding: 10px 16px 10px 16px;">
  43. <div style="display: flex;
  44. justify-content:space-between;
  45. align-items:center;">
  46. <div style="display: flex; align-items: flex-end;">
  47. <div style="font-size: 20px; font-weight: bold;">
  48. <span>设备</span>
  49. </div>
  50. <div style="font-size: 0.9rem; margin-left: 5px;">
  51. <span>共{{ page.total }}台</span>
  52. </div>
  53. </div>
  54. <div style="background-color: #eee; border-radius: 2.5rem; width: 0px; flex-grow: 1; margin: 0px 5px;">
  55. </div>
  56. <div style="line-height: 0px;" @click="()=>{showSearch = true}">
  57. <van-icon name="static/images/icon-search-black.png" size="2rem" />
  58. </div>
  59. <div style="line-height: 0px;" @click="()=>{showMore = true}">
  60. <van-icon name="plus" size="1.5rem" />
  61. </div>
  62. </div>
  63. </div>
  64. <div ref="scrollRef" class="container" style="overflow-y: auto; flex-grow: 1;">
  65. <van-pull-refresh style="min-height: 100%;" v-model="refreshing" @refresh="onRefresh">
  66. <van-swipe-cell v-for="item in dataList" style="background-color: #fff; margin-bottom: 10px;">
  67. <template #right>
  68. <div style="width: 50px; height: 100%; background-color: red;
  69. display: flex; justify-content:center; align-items: center;">
  70. <div>
  71. <div style="color: #fff; font-weight: bold;">
  72. <span>删除</span>
  73. </div>
  74. </div>
  75. </div>
  76. </template>
  77. <van-cell clickable :to="'/device/detail/'+item.mac">
  78. <div style="text-align: left;">
  79. <!-- 第一行 -->
  80. <div style="display: flex; justify-content: space-between; margin-bottom: 5px;">
  81. <div style=" font-size: 0.9rem;">
  82. <span v-if="item.username" style="color: #000;">{{item.username}}</span>
  83. <span v-else>未知用户</span>
  84. </div>
  85. <div style="font-size: 0.9rem;">{{ item.productName }}</div>
  86. </div>
  87. <!-- 第二行 -->
  88. <div style="display: flex; align-items: flex-end;">
  89. <div style="color: #000; font-weight: bold; font-size: 1.1rem;">
  90. <span>{{ item.mac }}</span>
  91. </div>
  92. <div style="margin-left: 5px; margin-bottom: 2px;">
  93. <template v-if="getTimeSpan(item.modifytime) < 1200">
  94. <van-tag round size="medium" type="success">在线</van-tag>
  95. </template>
  96. <template v-else>
  97. <van-tag round size="medium" type="danger">离线</van-tag>
  98. </template>
  99. </div>
  100. </div>
  101. <!-- 第三行 -->
  102. <div style="display: flex; justify-content: space-between; margin-bottom: 5px;">
  103. <div style="font-size: 0.9rem; margin-left: 0px;">
  104. <span>{{ item.createtime }}</span>
  105. </div>
  106. <div style="font-size: 0.9rem; margin-left: 0px;">
  107. <span>{{item.modifytime}}</span>
  108. </div>
  109. </div>
  110. <!-- 第四行 -->
  111. <div style="display: flex; justify-content: space-between;">
  112. <div v-if="JSON.parse(item.data).lat" style="display: flex; color: #000;" @click.stop="onLocation(item.mac)">
  113. <div>
  114. <van-icon name="static/images/icon-location2.png" size="1.5rem" />
  115. </div>
  116. <div style="margin-left: 3px;">位置</div>
  117. </div>
  118. <div v-else>暂无定位</div>
  119. <div></div>
  120. <div style="display: flex; color: #000;" @click.stop="onLog(item.mac)">
  121. <div>
  122. <van-icon name="static/images/icon-log.png" size="1.5rem" />
  123. </div>
  124. <div style="margin-left: 3px;">传输日志</div>
  125. </div>
  126. <!-- <div style="display: flex; color: #000;">
  127. <div>
  128. <van-icon name="static/images/icon-edit.png" size="1.5rem" />
  129. </div>
  130. <div style="margin-left: 3px;">编辑</div>
  131. </div> -->
  132. </div>
  133. </div>
  134. </van-cell>
  135. </van-swipe-cell>
  136. <!-- 数据提示 -->
  137. <div>
  138. <div v-if="refreshing"></div>
  139. <div v-else-if="dataList.length == 0" style="text-align: center; padding: 20px; color: #ccc;">
  140. <span>暂无数据</span>
  141. </div>
  142. <div v-else-if="isLoading" style="text-align: center; padding: 20px;">
  143. <van-loading type="spinner" color="#1989fa" />
  144. </div>
  145. <div v-else-if="page.pages <= page.pageNum" style="text-align: center; padding: 20px; color: #ccc;">
  146. <span>没有更多了</span>
  147. </div>
  148. <div v-else style="text-align: center; padding: 20px;">
  149. <span @click="clickLoading">点击加载</span>
  150. </div>
  151. </div>
  152. <div style="height: 60px; "></div>
  153. </van-pull-refresh>
  154. <!-- 返回顶部 -->
  155. <van-back-top target=".container" right="5vw" bottom="80px" />
  156. </div>
  157. </div>
  158. </template>
  159. <script setup lang="ts">
  160. import { onMounted, reactive, ref, onActivated, nextTick } from 'vue';
  161. import { onBeforeRouteLeave, useRouter } from 'vue-router'
  162. import { post_promise } from '@/network/axios';
  163. import QRScreen from '@/components/QR/Index.vue';
  164. import { showToast } from 'vant';
  165. // import { showToast } from 'vant'
  166. // 弹出
  167. const showSearch = ref(false);
  168. const showMore = ref(false);
  169. const showScan = ref(false);
  170. const onSearch = () => {
  171. onRefresh();
  172. showSearch.value = false
  173. }
  174. const onScan = () => {
  175. showMore.value = false;
  176. showScan.value = true;
  177. scanQR.value.open();
  178. }
  179. // 扫码结果
  180. const scanQR = ref();
  181. const scanSuccess = (e: any) => {
  182. showScan.value = false
  183. console.log(e)
  184. showToast(e.decodedText);
  185. }
  186. // 路由
  187. const router = useRouter();
  188. const onLocation = (deviceId: string) => {
  189. console.log(deviceId)
  190. // 跳转到定位
  191. router.push('/device/location/' + deviceId)
  192. }
  193. // 跳转到日志
  194. const onLog = (deviceId: string) => {
  195. console.log(deviceId)
  196. // 跳转到定位
  197. router.push('/device/log/' + deviceId)
  198. }
  199. // 搜索关键字
  200. const keyWord = ref('');
  201. // 分页
  202. const page = reactive({
  203. total: 0,
  204. pageNum: 1,
  205. pageSize: 10,
  206. pages: 0,
  207. })
  208. const dataList = ref<any[]>([]);
  209. const refreshing = ref(false);
  210. const isLoading = ref(false);
  211. const finished = ref(false);
  212. // 获取数据
  213. const getData = () => {
  214. console.log('onload.....')
  215. post_promise({url: '/device/search', data: {
  216. size: page.pageSize,
  217. page: page.pageNum,
  218. sort: "createtime",
  219. order: "desc",
  220. keyWord: keyWord.value,
  221. mac: ''
  222. }})
  223. .then((result) => {
  224. console.log(result)
  225. // 刷新结束
  226. isLoading.value = false;
  227. refreshing.value = false;
  228. // 处理成功结果
  229. if(result.code == 0){
  230. // 下拉刷新时清空列表
  231. const dataArr:any[] = result.data;
  232. const total = result.total;
  233. const pages = Math.ceil(total / page.pageSize);
  234. console.log(pages);
  235. console.log(dataArr)
  236. if(page.pageNum == 1){
  237. dataList.value.splice(0, dataList.value.length);
  238. }
  239. dataList.value.push(...dataArr);
  240. page.pages = pages
  241. page.total = total
  242. console.log(dataList.value, pages)
  243. }
  244. },(result) => {
  245. console.log(result)
  246. isLoading.value = false;
  247. refreshing.value = false;
  248. // 跳转到首页
  249. }).catch((ex) => {
  250. console.log(ex)
  251. isLoading.value = false;
  252. refreshing.value = false;
  253. // 跳转到首页
  254. })
  255. };
  256. const onRefresh = () => {
  257. console.log('onrefresh...')
  258. console.log(refreshing.value)
  259. page.pageNum = 1;
  260. // 清空列表数据
  261. finished.value = false;
  262. // 重新加载数据
  263. // 将 loading 设置为 true,表示处于加载状态
  264. isLoading.value = true;
  265. getData();
  266. };
  267. const getTimeSpan = (time: string) => {
  268. const before = Date.parse(time);
  269. const now = new Date().getTime();
  270. return (now - before) / 1000;
  271. }
  272. // 生命周期
  273. onMounted(() => {
  274. console.log('onMounted');
  275. // const before = Date.parse("2011-10-10 14:48:00");
  276. // const now = Date.parse("2011-10-10 15:08:00");
  277. // console.log(before)
  278. // console.log(now)
  279. // console.log(now - before)
  280. getData();
  281. })
  282. // 页面切换时缓存滚动条
  283. const scrollRef = ref<any>(null)
  284. const scrollTop = ref(0)
  285. onActivated(() => {
  286. console.log('onActived...')
  287. nextTick(() => {
  288. scrollRef.value.scrollTop = scrollTop.value
  289. })
  290. })
  291. onBeforeRouteLeave((to, from, next) => {
  292. console.log(to, from);
  293. console.log('onBeforeRouteLeave...')
  294. scrollTop.value = scrollRef.value.scrollTop
  295. next();
  296. })
  297. // 点击加载
  298. const clickLoading = () => {
  299. console.log('clickLoading');
  300. page.pageNum++
  301. isLoading.value = true
  302. getData();
  303. }
  304. </script>
  305. <style scoped>
  306. </style>