LoginController.php 4.9 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\Helper\Translate;
use App\Http\Logic\Bside\User\UserLoginLogic;
use App\Models\File\Image;
use App\Models\Service\Service;
use App\Models\SmsLog;
use App\Models\Template\BTemplate;
use App\Models\Template\Template;
use App\Models\Template\TemplateModule;
use App\Models\User\User as UserModel;
use App\Utils\EncryptUtils;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
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));
    }

    public function ceshi(){
        $templateModel = new Template();
        $list = $templateModel->list();
        foreach ($list as $k => $v){
            // 使用正则表达式和 preg_match_all 函数来匹配多个 img 标签的 src 值
            preg_match_all('/\"https:\/\/develop.globalso.com\/a\/image\/(.*?)\"/', $v['html'], $matches);
            if (!empty($matches[1])) {
                $srcValues = $matches[1];
                $imageModel = new Image();
                $html = $v['html'];
                foreach ($srcValues as $srcValue) {
                    $image_info = $imageModel->read(['hash'=>$srcValue]);
                    if($image_info !== false){
                        $path = basename($image_info['path']);
                        $html = $this->re($html,$srcValue,$path);
                    }
                }
                $templateModel->edit(['html'=>$html],['id'=>$v['id']]);
            }
        }
        return 1;
    }

    public function re(&$html,$srcValue,$path){
        $html = str_replace(
            $srcValue,
            $path,
            $html
        );
        return $html;
    }
}