AdminUserController.php 6.8 KB

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