182 lines
7.0 KiB
PHP
182 lines
7.0 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
require_once APPPATH . 'controllers/api/BaseController.php';
|
|
|
|
/**
|
|
* Notes:登录操作
|
|
* Created on: 2022/8/29 17:15
|
|
* Created by: dengbw
|
|
*/
|
|
class Login extends BaseController
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->load->model('market/Market_sys_admin_model', 'mdSysAdmin');
|
|
$this->load->model('market/Market_sys_role_model', 'mdSysRole');
|
|
$this->load->model('market/Market_sys_menu_model', 'mdSysMenu');
|
|
$this->load->model('market/Market_sys_login_record_model', 'mdSysLoginRecord');
|
|
}
|
|
|
|
/**
|
|
* Notes:用户登录
|
|
* Created on: 2022/9/8 14:49
|
|
* Created by: dengbw
|
|
*/
|
|
public function index_post()
|
|
{
|
|
$username = $this->input_param('username');
|
|
$password = $this->input_param('password');
|
|
$code = $this->input_param('code');
|
|
$code_key = $this->input_param('code_key');
|
|
if (!$username) {
|
|
$this->return_json('请输入登录账号');
|
|
}
|
|
if (!$password) {
|
|
$this->return_json('请输入登录密码');
|
|
}
|
|
if (!$code) {
|
|
$this->return_json('请输入验证码');
|
|
}
|
|
$redis = &load_cache('redis');
|
|
if (!$redis->get($code_key)) {
|
|
$this->return_json('验证码已过期');
|
|
}
|
|
if ($redis->get($code_key) != $code) {
|
|
$this->return_json('验证码错误');
|
|
}
|
|
$re = $this->mdSysAdmin->get(["username like '{$username}'" => null, 'status' => 0]);
|
|
if (!$re) {
|
|
$this->return_json('账号不存在');
|
|
return;
|
|
}
|
|
$this->load->library('api/record');
|
|
if (!password_verify($password, $re['password'])) {
|
|
$message = '密码错误';
|
|
$this->record->loginRecord(['userId' => $re['userId'], 'username' => $re['username'], 'nickname' => $re['nickname']
|
|
, 'loginType' => 1, 'comments' => $message]);
|
|
$this->return_json($message);
|
|
}
|
|
$user = ['userId' => $re['userId'], 'username' => $re['username'], 'nickname' => $re['nickname']
|
|
, 'avatar' => "https://qs.liche.cn/web/images/project/H5-ShiYu/default-avatar.jpg?v=1"
|
|
, 'sex' => $re['sex'], 'phone' => $re['phone'], 'introduction' => $re['introduction'], 'email' => $re['email']
|
|
, 'enabled' => true, 'accountNonLocked' => true, 'credentialsNonExpired' => true, 'accountNonExpired' => true];
|
|
$roles = $authorities = [];
|
|
if ($re['roleId']) {
|
|
$re_ro = $this->mdSysRole->get(['roleId' => $re['roleId'], 'status' => 0]);
|
|
if ($re_ro) {
|
|
$re_ro['userId'] = $re['userId'];
|
|
$roles[] = $re_ro;
|
|
if ($re_ro['menuIds']) {
|
|
$authorities = $this->mdSysMenu->select(["menuId in({$re_ro['menuIds']})" => null, 'status' => 0]
|
|
, 'sortNumber asc,menuId desc');
|
|
foreach ($authorities as $k => $v) {
|
|
$authorities[$k]['menuId'] = intval($v['menuId']);
|
|
$authorities[$k]['parentId'] = intval($v['parentId']);
|
|
$authorities[$k]['menuType'] = intval($v['menuType']);
|
|
$authorities[$k]['openType'] = intval($v['openType']);
|
|
$authorities[$k]['sortNumber'] = intval($v['sortNumber']);
|
|
$authorities[$k]['hide'] = intval($v['hide']);
|
|
$authorities[$k]['meta'] = json_decode($v['meta'], true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$user['roles'] = $roles;
|
|
$user['authorities'] = $authorities;
|
|
$data['access_token'] = Authorization::generateToken($re['userId']);
|
|
$data['user'] = $user;
|
|
$this->record->loginRecord(['userId' => $re['userId'], 'username' => $re['username'], 'nickname' => $re['nickname']]);
|
|
$redis->delete($code_key);//删除验证码
|
|
$this->return_response($data, '登录成功');
|
|
}
|
|
|
|
/**
|
|
* Notes:获取手机验证码
|
|
* Created on: 2022/10/13 16:54
|
|
* Created by: dengbw
|
|
*/
|
|
public function code_get()
|
|
{
|
|
$phone = $this->input_param('phone');
|
|
if (!$phone) {
|
|
$this->return_json('请输入绑定手机号');
|
|
}
|
|
$re = $this->mdSysAdmin->get(["phone" => $phone, 'status' => 0]);
|
|
if (!$re) {
|
|
$this->return_json('绑定手机号不存在');
|
|
return;
|
|
}
|
|
$code = rand(100000, 999999);
|
|
$redis = &load_cache('redis');
|
|
$redis->save($phone, $code, 10 * 60);
|
|
//send_sms($phone, $code);
|
|
$this->return_response(['code'=>$code], '短信验证码发送成功, 请注意查收!');
|
|
}
|
|
|
|
/**
|
|
* Notes:忘记密码
|
|
* Created on: 2022/10/13 15:34
|
|
* Created by: dengbw
|
|
*/
|
|
public function forget_post()
|
|
{
|
|
$phone = $this->input_param('phone');
|
|
$password = $this->input_param('password');
|
|
$password2 = $this->input_param('password2');
|
|
$code = $this->input_param('code');
|
|
if (!$phone) {
|
|
$this->return_json('请输入绑定手机号');
|
|
}
|
|
if (!$password) {
|
|
$this->return_json('请输入新的登录密码');
|
|
}
|
|
if (!$password2) {
|
|
$this->return_json('请再次输入登录密码');
|
|
}
|
|
if (mb_strlen($password) < 4) {
|
|
$this->return_json('请输入至少4个字符的新密码');
|
|
}
|
|
if ($password != $password2) {
|
|
$this->return_json('两次输入密码不一致');
|
|
}
|
|
if (!$code) {
|
|
$this->return_json('请输入验证码');
|
|
}
|
|
$redis = &load_cache('redis');
|
|
if (!$redis->get($phone)) {
|
|
$this->return_json('验证码已过期');
|
|
}
|
|
if ($redis->get($phone) != $code) {
|
|
$this->return_json('验证码错误');
|
|
}
|
|
$upDate['password'] = password_hash($password, PASSWORD_BCRYPT);
|
|
$this->mdSysAdmin->update($upDate, ['phone' => $phone, 'status' => 0]);
|
|
$redis->delete($phone);//删除验证码
|
|
$this->return_response([], '密码修改成功');
|
|
}
|
|
|
|
/**
|
|
* Notes:
|
|
* Created on: 2022/8/29 17:06
|
|
* Created by: dengbw
|
|
* http://market.dev.liche.cn/api/login/test
|
|
* http://market.liche.cn/api/login/test
|
|
*/
|
|
public function test()
|
|
{
|
|
//echo config('ele_admin.API_KEY');
|
|
$mobile = '13860164563';
|
|
$signer = new Lcobucci\JWT\Signer\Hmac\Sha256();//加密算法
|
|
$time = time();
|
|
$key = new Lcobucci\JWT\Signer\Key('market');
|
|
$token = (new Lcobucci\JWT\Builder())->issuedBy('http://market.dev.liche.cn')
|
|
->identifiedBy('4f1g23a12aa', true)//身份验证
|
|
->issuedAt($time)//签发时间
|
|
->expiresAt($time + 60)//多长时间以后才能用token,60秒以后才能用
|
|
->withClaim('mobile', $mobile)
|
|
->getToken($signer, $key);//配置项
|
|
echo 'token=' . $token;
|
|
}
|
|
} |