微信小程式寫完之後的真機除錯獲取不到資料的問題
阿新 • • 發佈:2018-12-11
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: '' } })
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; } }) }, })
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 };
confiag.js程式碼如下
class Config{
constructor() {
}
}
export { Config };