Browse Source

接口调试

master
向顺璞 4 years ago
parent
commit
6bef664d54
  1. 12
      app.js
  2. 50
      pages/auth/auth.js
  3. 2
      pages/auth/auth.wxml
  4. 7
      pages/auth/auth.wxss
  5. 7
      utils/api.js

12
app.js

@ -7,17 +7,7 @@ App({
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
// 登录
wx.login({
success: res => {
// 发送 res.code 到后台换取 openId, sessionKey, unionId
$api.getUserAuth(res.code)
.then(res => {
//请求成功
console.log(res)
})
}
})
},
globalData: {
userInfo: null

50
pages/auth/auth.js

@ -1,4 +1,5 @@
// pages/auth/auth.js
const $api = require('../../utils/api').API;
Page({
/**
@ -8,11 +9,60 @@ Page({
},
bindGetUserInfo: function (e){
if (e.detail.userInfo) {
//用户按了允许授权按钮
var that = this;
let info = e.detail.userInfo;
that.queryUsreInfo();
console.log(info);
return;
//授权成功后,跳转进入小程序首页
wx.switchTab({
url: '/pages/index/index'
})
} else {
//用户按了拒绝按钮
wx.showModal({
title:'警告',
content:'您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
showCancel:false,
confirmText:'返回授权',
success:function(res){
if (res.confirm) {
console.log('用户点击了“返回授权”')
}
}
})
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var that = this;
// 查看是否授权
wx.getSetting({
success: function (res) {
if (res.authSetting['scope.userInfo']) {
wx.getUserInfo({
success: function (res) {
//从数据库获取用户信息
that.queryUsreInfo();
//用户已经授权过
// wx.switchTab({
// url: '/pages/index/index'
// })
}
});
}
}
})
},
queryUsreInfo(){
},
/**

2
pages/auth/auth.wxml

@ -15,7 +15,7 @@
</view>
</view>
</view>
<button class="login">
<button class="login" type='primary' open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">
<image class="wx-log"></image>
<text class="wx-text">微信用户一键登录</text>
</button>

7
pages/auth/auth.wxss

@ -16,4 +16,11 @@
.log{
width: 150rpx;
height: 150rpx;
}
.login {
width: 667rpx;
height: 79rpx;
display: flex;
align-items: center;
justify-content: center;
}

7
utils/api.js

@ -9,7 +9,8 @@ const DELETE = 'DELETE';
function request(method, url, data) {
return new Promise(function (resolve, reject) {
let header = {
'content-type': 'application/json'
'content-type': 'application/json',
'Authorization': 'Basic YXBwOmFwcA==',
};
wx.request({
url: baseURL + url,
@ -36,8 +37,8 @@ function request(method, url, data) {
const API = {
getOpenid: (data) => request(GET, `auth/oauth/token?grant_type=password&username=${data.admin}&password=${data.password}`),
getUserAuth: (data) => request(POST, `auth/social/token`, data)
getOpenid: (data) => request(GET, `oauth/token?grant_type=password&username=${data.admin}&password=${data.password}`),
getUserAuth: (data) => request(POST, `social/token?code=MINI@${data}`)
};
module.exports = {
API: API

Loading…
Cancel
Save