LoginController.php
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* @remark :
* @name :LoginController.php
* @author :lyh
* @method :post
* @time :2023/8/19 9:11
*/
namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Helper\Common;
use App\Helper\Translate;
use App\Helper\Wechat;
use App\Http\Logic\Bside\User\UserLoginLogic;
use App\Models\Service\Service;
use App\Models\Sms\SmsLog;
use App\Models\User\User as UserModel;
use App\Utils\EncryptUtils;
use Mrgoon\AliSms\AliSms;
class LoginController extends BaseController
{
/**
* @remark :登录
* @name :login
* @author :lyh
* @method :post
* @time :2023/8/19 9:12
*/
public function login(){
$this->request->validate([
'mobile'=>['required', 'regex:/^1[3-9]\d{9}$/'],
'password'=>['required'],
],[
'mobile.required'=>'电话号码必须填写',
'password.required'=>'内容必须填写',
'mobile.regex' => '请输入正确的手机号码',
]);
$userLogic = new UserLoginLogic();
$res = $userLogic->login();
$this->response('请求成功',Code::SUCCESS,$res);
}
/**
* @remark :自动登录
* @name :autologin
* @author :lyh
* @method :post
* @time :2023/8/19 9:12
*/
public function autologin(UserLoginLogic $logic, EncryptUtils $encrypt)
{
$serviceSettingModel = new Service();
$info = $serviceSettingModel->read(['type'=>4]);
if($info === false){
$this->response('当前访问地址不存在',Code::USER_ERROR);
}
$data = $encrypt->unlock_url($this->param['code'], $info['values']);
$data = json_decode($data, true);
if(!isset($data['project_id']) && !isset($data['user_id'])){
$this->response('无效Code',Code::USER_ERROR);
}
$res = $logic->autologin($data);
$this->response('请求成功',Code::SUCCESS, $res);
}
/**
* @remark :发送验证码
* @name :sendLoginSms
* @author :lyh
* @method :post
* @time :2023/8/19 9:13
*/
public function sendLoginSms()
{
$this->request->validate([
'mobile'=>['required', 'regex:/^1[3-9]\d{9}$/'],
],[
'mobile.required' => '手机号码不能为空',
'mobile.regex' => '请输入正确的手机号码',
]);
$mobile = $this->param['mobile'];
$user = UserModel::where(['mobile' => $mobile])->first();
if (empty($user)) {
$this->response('请输入正确的手机号码!', Code::USER_LOGIN_ERROE);
}
$last_sms = SmsLog::getLastLog($mobile, SmsLog::TYPE_LOGIN);
if ($last_sms && $last_sms->use = SmsLog::USE_USABLE && time() - strtotime($last_sms->created_at) < 60) {
$this->response('请不要重复发送短信!', Code::USER_LOGIN_ERROE);
}
$template = config('aliyunsms.login_sms_temp');
$code['code'] = rand(100000,999999);
$ali_sms = new AliSms();
$send = $ali_sms->sendSms(strval($mobile), $template, $code);
if (empty($send->Code) && $send->Code != 'OK') {
$this->response('发送失败, 请稍后重试!', Code::USER_LOGIN_ERROE);
}
SmsLog::createLog($mobile, $code['code']);
$this->response('success');
}
/**
* @remark :根据关键字生成链接
* @name :pubLink
* @author :lyh
* @method :post
* @time :2023/6/28 16:13
*/
public function stringTranslation(){
$str = Translate::tran($this->param['str'], 'en');
$this->response('success',Code::SUCCESS,strtolower($str));
}
/**
* @remark :微信登录
* @name :wechatLogin
* @author :lyh
* @method :post
* @time :2023/8/23 18:46
*/
public function qrcode(){
$wechat = new Wechat();
$accessToken = $wechat->getAccessToken();
$data = $wechat->setQrcode('global-v6',$accessToken);
if(!empty($data)){
$data['url'] = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$data['ticket'];
}
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :微信回调方法
* @name :wechatLogin
* @author :lyh
* @method :post
* @time :2023/8/24 9:30
*/
public function eventMessage(){
$message = file_get_contents("php://input");
$message = simplexml_load_string($message, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR);
@file_put_contents(storage_path('logs/lyh_error.log'), $message . PHP_EOL, FILE_APPEND);
$this->response('success');
}
/**
* @remark :生成token
* @name :generateToken
* @author :lyh
* @method :post
* @time :2023/8/24 17:27
*/
public function generateToken(){
$data = [
'phone' => $this->param['phone'],
'from_order_id' => $this->param['from_order_id'], // 提单系统 同步到个项目的唯一凭证(数字或者字符串)
'timestamp' => time(), // 接收到字符串解密出来以后需要 验证时间不超过30秒 超过时间视为无效授权
];
$common = new Common();
$str = $common->encrypt($data);
$this->response('success',Code::SUCCESS,['str'=>$str]);
}
/**
* @remark :解token
* @name :globalSo_v6_login
* @author :lyh
* @method :post
* @time :2023/8/24 17:37
*/
public function globalSo_v6_login(){
$common = new Common();
$arr = $common->decrypt($this->param['token']);
$this->response('success',Code::SUCCESS,$arr);
}
}