提交 8f662b21 编写于 作者: D devil_gong

微信授权

上级 7d07a871
{{include file="public/header" /}}
<!-- header nav start -->
{{if isset($is_header) and $is_header eq 1}}
<!-- header top nav -->
{{include file="public/header_top_nav" /}}
<!-- search -->
{{include file="public/nav_search" /}}
<!-- goods category -->
{{include file="public/goods_category" /}}
{{/if}}
<!-- header nav end -->
<!-- conntent start -->
<div class="am-g my-content">
<div class="am-u-md-6 am-u-sm-centered">
<div class="am-panel am-radius tips-success">
<div class="am-panel-bd">
<i class="am-icon-check-circle am-icon-sm"></i>
<span class="msg">{{$msg}}</span>
<div class="tips-nav">
<a href="{{$Think.__MY_URL__}}" class="am-btn am-btn-secondary am-radius">回到首页</a>
</div>
</div>
</div>
</div>
</div>
<!-- conntent end -->
{{include file="public/footer" /}}
{{if !empty($data['body_html'])}}
{{$data.body_html|raw}}
{{/if}}
<script type="text/javascript">
{{if isset($is_parent) and $is_parent eq 1}}
setTimeout(function()
{
if(self.frameElement && self.frameElement.tagName == "IFRAME")
{
parent.location.reload();
}else{
window.location.href='{{$Think.__MY_URL__}}';
}
}, 1500);
{{else /}}
setTimeout(function()
{
window.location.href='{{$Think.__MY_URL__}}';
}, 1500);
{{/if}}
</script>
\ No newline at end of file
......@@ -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');
}
// 获取用户信息
$ret = $this->UserInfo($ret['data']);
if($ret['code'] != 0)
{
$this->assign('msg', $ret['msg']);
return $this->fetch('public/tips_error');
}
echo '<pre>';
print_r($ret);
// 处理用户数据
$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 '<pre>';
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);
......
# 用户
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
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\plugins\weixinwebauthorization\service;
use think\Db;
use app\service\UserService;
use app\service\PluginsService;
/**
* 微信登录服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class Service
{
/**
* 微信登录注册
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-12-03
* @desc description
* @param [array] $params [输入参数]
*/
public static function WeixinAuthReg($params = [])
{
// 获取登录用户
$user = UserService::LoginUserInfo();
if(!empty($user))
{
return DataReturn('已登录,请先退出', -1);
}
// 是否有登录纪录
if(!empty($params['openid']))
{
$user = UserService::UserInfo('weixin_web_openid', $params['openid']);
if(!empty($user))
{
// 用户登录
$ret = UserService::Login(['accounts'=>$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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册