微信小程序写完之后的真机调试获取不到数据的问题

2020-12-15 11:29发布

app.js中代码如下

import { Token } from 'utils/token-model.js';
var scence = 0;
App({
 onLaunch: function () {
   var that = this;
   var url = 'https://wx.knowdao.com/api/login/is_openid';
   wx.login({
     success: function (ress) {
       console.log(ress)
       var code = ress.code

       wx.request({
         url: url,
         method: 'POST',
         data: {
           code: code,
         },
         header: {
           "content-type": "application/x-www-form-urlencoded",
           'content-type': 'application/json'
         },
         success: function (res) {
           console.log(res)
           wx.setStorageSync('account', res.data.data.account);
           wx.setStorageSync('user_id', res.data.data.userid);
           wx.setStorageSync('openid', res.data.data.openid);
           wx.setStorageSync('session_key', res.data.data.session_key);
           wx.setStorage({
             key: 'openid',
             data: res.data.data.openid,
             success: function (res) {
               console.log(res)
             }
           })
           if (res.data.data.type == 1) {
             wx.redirectTo({
               url: '../login/login',
             })
           }
           // wx.getUserInfo({
           //   success: function (res) {
           //     console.log(res);
           //     var url = "https://wx.knowdao.com/api/login/update_userinfo";
           //     console.log(res.userInfo)
           //     wx.request({
           //       url: url,
           //       method: 'POST',
           //       data: {
           //         openid: res.data.data.openid,
           //         nickname: res.userInfo.nickName,
           //         avatarUrl: res.userInfo.avatarUrl,
           //         city: res.userInfo.city,
           //         gender: res.userInfo.gender,
           //       },
           //       header: {
           //         "content-type": "application/x-www-form-urlencoded",
           //         'content-type': 'application/json'
           //       },
           //       success: function (res) {
           //         console.log(res)
           //         if (res.data.status == 200) {
           //           console.log('success')
           //         } else {
           //           console.log('error')

           //         }
           //       }
           //     })
           //   }
           // })
         }
       })
     }
   })




 },
 onShow: function () {


   

 },
 onHide: function () {
   // this.globalData.scence = 1
   // wx.setStorageSync('scence', this.globalData.scence)
 },
 globalData: {
   account: '',
   stroge: 0,
   // user_id:null,
   userid:null,
   userInfo: null,
   times: null,
   urls: 'https://wx.knowdao.com',
   urlst: 'http://test.knowdao.com',
   token: ''
 }
})123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101

login.js中代码如下

const app = getApp()
import {
 Token
} from '../../utils/token-model.js';
Page({
 data: {
   //判断小程序的API,回调,参数,组件等是否在当前版本可用。
   canIUse: wx.canIUse('button.open-type.getUserInfo')
 },
 onLoad: function () {

   // var token = new Token();
   // token.verify();
   var that = this;
   //     console.log(res)
   //     if (res.authSetting['scope.userInfo']) {
   //       console.log('授权成功')
   //       wx.getUserInfo({
   //         success: function (res) {
   //           console.log(res)
   //           //从数据库获取用户信息
   //           that.queryUsreInfo();
   //           //用户已经授权过
   //           wx.switchTab({
   //             url: '../homepage/homepage'
   //           })
   //         }
   //       });
   //     }
   //   }
   // })
 },
 onShow: function () {

 },
 bindGetUserInfo: function (e) {
   if (e.detail.userInfo) {
     //用户按了允许授权按钮
     var that = this;
     var account = wx.getStorageSync('account')
     console.log(wx.getStorageSync('openid'))
     wx.getStorage({
       key: 'openid',
       success: function (res) {
         console.log(res)
         var openids = res
         var url = "https://wx.knowdao.com/api/login/get_openid";
         wx.request({
           url: url,
           method: 'POST',
           data: {
             account: wx.getStorageSync('account'),
             nickname: e.detail.userInfo.nickName,
             gender: e.detail.userInfo.gender,
             avatarUrl: e.detail.userInfo.avatarUrl,
             city: e.detail.userInfo.city,
             userid: wx.getStorageSync('user_id'),
             openid: openids.data,
           },
           header: {
             "content-type": "application/x-www-form-urlencoded",
             'content-type': 'application/json'
           },
           success: function (res) {
             console.log(res)

             if (res.data.status == 200) {
               wx.setStorageSync('token', res.data.data.token);
               wx.setStorageSync('success', 1);
               wx.switchTab({
                 url: '../homepage/homepage'
               })
             }
           }
         })
       }
     })

   } else {
     //用户按了拒绝按钮
     wx.showModal({
       title: '警告',
       content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
       showCancel: false,
       confirmText: '返回授权',
       success: function (res) {
         if (res.confirm) {
           console.log('用户点击了“返回授权”')
         }
       }
     })
   }
 },

 //获取用户信息接口
 queryUsreInfo: function () {
   wx.request({
     url: getApp().globalData.urlPath + 'hstc_interface/queryByOpenid',
     data: {
       openid: getApp().globalData.openid
     },
     header: {
       'content-type': 'application/json'
     },
     success: function (res) {
       console.log(res.data);
       getApp().globalData.userInfo = res.data;
     }
   })
 },

})123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114

