瀏覽代碼

修改网络请求,获取用户信息逻辑

menc 2 年之前
父節點
當前提交
737f1a300e
共有 9 個文件被更改,包括 149 次插入95 次删除
  1. 48 17
      base/httputil.js
  2. 13 2
      pages/denglu/denglu.js
  3. 4 4
      pages/denglu/denglu.wxml
  4. 42 38
      pages/index/index.js
  5. 3 3
      pages/index/index.wxml
  6. 11 13
      pages/me/me.js
  7. 12 9
      pages/qiye/qiye.js
  8. 15 8
      pages/xuncha/xuncha.js
  9. 1 1
      pages/xunjian/xunjian.js

+ 48 - 17
base/httputil.js

@@ -75,7 +75,7 @@ function loginSuccess(res) {
       icon: "none"
     })
   } else {
-    app.globalToken = res.data.data.access_token
+    app.globalToken = res.data.access_token
     //登录成功,获取首页数据
     this.initIndexPage()
   }
@@ -95,6 +95,11 @@ function loginSuccess(res) {
 
 function send_post(url, data, successfun) {
   console.log("令牌:", app.globalToken)
+  // if(app.globalToken==null){
+  //   wx.reLaunch({
+  //     url: '../denglu/denglu',
+  //   })
+  // }
   //如果令牌为空或过期,需要重新请求
 
   wx.request({
@@ -108,8 +113,16 @@ function send_post(url, data, successfun) {
     success(res) {
       // var d = decodeURIComponent(decodeURIComponent(res.data.data))
       // var json = JSON.parse(d)
-      successfun(res)
+      console.log("TAG","返回数据:"+res.data)
+      authToken(res.data)
+      successfun(res.data)
+    },
+    fail(res){
+      wx.showToast({
+        title: "网络请求失败",
+      })
     }
+
   })
 }
 
@@ -124,25 +137,30 @@ function post(url, data, successfun) {
     data: data,
     success(res) {
       successfun(res.data)
-    }
-  })
-}
-
-function wxpost(url, data, successfun) {
-  wx.request({
-    url: BASE_Server + url, //仅为示例,并非真实的接口地址
-    method: "POST",
-    header: {
-      'content-type': 'application/json',
-      'Authorization': 'Bearer ' + app.globalToken
     },
-    data: data,
-    success(res) {
-      successfun(res.data)
+    fail(res){
+      wx.showToast({
+        title: "网络请求失败",
+      })
     }
   })
 }
 
+// function wxpost(url, data, successfun) {
+//   wx.request({
+//     url: BASE_Server + url, //仅为示例,并非真实的接口地址
+//     method: "POST",
+//     header: {
+//       'content-type': 'application/json',
+//       'Authorization': 'Bearer ' + app.globalToken
+//     },
+//     data: data,
+//     success(res) {
+//       successfun(res.data)
+//     }
+//   })
+// }
+
 function get(url, data, successfun) {
   wx.request({
     url: BASE_Server + url, //仅为示例,并非真实的接口地址
@@ -153,6 +171,11 @@ function get(url, data, successfun) {
     params: data,
     success(res) {
       successfun(res.data)
+    },
+    fail(res){
+      wx.showToast({
+        title: "网络请求失败",
+      })
     }
   })
 }
@@ -212,7 +235,7 @@ module.exports = {
   send_photo: send_photo,
   send: send,
   post: post,
-  wxpost: wxpost,
+  // wxpost: wxpost,
   get: get,
   send_postdecode: send_postdecode,
 
@@ -232,4 +255,12 @@ function showLoading() {
 
 function hideLoading() {
   wx.hideLoading()
+}
+
+function authToken(res){
+  if(res.code==300){
+    wx.reLaunch({
+      url: '../denglu/denglu',
+    })
+  }
 }

+ 13 - 2
pages/denglu/denglu.js

@@ -5,9 +5,20 @@ Page({
    * 页面的初始数据
    */
   data: {
-
+    userName:"",
+    password:""
+  },
+  bindViewlogin(){
+    console.log("要提交给服务器的用户名和密码是",this.data.userName,this.data.password);
+  },
+  
+  bindViewUserName(e){
+    this.data.userName = e.detail.value;
+  },
+  
+  bindViewPassword(e){
+    this.data.password = e.detail.value
   },
-
   /**
    * 生命周期函数--监听页面加载
    */

+ 4 - 4
pages/denglu/denglu.wxml

@@ -15,10 +15,10 @@
   <!-- 中间内容 -->
   <view class="form_con">
     <view class="form_vi">
-      <input class="weui-input"  placeholder="请输入用户名"/>
-      <input class="weui-input" password type="text" placeholder="这是一个密码输入框" />
-      <button type="" class="dl_btn">登录</button>
-      <button type="" class="qx_btn">取消</button>
+      <input class="weui-input"  placeholder="请输入用户名" bindinput="bindViewUserName"/>
+      <input class="weui-input" password type="text" placeholder="这是一个密码输入框" bindinput="bindViewPassword"/>
+      <button type="" class="dl_btn" bindtap="bindViewlogin">登录</button>
+      <!-- <button type="" class="qx_btn">取消</button> -->
     </view>
   </view>
   <view class="btm_tit">长春市二道应急局</view>

+ 42 - 38
pages/index/index.js

@@ -57,6 +57,10 @@ Page({
     })
   },
 
+  onShow(){
+    this.initIndexPage();
+  },
+
   // 待办跳转
   bindViewDaiBan() {
     // wx.navigateTo({
@@ -145,25 +149,26 @@ Page({
   //首页功能js-孙一石
   init_userInfo(e) {
     this.data.dofun = e.currentTarget.dataset.fun
-    let that = this
-    if (app.globalToken == null) {
-      http.showLoading()
-      wx.getUserProfile({
-        desc: 'desc',
-        success: (res) => {
-          this.getSysUserInfo(res.userInfo)
-        },
-        fail: res => {
-          console.log(res)
-        },
-
-      })
-    } else {
+    // let that = this
+    // if (app.globalToken == null) {
+    //   http.showLoading()
+    //   wx.getUserProfile({
+    //     desc: 'desc',
+    //     success: (res) => {
+    //       this.getSysUserInfo(res.userInfo)
+    //     },
+    //     fail: res => {
+    //       console.log(res)
+    //     },
+
+    //   })
+    // } else {
       this.callByName(this.data.dofun)
-    }
+    // }
   },
 
 
+
   callByName(name) {
     if (name == "bindViewDaiBan") {
       //待办
@@ -183,19 +188,18 @@ Page({
   },
   initIndexPageSuccess(res) {
     this.setData({
-      xunjianImgCount: res.data.xunjianImgCount
+      xunjianImgCount: res.xunjianImgCount
     })
-    http.hideLoading()
   },
 
   loginSuccess(res) {
-    if (res.data.code != 200) {
+    if (res.code != 200) {
       wx.showToast({
-        title: res.data.msg,
+        title: res.msg,
         icon: "none"
       })
     } else {
-      app.globalToken = res.data.data.access_token
+      app.globalToken = res.data.access_token
       //登录成功,获取首页数据
       this.initIndexPage()
     }
@@ -209,26 +213,26 @@ Page({
 
 
 
-  getSysUserInfo(info) {
-    let that = this
-    wx.login({
-      success(res) {
-        var code = res.code
+  // getSysUserInfo(info) {
+  //   let that = this
+  //   wx.login({
+  //     success(res) {
+  //       var code = res.code
 
 
-        var data = {
-          wxCode: code,
-          wxNickName: info.nickName,
-          wxAvatarUrl: info.avatarUrl
-        }
-        // http.send_post_login("/minapp/AppLoginController/appLogin",data)
-        http.send_post("/auth/applogin", data, that.loginSuccess)
-      },
-      fail(res) {
-        console.log("ffff", res)
-      }
-    })
-  },
+  //       var data = {
+  //         wxCode: code,
+  //         wxNickName: info.nickName,
+  //         wxAvatarUrl: info.avatarUrl
+  //       }
+  //       // http.send_post_login("/minapp/AppLoginController/appLogin",data)
+  //       http.send_post("/auth/applogin", data, that.loginSuccess)
+  //     },
+  //     fail(res) {
+  //       console.log("ffff", res)
+  //     }
+  //   })
+  // },
   onTabItemTap(item){
     console.log("$$$$$$$",item)
   }

+ 3 - 3
pages/index/index.wxml

@@ -15,15 +15,15 @@
   <view class="head_banner wid15"></view>
   <!-- 快速跳转按钮 -->
   <view class="nav">
-    <view class="item" bindtap="init_userInfo" data-fun="bindViewDaiBan">
+    <view class="item" bindtap="bindViewDaiBan" data-fun="bindViewDaiBan">
       <image class="img" src="{{host}}/images/nav_db.png" />
       <text>待办</text>
     </view>
-    <view class="item" bindtap="init_userInfo" data-fun="bindViewQiYe">
+    <view class="item" bindtap="bindViewQiYe" data-fun="bindViewQiYe">
       <image class="img" src="{{host}}/images/nav_qy.png" />
       <text>企业</text>
     </view>
-    <view class="item" bindtap="init_userInfo" data-fun="bindViewXunCha">
+    <view class="item" bindtap="bindViewXunCha" data-fun="bindViewXunCha">
       <image class="img" src="{{host}}/images/nav_xc.png" />
       <text>巡查</text>
     </view>

+ 11 - 13
pages/me/me.js

@@ -25,7 +25,6 @@ Page({
 
     let that = this
     if (app.globalToken == null) {
-      http.showLoading()
       wx.getUserProfile({
         desc: 'desc',
         success: (res) => {
@@ -59,24 +58,23 @@ Page({
     })
   },
   loginSuccess(res) {
-   console.log(res)
     let that = this
-    if (res.data.code != 200) {
-      that.openid = res.data.data
+    if (res.code != 200) {
+      that.openid = res.openid
       wx.showToast({
-        title: res.data.msg,
+        title: res.msg,
         icon: "none"
       })
-      http.hideLoading()
+      // http.hideLoading()
       that.setData({
         isShowAccoutDialog : true
       })
       
     } else {
-      app.globalToken = res.data.data.access_token
-      console.log("的令牌:",app.globalToken )
+      app.globalToken = res.data.access_token
+      console.log("登陆成功的令牌:",app.globalToken )
       //登录成功,获取首页数据
-      http.hideLoading()
+      // http.hideLoading()
       this.setData({
         isBindSuccess : true
       })
@@ -93,7 +91,7 @@ clear(){
       wx.removeStorageSync('openid');
       app.globalToken=null;
      this.setData({
-        isShowAccoutDialog : false,
+      isShowAccoutDialog : false,
       isBindSuccess : false,
       userInfo:null,
       
@@ -103,7 +101,7 @@ clear(){
      
      })
     wx.reLaunch({
-      url: '../index/index'
+      url: '../denglu/denglu'
     })
     },
   })
@@ -122,10 +120,10 @@ clear(){
   },
   modalSubmitSuccess(res){
     wx.showToast({
-      title: res.data.msg,
+      title: res.msg,
       icon: "none"
     })
-    if (res.data.code == 200) {
+    if (res.code == 200) {
       this.setData({
         isShowAccoutDialog : false,
         isBindSuccess : true

+ 12 - 9
pages/qiye/qiye.js

@@ -9,7 +9,6 @@ Page({
   
   },
   onShow(){
-    console.log("TAG","onShow");
   this.initQiyePage()
   },
   data: {
@@ -61,17 +60,21 @@ Page({
     http.send_post("/system/AppEnterpriseController/getInitEnterpriseList", null, this.getQiyeEnterpriseListSuccess)
   },
   getQiyePageSuccess(res) {
-    let enterpriseCount = res.data.enterpriseCount;
-    this.setData({
-      weekCount: enterpriseCount.weekCount,
-      monthCount: enterpriseCount.monthCount,
-      totalCount: enterpriseCount.totalCount,
-    })
+    if(res.code==200){
+      let enterpriseCount = res.enterpriseCount;
+      this.setData({
+        weekCount: enterpriseCount.weekCount,
+        monthCount: enterpriseCount.monthCount,
+        totalCount: enterpriseCount.totalCount,
+      })
+    }
+  
   },
   getQiyeEnterpriseListSuccess(res) {
+    if(res.code==200){
     this.setData({
-      enterpriseList: res.data.rows
-    })
+      enterpriseList: res.rows
+    })}
   },
   changeSearch(e) {
     console.log(e.detail.value)

+ 15 - 8
pages/xuncha/xuncha.js

@@ -38,19 +38,26 @@ Page({
     http.post("/system/AppXunchaController/getXunchaCount", null, this.getXunchaCountSuccess)
   },
   getXunchaCountSuccess(res) {
-    this.setData({
-      todayXunchaCount: res.xunchaCount.todayXunchaCount,
-      todayRiskCount: res.xunchaCount.todayRiskCount,
-      todayEnterpriseCount: res.xunchaCount.todayEnterpriseCount
-    })
+  
+    if(res.code==200){
+      this.setData({
+          todayXunchaCount: res.xunchaCount.todayXunchaCount,
+          todayRiskCount: res.xunchaCount.todayRiskCount,
+          todayEnterpriseCount: res.xunchaCount.todayEnterpriseCount
+        })
+    }
+  
   },
   getXunchaList() {
     http.post("/system/AppXunchaController/getXunchaList", null, this.getXunchaListSuccess)
   },
   getXunchaListSuccess(res) {
-    this.setData({
-      xunchaList: res.rows
-    })
+    if(res.code==200){
+       this.setData({
+        xunchaList: res.rows
+       })
+    }
+   
   },
 
 

+ 1 - 1
pages/xunjian/xunjian.js

@@ -340,7 +340,7 @@ Page({
         obj.longitude = res.longitude
         obj.xuanjianItems = that.data.xunjianItems
         obj.busEnterpriseId = that.data.busEnterpriseId
-        http.wxpost("/system/AppXunjianController/submitXunjian", obj, that.submitXujianSuccess)
+        http.send_post("/system/AppXunjianController/submitXunjian", obj, that.submitXujianSuccess)
       }
     })