Customer.php 9.0 KB

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