AdminUserController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. return $this->fetch();
  51. }
  52. /**
  53. * 添加提交
  54. */
  55. public function authAddPost()
  56. {
  57. if ($this->request->isPost()) {
  58. $post = $this->request->param('post/a');
  59. $validate = Validate::make([
  60. 'name' => 'require',
  61. 'idcard' => 'require|idCard|unique:user_auth',
  62. 'mobile' => 'require|mobile|unique:user_auth',
  63. ], [
  64. 'name' => '请输入姓名',
  65. 'idcard.require' => '请输入身份证号',
  66. 'idcard.idcard' => '身份证号不正确',
  67. 'idcard.unique' => '身份证号已存在',
  68. 'mobile.require' => '请输入手机号',
  69. 'mobile.mobile' => '手机号不正确',
  70. 'mobile.unique' => '手机号已存在',
  71. ]);
  72. if (!$validate->check($post)) {
  73. $this->error($validate->getError());
  74. }
  75. UserAuthModel::create($post);
  76. $this->success('操作成功');
  77. }
  78. }
  79. /**
  80. * 编辑
  81. */
  82. public function authEdit()
  83. {
  84. $id = $this->request->param('id', 0);
  85. if (empty($id)) {
  86. $this->error('数据不存在');
  87. }
  88. $auth = UserAuthModel::get($id);
  89. if (empty($auth)) {
  90. $this->error('数据不存在');
  91. }
  92. $this->assign('auth', $auth);
  93. return $this->fetch();
  94. }
  95. /**
  96. * 添加提交
  97. */
  98. public function authEditPost()
  99. {
  100. if ($this->request->isPost()) {
  101. $post = $this->request->param('post/a');
  102. $validate = Validate::make([
  103. 'name' => 'require',
  104. 'idcard' => 'require|idCard|unique:user_auth,idcard,' . $post['id'],
  105. 'mobile' => 'require|mobile|unique:user_auth,mobile,' . $post['id'],
  106. ], [
  107. 'name' => '请输入姓名',
  108. 'idcard.require' => '请输入身份证号',
  109. 'idcard.idcard' => '身份证号不正确',
  110. 'idcard.unique' => '身份证号已存在',
  111. 'mobile.require' => '请输入手机号',
  112. 'mobile.mobile' => '手机号不正确',
  113. 'mobile.unique' => '手机号已存在',
  114. ]);
  115. if (!$validate->check($post)) {
  116. $this->error($validate->getError());
  117. }
  118. UserAuthModel::update($post, ['id' => $post['id']]);
  119. $this->success('操作成功');
  120. }
  121. }
  122. /**
  123. * 删除用户
  124. */
  125. public function authDelete()
  126. {
  127. $id = $this->request->param('id', 0, 'intval');
  128. if (UserAuthModel::destroy($id) !== false) {
  129. $this->success("删除成功!");
  130. } else {
  131. $this->error("删除失败!");
  132. }
  133. }
  134. /**
  135. * 导入
  136. * @throws \think\db\exception\DataNotFoundException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. * @throws \think\exception\DbException
  139. */
  140. public function import()
  141. {
  142. $url = $this->request->post('url');
  143. $file = WEB_ROOT . trim($url, '/');
  144. if (file_exists($file)) {
  145. $excel = new Excel();
  146. $data = $excel->import($file, ['no', 'name', 'company', 'idcard', 'marry', 'mobile', 'idtype'], 1);
  147. $idtype = Constant::ID_TYPE;
  148. $marry = Constant::MARRY;
  149. foreach ($data as $k => $v) {
  150. if (empty($v['name'])) {
  151. $this->error("第" . ($k + 2) . "行的姓名不能为空");
  152. }
  153. if (empty($v['idcard'])) {
  154. $this->error("第" . ($k + 2) . "行的身份证号不能为空");
  155. }
  156. if (empty($v['mobile'])) {
  157. $this->error("第" . ($k + 2) . "行的手机号不能为空");
  158. }
  159. if (!Fun::isIdCard($v['idcard'])) {
  160. $this->error("第" . ($k + 2) . "行的身份证号格式错误");
  161. }
  162. if (!Fun::isMobile($v['mobile'])) {
  163. $this->error("第" . ($k + 2) . "行的手机号格式错误");
  164. }
  165. $idcardCheck = UserAuthModel::where('idcard', $v['idcard'])->find();
  166. if (!empty($idcardCheck)) {
  167. $this->error("第" . ($k + 2) . "行的身份证号已存在");
  168. }
  169. $mobileCheck = UserAuthModel::where('mobile', $v['mobile'])->find();
  170. if (!empty($mobileCheck)) {
  171. $this->error("第" . ($k + 2) . "行的手机号已存在");
  172. }
  173. if (in_array($v['idtype'], $idtype)) {
  174. unset($v['idtype']);
  175. }
  176. if (in_array($v['marry'], $marry)) {
  177. unset($v['marry']);
  178. }
  179. UserAuthModel::create($v);
  180. }
  181. }
  182. $this->success('导入成功');
  183. }
  184. }