AddController.class.php 1.82 KB
<?php
/**
 * 【后台】新增照片接口
 * AddController.class.php
 * User: caijianhua
 * Date: 2017/8/29
 * Time: 下午5:15
 */

namespace Apicp\Controller\Pic;

use Common\Common\AttachOperation;
use Common\Service\PlanPicService;

class AddController extends \Apicp\Controller\AbstractController
{
    /** @var  PlanPicService 照片表 */
    protected $plan_pic_service;

    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {
            return false;
        }

        $this->plan_pic_service = new PlanPicService();
        return true;
    }

    /**
     * 【后台】新增照片接口
     *
     * @author 蔡建华
     */
    public function Index_post()
    {
        $plan_id = I('post.plan_id');
        $pic_name = I('post.pic_name');
        $pic_at_id = I('post.pic_at_id');
        if (empty($plan_id)) {
            // 图片ID不能为空
            E('_EMPTY_PLAN_PIC_ID');
        }
        if (empty($pic_name)) {
            // 图片名称不能为空
            E('_EMPTY_PIC_NAME');
        }
        if (strlen($pic_name) > PlanPicService::PIC_NAME_MAX_LENGTH) {
            // 图片名称过长
            E('_ERR_PIC_LENGTH');
        }
        // 组织入库数据
        $insert_data = [
            'plan_id' => $plan_id,
            'pic_name' => $pic_name,
            'pic_uid' => '',
            'pic_at_id' => $pic_at_id,
            'pic_type' => PlanPicService::PIC_UPLOAD_TYPE_ADMIN
        ];
        // 新增图片
        $this->_result['pic_id'] = $this->plan_pic_service->insert($insert_data);

        // 附件操作
        $attach_serv = new AttachOperation();
        $attach_serv->insert_attach(
            APP_DIR,
            'plan',
            $plan_id,
            ['attach_ids' => [$pic_at_id]]
        );

        return true;
    }
}