From a11b758d70aea559232384603d7d0597de55f1e1 Mon Sep 17 00:00:00 2001 From: gongfuxiang <2499232802@qq.com> Date: Thu, 17 Jan 2019 01:24:37 +0800 Subject: [PATCH] wechat mini login --- application/api/controller/User.php | 2 +- public/appmini/old/weixin/app.js | 81 +++++++++---------- .../appmini/old/weixin/pages/login/login.js | 8 +- 3 files changed, 44 insertions(+), 47 deletions(-) diff --git a/application/api/controller/User.php b/application/api/controller/User.php index d6220d95c..6a681915a 100755 --- a/application/api/controller/User.php +++ b/application/api/controller/User.php @@ -142,7 +142,7 @@ class User extends Common $result['referrer']= isset($this->data_post['referrer']) ? intval($this->data_post['referrer']) : 0; return UserService::AuthUserProgram($result, 'weixin_openid'); } - return DataReturn('获取用户信息失败', -100); + return DataReturn(empty($result) ? '获取用户信息失败' : $result, -100); } /** diff --git a/public/appmini/old/weixin/app.js b/public/appmini/old/weixin/app.js index 9f8acf363..26f4167e4 100755 --- a/public/appmini/old/weixin/app.js +++ b/public/appmini/old/weixin/app.js @@ -60,7 +60,7 @@ App({ // 请求地址 request_url: "{{request_url}}", - //request_url: 'https://test.shopxo.net/', + request_url: 'https://test.shopxo.net/', // 基础信息 application_title: "{{application_title}}", @@ -192,8 +192,9 @@ App({ * 用户登录 * object 回调操作对象 * method 回调操作对象的函数 + * auth_data 授权数据 */ - user_auth_login(object, method) { + user_auth_login(object, method, auth_data) { wx.showLoading({ title: "授权中..." }); var $this = this; wx.checkSession({ @@ -201,9 +202,9 @@ App({ var openid = wx.getStorageSync($this.data.cache_user_login_key); if ((openid || null) == null) { - $this.user_login(object, method); + $this.user_login(object, method, auth_data); } else { - $this.get_user_login_info(object, method, openid); + $this.get_user_login_info(object, method, openid, auth_data); } }, fail: function () { @@ -216,8 +217,9 @@ App({ * 用户登录 * object 回调操作对象 * method 回调操作对象的函数 + * auth_data 授权数据 */ - user_login(object, method) { + user_login(object, method, auth_data) { var $this = this; wx.login({ success: (res) => { @@ -234,7 +236,7 @@ App({ key: $this.data.cache_user_login_key, data: res.data.data }); - $this.get_user_login_info(object, method, res.data.data); + $this.get_user_login_info(object, method, res.data.data, auth_data); } else { wx.hideLoading(); $this.showToast(res.data.msg); @@ -259,48 +261,41 @@ App({ * object 回调操作对象 * method 回调操作对象的函数 * openid 用户openid + * auth_data 授权数据 */ - get_user_login_info(object, method, openid) { + get_user_login_info(object, method, openid, auth_data) { var $this = this; - wx.getUserInfo({ - withCredentials: true, - success: function (res) { - // 远程解密数据 - wx.request({ - url: $this.get_request_url('WechatUserInfo', 'user'), - method: 'POST', - data: { encrypted_data: res.encryptedData, iv: res.iv, openid: openid }, - dataType: 'json', - header: { 'content-type': 'application/x-www-form-urlencoded' }, - success: (res) => { - wx.hideLoading(); - if (res.data.code == 0) { - wx.setStorage({ - key: $this.data.cache_user_info_key, - data: res.data.data, - success: (res) => { - if (typeof object === 'object' && (method || null) != null) { - object[method](); - } - }, - fail: () => { - $this.showToast('用户信息缓存失败'); - } - }); - } else { - $this.showToast(res.data.msg); + + // 远程解密数据 + wx.request({ + url: $this.get_request_url('WechatUserInfo', 'user'), + method: 'POST', + data: { encrypted_data: auth_data.encryptedData, iv: auth_data.iv, openid: openid }, + dataType: 'json', + header: { 'content-type': 'application/x-www-form-urlencoded' }, + success: (res) => { + wx.hideLoading(); + if (res.data.code == 0) { + wx.setStorage({ + key: $this.data.cache_user_info_key, + data: res.data.data, + success: (res) => { + if (typeof object === 'object' && (method || null) != null) { + object[method](); + } + }, + fail: () => { + $this.showToast('用户信息缓存失败'); } - }, - fail: () => { - wx.hideLoading(); - $this.showToast('服务器请求出错'); - }, - }); + }); + } else { + $this.showToast(res.data.msg); + } }, - fail: (e) => { + fail: () => { wx.hideLoading(); - $this.showToast('获取用户信息失败'); - } + $this.showToast('服务器请求出错'); + }, }); }, diff --git a/public/appmini/old/weixin/pages/login/login.js b/public/appmini/old/weixin/pages/login/login.js index 4151300e2..e92f513a8 100755 --- a/public/appmini/old/weixin/pages/login/login.js +++ b/public/appmini/old/weixin/pages/login/login.js @@ -30,15 +30,17 @@ Page({ * 登录授权事件 */ get_user_info_event(e) { - this.user_auth_code(); + console.log(e); + this.user_auth_code(null, null, e.detail); }, /** * 用户授权 * object 回调操作对象 * method 回调操作对象的函数 + * auth_data 授权数据 */ - user_auth_code(object, method) { + user_auth_code(object, method, auth_data) { // 请求授权接口 var $this = this; wx.getSetting({ @@ -46,7 +48,7 @@ Page({ if (!res.authSetting['scope.userInfo']) { $this.setData({ user: null}); } else { - app.user_auth_login($this, 'user_auth_back_event'); + app.user_auth_login($this, 'user_auth_back_event', auth_data); } }, fail: (e) => { -- GitLab