Sfoglia il codice sorgente

完成首页查询

zhengqiang 5 anni fa
parent
commit
f7a544d605
9 ha cambiato i file con 203 aggiunte e 34 eliminazioni
  1. 2 1
      config/dev.env.js
  2. 7 3
      config/index.js
  3. 1 0
      package.json
  4. 2 2
      public/index.html
  5. 18 0
      src/api/remoteApi.js
  6. 132 27
      src/components/Home.vue
  7. 8 1
      src/main.js
  8. 9 0
      src/plugins/AxiosPlugin.js
  9. 24 0
      src/utils/request.js

+ 2 - 1
config/dev.env.js

@@ -3,5 +3,6 @@ const merge = require('webpack-merge')
 const prodEnv = require('./prod.env')
 
 module.exports = merge(prodEnv, {
-  NODE_ENV: '"development"'
+  NODE_ENV: '"development"',
+  BACKEND_URL:'"/epay-server"'
 })

+ 7 - 3
config/index.js

@@ -6,15 +6,19 @@ const path = require('path')
 
 module.exports = {
   dev: {
-
     // Paths
     assetsSubDirectory: 'static',
     assetsPublicPath: '/',
-    proxyTable: {},
+    proxyTable: {
+      '/epay-server': {
+        target: 'http://localhost:8080',
+	    	changeOrigin:true
+	  	}
+    },
 
     // Various Dev Server settings
     host: 'localhost', // can be overwritten by process.env.HOST
-    port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
+    port: 8083, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
     autoOpenBrowser: false,
     errorOverlay: true,
     notifyOnErrors: true,

+ 1 - 0
package.json

@@ -14,6 +14,7 @@
     "build": "node build/build.js"
   },
   "dependencies": {
+    "axios": "^0.19.0",
     "vant": "^2.2.14",
     "vue": "^2.5.2",
     "vue-router": "^3.0.1"

+ 2 - 2
public/index.html

@@ -3,9 +3,9 @@
   <head>
     <meta charset="utf-8">
     <meta http-equiv="X-UA-Compatible" content="IE=edge">
-    <meta name="viewport" content="width=device-width,initial-scale=1.0">
+    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
     <link rel="icon" href="<%= BASE_URL %>favicon.ico">
-    <title>ammeter</title>
+    <title>智能电表缴费</title>
   </head>
   <body>
     <noscript>

+ 18 - 0
src/api/remoteApi.js

@@ -0,0 +1,18 @@
+import request from '@/utils/request'
+
+var basePath = process.env.BACKEND_URL
+
+var remoteApi = {
+    queryRoomList : function(type,parentId){
+        return request.get(basePath + "/mobileApi/queryRoomList",{
+            params:{
+                type,parentId
+            }
+        });
+    },
+    queryPower: function(roomId){
+        return request.get(basePath + `/mobileApi/queryPower?roomId=${roomId}`);
+    }
+}
+
+export default remoteApi

+ 132 - 27
src/components/Home.vue

@@ -9,7 +9,7 @@
           请选择园区
         </van-cell>
         <van-dropdown-menu>
-          <van-dropdown-item v-model="value1" :options="option1" />
+          <van-dropdown-item v-model="selectedAreaId" :options="areaList" @change="areaChange"/>
         </van-dropdown-menu>
       </van-cell-group>
       <van-cell-group>
@@ -18,7 +18,7 @@
           请选择楼栋号
         </van-cell>
         <van-dropdown-menu>
-          <van-dropdown-item v-model="value1" :options="option2" />
+          <van-dropdown-item v-model="selectedBuildingId" :options="buildingList" @change="buildingChange"/>
         </van-dropdown-menu>
       </van-cell-group>
       <van-cell-group>
@@ -27,47 +27,152 @@
           请选择房间号
         </van-cell>
         <van-dropdown-menu>
-          <van-dropdown-item v-model="value1" :options="option3" />
+          <van-dropdown-item v-model="selectedRoomId" :options="roomList" @change="roomChange"/>
         </van-dropdown-menu>
       </van-cell-group>
       <div class="fyy-button">
-        <van-button type="primary">查询</van-button>
-        <p>剩余电量:8</p>
+        <van-button type="primary" @click="queryPower(selectedRoomId)">查询</van-button>
+        <p>
+          剩余电量:<span v-html="remainingPower" class="power"></span>
+          <span v-if="remainingPower!=null">度</span>
+          &nbsp;
+          当前状态:<span v-html="stateText"></span>
+        </p>
       </div>
     </div>
     <div class="fyyfooter">
-      <router-link to="/Pay">
-        <van-button type="bigred">充值</van-button>
-      </router-link>
+      <van-button type="bigred" @click="recharge()">充值</van-button>
     </div>
   </div>
 </template>
-
 <script>
+import remoteApi from '@/api/remoteApi'
+
 export default {
   data() {
     return {
-      value1: 0,
-      value2: "a",
-      option1: [
-        { text: "东校区", value: 0 },
-        { text: "西校区", value: 1 },
-        { text: "北校区", value: 2 }
-      ],
-      option2: [
-        { text: "1号楼", value: 0 },
-        { text: "2号楼", value: 1 },
-        { text: "3号楼", value: 2 }
-      ],
-      option3: [
-        { text: "501", value: 0 },
-        { text: "502", value: 1 },
-        { text: "503", value: 2 }
-      ]
+      remainingPower: null,
+      state:null,
+      selectedAreaId: null,
+      selectedBuildingId: null,
+      selectedRoomId: null,
+      areaList: [],
+      buildingList: [],
+      roomList: []
     };
+  },
+  computed: {
+    stateText (){
+      if(this.state==null){
+        return "未知";
+      }
+      else if(this.state==1){
+        return "断电";
+      }
+      else if(this.state==0){
+        return "通电";
+      }
+    }
+  },
+  mounted (){
+    var self = this
+
+    remoteApi.queryRoomList(1,null).then((resp)=>{
+      console.log(resp);
+
+      if(resp.result){
+
+        self.areaList = resp.data.map((e)=>{
+          return {
+            value : e.id,
+            text : e.name
+          }
+        });
+      }
+    });
+  },
+  methods: {
+    areaChange (value) {
+      var self = this
+
+      remoteApi.queryRoomList(2,value).then((resp)=>{
+        console.log(resp);
+
+        if(resp.result){
+          self.buildingList = resp.data.map((e)=>{
+            return {
+              value : e.id,
+              text : e.name
+            }
+          });
+        }
+      });
+    },
+    queryPower (roomId) {
+      var self = this
+
+      self.$toast.loading({
+        message: '加载中...',
+        forbidClick: true,
+        loadingType: 'spinner',
+        duration: 10000
+      });
+
+      remoteApi.queryPower(roomId).then((resp)=>{
+        console.log(resp);
+
+        self.$toast.clear();
+
+        if(resp.result){
+          self.$toast.success('查询成功!')
+
+          self.remainingPower = resp.data.remain
+          self.state = resp.data.state
+        }
+        else {
+          self.$toast.fail(resp.message)
+        }
+      });
+    },
+    buildingChange (value) {
+      var self = this
+
+      remoteApi.queryRoomList(3,value).then((resp)=>{
+        console.log(resp);
+
+        if(resp.result){
+          self.roomList = resp.data.map((e)=>{
+            return {
+              value : e.id,
+              text : e.name
+            }
+          });
+        }
+      });
+    },
+    roomChange (roomId) {
+      this.queryPower(roomId);
+    },
+    recharge (){
+      if(this.selectedRoomId == null) {
+        this.$toast.fail("请先选择房间!");
+      }
+      else{
+        this.$router.push({
+          path : '/Pay',
+          query: {
+            areaId : this.selectedAreaId,
+            buildingId: this.selectedBuildingId,
+            roomId : this.selectedRoomId 
+          }
+        });
+      }
+    }
   }
 };
 </script>
-
 <style scoped>
+.power{
+  font-size:150%;
+}
 </style>

+ 8 - 1
src/main.js

@@ -10,7 +10,11 @@ import { DropdownMenu, DropdownItem } from 'vant'
 import { Button } from 'vant'
 import { Collapse, CollapseItem } from 'vant'
 import { Field } from 'vant'
-import { Cell, CellGroup } from 'vant';
+import { Cell, CellGroup } from 'vant'
+import { Toast } from 'vant'
+
+import axios from 'axios'
+import AxiosPlugin from './plugins/AxiosPlugin'
 
 Vue.use(Cell).use(CellGroup);
 Vue.use(Field)
@@ -18,6 +22,9 @@ Vue.use(Collapse).use(CollapseItem)
 Vue.use(Button)
 Vue.use(NavBar)
 Vue.use(DropdownMenu).use(DropdownItem)
+Vue.use(Toast)
+
+Vue.use(AxiosPlugin)
 
 Vue.config.productionTip = false
 

+ 9 - 0
src/plugins/AxiosPlugin.js

@@ -0,0 +1,9 @@
+import request from '@/utils/request'
+
+// 安装axios作为vue插件
+export default ({
+  install: function (Vue, options) {
+    Vue.prototype.$http = request
+    Vue.http = request
+  }
+})

+ 24 - 0
src/utils/request.js

@@ -0,0 +1,24 @@
+import axios from 'axios'
+
+// post 跨域
+axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
+
+axios.interceptors.request.use(function (config) {
+  //config.headers['Authorization'] = getToken();
+  return config;
+},function (error) {
+  return Promise.reject(error);
+});
+
+axios.interceptors.response.use(
+  response=>{
+    console.log(response.data.code);
+    return response.data;
+  },
+  error => {
+    console.log(error);
+    return error;
+  }
+)
+
+export default axios;