| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\Customer as CustomerModel;
- use app\common\controller\Backend;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use Exception;
- /**
- * 客户管理(customer 表,含 H5 登录账号)
- *
- * @icon fa fa-circle-o
- */
- class Customer extends Backend
- {
- /**
- * @var \app\admin\model\Customer
- */
- protected $model = null;
- protected $noNeedRight = ['company_type_options', 'companyTypeOptions'];
- public function _initialize()
- {
- parent::_initialize();
- $this->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();
- }
- }
|