From 8f662b216ef3fa8ea19240bf357827a95c5f6612 Mon Sep 17 00:00:00 2001 From: devil_gong Date: Fri, 24 May 2019 17:01:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=8E=88=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../index/public/success.html | 55 +++++++++++ .../weixinwebauthorization/index/Auth.php | 75 ++++++++++++--- .../weixinwebauthorization/install.sql | 2 + .../service/Service.php | 91 +++++++++++++++++++ 4 files changed, 210 insertions(+), 13 deletions(-) create mode 100644 application/plugins/view/weixinwebauthorization/index/public/success.html create mode 100644 application/plugins/weixinwebauthorization/install.sql create mode 100755 application/plugins/weixinwebauthorization/service/Service.php diff --git a/application/plugins/view/weixinwebauthorization/index/public/success.html b/application/plugins/view/weixinwebauthorization/index/public/success.html new file mode 100644 index 000000000..e462f9b55 --- /dev/null +++ b/application/plugins/view/weixinwebauthorization/index/public/success.html @@ -0,0 +1,55 @@ +{{include file="public/header" /}} + + +{{if isset($is_header) and $is_header eq 1}} + + {{include file="public/header_top_nav" /}} + + + {{include file="public/nav_search" /}} + + + {{include file="public/goods_category" /}} +{{/if}} + + + +
+
+
+
+ + {{$msg}} + +
+
+
+
+ + +{{include file="public/footer" /}} + +{{if !empty($data['body_html'])}} + {{$data.body_html|raw}} +{{/if}} + + \ No newline at end of file diff --git a/application/plugins/weixinwebauthorization/index/Auth.php b/application/plugins/weixinwebauthorization/index/Auth.php index 49ed723b7..a163a35b3 100644 --- a/application/plugins/weixinwebauthorization/index/Auth.php +++ b/application/plugins/weixinwebauthorization/index/Auth.php @@ -12,6 +12,7 @@ namespace app\plugins\weixinwebauthorization\index; use think\Controller; use app\service\PluginsService; +use app\plugins\weixinwebauthorization\service\Service; /** * 微信登录 - 登录授权 @@ -74,28 +75,69 @@ class Auth extends Controller return $this->fetch('public/tips_error'); } - // 本地获取access_token - // 远程获取access_token $ret = $this->RemoteAccessToken($params); + if($ret['code'] != 0) + { + $this->assign('msg', $ret['msg']); + return $this->fetch('public/tips_error'); + } - echo '
';
-        print_r($ret);
+        // 获取用户信息
+        $ret = $this->UserInfo($ret['data']);
+        if($ret['code'] != 0)
+        {
+            $this->assign('msg', $ret['msg']);
+            return $this->fetch('public/tips_error');
+        }
+
+        // 处理用户数据
+        $ret = Service::WeixinAuthReg($ret['data']);
+        if($ret['code'] == 0)
+        {
+            $this->assign('msg', $ret['msg']);
+            $this->assign('data', $ret['data']);
+            return $this->fetch('../../../plugins/view/weixinwebauthorization/index/public/success');
+        } else {
+            $this->assign('msg', $ret['msg']);
+            return $this->fetch('public/error');
+        }
     }
 
     /**
-     * 获取access_token
+     * 获取用户信息
      * @author  Devil
      * @blog    http://gong.gg/
      * @version 1.0.0
      * @date    2019-05-24
      * @desc    description
-     * @param   array           $params [description]
+     * @param    [array]          $params [输入参数]
      */
-    private function AccessToken($params = [])
+    private function UserInfo($params = [])
     {
-        
-        
+        // 参数校验
+        if(empty($params['access_token']))
+        {
+            return DataReturn('access_token为空', -1);
+        }
+        if(empty($params['openid']))
+        {
+            return DataReturn('openid为空', -1);
+        }
+
+        // 获取用户详细信息
+        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$params['access_token'].'&openid='.$params['openid'].'&lang=zh_CN';
+        $data = json_decode(file_get_contents($url), true);
+        if(empty($data['openid']))
+        {
+            if(empty($data['errmsg']))
+            {
+                return DataReturn('获取用户信息失败', -100);
+            } else {
+                return DataReturn($data['errmsg'], -100);
+            }
+        }
+        return DataReturn('获取成功', 0, $data);
     }
 
     /**
@@ -105,7 +147,7 @@ class Auth extends Controller
      * @version 1.0.0
      * @date    2019-05-24
      * @desc    description
-     * @param   array           $params [description]
+     * @param    [array]          $params [输入参数]
      */
     private function RemoteAccessToken($params = [])
     {
@@ -129,9 +171,16 @@ class Auth extends Controller
             // 获取access_token
             $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$ret['data']['appid'].'&secret='.$ret['data']['secret'].'&code='.$params['code'].'&grant_type=authorization_code';
             $data = json_decode(file_get_contents($url), true);
-
-            echo '
';
-            print_r($data);die;
+            if(empty($data['access_token']))
+            {
+                if(empty($data['errmsg']))
+                {
+                    return DataReturn('获取access_token失败', -100);
+                } else {
+                    return DataReturn($data['errmsg'], -100);
+                }
+            }
+            return DataReturn('获取成功', 0, $data);
 
         } else {
             return DataReturn($ret['msg'], -1);
diff --git a/application/plugins/weixinwebauthorization/install.sql b/application/plugins/weixinwebauthorization/install.sql
new file mode 100644
index 000000000..d6e7d3d7d
--- /dev/null
+++ b/application/plugins/weixinwebauthorization/install.sql
@@ -0,0 +1,2 @@
+# 用户
+ALTER TABLE `s_user` add `weixin_web_openid` char(60) NOT NULL DEFAULT '' COMMENT '微信web用户openid' after `weixin_openid`;
\ No newline at end of file
diff --git a/application/plugins/weixinwebauthorization/service/Service.php b/application/plugins/weixinwebauthorization/service/Service.php
new file mode 100755
index 000000000..8b4458c86
--- /dev/null
+++ b/application/plugins/weixinwebauthorization/service/Service.php
@@ -0,0 +1,91 @@
+$user['username'], 'pwd'=>$user['username']]);
+                if($ret['code'] == 0)
+                {
+                    return DataReturn('登录成功', 0, $ret['data']);
+                }
+            }
+        } else {
+            return DataReturn('用户openid为空', -1);
+        }
+
+        // 游客数据
+        $salt = GetNumberCode(6);
+        $data = [
+            'weixin_web_openid' => $params['openid'],
+            'username'          => $params['openid'],
+            'nickname'          => empty($params['nickname']) ? '' : $params['nickname'],
+            'gender'            => empty($params['sex']) ? 0 : (isset($params['sex']) && $params['sex'] == 1) ? 2 : 1,
+            'province'          => empty($params['province']) ? '' : $params['province'],
+            'city'              => empty($params['city']) ? '' : $params['city'],
+            'avatar'            => empty($params['headimgurl']) ? '' : $params['headimgurl'],
+            'status'            => 0,
+            'salt'              => $salt,
+            'pwd'               => LoginPwdEncryption($params['openid'], $salt),
+            'add_time'          => time(),
+            'upd_time'          => time(),
+        ];
+
+        // 数据添加
+        $ret = UserService::UserInsert($data, $params);
+        if($ret['code'] == 0)
+        {
+            // 用户登录session纪录
+            if(UserService::UserLoginRecord($ret['data']['user_id']))
+            {
+                return DataReturn('登录成功', 0, $ret['data']);
+            }
+        }
+        return DataReturn('登录失败', -100);
+    }
+}
+?>
\ No newline at end of file