| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- declare(strict_types=1);
- namespace app\admin\controller\user;
- use app\common\controller\Backend;
- use app\admin\traits\Actions;
- use think\annotation\route\Group;
- use think\annotation\route\Route;
- use app\common\model\GroupUser as GroupUserModel;
- use think\facade\Db;
- #[Group("user/group_user")]
- class GroupUser extends Backend
- {
- use Actions;
- protected function _initialize()
- {
- parent::_initialize();
- $this->model = new GroupUserModel();
- }
- /**
- * 列表
- */
- #[Route('GET,POST,JSON', 'index')]
- public function index()
- {
- if (false === $this->request->isAjax()) {
- return $this->fetch();
- }
- if ($this->request->post('selectpage')) {
- return $this->selectpage();
- }
- $type = (string)$this->request->get('type', 2);
- [$where, $order, $limit, $with] = $this->buildparams();
- $list=[];
- if ($type == '1'){
- $list = $this->model->alias('g')
- ->join("yun_user u", "g.pid = u.id", "LEFT")
- ->with($with)
- ->where($where)
- ->order($order)
- ->field(['u.*','u.nickname' => 'manage_nickname'])
- ->group('g.pid')
- ->paginate($limit)->each(function ($item, $key) {
- $item['avatar']=$this->startsWithHttp($item['avatar'])?$item['avatar']:request()->domain().'/' . $item['avatar'];
- return $item;
- });
- }else if($type == '2'){
- $list = $this->model->alias('g')
- ->join("yun_user u", "g.pid = u.id", "INNER")
- ->with($with)
- ->where($where)
- ->order($order)
- ->field(['g.*'])
- ->paginate($limit)->each(function ($item, $key) {
- $item['avatar']=$this->startsWithHttp($item['avatar'])?$item['avatar']:request()->domain().'/' . $item['avatar'];
- return $item;
- });
- }
-
- $result = ['total' => $list->total(), 'rows' => $list->items(), 'ceshi' => $type];
- return json($result);
- }
- /**
- * 添加
- */
- #[Route('GET,POST', 'add')]
- public function add()
- {
- if (false === $this->request->isPost()) {
- return $this->fetch();
- }
- $params = array_merge($this->request->post("row/a"), $this->postParams);
- if (empty($params)) {
- $this->error(__('提交的参数不能为空'));
- }
- if (!$this->request->checkToken('__token__', ['__token__' => $this->request->post('__token__')])) {
- $this->error(__('token错误,请刷新页面重试'));
- }
- foreach ($params as &$value) {
- if (is_array($value)) {
- $value = implode(',', $value);
- }
- if ($value === '') {
- $value = null;
- }
- }
- $result = false;
- Db::startTrans();
- try {
- $params['salt'] = str_rand(4);
- $params['password'] = md5(md5($params['password'] . $params['salt']));
- $params['avatar'] = empty($params['avatar']) ? '/assets/img/logo.jpg' : $params['avatar']; //默认头像
- $result = $this->model->save($params);
- if ($this->callback) {
- $callback = $this->callback;
- $callback($this->model);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('没有新增任何数据'));
- }
- $this->success();
- }
- /**
- * 编辑
- */
- #[Route('GET,POST', 'edit')]
- public function edit(mixed $row = null)
- {
- $ids = $this->request->get('ids');
- if (!$row || is_array($row)) {
- $row = $this->model->find($ids);
- }
- if (!$row) {
- $this->error(__('没有找到记录'));
- }
- if (count($this->volidateFields) > 0) {
- foreach ($this->volidateFields as $field => $value) {
- if ($row[$field] != $value) {
- $this->error(__('没有操作权限'));
- }
- }
- }
- if (false === $this->request->isPost()) {
- $row['password'] = '';
- $this->assign('row', $row);
- return $this->fetch();
- }
- $params = array_merge($this->request->post("row/a"), $this->postParams);
- if (empty($params)) {
- $this->error(__('提交的参数不能为空'));
- }
- if (!$this->request->checkToken('__token__', ['__token__' => $this->request->post('__token__')])) {
- $this->error(__('token错误,请刷新页面重试'));
- }
- foreach ($params as &$value) {
- if (is_array($value)) {
- $value = implode(',', $value);
- }
- if ($value === '') {
- $value = null;
- }
- }
- $result = false;
- Db::startTrans();
- try {
- if (!empty($params['password'])) {
- $params['salt'] = str_rand(4);
- $params['password'] = md5(md5($params['password'] . $params['salt']));
- } else {
- unset($params['password']);
- }
- if ($params['type'] == '1' && empty($params['group_name'])) {
- $this->error(__('管理身份,必须设置组名'));
- }
- $params['avatar'] = empty($params['avatar']) ? '/assets/img/logo.jpg' : $params['avatar']; //默认头像
- $result = $row->save($params);
- if ($this->callback) {
- $callback = $this->callback;
- $callback($row);
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if (false === $result) {
- $this->error(__('没有数据被更新'));
- }
- $this->success();
- }
- /**
- * 列表
- */
- #[Route('GET,JSON', 'group_list')]
- public function group_list()
- {
- if (false === $this->request->isAjax()) {
- return $this->fetch();
- }
- $pid = (string)$this->request->get('pid', '0');
- [$where, $order, $limit, $with] = $this->buildparams();
- $where_item = [];
- if ($pid != '0') $where_item[] = ['pid', '=', (string)$pid];
- $list = $this->model
- ->with($with)
- ->where($where_item)
- ->where($where)
- ->order($order)
- ->paginate($limit)->each(function ($item, $key) {
- $item['avatar']=$this->startsWithHttp($item['avatar'])?$item['avatar']:request()->domain().'/' . $item['avatar'];
- return $item;
- });
- $result = ['total' => $list->total(), 'rows' => $list->items()];
- return json($result);
- }
- // 方法1.1:使用strpos()精准匹配
- public function startsWithHttp($url)
- {
- return strpos($url, 'https://') === 0
- || strpos($url, 'http://') === 0;
- }
- }
|