AdminUserController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: Powerless < wzxaini9@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\user\controller;
  12. use app\common\Excel;
  13. use app\common\Fun;
  14. use app\love\model\UserAuthModel;
  15. use cmf\controller\AdminBaseController;
  16. use \think\facade\Request;
  17. use think\facade\Validate;
  18. class AdminUserController extends AdminBaseController
  19. {
  20. /**
  21. * 允许注册用户列表
  22. */
  23. public function auth()
  24. {
  25. $list = UserAuthModel::
  26. where(function ($query) {
  27. if (!empty($data['keyword'])) {
  28. $keyword = $data['keyword'];
  29. $query->where('name|idcard|mobile', 'like', "%$keyword%");
  30. }
  31. })
  32. ->order("id DESC")
  33. ->paginate(10, false, [
  34. 'query' => Request::param(),//不丢失已存在的url参数
  35. ]);
  36. $this->assign('keyword', $this->request->param('keyword') ?: '');
  37. // 获取分页显示
  38. $page = $list->render();
  39. $this->assign('list', $list);
  40. $this->assign('page', $page);
  41. // 渲染模板输出
  42. return $this->fetch();
  43. }
  44. /**
  45. * 添加
  46. */
  47. public function authAdd()
  48. {
  49. return $this->fetch();
  50. }
  51. /**
  52. * 添加提交
  53. */
  54. public function authAddPost()
  55. {
  56. if ($this->request->isPost()) {
  57. $post = $this->request->param('post/a');
  58. $validate = Validate::make([
  59. 'name' => 'require',
  60. 'idcard' => 'require|idCard|unique:user_auth',
  61. 'mobile' => 'require|mobile|unique:user_auth',
  62. ], [
  63. 'name' => '请输入姓名',
  64. 'idcard.require' => '请输入身份证号',
  65. 'idcard.idcard' => '身份证号不正确',
  66. 'idcard.unique' => '身份证号已存在',
  67. 'mobile.require' => '请输入手机号',
  68. 'mobile.mobile' => '手机号不正确',
  69. 'mobile.unique' => '手机号已存在',
  70. ]);
  71. if (!$validate->check($post)) {
  72. $this->error($validate->getError());
  73. }
  74. UserAuthModel::create($post);
  75. $this->success('操作成功');
  76. }
  77. }
  78. /**
  79. * 编辑
  80. */
  81. public function authEdit()
  82. {
  83. $id = $this->request->param('id', 0);
  84. if (empty($id)) {
  85. $this->error('数据不存在');
  86. }
  87. $auth = UserAuthModel::get($id);
  88. if (empty($auth)) {
  89. $this->error('数据不存在');
  90. }
  91. $this->assign('auth', $auth);
  92. return $this->fetch();
  93. }
  94. /**
  95. * 添加提交
  96. */
  97. public function authEditPost()
  98. {
  99. if ($this->request->isPost()) {
  100. $post = $this->request->param('post/a');
  101. $validate = Validate::make([
  102. 'name' => 'require',
  103. 'idcard' => 'require|idCard|unique:user_auth,idcard,' . $post['id'],
  104. 'mobile' => 'require|mobile|unique:user_auth,mobile,' . $post['id'],
  105. ], [
  106. 'name' => '请输入姓名',
  107. 'idcard.require' => '请输入身份证号',
  108. 'idcard.idcard' => '身份证号不正确',
  109. 'idcard.unique' => '身份证号已存在',
  110. 'mobile.require' => '请输入手机号',
  111. 'mobile.mobile' => '手机号不正确',
  112. 'mobile.unique' => '手机号已存在',
  113. ]);
  114. if (!$validate->check($post)) {
  115. $this->error($validate->getError());
  116. }
  117. UserAuthModel::update($post, ['id' => $post['id']]);
  118. $this->success('操作成功');
  119. }
  120. }
  121. /**
  122. * 删除用户
  123. */
  124. public function authDelete()
  125. {
  126. $id = $this->request->param('id', 0, 'intval');
  127. if (UserAuthModel::destroy($id) !== false) {
  128. $this->success("删除成功!");
  129. } else {
  130. $this->error("删除失败!");
  131. }
  132. }
  133. /**
  134. * 导入
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. * @throws \think\exception\DbException
  138. */
  139. public function import()
  140. {
  141. $url = $this->request->post('url');
  142. $file = WEB_ROOT . trim($url, '/');
  143. if (file_exists($file)) {
  144. $excel = new Excel();
  145. $data = $excel->import($file, ['name', 'idcard', 'mobile', 'company'], 1);
  146. foreach ($data as $k => $v) {
  147. if (empty($v['name'])) {
  148. $this->error("第" . ($k + 2) . "行的姓名不能为空");
  149. }
  150. if (empty($v['idcard'])) {
  151. $this->error("第" . ($k + 2) . "行的身份证号不能为空");
  152. }
  153. if (empty($v['mobile'])) {
  154. $this->error("第" . ($k + 2) . "行的手机号不能为空");
  155. }
  156. if (!Fun::isIdCard($v['idcard'])) {
  157. $this->error("第" . ($k + 2) . "行的身份证号格式错误");
  158. }
  159. if (!Fun::isMobile($v['mobile'])) {
  160. $this->error("第" . ($k + 2) . "行的手机号格式错误");
  161. }
  162. $idcardCheck = UserAuthModel::where('idcard',$v['idcard'])->find();
  163. if (!empty($idcardCheck)) {
  164. $this->error("第" . ($k + 2) . "行的身份证号已存在");
  165. }
  166. $mobileCheck = UserAuthModel::where('mobile',$v['mobile'])->find();
  167. if (!empty($mobileCheck)) {
  168. $this->error("第" . ($k + 2) . "行的手机号已存在");
  169. }
  170. UserAuthModel::create($v);
  171. }
  172. }
  173. $this->success('导入成功');
  174. }
  175. }