Customer.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Customer as CustomerModel;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use Exception;
  9. /**
  10. * 客户管理(customer 表,含 H5 登录账号)
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Customer extends Backend
  15. {
  16. /**
  17. * @var \app\admin\model\Customer
  18. */
  19. protected $model = null;
  20. /** 顶部搜索框:多字段模糊匹配(OR + LIKE) */
  21. protected $searchFields = 'company_name,username,account,email,phone,company_type';
  22. protected $noNeedRight = ['company_type_options', 'companyTypeOptions'];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new CustomerModel;
  27. $this->view->assign('statusList', $this->model->getStatusList());
  28. $action = strtolower((string)$this->request->action());
  29. if (in_array($action, ['add', 'edit'], true)) {
  30. try {
  31. $list = $this->collectDistinctCompanyTypes();
  32. } catch (\Throwable $e) {
  33. $list = [];
  34. }
  35. $this->assign('companyTypeOptions', $list);
  36. }
  37. }
  38. public function companyTypeOptions()
  39. {
  40. try {
  41. $list = $this->collectDistinctCompanyTypes();
  42. } catch (\Throwable $e) {
  43. $list = [];
  44. }
  45. return json(['code' => 1, 'msg' => '', 'data' => ['list' => $list]]);
  46. }
  47. /**
  48. * @return string[]
  49. */
  50. protected function collectDistinctCompanyTypes(): array
  51. {
  52. $rows = Db::name('customer')->column('company_type');
  53. if (!is_array($rows)) {
  54. return [];
  55. }
  56. $set = [];
  57. foreach ($rows as $v) {
  58. $v = trim((string)$v);
  59. if ($v === '') {
  60. continue;
  61. }
  62. foreach (preg_split('/[、,,]+/u', $v) as $p) {
  63. $p = trim($p);
  64. if ($p !== '') {
  65. $set[$p] = true;
  66. }
  67. }
  68. }
  69. $list = array_keys($set);
  70. sort($list, SORT_STRING);
  71. return $list;
  72. }
  73. public function index()
  74. {
  75. $this->relationSearch = false;
  76. $this->request->filter(['strip_tags', 'trim']);
  77. if ($this->request->isAjax()) {
  78. if ($this->request->request('keyField')) {
  79. return $this->selectpage();
  80. }
  81. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  82. $list = $this->model
  83. ->where($where)
  84. ->order($sort, $order)
  85. ->paginate($limit);
  86. foreach ($list as $row) {
  87. $row->visible([
  88. 'id', 'company_name', 'username', 'account', 'email', 'phone', 'company_type',
  89. 'createtime', 'updatetime', 'status',
  90. ]);
  91. }
  92. return json(['total' => $list->total(), 'rows' => $list->items()]);
  93. }
  94. return $this->view->fetch();
  95. }
  96. public function add()
  97. {
  98. if (false === $this->request->isPost()) {
  99. return $this->view->fetch();
  100. }
  101. $params = $this->request->post('row/a');
  102. if (empty($params)) {
  103. $this->error(__('Parameter %s can not be empty', ''));
  104. }
  105. $params = $this->preExcludeFields($params);
  106. $phone = trim((string)($params['phone'] ?? ''));
  107. $phone = str_replace([',', ';', ';', '|', '|', "\n", "\r", "\t"], ',', $phone);
  108. if (strpos($phone, ',') !== false) {
  109. $phone = trim(explode(',', $phone)[0]);
  110. }
  111. if ($phone === '') {
  112. $this->error('请填写手机号');
  113. }
  114. if (!preg_match('/^1\d{10}$/', $phone)) {
  115. $this->error('手机号须为11位');
  116. }
  117. $account = trim((string)($params['account'] ?? ''));
  118. if ($account === '') {
  119. $account = $phone;
  120. }
  121. if (!preg_match('/^1\d{10}$/', $account)) {
  122. $this->error('登录账号须为11位手机号');
  123. }
  124. if (Db::name('customer')->where('phone', $phone)->find()) {
  125. $this->error('该手机号已存在');
  126. }
  127. if (Db::name('customer')->where('account', $account)->find()) {
  128. $this->error('该登录账号已存在');
  129. }
  130. $email = trim((string)($params['email'] ?? ''));
  131. $email = str_replace([',', ';', ';', '|', '|', "\n", "\r", "\t"], ',', $email);
  132. if (strpos($email, ',') !== false) {
  133. $email = trim(explode(',', $email)[0]);
  134. }
  135. $pwd = trim((string)($params['password'] ?? ''));
  136. if ($pwd === '') {
  137. $pwd = $phone;
  138. }
  139. $status = isset($params['status']) ? (string)$params['status'] : '';
  140. if ($status === '') {
  141. $status = '1';
  142. }
  143. $now = date('Y-m-d H:i:s');
  144. $data = [
  145. 'company_name' => trim((string)($params['company_name'] ?? '')),
  146. 'username' => trim((string)($params['username'] ?? '')),
  147. 'phone' => $phone,
  148. 'account' => $account,
  149. 'email' => $email,
  150. 'password' => md5(md5($pwd)),
  151. 'company_type' => trim((string)($params['company_type'] ?? '')),
  152. 'status' => $status,
  153. 'createtime' => $now,
  154. 'updatetime' => $now,
  155. ];
  156. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  157. $data[$this->dataLimitField] = $this->auth->id;
  158. }
  159. $v = validate('app\\admin\\validate\\Customer');
  160. if (!$v->scene('add')->check($data)) {
  161. $this->error($v->getError());
  162. }
  163. try {
  164. Db::name('customer')->insert($data);
  165. } catch (PDOException|Exception $e) {
  166. $this->error($e->getMessage());
  167. }
  168. $this->success();
  169. }
  170. public function edit($ids = null)
  171. {
  172. $row = $this->model->get($ids);
  173. if (!$row) {
  174. $this->error(__('No Results were found'));
  175. }
  176. $adminIds = $this->getDataLimitAdminIds();
  177. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  178. $this->error(__('You have no permission'));
  179. }
  180. if (false === $this->request->isPost()) {
  181. $this->view->assign('row', $row);
  182. return $this->view->fetch();
  183. }
  184. $params = $this->request->post('row/a');
  185. if (empty($params)) {
  186. $this->error(__('Parameter %s can not be empty', ''));
  187. }
  188. $params = $this->preExcludeFields($params);
  189. $rowId = (int)$row['id'];
  190. $phone = trim((string)($params['phone'] ?? ''));
  191. $phone = str_replace([',', ';', ';', '|', '|', "\n", "\r", "\t"], ',', $phone);
  192. if (strpos($phone, ',') !== false) {
  193. $phone = trim(explode(',', $phone)[0]);
  194. }
  195. if ($phone === '') {
  196. $this->error('请填写手机号');
  197. }
  198. if (!preg_match('/^1\d{10}$/', $phone)) {
  199. $this->error('手机号须为11位');
  200. }
  201. $account = trim((string)($params['account'] ?? ''));
  202. if ($account === '') {
  203. $account = $phone;
  204. }
  205. if (!preg_match('/^1\d{10}$/', $account)) {
  206. $this->error('登录账号须为11位手机号');
  207. }
  208. if (Db::name('customer')->where('phone', $phone)->where('id', '<>', $rowId)->find()) {
  209. $this->error('该手机号已存在');
  210. }
  211. if (Db::name('customer')->where('account', $account)->where('id', '<>', $rowId)->find()) {
  212. $this->error('该登录账号已存在');
  213. }
  214. $email = trim((string)($params['email'] ?? ''));
  215. $email = str_replace([',', ';', ';', '|', '|', "\n", "\r", "\t"], ',', $email);
  216. if (strpos($email, ',') !== false) {
  217. $email = trim(explode(',', $email)[0]);
  218. }
  219. $status = isset($params['status']) ? (string)$params['status'] : '1';
  220. if ($status === '') {
  221. $status = '1';
  222. }
  223. $data = [
  224. 'id' => $rowId,
  225. 'company_name' => trim((string)($params['company_name'] ?? '')),
  226. 'username' => trim((string)($params['username'] ?? '')),
  227. 'phone' => $phone,
  228. 'account' => $account,
  229. 'email' => $email,
  230. 'company_type' => trim((string)($params['company_type'] ?? '')),
  231. 'status' => $status,
  232. 'updatetime' => date('Y-m-d H:i:s'),
  233. ];
  234. $pwd = trim((string)($params['password'] ?? ''));
  235. if ($pwd !== '') {
  236. $data['password'] = md5(md5($pwd));
  237. }
  238. $v = validate('app\\admin\\validate\\Customer');
  239. if (!$v->scene('edit')->check($data)) {
  240. $this->error($v->getError());
  241. }
  242. unset($data['id']);
  243. try {
  244. $aff = Db::name('customer')->where('id', $rowId)->update($data);
  245. } catch (PDOException|Exception $e) {
  246. $this->error($e->getMessage());
  247. }
  248. if ($aff === false) {
  249. $this->error(__('No rows were updated'));
  250. }
  251. $this->success();
  252. }
  253. }