token-model.js代码如下

import { Config } from 'config.js';
var app = getApp();
class Token {
 constructor() {
   console.log(this)
   // this.verifyUrl = Config.restUrl + '/api/validate/token';
   this.tokenUrl = Config.restUrl + '/api/login/get_openid';
   this.veriUrl = Config.restUrl + '/api/validate/token';
 }
 verify() {
   var token = wx.getStorageSync('token');
   console.log(token)
   if (!token) {
     this.getTokenFromServer(token);
   } else {
     this._verifyFromServer(token);
   }
 }
 // 携带令牌去服务器校验令牌
 _verifyFromServer(token) {
   console.log(11212)
   var that = this;
   const account = wx.getStorageSync("account")
   const tokenn = wx.getStorageSync("token")
   wx.request({
     url: that.veriUrl,
     method: 'POST',
     data: {
       token: tokenn,
       account: account
     },
     success: function (res) {
       console.log(res)

       var valid = res.data.data.token;
       console.log(valid)
       console.log(wx.getStorageSync('token'))
       if (wx.getStorageSync('token') !== vertoken ) {
         console.log(111111111111111111)
         // that.getTokenFromServer();
       }
     },
     fail: function(res){
       console.log(res)
     }
   })
 }
 changlink(url) {
   //建立连接
   console.log(url)
   wx.connectSocket({
     url: url,
   })
   //连接成功
   const userid = wx.getStorageSync('userid')
   wx.onSocketOpen(function () {
     const msg = {}
     msg.type = 'login';
     msg.user_id = userid;
     var b = JSON.stringify(msg)
     console.log(b)
     wx.sendSocketMessage({
       data: b,
     })
     console.log('连接成功');
   })
   wx.onSocketMessage(function (res) {
     console.log(res)

   })

 }


 // 从服务器获取token
 getTokenFromServer(callback) {
   var that = this;

   wx.login({
     success: function (res) {
       let code = res.code
       wx.getUserInfo({
         lang: "zh_CN",
         success: res => {
           let userInfo = res.userInfo
           wx.request({
             url: that.tokenUrl,
             method: 'POST',
             data: {
               code: code,
               nickname: userInfo.nickName,
               gender: userInfo.gender,
               avatarUrl: userInfo.avatarUrl,
               city: userInfo.city,
             },
             header: {
               "content-type": "application/x-www-form-urlencoded",
               'content-type': 'application/json'
             },
             success: function (res) {
               console.log(res)
               wx.setStorageSync('token', res.data.data.token);
               wx.setStorageSync('account', res.data.data.account);
               wx.setStorageSync('user_id', res.data.data.user_id);
               wx.setStorageSync('session_key', res.data.data.session_key);
               
             }
           })
         }
       })

     }
   })
   
 }

}

export { Token };123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119

confiag.js代码如下

class Config{
constructor() {

}
}

Config.restUrl = ‘https://wx.knowdao.com’;

export { Config };


作者:前端阿龙

链接:https://blog.csdn.net/weixin_42790916/article/details/82835454

来源:CSDN
著作权归作者所有,转载请联系作者获得授权,切勿私自转载。