Index.php 17.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
<?php

namespace app\api\controller;

use app\api\model\Category;
use app\api\model\Factory;
use app\api\model\Job;
use app\api\model\JobCollect;
use app\api\model\JobKeyword;
use app\common\model\User;
use fast\Tree;
use app\common\controller\Wechat;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['categoryList','factoryList','jobList','jobInfo','agentContent'];
    protected $noNeedRight = ['*'];

    /**
     * @ApiWeigh    (99)
     * @ApiTitle    (首页分类)
     * @ApiSummary  (首页分类)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "请求成功",
        "time": "1605707229",
        "data": [{
            "id": 1, //分类ID
            "name": "附近工作", //分类名称
            "image": "https://app.mockplus.cn/api/v1/file/thumb?&img=https://img02.mockplus.cn/idoc/sketch/2020-11-02/4077abe2-8f9e-4cf0-b95f-7a402ba0394e.F42C51D8-3246-425F-B6D9-436A3BD0FF3B.png&p=25"
        }]
    })
     */
    public function categoryList()
    {
        $list = Category::order('weigh asc')->field('id,name,image')->select();
        $this->success('请求成功',$list);
    }

    /**
     * @ApiWeigh    (97)
     * @ApiTitle    (工厂列表)
     * @ApiSummary  (工厂列表)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "请求成功",
        "time": "1605944895",
        "data": [{
            "id": 2, //工厂ID
            "factory_name": "你好", //工厂名称
            "factory_price": "1.00" //工厂工价
        }]
    })
     */
    public function factoryList()
    {
        $list = Factory::order('weigh desc')->field('id,factory_name,factory_price')->select();
        $this->success('请求成功',$list);
    }

    /**
     * @ApiWeigh (95)
     * @ApiTitle    (搜索历史)
     * @ApiSummary  (搜索历史)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1606218663",
        "data": [ //关键词
            "nihao"
        ]
    })
     */
    public function keywordList()
    {
        $list = JobKeyword::where('user_id',$this->auth->id)
            ->order('updatetime desc')
            ->limit(10)
            ->column('keyword');
        $this->success('成功',$list);
    }

    /**
     * @ApiWeigh (93)
     * @ApiTitle    (搜索历史-清空)
     * @ApiSummary  (搜索历史-清空)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=false, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1601351666",
        "data": null
    })
     */
    public function keywordClear()
    {
        JobKeyword::where('user_id',$this->auth->id)->delete();
        $this->success('清空搜索历史成功');
    }

    /**
     * @ApiWeigh    (91)
     * @ApiTitle    (附近工作 | 长期工 | 短期工 | 兼职)
     * @ApiSummary  (职位列表)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
     * @ApiParams   (name="lng", type="string", required=false, description="经度,(附近工作必填)")
     * @ApiParams   (name="lat", type="string", required=false, description="纬度,(附近工作必填)")
     * @ApiParams   (name="type", type="string", required=false, description="分类:1=长期工,2=兼职,3=短期工")
     * @ApiParams   (name="keyword", type="string", required=false, description="搜索关键词")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1605954640",
        "data": {
            "total": 2, //数据总数
            "per_page": 15,
            "current_page": 1,
            "last_page": 1,
            "data": [{
                "id": 2, //职位ID
                "job_name": "测试职位2", //职位名称
                "cover": "http://www.recruit.top/uploads/20201121/3451459c2469a191a84de24d2e6852b5.png", //职位封面图
                "factory_price": "40.00", //工厂工价(元/时)
                "subsidy_price": "40.00", //平台补贴(元/时)
                "factory_price_total": "40.00", //工价
                "salary": "6000~7000", //月收入
                "people_num": 23 //报名人数
            }]
        }
    })
     */
    public function jobList()
    {
        $page = $this->request->param('page', 1, 'intval');
        $page_num = $this->request->param('page_num', 10, 'intval');
        $lng = $this->request->param('lng');
        $lat = $this->request->param('lat');
        $type = $this->request->param('type'); //分类:1=长期工,2=兼职,3=短期工
        $keyword = $this->request->param('keyword');
        $where = '';
        // 关键字
        if(!empty($keyword)){
            // 记录搜索关键词
            if($this->auth->id){
                $has = JobKeyword::where('user_id',$this->auth->id)
                    ->where('keyword',$keyword)
                    ->find();
                if(!$has){
                    $keyword_list = JobKeyword::order('createtime asc')->select();
                    // 超过10条的删除
                    if(count($keyword_list) > 10){
                        JobKeyword::where('id',$keyword_list[0]['id'])->delete();
                    }
                    JobKeyword::create([
                        'user_id' => $this->auth->id,
                        'keyword' => $keyword
                    ]);
                }else{
                    $has->updatetime = time();
                    $has->save();
                }
            }
            $where .= "job_name like '%{$keyword}%'";
        }
        // 哪些人及其下级可查看
        if($this->auth->id){
            $parentsIds = Tree::instance()->init(collection(User::select())->toArray())->getParentsIds($this->auth->id, true);
            foreach ($parentsIds as $pid){
                $findinset[] = "find_in_set({$pid},user_ids)";
            }
            $where .= $where ? ' and ' : '';
            $where .= "(".implode(' or ',$findinset)." or user_ids = '')";
        }
        // 附近
        $distance = '1';
        if($lng && $lat){
            $distance = "ROUND(
                6378.138 * 2 * ASIN(
                    SQRT(
                        POW(
                            SIN(
                                (
                                    {$lat} * PI() / 180 - lat * PI() / 180
                                ) / 2
                            ),
                            2
                        ) + COS({$lat} * PI() / 180) * COS(lat * PI() / 180) * POW(
                            SIN(
                                (
                                    {$lng} * PI() / 180 - lng * PI() / 180
                                ) / 2
                            ),
                            2
                        )
                    )
                ) , 2
            )";
        }
        // 职位类型
        if($type){
            $where .= $where ? ' and ' : '';
            $where .= "find_in_set({$type},type)";
        }
        $data = Job::where($where)
            ->field("*,{$distance} distance")
            ->order(['distance'=>'asc','weigh'=>'desc'])
            ->paginate($page_num,false,['page'=>$page])
            ->each(function($v){
                $v->visible(['id','cover','job_name','salary','people_num','factory_price_total','factory_price','subsidy_price']);
            })->toArray();
        $this->success('成功',$data);
    }

    /**
     * @ApiWeigh    (91)
     * @ApiTitle    (职位收藏)
     * @ApiSummary  (职位收藏)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders (name=token, type=string, required=false, description="请求的Token")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
     * @ApiParams   (name="keyword", type="string", required=false, description="搜索关键词")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1605954640",
        "data": {
            "total": 2, //数据总数
            "per_page": 15,
            "current_page": 1,
            "last_page": 1,
            "data": [{
                "id": 2, //职位ID
                "job_name": "测试职位2", //职位名称
                "cover": "http://www.recruit.top/uploads/20201121/3451459c2469a191a84de24d2e6852b5.png", //职位封面图
                "factory_price": "40.00", //工厂工价(元/时)
                "subsidy_price": "40.00", //平台补贴(元/时)
                "factory_price_total": "40.00", //工价
                "salary": "6000~7000", //月收入
                "people_num": 23 //报名人数
            }]
        }
    })
     */
    public function collectList()
    {
        $page = $this->request->param('page', 1, 'intval');
        $page_num = $this->request->param('page_num', 10, 'intval');
        $keyword = $this->request->param('keyword');
        $where = '';
        // 关键字
        if(!empty($keyword)){
            // 记录搜索关键词
            if($this->auth->id){
                $has = JobKeyword::where('user_id',$this->auth->id)
                    ->where('keyword',$keyword)
                    ->find();
                if(!$has){
                    $keyword_list = JobKeyword::order('createtime asc')->select();
                    // 超过10条的删除
                    if(count($keyword_list) > 10){
                        JobKeyword::where('id',$keyword_list[0]['id'])->delete();
                    }
                    JobKeyword::create([
                        'user_id' => $this->auth->id,
                        'keyword' => $keyword
                    ]);
                }else{
                    $has->updatetime = time();
                    $has->save();
                }
            }
            $where .= "j.job_name like '%{$keyword}%'";
        }
        // 哪些人及其下级可查看
        if($this->auth->id){
            $parentsIds = Tree::instance()->init(collection(User::select())->toArray())->getParentsIds($this->auth->id, true);
            foreach ($parentsIds as $pid){
                $findinset[] = "find_in_set({$pid},user_ids)";
            }
            $where .= $where ? ' and ' : '';
            $where .= "(".implode(' or ',$findinset)." or user_ids = '')";
        }
        
        $data = Job::alias('j')
            ->join('job_collect jc','jc.job_id = j.id and user_id = '.$this->auth->id)
            ->where($where)
            ->field('j.*')
            ->order(['jc.createtime'=>'desc','j.weigh'=>'desc'])
            ->paginate($page_num,false,['page'=>$page])
            ->each(function($v){
                $v->visible(['id','cover','job_name','salary','people_num','factory_price_total','factory_price','subsidy_price']);
            })->toArray();
        $this->success('成功',$data);
    }

    /**
     * @ApiWeigh    (89)
     * @ApiTitle    (职位详情)
     * @ApiSummary  (职位详情)
     * @ApiMethod   (POST)
     *
     * @ApiParams   (name="job_id", type="inter", required=true, description="职位ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1606033046",
        "data": {
            "id": 1, //职位ID
            "job_name": "测试职位", //职位名称
            "images": [{ //图片
                "type": "image", //文件类型:image=图片,video=视频
                "url": "http://www.recruit.top/uploads/20201121/3451459c2469a191a84de24d2e6852b5.png" //文件地址
            }],
            "content": "<p>456</p>", //职位详情
            "factory_price": "60.00", //正常工价
            "subsidy_price": "10.00", //平台补贴价格
            "factory_price_total": "70.00", //补贴后工价
            "salary": "5000~6000", // 月收入
            "is_collect": 0 //是否收藏:0=否,1=是
        }
    })
     */
    public function jobInfo()
    {
        $job_id = $this->request->param('job_id');
        empty($job_id) && $this->error('缺少必需参数');
        $info = Job::get($job_id);
        empty($info) && $this->error('职位信息不存在');
        $collect = JobCollect::where('user_id',$this->auth->id)->where('job_id',$job_id)->find();
        $info->is_collect = !empty($collect) ? 1 : 0;
        $info->visible(['id','images','job_name','factory_price_total','factory_price','subsidy_price','salary','content'])->append(['is_collect']);
        $this->success('成功',$info);
    }

    /**
     * @ApiWeigh    (87)
     * @ApiTitle    (收藏职位)
     * @ApiSummary  (收藏职位)
     * @ApiMethod   (POST)
     *
     * @ApiParams   (name="job_id", type="inter", required=true, description="职位ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "收藏成功/取消收藏成功",
        "time": "1606033046",
        "data": null
    })
     */
    public function jobCollect()
    {
        $job_id = $this->request->param('job_id');
        empty($job_id) && $this->error('缺少必需参数');
        $info = Job::get($job_id);
        empty($info) && $this->error('职位信息不存在');
        $where = [
            'user_id' => $this->auth->id,
            'job_id' => $job_id
        ];
        $collect = JobCollect::where($where)->find();
        if(!empty($collect)){
            $collect->delete();
            $this->success('取消收藏成功');
        }
        JobCollect::create($where);
        $this->success('收藏成功');
    }

    /**
     * @ApiWeigh    (85)
     * @ApiTitle    (职位海报)
     * @ApiSummary  (职位海报)
     * @ApiMethod   (POST)
     * @ApiParams   (name="job_id", type="inter", required=true, description="职位ID")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功',
        "data": {
            "url": "http://www.recruit.top/uploads/job/1.png", //职位海报地址
        }
    })
     */
    public function jobPoster()
    {
        $job_id = $this->request->param('job_id');
        empty($job_id) && $this->error('缺少必需参数');
        $job = Job::get($job_id);
        empty($job) && $this->error('职位信息不存在');

        // 本地路径
        $dir = 'uploads/job';
        if (!file_exists($dir)){
            mkdir($dir,0777,true);
        }

        // 职位小程序码
        $user_id = $this->auth->id > 0 ? $this->auth->id : 0;
        $qrcode_id = "{$job_id}_{$user_id}";
        $qrcode = $dir.'/qrcode_'.$qrcode_id.'.png';
        if(!file_exists($qrcode)){
            $user_id = $this->auth->id > 0 ? $this->auth->id : 0;
            $response = Wechat::miniProgram()->app_code->getUnlimit($qrcode_id, [
                'page'  => 'pages/zhiwei_xq/zhiwei_xq',
                'width' => 280, //最小宽度280
            ]);
            if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
                $response->saveAs($dir, str_replace($dir.'/','',$qrcode));
            }
            // 280不满足,再缩小
            \think\Image::open($qrcode)->thumb(120,120,\think\Image::THUMB_CENTER)->save($qrcode);
        }
        //将职位封面图保存到本地
        $job_cover = $dir.'/cover_'.$job_id.'.png';
        file_put_contents($job_cover,file_get_contents($job['cover']));
        \think\Image::open($job_cover)->thumb(84,84,\think\Image::THUMB_CENTER)->save($job_cover);

        $path_ttf = ROOT_PATH.'public/assets/fonts/PingFang.ttf';
        $filename = $dir.'/'.$job_id.'.png';

        $image = \think\Image::open(ROOT_PATH.'public/assets/img/miniProgram/job_back.png');
        // 职位名称居中
        $size = 12;
        $box1 = imagettfbbox($size, 0, $path_ttf, $job['job_name']);
        $box1_minx = min($box1[0], $box1[2], $box1[4], $box1[6]);
        $box1_maxx = max($box1[0], $box1[2], $box1[4], $box1[6]);
        /* 计算文字初始坐标和尺寸 */
        $w = $box1_maxx - $box1_minx;
        $box1_minx += ($image->width() - $w) / 2;

        // 备注居中
        $last_text = '长安此图识别二维码,进入小程序';
        $box2 = imagettfbbox($size, 0, $path_ttf, $last_text);
        $box2_minx = min($box2[0], $box2[2], $box2[4], $box2[6]);
        $box2_maxx = max($box2[0], $box2[2], $box2[4], $box2[6]);
        /* 计算文字初始坐标和尺寸 */
        $w = $box2_maxx - $box2_minx;
        $box2_minx += ($image->width() - $w) / 2;
        $image->water($job_cover,[129,120])
            ->text($job['job_name'],$path_ttf,$size,'#020202',[$box1_minx,215])
            ->water($qrcode,[111,262])
            ->text($last_text,$path_ttf,$size,'#969799',[$box2_minx,410])
            ->save($filename);
        $url = request()->domain().'/'.$filename.'?v='.time();
        $this->success('成功',compact('url'));
    }

    /**
     * @ApiWeigh    (83)
     * @ApiTitle    (入驻代理)
     * @ApiSummary  (入驻代理)
     * @ApiMethod   (POST)
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功',
        "data": "<p>入驻代理介绍</p>" //入驻代理介绍
    })
     */
    public function agentContent()
    {
        $this->success('成功',config('site.agent_content'));
    }
}