| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\Db;
- /**
- *
- *
- * @icon fa fa-circle-o
- */
- class Customer extends Backend
- {
- /**
- * Customer模型对象
- * @var \app\admin\model\Customer
- */
- protected $model = null;
- /**
- * 弹窗内 AJAX 拉取业务分类候选,免配菜单权限(与路由 action 小写一致)
- */
- protected $noNeedRight = ['company_type_options', 'companyTypeOptions'];
- public function _initialize()
- {
- parent::_initialize();
- $this->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() 空壳)。
- */
- }
|