From 1474fffba7246b845a631a5bee0a33b81826ebb4 Mon Sep 17 00:00:00 2001 From: devil_gong Date: Fri, 25 Jan 2019 10:54:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=8E=88=E6=9D=83=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/User.php | 26 ++++++++++++++++++-------- application/service/UserService.php | 18 ++++++++++++++++-- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/application/api/controller/User.php b/application/api/controller/User.php index 6a681915a..3c28fc5f4 100755 --- a/application/api/controller/User.php +++ b/application/api/controller/User.php @@ -131,16 +131,26 @@ class User extends Common */ public function WechatUserInfo() { - $result = (new \base\Wechat(MyC('common_app_mini_weixin_appid'), MyC('common_app_mini_weixin_appsecret')))->DecryptData(input('encrypted_data'), input('iv'), input('openid')); + // 参数 + $params = input(); - if(is_array($result)) + // 先从数据库获取用户信息 + $user = UserService::UserInfo('weixin_openid', $params['openid']); + if(empty($user)) { - $result['nick_name'] = isset($result['nickName']) ? $result['nickName'] : ''; - $result['avatar'] = isset($result['avatarUrl']) ? $result['avatarUrl'] : ''; - $result['gender'] = empty($result['gender']) ? 0 : ($result['gender'] == 2) ? 1 : 2; - $result['openid'] = $result['openId']; - $result['referrer']= isset($this->data_post['referrer']) ? intval($this->data_post['referrer']) : 0; - return UserService::AuthUserProgram($result, 'weixin_openid'); + $result = (new \base\Wechat(MyC('common_app_mini_weixin_appid'), MyC('common_app_mini_weixin_appsecret')))->DecryptData($params['encrypted_data'], $params['iv'], $openid); + + if(is_array($result)) + { + $result['nick_name'] = isset($result['nickName']) ? $result['nickName'] : ''; + $result['avatar'] = isset($result['avatarUrl']) ? $result['avatarUrl'] : ''; + $result['gender'] = empty($result['gender']) ? 0 : ($result['gender'] == 2) ? 1 : 2; + $result['openid'] = $result['openId']; + $result['referrer']= isset($this->data_post['referrer']) ? intval($this->data_post['referrer']) : 0; + return UserService::AuthUserProgram($result, 'weixin_openid'); + } + } else { + return DataReturn('授权成功', 0, $user); } return DataReturn(empty($result) ? '获取用户信息失败' : $result, -100); } diff --git a/application/service/UserService.php b/application/service/UserService.php index 60c8a8bd1..3b561d2ba 100755 --- a/application/service/UserService.php +++ b/application/service/UserService.php @@ -1383,8 +1383,7 @@ class UserService 'city' => empty($params['city']) ? '' : $params['city'], 'referrer' => isset($params['referrer']) ? intval($params['referrer']) : 0, ]; - $where = [$field=>$params['openid'], 'is_delete_time'=>0]; - $user = Db::name('User')->where($where)->find(); + $user = self::UserInfo($field, $params['openid']); if(!empty($user)) { $data = $user; @@ -1394,6 +1393,21 @@ class UserService return DataReturn('授权成功', 0, $data); } + /** + * 根据字段获取用户信息 + * @author Devil + * @blog http://gong.gg/ + * @version 1.0.0 + * @date 2019-01-25 + * @desc description + * @param [string] $field [字段名称] + * @param [string] $value [字段值] + */ + public static function UserInfo($field, $value) + { + return Db::name('User')->where([$field=>$value, 'is_delete_time'=>0])->find(); + } + /** * app用户注册 * @author Devil