AuthorityEditController.class.php 1.97 KB
<?php

/**
 * 权限范围编辑
 * AbstractController.class.php
 * $author$
 */

namespace Apicp\Controller\Integral;

use Com\PackageValidate;
use Common\Model\AuthorityMainModel;
use Common\Service\AuthorityMainService;

class AuthorityEditController extends AbstractController
{
    public function index()
    {
        $validate = new PackageValidate(
            [
                'am_id' => 'require',
                'visibleRange' => 'require',
                'visibleRangeType' => 'in:' . implode(',', AuthorityMainModel::VISIBLE_RANGE_TYPE)
            ],
            [
                'am_id.require' => L('_ERR_PARAMS_CAN_NOT_EMPTY', ['name' => '可见范围主表ID']),
                'visibleRange.require' => L('_ERR_PARAMS_CAN_NOT_EMPTY', ['name' => '可见范围']),
                'visibleRangeType.in' => L('_ERR_PARAMS_NOT_IN_SCOPE', ['name' => '可见范围类型'])
            ],
            [
                'am_id',
                'visibleRange',
                'visibleRangeType'
            ]
        );
        $postData = $validate->postData;

        // 判断主表数据是否存在
        $authorityMainServ = new AuthorityMainService();
        $mainData = $authorityMainServ->get($postData['am_id']);
        if (empty($mainData)) {
            E(L('_ERR_DATA_IS_NOT_EXIST', ['name' => '主表']));
        }

        try {
            $mainServ = new AuthorityMainService();
            $mainServ->start_trans();

            // 更新数据
            settype($postData['visibleRange'], 'array');
            $mainServ->update(
                $postData['am_id'],
                [
                    'visible_range' => implode(',', $postData['visibleRange']),
                    'visible_range_type' => $postData['visibleRangeType']
                ]
            );

            $mainServ->commit();
        } catch (\Exception $e) {
            E($e->getCode() . ':' . $e->getMessage());
            $mainServ->rollback();
        }

        return true;
    }
}