| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace app\admin\controller;
- use app\admin\service\AuthService;
- use app\BaseController;
- use app\common\model\StoreModel;
- use think\App;
- use think\facade\View;
- class AuthGroupCopy extends BaseController
- {
- private $authService;
- private $storeModel;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->authService = new AuthService();
- $this->storeModel = new StoreModel();
- }
- public function index() {
- return view();
- }
- /**
- * @return \think\response\Json|\think\response\View
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function add() {
- $all_groups = $this->authService->authGroupModel->fetchAllGroups();
- if ($this->request->isAjax()) {
- $params = $this->request->param();
- $res = $this->authService->authGroupModel->save([
- 'pid' => $params['pid'] ?? 1,
- 'name' => format_string($params['name'] ?? null),
- 'rules' => format_string($params['rule_ids'] ?? null),
- "store_ids" => isset($params["store_ids"]) ? join(',', $params['store_ids']) : null,
- 'describe' => format_string($params['describe'] ?? null),
- ]);
- return $this->ok($res);
- }
- $stores = $this->storeModel->findAllStore();
- View::assign([
- "stores" => $stores,
- "all_groups" => recursion($all_groups, 0)
- ]);
- return view();
- }
- /**
- * @return \think\response\Json|\think\response\View
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function edit() {
- $params = $this->request->param();
- $id = $params['id'];
- $all_groups = $this->authService->authGroupModel->fetchAllGroups();
- $group = $this->authService->authGroupModel->findById($id);
- if ($this->request->isAjax()) {
- $res = $this->authService->authGroupModel->where('id', $params['id'])->save([
- 'pid' => $params['pid'] ?? $group->pid,
- 'name' => $params['name'] ?? $group->name,
- 'rules' => !isset($params['rule_ids']) ? null : $params['rule_ids'],
- "store_ids" => isset($params["store_ids"]) ? join(',', $params['store_ids']) : null,
- 'describe' => $params['describe'] ?? $group->describe,
- 'update_time' => time()
- ]);
- return $this->ok($res);
- }
- $stores = $this->storeModel->findAllStore();
- $store_ids = $group->store_ids ? explode(",",$group->store_ids) : [];
- foreach ($stores as &$item) $item->isChecked = in_array($item->id, $store_ids);
- View::assign([
- 'group' => $group,
- "stores" => $stores,
- "all_groups" => recursion($all_groups, 0)
- ]);
- return view();
- }
- public function delete(\think\Request $request) {
- $params = $request->param();
- if(!isset($params['ids']))
- return $this->fail(lang("Please select the data you want to delete"));
- $this->authService->authGroupModel->deleteByIds(explode(',', $params['ids']));
- return $this->ok(true);
- }
- public function findAllAuthGroups() {
- $groups = $this->authService->authGroupModel->fetchAllGroups()->toArray();
- $format_groups = array_map(function ($data) {
- return [
- 'id' => $data['id'],
- 'title' => $data['name'],
- 'pid' => $data['pid'],
- 'field' => $data['name'],
- 'spread' => true,
- 'checked' => false,
- 'disabled' => $data['id'] == 1
- ];
- }, $groups);
- return $this->ok(recursion($format_groups));
- }
- }
|