| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // |
- // +----------------------------------------------------------------------
- // +----------------------------------------------------------------------
- // | 会员管理
- // +----------------------------------------------------------------------
- namespace app\admin\controller\user;
- use app\admin\model\user\User as UserModel;
- use app\admin\model\user\UserGroup;
- use app\common\controller\Backend;
- use app\common\library\Auth;
- use think\exception\ValidateException;
- class User extends Backend
- {
- protected $searchFields = 'id,username,nickname';
- //初始化
- protected function initialize()
- {
- parent::initialize();
- $this->modelClass = new UserModel;
- $this->assign('groupCache', UserGroup::column('name', 'id'));
- }
- /**
- * 会员列表
- */
- public function index()
- {
- $this->relationSearch = true;
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
- $list = $this->modelClass
- ->withJoin(['group' => ['id', 'name']])
- ->where($where)
- ->page($page, $limit)
- ->select();
- $total = $this->modelClass
- ->withJoin(['group'])
- ->where($where)
- ->order($sort, $order)
- ->count();
- $result = ["code" => 0, "count" => $total, "data" => $list];
- return json($result);
- }
- return $this->fetch();
- }
- /**
- * 会员增加
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- try {
- $this->validate($params, 'app\admin\validate\user\User');
- } catch (ValidateException $e) {
- $this->error($e->getMessage());
- }
- if (Auth::instance()->register($params['username'], $params['password'], $params['email'], $params['mobile'], $params)) {
- $this->success("添加会员成功!", url("index"));
- } else {
- $this->error(Auth::instance()->getError() ?: '添加会员失败!');
- }
- } else {
- return $this->fetch();
- }
- }
- /**
- * 会员编辑
- */
- public function edit()
- {
- if ($this->request->isPost()) {
- $userid = $this->request->param('id/d', 0);
- $params = $this->request->post("row/a");
- try {
- $this->validate($params, 'app\admin\validate\user\User.edit');
- } catch (ValidateException $e) {
- $this->error($e->getMessage());
- }
- //获取用户信息
- $row = UserModel::find($userid);
- if (empty($row)) {
- $this->error('该会员不存在!');
- }
- $row->save($params);
- $this->success("更新成功!", url("index"));
- } else {
- $userid = $this->request->param('id/d', 0);
- $data = $this->modelClass->find($userid);
- if (empty($data)) {
- $this->error("该会员不存在!");
- }
- $this->assign("data", $data);
- return $this->fetch();
- }
- }
- /**
- * 会员删除
- */
- public function del()
- {
- $ids = $this->request->param('id/a', null);
- if (empty($ids)) {
- $this->error('请选择需要删除的会员!');
- }
- if (!is_array($ids)) {
- $ids = [0 => $ids];
- }
- foreach ($ids as $uid) {
- Auth::instance()->delete($uid);
- }
- $this->success("删除成功!");
- }
- }
|