AdminUserController.php 7.0 KB

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