| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 | <?phpnamespace app\admin\controller\general;use app\common\controller\Backend;use app\common\model\Config as ConfigModel;use think\Cache;use think\Db;use think\Exception;use think\Validate;/** * AI测量素材配置 * * @icon   fa fa-camera * @remark AI测量功能的素材配置管理,包含自拍模式和帮拍模式的配置项 */class AiMeasureConfig extends Backend{    /**     * @var \app\common\model\Config     */    protected $model = null;    protected $noNeedRight = ['check', 'selectpage'];    public function _initialize()    {        parent::_initialize();        $this->model = new ConfigModel;    }    /**     * 查看     */    public function index()    {        // AI测量素材配置分组        $aiMeasureGroups = [            'ai_measure_selfie' => 'AI测量自拍模式',            'ai_measure_helper' => 'AI测量帮拍模式',            // 'ai_measure_common' => 'AI测量通用配置'        ];                $siteList = [];        foreach ($aiMeasureGroups as $k => $v) {            $siteList[$k]['name'] = $k;            $siteList[$k]['title'] = $v;            $siteList[$k]['list'] = [];        }        // 获取AI测量相关的配置项        $configList = $this->model->where('group', 'in', array_keys($aiMeasureGroups))->select();                foreach ($configList as $k => $v) {            if (!isset($siteList[$v['group']])) {                continue;            }            $value = $v->toArray();            $value['title'] = __($value['title']);            if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {                $value['value'] = explode(',', $value['value']);            }            $value['content'] = json_decode($value['content'], true);            $value['tip'] = htmlspecialchars($value['tip']);            $siteList[$v['group']]['list'][] = $value;        }                $index = 0;        foreach ($siteList as $k => &$v) {            $v['active'] = !$index ? true : false;            $index++;        }                $this->view->assign('siteList', $siteList);        $this->view->assign('typeList', ConfigModel::getTypeList());        $this->view->assign('groupList', $aiMeasureGroups);        return $this->view->fetch();    }    /**     * 添加     */    public function add()    {        if (!config('app_debug')) {            $this->error(__('Only work at development environment'));        }        if ($this->request->isPost()) {            $this->token();            $params = $this->request->post("row/a", [], 'trim');            if ($params) {                // 限制只能添加AI测量相关的配置组                if (!in_array($params['group'], ['ai_measure_selfie', 'ai_measure_helper', 'ai_measure_common'])) {                    $this->error(__('Invalid group parameter'));                }                                foreach ($params as $k => &$v) {                    $v = is_array($v) && $k !== 'setting' ? implode(',', $v) : $v;                }                if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {                    $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);                } else {                    $params['content'] = '';                }                try {                    $result = $this->model->create($params);                } catch (Exception $e) {                    $this->error($e->getMessage());                }                if ($result !== false) {                    try {                        ConfigModel::refreshFile();                    } catch (Exception $e) {                        $this->error($e->getMessage());                    }                    $this->success();                } else {                    $this->error($this->model->getError());                }            }            $this->error(__('Parameter %s can not be empty', ''));        }        return $this->view->fetch();    }    /**     * 编辑     * @param null $ids     */    public function edit($ids = null)    {        if ($this->request->isPost()) {            $this->token();            $row = $this->request->post("row/a", [], 'trim');            if ($row) {                $configList = [];                // 只处理AI测量相关的配置项                $aiMeasureConfigs = $this->model->where('group', 'in', ['ai_measure_selfie', 'ai_measure_helper', 'ai_measure_common'])->select();                                foreach ($aiMeasureConfigs as $v) {                    if (isset($row[$v['name']])) {                        $value = $row[$v['name']];                        if (is_array($value) && isset($value['field'])) {                            $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);                        } else {                            $value = is_array($value) ? implode(',', $value) : $value;                        }                        $v['value'] = $value;                        $configList[] = $v->toArray();                    }                }                try {                    $this->model->allowField(true)->saveAll($configList);                } catch (Exception $e) {                    $this->error($e->getMessage());                }                try {                    ConfigModel::refreshFile();                } catch (Exception $e) {                    $this->error($e->getMessage());                }                $this->success();            }            $this->error(__('Parameter %s can not be empty', ''));        }    }    /**     * 删除     * @param string $ids     */    public function del($ids = "")    {        if (!config('app_debug')) {            $this->error(__('Only work at development environment'));        }        $name = $this->request->post('name');        $config = ConfigModel::getByName($name);        if ($name && $config) {            // 检查是否为AI测量配置项            if (!in_array($config['group'], ['ai_measure_selfie', 'ai_measure_helper', 'ai_measure_common'])) {                $this->error(__('Can only delete AI measurement configuration items'));            }                        try {                $config->delete();                ConfigModel::refreshFile();            } catch (Exception $e) {                $this->error($e->getMessage());            }            $this->success();        } else {            $this->error(__('Invalid parameters'));        }    }    /**     * 检测配置项是否存在     * @internal     */    public function check()    {        $params = $this->request->post("row/a");        if ($params) {            $config = $this->model->get($params);            if (!$config) {                $this->success();            } else {                $this->error(__('Name already exist'));            }        } else {            $this->error(__('Invalid parameters'));        }    }    public function selectpage()    {        $id = $this->request->get("id/d");        $config = \app\common\model\Config::get($id);        if (!$config) {            $this->error(__('Invalid parameters'));        }        $setting = $config['setting'];        //自定义条件        $custom = isset($setting['conditions']) ? (array)json_decode($setting['conditions'], true) : [];        $custom = array_filter($custom);        $this->request->request(['showField' => $setting['field'], 'keyField' => $setting['primarykey'], 'custom' => $custom, 'searchField' => [$setting['field'], $setting['primarykey']]]);        $this->model = \think\Db::connect()->setTable($setting['table']);        return parent::selectpage();    }}
 |