model = new CustomerModel; $this->view->assign('statusList', $this->model->getStatusList()); $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); } } public function companyTypeOptions() { try { $list = $this->collectDistinctCompanyTypes(); } catch (\Throwable $e) { $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; } public function index() { $this->relationSearch = false; $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $row) { $row->visible([ 'id', 'company_name', 'username', 'account', 'email', 'phone', 'company_type', 'createtime', 'updatetime', 'status', ]); } return json(['total' => $list->total(), 'rows' => $list->items()]); } return $this->view->fetch(); } public function add() { if (false === $this->request->isPost()) { return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); $phone = trim((string)($params['phone'] ?? '')); $phone = str_replace([',', ';', ';', '|', '|', "\n", "\r", "\t"], ',', $phone); if (strpos($phone, ',') !== false) { $phone = trim(explode(',', $phone)[0]); } if ($phone === '') { $this->error('请填写手机号'); } if (!preg_match('/^1\d{10}$/', $phone)) { $this->error('手机号须为11位'); } $account = trim((string)($params['account'] ?? '')); if ($account === '') { $account = $phone; } if (!preg_match('/^1\d{10}$/', $account)) { $this->error('登录账号须为11位手机号'); } if (Db::name('customer')->where('phone', $phone)->find()) { $this->error('该手机号已存在'); } if (Db::name('customer')->where('account', $account)->find()) { $this->error('该登录账号已存在'); } $email = trim((string)($params['email'] ?? '')); $email = str_replace([',', ';', ';', '|', '|', "\n", "\r", "\t"], ',', $email); if (strpos($email, ',') !== false) { $email = trim(explode(',', $email)[0]); } $pwd = trim((string)($params['password'] ?? '')); if ($pwd === '') { $pwd = $phone; } $status = isset($params['status']) ? (string)$params['status'] : ''; if ($status === '') { $status = '1'; } $now = date('Y-m-d H:i:s'); $data = [ 'company_name' => trim((string)($params['company_name'] ?? '')), 'username' => trim((string)($params['username'] ?? '')), 'phone' => $phone, 'account' => $account, 'email' => $email, 'password' => md5(md5($pwd)), 'company_type' => trim((string)($params['company_type'] ?? '')), 'status' => $status, 'createtime' => $now, 'updatetime' => $now, ]; if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $data[$this->dataLimitField] = $this->auth->id; } $v = validate('app\\admin\\validate\\Customer'); if (!$v->scene('add')->check($data)) { $this->error($v->getError()); } try { Db::name('customer')->insert($data); } catch (PDOException|Exception $e) { $this->error($e->getMessage()); } $this->success(); } public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } if (false === $this->request->isPost()) { $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); $rowId = (int)$row['id']; $phone = trim((string)($params['phone'] ?? '')); $phone = str_replace([',', ';', ';', '|', '|', "\n", "\r", "\t"], ',', $phone); if (strpos($phone, ',') !== false) { $phone = trim(explode(',', $phone)[0]); } if ($phone === '') { $this->error('请填写手机号'); } if (!preg_match('/^1\d{10}$/', $phone)) { $this->error('手机号须为11位'); } $account = trim((string)($params['account'] ?? '')); if ($account === '') { $account = $phone; } if (!preg_match('/^1\d{10}$/', $account)) { $this->error('登录账号须为11位手机号'); } if (Db::name('customer')->where('phone', $phone)->where('id', '<>', $rowId)->find()) { $this->error('该手机号已存在'); } if (Db::name('customer')->where('account', $account)->where('id', '<>', $rowId)->find()) { $this->error('该登录账号已存在'); } $email = trim((string)($params['email'] ?? '')); $email = str_replace([',', ';', ';', '|', '|', "\n", "\r", "\t"], ',', $email); if (strpos($email, ',') !== false) { $email = trim(explode(',', $email)[0]); } $status = isset($params['status']) ? (string)$params['status'] : '1'; if ($status === '') { $status = '1'; } $data = [ 'id' => $rowId, 'company_name' => trim((string)($params['company_name'] ?? '')), 'username' => trim((string)($params['username'] ?? '')), 'phone' => $phone, 'account' => $account, 'email' => $email, 'company_type' => trim((string)($params['company_type'] ?? '')), 'status' => $status, 'updatetime' => date('Y-m-d H:i:s'), ]; $pwd = trim((string)($params['password'] ?? '')); if ($pwd !== '') { $data['password'] = md5(md5($pwd)); } $v = validate('app\\admin\\validate\\Customer'); if (!$v->scene('edit')->check($data)) { $this->error($v->getError()); } unset($data['id']); try { $aff = Db::name('customer')->where('id', $rowId)->update($data); } catch (PDOException|Exception $e) { $this->error($e->getMessage()); } if ($aff === false) { $this->error(__('No rows were updated')); } $this->success(); } }