index.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <view class="page-container">
  3. <!-- 顶部导航 -->
  4. <view class="header">
  5. <view class="search-area">
  6. <view class="filter-box">
  7. <view v-for="(item, index) in statusList" :key="index"
  8. :class="['filter-item', item.selected ? 'filter-item-active' : '']" @click="toggleFilter(index)">
  9. {{ item.name }}
  10. </view>
  11. </view>
  12. </view>
  13. </view>
  14. <!-- 列表区域 -->
  15. <scroll-view class="list-container" scroll-y @scrolltolower="loadMore" @refresherrefresh="refresh" refresher-enabled
  16. :refresher-triggered="isRefreshing">
  17. <view v-if="filteredPoleList.length > 0">
  18. <view v-for="(item, index) in filteredPoleList" :key="index" class="pole-card">
  19. <view class="pole-number">{{ item.name+item.number }}</view>
  20. <view class="pole-info">
  21. <text :class="['pole-status', `status-${item.status}`]">
  22. {{ getStatusText(item.status) }}
  23. </text>
  24. <view class="light-control">
  25. <view>
  26. <text class="pole-light">灯光状态</text>
  27. <switch :checked="item.lightOn" @change="(e) => handleSwitchChange(e, index, item.number)"
  28. :disabled="item.status === 'offline'" color="#1890ff" class="light-switch" />
  29. </view>
  30. <view>
  31. <button class="pole-button" size="mini" @click="goDetail(item)" type="primary">查看详情</button>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. <!-- 空状态 -->
  38. <view v-else class="empty-state">
  39. <uni-icons type="info" size="50" color="#999" />
  40. <text class="empty-text">暂无数据</text>
  41. <button class="refresh-btn" @click="refresh">刷新</button>
  42. </view>
  43. </scroll-view>
  44. </view>
  45. </template>
  46. <script lang="ts" setup>
  47. import { ref, reactive, onMounted, computed } from 'vue';
  48. import { clientGet } from '../../utils/request';
  49. const BASE_URL = import.meta.env.VITE_APP_BASE_URL;
  50. const showSearch = ref(false);
  51. const isRefreshing = ref(false);
  52. const statusList = reactive<{ name : string, selected : boolean }[]>([
  53. { name: '在线', selected: false },
  54. { name: '离线', selected: false }
  55. ]);
  56. const poleList = ref<{ name ?: string, number : string, status : 'normal' | 'offline', lightOn : boolean }[]>([
  57. { name: '灯杆', number: '40005289', status: 'normal', lightOn: false },
  58. { name: '灯杆', number: '40005272', status: 'normal', lightOn: false },
  59. { name: '灯杆', number: '40005274', status: 'normal', lightOn: false },
  60. { name: '灯杆', number: '40005281', status: 'normal', lightOn: false },
  61. ]);
  62. const handleSwitchChange = async (e : any, index : number, poleNumber : string) => {
  63. if (poleList.value[index].status === 'offline') {
  64. uni.showToast({
  65. title: '设备离线无法操作',
  66. icon: 'none'
  67. });
  68. return;
  69. }
  70. let res;
  71. if (e.detail.value) {
  72. res = await turnLightOrTurnOff(poleNumber, true)
  73. } else {
  74. res = await turnLightOrTurnOff(poleNumber, false)
  75. }
  76. if(res){
  77. poleList.value[index].lightOn = e.detail.value;
  78. }else{
  79. uni.showToast({
  80. title:'操作失败',
  81. duration:1000
  82. })
  83. }
  84. };
  85. const turnLightOrTurnOff = async (poleNumber : string, isTurn : boolean) => {
  86. uni.showLoading({
  87. title:"正在操作..."
  88. })
  89. const res = await clientGet('/pole/instruct/issued/reportEnvironmentalData',{
  90. cmd: '6011',
  91. lightNums: poleNumber,
  92. packageId: '1050',
  93. brightness: (isTurn ? 100 : 0).toString()
  94. })
  95. uni.hideLoading();
  96. if ((res as any).code === 200) {
  97. uni.showToast({
  98. title: "操作成功",
  99. duration: 1000
  100. })
  101. return true;
  102. }
  103. return false;
  104. }
  105. const getStatusText = (status : string) => {
  106. const statusMap : Record<string, string> = {
  107. normal: '在线',
  108. offline: '离线'
  109. };
  110. return statusMap[status] || status;
  111. };
  112. const toggleSearch = () => {
  113. showSearch.value = !showSearch.value;
  114. };
  115. const toggleFilter = (index : number) => {
  116. statusList[index].selected = !statusList[index].selected;
  117. };
  118. const goBack = () => {
  119. uni.navigateBack();
  120. };
  121. const goDetail = (item : any) => {
  122. uni.setStorageSync("poleNumber",item.number);
  123. uni.navigateTo({
  124. url: `/pages/poleDetail/index`
  125. });
  126. };
  127. const loadMore = () => {
  128. // 加载更多数据
  129. console.log('load more');
  130. };
  131. const refresh = async () => {
  132. statusList.forEach(item => {
  133. item.selected = false;
  134. })
  135. isRefreshing.value = true;
  136. await getPoleListStatus();
  137. isRefreshing.value = false;
  138. };
  139. const getPoleStatus = async (poleNumber : string) => {
  140. const res = await clientGet("/pole/instruct/issued/equipmentStatus",{
  141. cmd: '6012',
  142. lightNums: poleNumber,
  143. packageId: '1050'
  144. });
  145. return res;
  146. };
  147. const getPoleListStatus = async () => {
  148. uni.showLoading({
  149. title: "正在获取最新数据...",
  150. mask: true,
  151. });
  152. try {
  153. const resList = [];
  154. for (let s of poleList.value) {
  155. console.log(s.number);
  156. const res = await getPoleStatus(s.number);
  157. resList.push(res);
  158. }
  159. poleList.value = resList.map(item => {
  160. const data = JSON.parse(JSON.parse(item.data).params);
  161. if (data.length < 1) {
  162. console.error('获取数据失败');
  163. uni.showToast({
  164. title: '获取数据失败',
  165. duration: 2000
  166. });
  167. return;
  168. }
  169. const light_num = data[0].light_num;
  170. const brightness = data[0].brightness;
  171. const onlineStatus = data[0].onlineStatus;
  172. const name = poleList.value.filter(item => {
  173. return item.number === light_num;
  174. })[0]?.name;
  175. const isLightOn = parseInt(brightness) === 100;
  176. const status = parseInt(onlineStatus) === 1;
  177. return ({
  178. name,
  179. number: light_num,
  180. status: status ? 'normal' : 'offline',
  181. lightOn: isLightOn
  182. });
  183. });
  184. } finally {
  185. uni.hideLoading();
  186. }
  187. };
  188. const filteredPoleList = computed(() => {
  189. const selectedStatuses = statusList.filter(item => item.selected).map(item => item.name);
  190. if (selectedStatuses.length === 0) {
  191. return poleList.value;
  192. }
  193. return poleList.value.filter(pole => {
  194. const statusText = getStatusText(pole.status);
  195. return selectedStatuses.includes(statusText);
  196. });
  197. });
  198. onMounted(() => {
  199. getPoleListStatus();
  200. });
  201. </script>
  202. <style scoped>
  203. page {
  204. height: 100%;
  205. }
  206. .page-container {
  207. height: 100%;
  208. display: flex;
  209. flex-direction: column;
  210. background-color: #f5f5f5;
  211. }
  212. .header {
  213. flex-shrink: 0;
  214. background-color: #ffffff;
  215. }
  216. .search-area {
  217. padding: 20rpx 30rpx;
  218. background-color: #ffffff;
  219. }
  220. .search-box {
  221. display: flex;
  222. align-items: center;
  223. background-color: #f5f5f5;
  224. padding: 16rpx 20rpx;
  225. border-radius: 8rpx;
  226. }
  227. .search-input-icon {
  228. width: 16px;
  229. height: 16px;
  230. margin-right: 16rpx;
  231. }
  232. .search-input {
  233. flex: 1;
  234. font-size: 14px;
  235. }
  236. .filter-box {
  237. display: flex;
  238. margin-top: 20rpx;
  239. gap: 20rpx;
  240. }
  241. .filter-item {
  242. padding: 10rpx 30rpx;
  243. border-radius: 30rpx;
  244. font-size: 14px;
  245. border: 1px solid #ddd;
  246. color: #666;
  247. }
  248. .filter-item-active {
  249. background-color: #e6f7ff;
  250. border-color: #1890ff;
  251. color: #1890ff;
  252. }
  253. .list-container {
  254. flex: 1;
  255. overflow: auto;
  256. padding: 20rpx;
  257. }
  258. .pole-card {
  259. background-color: #ffffff;
  260. border-radius: 12rpx;
  261. padding: 30rpx;
  262. margin-bottom: 20rpx;
  263. display: flex;
  264. justify-content: space-between;
  265. align-items: center;
  266. width: 650rpx;
  267. }
  268. .pole-number {
  269. font-size: 32rpx;
  270. font-weight: 600;
  271. color: #333;
  272. }
  273. .pole-info {
  274. display: flex;
  275. flex-direction: column;
  276. align-items: flex-end;
  277. gap: 10rpx;
  278. }
  279. .pole-status {
  280. font-size: 14px;
  281. padding: 4rpx 16rpx;
  282. border-radius: 4rpx;
  283. }
  284. .status-normal {
  285. background-color: #f6ffed;
  286. color: #52c41a;
  287. }
  288. .status-fault {
  289. background-color: #fff2f0;
  290. color: #ff4d4f;
  291. }
  292. .status-offline {
  293. background-color: #f5f5f5;
  294. color: #999;
  295. }
  296. .pole-light {
  297. font-size: 14px;
  298. color: #666;
  299. margin-right: 10rpx;
  300. }
  301. .light-control {
  302. display: flex;
  303. align-items: center;
  304. justify-content: space-between;
  305. flex-wrap: wrap;
  306. gap: 10rpx;
  307. }
  308. .light-state {
  309. font-size: 14px;
  310. color: #1890ff;
  311. }
  312. .light-icon {
  313. width: 16px;
  314. height: 16px;
  315. }
  316. .light-switch {
  317. transform: scale(0.8);
  318. }
  319. .empty-state {
  320. display: flex;
  321. flex-direction: column;
  322. align-items: center;
  323. justify-content: center;
  324. padding-top: 200rpx;
  325. }
  326. .empty-text {
  327. margin: 30rpx 0;
  328. font-size: 14px;
  329. color: #999;
  330. }
  331. .refresh-btn {
  332. width: 200rpx;
  333. height: 80rpx;
  334. line-height: 80rpx;
  335. font-size: 14px;
  336. color: #fff;
  337. background-color: #1890ff;
  338. border-radius: 40rpx;
  339. }
  340. </style>