Customer.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. *
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Customer extends Backend
  11. {
  12. /**
  13. * Customer模型对象
  14. * @var \app\admin\model\Customer
  15. */
  16. protected $model = null;
  17. /**
  18. * 弹窗内 AJAX 拉取业务分类候选,免配菜单权限(与路由 action 小写一致)
  19. */
  20. protected $noNeedRight = ['company_type_options', 'companyTypeOptions'];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Customer;
  25. $action = strtolower((string)$this->request->action());
  26. if (in_array($action, ['add', 'edit'], true)) {
  27. try {
  28. $list = $this->collectDistinctCompanyTypes();
  29. } catch (\Throwable $e) {
  30. $list = [];
  31. }
  32. $this->assign('companyTypeOptions', $list);
  33. }
  34. }
  35. /**
  36. * 从 customer.company_type 拆词去重,供新增/编辑多选
  37. */
  38. public function companyTypeOptions()
  39. {
  40. try {
  41. $list = $this->collectDistinctCompanyTypes();
  42. } catch (\Throwable $e) {
  43. $list = [];
  44. }
  45. // 直接 JSON,避免 success 参数顺序与 isAjax 在弹层内不一致导致前端拿不到 list
  46. return json(['code' => 1, 'msg' => '', 'data' => ['list' => $list]]);
  47. }
  48. /**
  49. * @return string[]
  50. */
  51. protected function collectDistinctCompanyTypes(): array
  52. {
  53. $rows = Db::name('customer')->column('company_type');
  54. if (!is_array($rows)) {
  55. return [];
  56. }
  57. $set = [];
  58. foreach ($rows as $v) {
  59. $v = trim((string)$v);
  60. if ($v === '') {
  61. continue;
  62. }
  63. foreach (preg_split('/[、,,]+/u', $v) as $p) {
  64. $p = trim($p);
  65. if ($p !== '') {
  66. $set[$p] = true;
  67. }
  68. }
  69. }
  70. $list = array_keys($set);
  71. sort($list, SORT_STRING);
  72. return $list;
  73. }
  74. /*
  75. * 新增/编辑/删除:未在本类重写时,直接使用父类混入的 \app\admin\library\traits\Backend 中的 add/edit/del(弹窗提交、校验、入库等)。
  76. * 本模块相关逻辑位置:业务分类下拉数据在 _initialize 里 assign;表单与多选在 view + public/assets/js/backend/customer.js;入库前字段处理在 model Customer。
  77. * 若要在保存前后加自定义逻辑,再把 trait 里对应方法复制到本类并修改(勿只写 parent::add() 空壳)。
  78. */
  79. }