1 line
5.8 KiB
PHP
Executable File
1 line
5.8 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\index\controller;
|
|
|
|
|
|
|
|
use app\index\model\User as UserModel;
|
|
|
|
use app\index\model\Setting;
|
|
|
|
use think\facade\View;
|
|
|
|
use think\facade\Session;
|
|
|
|
use think\captcha\facade\Captcha;
|
|
|
|
use hema\wechat\Driver;
|
|
|
|
use hema\sms\Driver as Sms;
|
|
|
|
use think\facade\Cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户认证
|
|
|
|
*/
|
|
|
|
class Passport extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 生成验证码
|
|
|
|
**/
|
|
|
|
public function captcha()
|
|
|
|
{
|
|
|
|
return Captcha::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户登录
|
|
|
|
*/
|
|
|
|
public function login()
|
|
|
|
{
|
|
|
|
if (!$this->request->isAjax()) {
|
|
|
|
// 验证登录状态
|
|
|
|
if (isset($this->user) AND (int)$this->user['is_login'] === 1) {
|
|
|
|
return redirect('/user');
|
|
|
|
}
|
|
|
|
View::layout(false);
|
|
|
|
$values = Setting::getItem('wxweb');
|
|
|
|
$wx = new Driver;
|
|
|
|
$ticket = $wx->qrcodeCreate();
|
|
|
|
View::assign('ticket', $ticket);
|
|
|
|
View::assign('time', time());
|
|
|
|
View::assign('app_id', $values['app_id']);
|
|
|
|
View::assign('key', 'user');
|
|
|
|
View::assign('title', '用户登录');
|
|
|
|
View::assign('description', '');
|
|
|
|
View::assign('keywords', '');
|
|
|
|
return View::fetch();
|
|
|
|
}
|
|
|
|
$model = new UserModel;
|
|
|
|
if (($model->login($this->postData('data'))) === false) {
|
|
|
|
return $this->renderError($model->getError() ?: '登录失败');
|
|
|
|
}
|
|
|
|
return $this->renderSuccess('登录成功', '/user');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户注册
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
if (!$this->request->isAjax()) {
|
|
|
|
// 验证登录状态
|
|
|
|
if (isset($this->user) AND (int)$this->user['is_login'] === 1) {
|
|
|
|
return redirect('/user');
|
|
|
|
}
|
|
|
|
View::layout(false);
|
|
|
|
$values = Setting::getItem('wxweb');
|
|
|
|
$wx = new Driver;
|
|
|
|
$ticket = $wx->qrcodeCreate();
|
|
|
|
View::assign('ticket', $ticket);
|
|
|
|
View::assign('time', time());
|
|
|
|
View::assign('app_id', $values['app_id']);
|
|
|
|
View::assign('key', 'user');
|
|
|
|
View::assign('title', '用户注册');
|
|
|
|
View::assign('description', '');
|
|
|
|
View::assign('keywords', '');
|
|
|
|
return View::fetch();
|
|
|
|
}
|
|
|
|
$model = new UserModel;
|
|
|
|
if (($model->login($this->postData('data'))) === false) {
|
|
|
|
return $this->renderError($model->getError() ?: '注册失败');
|
|
|
|
}
|
|
|
|
return $this->renderSuccess('注册成功', '/user');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 退出登录
|
|
|
|
*/
|
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
// 清空登录状态
|
|
|
|
Session::delete('hema_user');
|
|
|
|
return redirect('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 微信扫码登录
|
|
|
|
*/
|
|
|
|
public function wxlogin(string $code = '',$state = 0)
|
|
|
|
{
|
|
|
|
$wx = new Driver;
|
|
|
|
//通过code获取access_token
|
|
|
|
if(!$result = $wx->getWebToken($code)){
|
|
|
|
die(hema_json(['code' => 0, 'msg' => $wx->getError()]));
|
|
|
|
}
|
|
|
|
//获取用户信息
|
|
|
|
if(!$result = $wx->getUserinfo($result['openid'],$result['access_token'])){
|
|
|
|
die(hema_json(['code' => 0, 'msg' => $wx->getError()]));
|
|
|
|
}
|
|
|
|
if(!$user = UserModel::where('union_id',$result['unionid'])->where('status','>',10)->find()){
|
|
|
|
$user = new UserModel;
|
|
|
|
$user->save([
|
|
|
|
'union_id' => $result['unionid'],
|
|
|
|
'user_name' => time(),
|
|
|
|
'password' => hema_hash(get_captcha(6)),
|
|
|
|
'nickname' => preg_replace('/[\xf0-\xf7].{3}/', '', $result['nickname']),
|
|
|
|
'avatar' => $result['headimgurl'],
|
|
|
|
'gender' => $result['sex'],
|
|
|
|
'province' => $result['province'],
|
|
|
|
'city' => $result['city'],
|
|
|
|
'status' => 20, //商户管理
|
|
|
|
]);
|
|
|
|
$user = UserModel::where('union_id',$result['unionid'])->where('status','>',10)->find();
|
|
|
|
}else{
|
|
|
|
//如果之前没有生成账号和密码,这里重新生成
|
|
|
|
if(empty($user['user_name'])){
|
|
|
|
$user->save([
|
|
|
|
'user_name' => time(),
|
|
|
|
'password' => hema_hash('123456')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 保存登录状态
|
|
|
|
Session::set('hema_user', [
|
|
|
|
'user' => $user,
|
|
|
|
'is_login' => true,
|
|
|
|
]);
|
|
|
|
return redirect('/user');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 关注扫码登录
|
|
|
|
*/
|
|
|
|
public function qrlogin($data)
|
|
|
|
{
|
|
|
|
if(isset($data['union_id'])){
|
|
|
|
$filter['union_id'] = $data['union_id'];
|
|
|
|
}else{
|
|
|
|
$filter['open_id'] = $data['open_id'];
|
|
|
|
}
|
|
|
|
$user = UserModel::where($filter)->where('status','>',10)->find()->toArray();
|
|
|
|
// 保存登录状态
|
|
|
|
Session::set('hema_user', [
|
|
|
|
'user' => $user,
|
|
|
|
'is_login' => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 检测是否扫码
|
|
|
|
*/
|
|
|
|
public function checklogin($ticket='')
|
|
|
|
{
|
|
|
|
if($scan = Cache::get($ticket)){
|
|
|
|
Cache::delete($ticket);
|
|
|
|
return $this->renderSuccess('扫码成功','',$scan);
|
|
|
|
}
|
|
|
|
return $this->renderError('等待扫码');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 发动短信验证码
|
|
|
|
*/
|
|
|
|
public function sendsms($phone)
|
|
|
|
{
|
|
|
|
$values = Setting::getItem('sms');
|
|
|
|
if($values['gateway'] == ''){
|
|
|
|
return $this->renderError('未配置短信平台');
|
|
|
|
}
|
|
|
|
if($values['scene']['captcha'] == 0){
|
|
|
|
return $this->renderError('未开启短信验证码');
|
|
|
|
}
|
|
|
|
if($error = get_addons_status('sms'.$values['gateway'])){
|
|
|
|
return $this->renderError($error);
|
|
|
|
}
|
|
|
|
$sms = new Sms($values['gateway']);
|
|
|
|
$code = get_captcha();
|
|
|
|
if($sms->sendSms($phone, ['code' => $code])){
|
|
|
|
Cache::set($phone . '_' . $code,'sms_captcha',300);
|
|
|
|
return $this->renderSuccess('发送成功');
|
|
|
|
}
|
|
$error = $sms->getError() ?: '短信验证码发送失败';
|
|
return $this->renderError($error);
|
|
|
|
}
|
|
|
|
}
|
|
|