WokerTask/app/api/controller/LoginController.php

293 lines
8.6 KiB
PHP
Raw Normal View History

2023-12-27 14:06:33 +08:00
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\api\controller;
use app\common\cache\UserTokenCache;
use app\common\model\Company;
use app\api\validate\{LoginAccountValidate, RegisterValidate, WebScanLoginValidate, WechatLoginValidate};
use app\api\logic\LoginLogic;
use app\api\logic\UserLogic;
use app\api\service\UserTokenService;
use app\common\model\user\User;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\{Db, Config};
use app\common\service\FileService;
/**
* 登录注册
* Class LoginController
* @package app\api\controller
*/
class LoginController extends BaseApiController
{
public array $notNeedLogin = ['register', 'account', 'logout', 'codeUrl', 'oaLogin', 'mnpLogin', 'getScanCode', 'scanLogin', 'shop_account','checkToken', 'parseToken'];
/**
* @notes 注册账号
* @return \think\response\Json
* @author 段誉
* @date 2022/9/7 15:38
*/
public function register()
{
return $this->fail('暂未开放注册,请联系管理员添加账号');
$params = (new RegisterValidate())->post()->goCheck('register');
$result = LoginLogic::register($params);
if (true === $result) {
return $this->success('注册成功', [], 1, 1);
}
return $this->fail(LoginLogic::getError());
}
public function add()
{
$params = Request()->param();
$params['company_id'] = $this->userInfo['company_id'];
$result = LoginLogic::register($params);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(LoginLogic::getError());
}
public function setInfo()
{
$params = Request()->param();
$result = UserLogic::setInfo($params['user_id'], ['field' => 'is_captain', 'value' => $params['is_captain']]);
if (false === $result) {
return $this->fail(UserLogic::getError());
}
return $this->success('操作成功', [], 1, 1);
}
/**
* @notes 账号密码/手机号密码/手机号验证码登录
* @return \think\response\Json
* @author 段誉
* @date 2022/9/16 10:42la_other_user
*/
public function account()
{
$params = (new LoginAccountValidate())->post()->goCheck();
$result = LoginLogic::login($params);
if (false === $result) {
return $this->fail(LoginLogic::getError());
}
return $this->data($result);
}
/**
* 商城登录
*/
public function shop_account()
{
$params = $this->request->param();
if (isset($params['shop_token']) && $params['shop_token'] != '') {
$result = LoginLogic::shop($params['shop_token']);
if (false === $result) {
return $this->fail(LoginLogic::getError());
}
return $this->data($result);
}
return $this->fail('无登录信息');
}
/**
* @notes 退出登录
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2022/9/16 10:42
*/
public function logout()
{
LoginLogic::logout($this->userInfo);
return $this->success();
}
/**
* @notes 获取微信请求code的链接
* @return \think\response\Json
* @author 段誉
* @date 2022/9/15 18:27
*/
public function codeUrl()
{
$url = $this->request->get('url');
$result = ['url' => LoginLogic::codeUrl($url)];
return $this->success('获取成功', $result);
}
/**
* @notes 公众号登录
* @return \think\response\Json
* @throws \GuzzleHttp\Exception\GuzzleException
* @author 段誉
* @date 2022/9/20 19:48
*/
public function oaLogin()
{
$params = (new WechatLoginValidate())->post()->goCheck('oa');
$res = LoginLogic::oaLogin($params);
if (false === $res) {
return $this->fail(LoginLogic::getError());
}
return $this->success('', $res);
}
/**
* @notes 小程序-登录接口
* @return \think\response\Json
* @author 段誉
* @date 2022/9/20 19:48
*/
public function mnpLogin()
{
$params = (new WechatLoginValidate())->post()->goCheck('mnpLogin');
$res = LoginLogic::mnpLogin($params);
if (false === $res) {
return $this->fail(LoginLogic::getError());
}
return $this->success('', $res);
}
/**
* @notes 小程序绑定微信
* @return \think\response\Json
* @author 段誉
* @date 2022/9/20 19:48
*/
public function mnpAuthBind()
{
$params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
$params['user_id'] = $this->userId;
$result = LoginLogic::mnpAuthLogin($params);
if ($result === false) {
return $this->fail(LoginLogic::getError());
}
return $this->success('绑定成功', [], 1, 1);
}
/**
* @notes 公众号绑定微信
* @return \think\response\Json
* @throws \GuzzleHttp\Exception\GuzzleException
* @author 段誉
* @date 2022/9/20 19:48
*/
public function oaAuthBind()
{
$params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
$params['user_id'] = $this->userId;
$result = LoginLogic::oaAuthLogin($params);
if ($result === false) {
return $this->fail(LoginLogic::getError());
}
return $this->success('绑定成功', [], 1, 1);
}
/**
* @notes 获取扫码地址
* @return \think\response\Json
* @author 段誉
* @date 2022/10/20 18:25
*/
public function getScanCode()
{
$redirectUri = $this->request->get('url/s');
$result = LoginLogic::getScanCode($redirectUri);
if (false === $result) {
return $this->fail(LoginLogic::getError() ?? '未知错误');
}
return $this->success('', $result);
}
/**
* @notes 网站扫码登录
* @return \think\response\Json
* @author 段誉
* @date 2022/10/21 10:28
*/
public function scanLogin()
{
$params = (new WebScanLoginValidate())->post()->goCheck();
$result = LoginLogic::scanLogin($params);
if (false === $result) {
return $this->fail(LoginLogic::getError() ?? '登录失败');
}
return $this->success('', $result);
}
/**
* @notes 更新用户头像昵称
* @return \think\response\Json
* @author 段誉
* @date 2023/2/22 11:15
*/
public function updateUser()
{
$params = (new WechatLoginValidate())->post()->goCheck("updateUser");
LoginLogic::updateUser($params, $this->userId);
return $this->success('操作成功', [], 1, 1);
}
public function checkToken(): \think\response\Json
{
$token = $this->request->post('token');
if(empty($token)){
return $this->fail('token参数错误');
}
$userInfo = (new UserTokenCache())->getUserInfo($token);
if(!$userInfo){
return $this->fail('token无效');
}
//获取公司信息
$company = Company::field('company_name,company_type')->where('id',$userInfo['company_id'])->findOrEmpty();
if(!$company->isEmpty()){
$userInfo['company_name'] = $company['company_name'];
$userInfo['company_type'] = $company['company_type'];
}
return $this->success('请求成功', $userInfo);
}
public function parseToken()
{
$token = $this->request->post('token');
if(empty($token)){
return $this->fail('token参数不能为空');
}
$result = LoginLogic::parseToken($token);
if (false === $result) {
return $this->fail(LoginLogic::getError() ?? '登录失败');
}
return $this->success('请求成功', $result);
}
}