// +---------------------------------------------------------------------- namespace app\user\controller; use app\common\Constant; use app\common\Excel; use app\common\Fun; use app\love\model\UserAuthModel; use cmf\controller\AdminBaseController; use \think\facade\Request; use think\facade\Validate; class AdminUserController extends AdminBaseController { /** * 允许注册用户列表 */ public function auth() { $keyword = $this->request->param('keyword'); $list = UserAuthModel::where(function ($query) use ($keyword) { if (!empty($keyword)) { $query->where('name|idcard|mobile', 'like', "%$keyword%"); } }) ->order("id DESC") ->paginate(10, false, [ 'query' => Request::param(),//不丢失已存在的url参数 ]); $count = UserAuthModel::where(function ($query) use ($keyword) { if (!empty($keyword)) { $query->where('name|idcard|mobile', 'like', "%$keyword%"); } }) ->count(); $this->assign('total', $count); $this->assign('keyword', $keyword ?: ''); // 获取分页显示 $page = $list->render(); $this->assign('list', $list); $this->assign('page', $page); // 渲染模板输出 return $this->fetch(); } /** * 添加 */ public function authAdd() { $this->assign('idtype', Constant::ID_TYPE); $this->assign('marry', Constant::MARRY); return $this->fetch(); } /** * 添加提交 */ public function authAddPost() { if ($this->request->isPost()) { $post = $this->request->param('post/a'); $validate = Validate::make([ 'name' => 'require', 'idcard' => 'require|idCard|unique:user_auth', 'mobile' => 'require|mobile|unique:user_auth', ], [ 'name' => '请输入姓名', 'idcard.require' => '请输入身份证号', 'idcard.idcard' => '身份证号不正确', 'idcard.unique' => '身份证号已存在', 'mobile.require' => '请输入手机号', 'mobile.mobile' => '手机号不正确', 'mobile.unique' => '手机号已存在', ]); if (!$validate->check($post)) { $this->error($validate->getError()); } UserAuthModel::create($post); $this->success('操作成功'); } } /** * 编辑 */ public function authEdit() { $id = $this->request->param('id', 0); if (empty($id)) { $this->error('数据不存在'); } $auth = UserAuthModel::get($id); if (empty($auth)) { $this->error('数据不存在'); } $this->assign('auth', $auth); $this->assign('idtype', Constant::ID_TYPE); $this->assign('marry', Constant::MARRY); return $this->fetch(); } /** * 添加提交 */ public function authEditPost() { if ($this->request->isPost()) { $post = $this->request->param('post/a'); $validate = Validate::make([ 'name' => 'require', 'idcard' => 'require|idCard|unique:user_auth,idcard,' . $post['id'], 'mobile' => 'require|mobile|unique:user_auth,mobile,' . $post['id'], ], [ 'name' => '请输入姓名', 'idcard.require' => '请输入身份证号', 'idcard.idcard' => '身份证号不正确', 'idcard.unique' => '身份证号已存在', 'mobile.require' => '请输入手机号', 'mobile.mobile' => '手机号不正确', 'mobile.unique' => '手机号已存在', ]); if (!$validate->check($post)) { $this->error($validate->getError()); } UserAuthModel::update($post, ['id' => $post['id']]); $this->success('操作成功'); } } /** * 删除用户 */ public function authDelete() { $id = $this->request->param('id', 0, 'intval'); if (UserAuthModel::destroy($id) !== false) { $this->success("删除成功!"); } else { $this->error("删除失败!"); } } /** * 导入 * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function import() { $url = $this->request->post('url'); $file = WEB_ROOT . trim($url, '/'); if (file_exists($file)) { $excel = new Excel(); $data = $excel->import($file, ['no', 'name', 'company', 'idcard', 'marry', 'mobile', 'id_type', 'marry'], 1); $idtype = Constant::ID_TYPE; $marry = Constant::MARRY; foreach ($data as $k => $v) { if (empty($v['name'])) { $this->error("第" . ($k + 2) . "行的姓名不能为空"); } if (empty($v['idcard'])) { $this->error("第" . ($k + 2) . "行的身份证号不能为空"); } if (empty($v['mobile'])) { $this->error("第" . ($k + 2) . "行的手机号不能为空"); } if (!Fun::isIdCard($v['idcard'])) { $this->error("第" . ($k + 2) . "行的身份证号格式错误"); } if (!Fun::isMobile($v['mobile'])) { $this->error("第" . ($k + 2) . "行的手机号格式错误"); } $idcardCheck = UserAuthModel::where('idcard', $v['idcard'])->find(); if (!empty($idcardCheck)) { $this->error("第" . ($k + 2) . "行的身份证号已存在"); } $mobileCheck = UserAuthModel::where('mobile', $v['mobile'])->find(); if (!empty($mobileCheck)) { $this->error("第" . ($k + 2) . "行的手机号已存在"); } if (!in_array($v['id_type'], $idtype)) { $this->error("第" . ($k + 2) . "行的身份类型错误"); } if (!in_array($v['marry'], $marry)) { $this->error("第" . ($k + 2) . "行的婚姻状况错误"); } UserAuthModel::create($v); } } $this->success('导入成功'); } }