LoginController.php 8.5 KB
<?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\Events\WebSocketMessage;
use App\Events\WebSocketMessageSent;
use App\Helper\Arr;
use App\Helper\Common;
use App\Helper\Socket;
use App\Helper\Translate;
use App\Helper\Wechat;
use App\Http\Logic\Bside\User\UserLoginLogic;
use App\Models\Project\Project;
use App\Models\Service\Service;
use App\Models\Sms\SmsLog;
use App\Models\User\User as UserModel;
use App\Utils\EncryptUtils;
use \Illuminate\Support\Facades\Cache;
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();
        $project = $userLogic->login();
        $this->response('success',Code::SUCCESS,$project);
    }

    /**
     * @remark :根据选择的项目id登录
     * @name   :projectLogin
     * @author :lyh
     * @method :post
     * @time   :2023/8/29 15:21
     */
    public function projectLogin(){
        $this->request->validate([
            'project_id' => 'required',
            'mobile' => 'required'
        ],[
            'project_id.required' => '请选择项目',
            'mobile.required' => '参数错误',
        ]);
        $userLogic = new UserLoginLogic();
        $info = $userLogic->projectLogin();
        $this->response('success',Code::SUCCESS,$info);
    }

    /**
     * @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('success',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);
        $data = $this->wechatLogin($message->FromUserName);
        $socket = new Socket();
        $socket->socket(json_encode($data));
        $arr = [
            'ToUserName' => 'gh_27174ac5c9d8',
            'FromUserName' => 'oMbcI6gRzvfrU8ssGsEyvzXOO94w',
            'CreateTime' => '1693385898',
            'MsgType' => 'event',
            'Event' => 'subscribe',
            'EventKey' => 'qrscene_global-v6_v6',
            'Ticket' => 'gQF27zwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyWnppdHhWTlRjOEcxTlBpTE5BY2IAAgRjBO9kAwQQDgAA',
        ];
        return "<xml>
                <ToUserName><![CDATA[$message->FromUserName]]></ToUserName>
                <FromUserName><![CDATA[$message->ToUserName]]></FromUserName>
                <CreateTime>".time()."</CreateTime>
                <MsgType><![CDATA[text]]></MsgType>
                <Content><![CDATA[扫码成功了]]></Content>
            </xml>";
    }


    /**
     * @remark :解token
     * @name   :globalSo_v6_login
     * @author :lyh
     * @method :post
     * @time   :2023/8/24 17:37
     */
    public function globalSo_v6_login(UserLoginLogic $logic){
        $common = new Common();
        $arr = $common->decrypt($this->param['token']);
        if(empty($arr)){
            $this->response('登录失败',Code::USER_ERROR);
        }
        $userModel = new UserModel();
        $info = $userModel->read(['mobile'=>$arr['phone']]);
        if($info === false){
            $this->response('当前用户不存在请联系管理员',Code::USER_ERROR);
        }
        $data = [
            'user_id'=>$info['id'],
            'manager_id'=>0,
        ];
        $info = $logic->autologin($data);
        $this->response('success',Code::SUCCESS,['info'=>$info]);
    }

    /**
     * @remark :微信登录处理数据
     * @name   :wechatLogin
     * @author :lyh
     * @method :post
     * @time   :2023/8/31 9:13
     */
    public function wechatLogin($wechat){
        //查看当前账户是否绑定
        $userModel = new UserModel();
        $info = $userModel->read(['wechat'=>$wechat]);
        if($info === false){
            $data = [
                'code'=>Code::USER_LOGIN_ERROE,
                'message'=>'当前用户未绑定账户,请绑定后',
            ];
        }else {
            //获取项目详情
            $info = $this->assembleParam($info['mobile'],$info['project_id']);
            if(isset($info['token']) && !empty($info['token'])){
                //清除上一次用户缓存
                Cache::pull($info['token']);
            }
            //生成新token
            $token = md5(uniqid().$info['id']);
            //存储缓存
            $info['token'] = $token;
            Cache::add($token,$info,3600);
            //更新用户信息
            $this->model->edit(['token'=>$token],['id'=>$info['id']]);
            $data = [
                'code'=>Code::SUCCESS,
                'message'=>'当前用户未绑定账户,请绑定后',
                'data'=>$info
            ];
        }
        return $data;
    }


    public function ceshi(){
        $data = [
            'code'=>Code::USER_LOGIN_ERROE,
            'message'=>'当前用户未绑定账户,请绑定后',
        ];
        $webSocket = new Socket();
        $webSocket->send('hello');
        $response = $webSocket->receive();
        $webSocket->close();
    }
}