GeoController.php 7.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2025/10/23
 * Time: 17:29
 */
namespace App\Http\Controllers\Api;

use App\Enums\Common\Code;
use App\Models\Geo\GeoConf;
use App\Models\Geo\GeoConfirm;
use App\Models\Geo\GeoWritings;
use App\Models\Manage\ManageHr;
use App\Models\Project\Project;
use App\Services\DingService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;

/**
 * GEO相关
 * Class GeoController
 * @package App\Http\Controllers\Api
 */
class GeoController extends BaseController
{
    /**
     * 获取确认文章列表
     * @param Request $request
     * @return false|string
     */
    public function getWritingsList()
    {
        try {
            $token = trim($this->param['token']);
            $param = Crypt::decrypt($token);
        }catch (\Exception $e){
            $this->response('非法请求',Code::SYSTEM_ERROR);
        }
        if ($param['send_at'] + 86400 < time()) {
            $this->response('非法请求,已过期',Code::SYSTEM_ERROR);
        }
        $project_id = $param['project_id'];
        $projectModel = new Project();
        $projectInfo = $projectModel->read(['id' => $project_id],['title','version']);
        $geoWritingsModel = new GeoWritings();
        $lists  = $geoWritingsModel->list(['project_id' => $project_id, 'status' => ['in',[GeoWritings::STATUS_RUNNING,GeoWritings::STATUS_FINISH]] ,'is_del' => GeoWritings::IS_DEL_FALSE],'id',['title', 'status', 'uniqid', 'confirm_at']);
        $result = [
            'project' => $projectInfo,
            'list' => $lists
        ];
        $this->response('success',Code::SUCCESS,$result);
    }

    /**
     * 获取详情
     * @param Request $request
     * @return false|string
     */
    public function getWritingsDetail()
    {
        $geoWritingsModel = new GeoWritings();
        $token = trim($this->param['token']);
        $detail = $geoWritingsModel->read(['uniqid' => $token],['title', 'content', 'status']);
        $this->response('success',Code::SUCCESS,$detail);
    }


    /**
     * 确认核心文章数据
     * @param Request $request
     * @return false|string
     */
    public function confirmWritings()
    {
        $this->request->validate([
            'token' => 'required',
            'title' => 'required|max:120',
            'content' => 'required'
        ], [
            'token.required' => '非法请求',
            'title.required' => '标题不能为空',
            'title.max' => '最大长度不能超过120字符',
            'content.required' => '内容不能为空',
        ]);
        $token = trim($this->param['token']);
        $geoWritingsModel = new GeoWritings();
        $info = $geoWritingsModel->read(['uniqid' => $token]);
        if ($info === false){
            return $this->response('非法请求',Code::SYSTEM_ERROR);
        }
        if ($info['status'] == GeoWritings::STATUS_FINISH){
            return $this->response('当前文章已确认,不可再次确认',Code::SYSTEM_ERROR);
        }
        try {
            $this->param['confirm_ip'] = $this->request->ip();
            $this->param['confirm_at'] = date('Y-m-d H:i:s');
            $this->param['content_length'] = strlen($this->param['content']);
            $this->param['status'] = GeoWritings::STATUS_FINISH;
            $geoWritingsModel->edit($this->param,['uniqid' => $token]);
            $geoConfModel = new GeoConf();
            $confInfo = $geoConfModel->read(['project_id'=>$info['project_id']]);
            $hrModel = new ManageHr();
            $manage_name = $hrModel->getName($confInfo['manager_id'] ??'');
            $dingService = new DingService();
            $dingService->handle([
                'keyword' => '项目数据确认',
                'msg' =>
                    'cm:文章确认完成' . PHP_EOL .
                    '项目名称:'.$confInfo['company'] ?? '' . PHP_EOL .
                    '负责人:'.$manage_name . PHP_EOL,
                'isAtAll' => false, // 是否@所有人
            ], 'https://oapi.dingtalk.com/robot/send?access_token=4effe85882009a8a1617dbeadc38c350f832deef7431ce10f5fda751b4c82fb9');
        }catch (\Exception $e){
            $this->response('非法请求',Code::SYSTEM_ERROR);
        }
        $this->response('success',Code::SUCCESS);
    }

    /**
     * 获取确认数据
     * @param Request $request
     * @return false|string
     */
    public function getConfirm()
    {
        $token = trim($this->param['token']);
        $data = GeoConfirm::where(['uniqid' => $token])->first();
        if (empty($data)){
            return $this->response('确认链接已重置,请查看最新推送链接!',Code::SYSTEM_ERROR);
        }
        $content = explode("\n", $data->content);
        $confirm = explode("\n", $data->confirm);
        $max_num = $data->max_num;
        $type = $data->type;
        $status = $data->status;
        $projectModel = new Project();
        $projectInfo = $projectModel->read(['id' => $data->project_id],['title','version']);
        $project_title = $projectInfo['title'] ?? '';
        $result = compact('content', 'confirm', 'type', 'status', 'project_title','max_num');
        $this->response('success',Code::SUCCESS,$result);
    }

    /**
     * 保存确认数据
     * 验证当前确认数据状态, 不可重复确认
     * @param Request $request
     */
    public function saveConfirm()
    {
        $this->request->validate([
            'uniqid' => 'required',
            'confirm' => 'required',
            'confirm_num' => 'required',
        ], [
            'uniqid.required' => '非法请求',
            'confirm.required' => '客户确认内容不能为空',
            'confirm_num.max' => '客户确认数量不能为空',
        ]);
        $geoConfirmModel = new GeoConfirm();
        $info = $geoConfirmModel->read(['uniqid'=>$this->param['uniqid']]);
        if ($info === false){
            $this->response('当前数据不存在已被删除',Code::SYSTEM_ERROR);
        }
        try {
            $this->param['status'] = $geoConfirmModel::STATUS_FINISH;
            $this->param['confirm_ip'] = $this->request->ip();
            $this->param['confirm_at'] = date('Y-m-d H:i:s');
            $result = $geoConfirmModel->edit($this->param,['id'=>$info['$info']]);
            $geoConfModel = new GeoConf();
            $confInfo = $geoConfModel->read(['project_id'=>$info['project_id']]);
            $hrModel = new ManageHr();
            $manage_name = $hrModel->getName($confInfo['manager_id'] ??'');
            $dingService = new DingService();
            $dingService->handle([
                'keyword' => '项目数据确认',
                'msg' =>
                    'cm:'.(($info['type'] == 1) ? '标题确认' : '关键词确认'). PHP_EOL .
                    '项目名称:'.$confInfo['company'] ?? '' . PHP_EOL .
                    '负责人:'.$manage_name . PHP_EOL,
                'isAtAll' => false, // 是否@所有人
            ], 'https://oapi.dingtalk.com/robot/send?access_token=4effe85882009a8a1617dbeadc38c350f832deef7431ce10f5fda751b4c82fb9');
        }catch (\Exception $e){
            $this->response('非法请求',Code::SYSTEM_ERROR);
        }
        $this->response('success',Code::SUCCESS,$result);
    }
}