From 6bef664d542a4c3b2991b6f46dba0f7d87bf133c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E9=A1=BA=E7=92=9E?= Date: Tue, 22 Jun 2021 11:33:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 12 +---------- pages/auth/auth.js | 50 ++++++++++++++++++++++++++++++++++++++++++++ pages/auth/auth.wxml | 2 +- pages/auth/auth.wxss | 7 +++++++ utils/api.js | 7 ++++--- 5 files changed, 63 insertions(+), 15 deletions(-) diff --git a/app.js b/app.js index 3c7e166..b553a30 100644 --- a/app.js +++ b/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 diff --git a/pages/auth/auth.js b/pages/auth/auth.js index c1ab869..1592348 100644 --- a/pages/auth/auth.js +++ b/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(){ + }, /** diff --git a/pages/auth/auth.wxml b/pages/auth/auth.wxml index 145f96e..0c90cda 100644 --- a/pages/auth/auth.wxml +++ b/pages/auth/auth.wxml @@ -15,7 +15,7 @@ - diff --git a/pages/auth/auth.wxss b/pages/auth/auth.wxss index 2b5ed0b..cfd06e3 100644 --- a/pages/auth/auth.wxss +++ b/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; } \ No newline at end of file diff --git a/utils/api.js b/utils/api.js index 9682a92..fe0b7d5 100644 --- a/utils/api.js +++ b/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