IndexController.php 5.4 KB
<?php

namespace App\Http\Controllers\Aside\Com;

use App\Enums\Common\Code;
use App\Enums\Common\Common;
use App\Http\Controllers\Aside\BaseController;
use App\Http\Logic\Aside\Manage\MenuLogic;
use App\Models\Domain\DomainInfo;
use App\Models\Inquiry\InquiryData;
use App\Models\Manage\Manage;
use App\Models\Product\Keyword;
use App\Models\Product\Product;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Services\ProjectServer;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

/**
 * Class IndexController
 * @package App\Http\Controllers\Aside
 * @author zbj
 * @date 2023/4/19
 */
class IndexController extends BaseController
{
    /**
     * 用户菜单
     * @param MenuLogic $logic
     * @return \Illuminate\Http\JsonResponse
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @author zbj
     * @date 2023/6/21
     */
    public function get_menu(MenuLogic $logic)
    {
        if($this->manage['gid'] == 0){ //超级管理员
            $menus = $logic->getAllMenu();
        }else{
            $menus = $logic->getMenuByGroupId($this->manage['gid']);
        }
        $this->response('success',Code::SUCCESS,$menus);
    }

    /**
     * @remark :修改密码
     * @name   :editPassword
     * @author :lyh
     * @method :post
     * @time   :2023/9/11 9:10
     */
    public function editPassword(){
        $this->request->validate([
//            'oldPassword'=>'required',
            'password' => 'required',
            'confirm'=>'required',
        ], [
//            'oldPassword.required' => '请输入原密码',
            'password.required' => '请输入新密码',
            'confirm.required' => '请再次输入新密码',
        ]);
        //查询员密码是否正确
        $managerModel = new Manage();
        $info = $managerModel->read(['id'=>$this->manage['id']]);
//        if(!Hash::check($this->param['oldPassword'], $info['password'])){
//            $this->response('原密码错误',Code::USER_REGISTER_ERROE);
//        }
        if($this->param['password'] != $this->param['confirm']){
            $this->response('两次密码不一致');
        }
        $rs = $managerModel->edit(['password'=>Hash::make($this->param['password'])],['id'=>$this->manage['id']]);
        if($rs === false){
            $this->response('系统错误',Code::SYSTEM_ERROR);
        }
        Cache::pull(Common::MANAGE_TOKEN . $info['token']);
        $this->response('success');
    }

    /**
     * @remark :同步询盘记录
     * @name   :sync_inquiry
     * @author :lyh
     * @method :post
     * @time   :2023/11/16 9:51
     */
    public function sync_inquiry(){
        $this->request->validate([
            'data' => 'required|array',
            'identifying'=>'required',
        ], [
            'data.required' => '自定义询盘数据不为空',
            'data.array' => '必须为数组',
            'identifying.required' => '唯一标识不为空',
        ]);
        $inquiryModel = new InquiryData();
        $rs = $inquiryModel->add($this->param);
        if($rs === false){
            $this->response('error',Code::SYSTEM_ERROR);
        }
        $this->response('success');
    }

    /**
     * @remark :根据关键字获取产品主图
     * @name   :getKeywordList
     * @author :lyh
     * @method :post
     * @time   :2024/2/23 16:28
     */
    public function getKeywordImage(){
        $arr = explode('/',trim(str_replace('https://', '', $this->param['url']),'/'));
        if(empty($arr) || !is_array($arr)){
            $this->response('当前项目不存在..',Code::SYSTEM_ERROR);
        }
        $domainModel = new DomainInfo();
        $domainInfo = $domainModel->read(['domain'=>$arr[0]]);
        if($domainInfo === false){
            $this->response('当前项目不存在.',Code::SYSTEM_ERROR);
        }
        ProjectServer::useProject($domainInfo['project_id']);
        $routeMapModel = new RouteMap();
        $routeInfo = $routeMapModel->read(['route'=>$arr[1]]);
        if($domainInfo === false){
            $this->response('当前路由不存在.',Code::SYSTEM_ERROR);
        }
        $keywordModel = new Keyword();
        $keywordInfo = $keywordModel->read(['id'=>$routeInfo['source_id']]);
        $count = Product::where('keyword_id','like' ,'%,'.$keywordInfo['id'].',%')->count();
        $productModel = new Product();
        if($count < 5){
            $productList = $productModel->list([],'sort',['thumb','title']);
            //获取7个产品主图
        }else{
            $productList = $productModel->list(['keyword_id'=>['like','%,'.$keywordInfo['id'].',%']],['thumb','title']);
        }
        $product_image = [];
        foreach ($productList as $k => $v){
            $image = [];
            if(!empty($v['thumb']) && !empty($v['thumb']['url'])){
                $image['image'] = getImageUrl($v['thumb']['url']);
                $image['title'] = $v['title'];
                $product_image[] = $image;
            }
            if(count($product_image) > 6){
                break;
            }
        }
        $data = [
            'title'=>$keywordInfo['title'],
            'keyword_title'=>$keywordInfo['keyword_title'],
            'keyword_content'=>$keywordInfo['keyword_content'],
            'product_list'=>$product_image
        ];
        DB::disconnect('custom_mysql');
        $this->response('success',Code::SUCCESS,$data);
    }

}