model = new \app\admin\model\Customer; $action = strtolower((string)$this->request->action()); if (in_array($action, ['add', 'edit'], true)) { try { $list = $this->collectDistinctCompanyTypes(); } catch (\Throwable $e) { $list = []; } $this->assign('companyTypeOptions', $list); } } /** * 从 customer.company_type 拆词去重,供新增/编辑多选 */ public function companyTypeOptions() { try { $list = $this->collectDistinctCompanyTypes(); } catch (\Throwable $e) { $list = []; } // 直接 JSON,避免 success 参数顺序与 isAjax 在弹层内不一致导致前端拿不到 list return json(['code' => 1, 'msg' => '', 'data' => ['list' => $list]]); } /** * @return string[] */ protected function collectDistinctCompanyTypes(): array { $rows = Db::name('customer')->column('company_type'); if (!is_array($rows)) { return []; } $set = []; foreach ($rows as $v) { $v = trim((string)$v); if ($v === '') { continue; } foreach (preg_split('/[、,,]+/u', $v) as $p) { $p = trim($p); if ($p !== '') { $set[$p] = true; } } } $list = array_keys($set); sort($list, SORT_STRING); return $list; } /* * 新增/编辑/删除:未在本类重写时,直接使用父类混入的 \app\admin\library\traits\Backend 中的 add/edit/del(弹窗提交、校验、入库等)。 * 本模块相关逻辑位置:业务分类下拉数据在 _initialize 里 assign;表单与多选在 view + public/assets/js/backend/customer.js;入库前字段处理在 model Customer。 * 若要在保存前后加自定义逻辑,再把 trait 里对应方法复制到本类并修改(勿只写 parent::add() 空壳)。 */ }