request->isAjax()) { $id = input('post.id'); $sort = input('post.sort'); $num = input('post.num/d', 1); if($num <= 0){ $num = 1; } if(!in_array($sort, ['up', 'down'], true)){ return $this->json(2, '参数错误'); } $item = MInvitationTemplateClass::getById($id); if(empty($item)){ return $this->json(3, '权限不存在'); } if($sort == 'up'){ $where = "parent_id = {$item['parent_id']} and sort < {$item['sort']}"; $order = "sort desc"; }else{ $where = "parent_id = {$item['parent_id']} and sort > {$item['sort']}"; $order = "sort asc"; } $forSortItems = MInvitationTemplateClass::getListByWhereAndOrder($where, $order, $num); if(!empty($forSortItems)){ $updateData = []; $forSortCount = count($forSortItems); for($i = 0; $i < $forSortCount; $i++){ if($i == 0){ $updateData[] = [ 'id' => $forSortItems[$i]['id'], 'sort' => $item['sort'] ]; }else{ $updateData[] = [ 'id' => $forSortItems[$i]['id'], 'sort' => $forSortItems[$i - 1]['sort'] ]; } } $updateData[] = [ 'id' => $item['id'], 'sort' => $forSortItems[$i - 1]['sort'] ]; if(!empty($updateData)){ $model = new MInvitationTemplateClass(); $model->saveAll($updateData); return $this->json(); } } return $this->json(4, '无须调整排序!'); } return $this->json(1, '非法请求!'); } /** * 删除 */ public function del() { if ($this->request->isAjax()) { $id = input('post.id/d'); $item = MInvitationTemplateClass::getById($id); if(empty($item)){ return $this->json(1, '信息不存在'); } $children = MInvitationTemplateClass::getListByParentId($id); if(!empty($children)){ return $this->json(2, '当前权限有下级权限,不可删除'); } MInvitationTemplateClass::destroy($id); return $this->json(); } return $this->json(1, '非法请求!'); } /** * 修改 */ public function edit() { if($this->request->isPost()){ $item = input('post.item/a'); $id = input('post.id'); $InvitationTemplateClass = MInvitationTemplateClass::getById($id); if(empty($InvitationTemplateClass)){ return $this->json(1, '信息不存在'); } try { validate(VMInvitationTemplateClass::class)->check($item); MInvitationTemplateClass::updateById($id, $item); return $this->json(); } catch (ValidateException $e) { return $this->json(3, $e->getError()); } } $id = input('param.id/d'); $InvitationTemplateClass = MInvitationTemplateClass::getById($id); if(empty($InvitationTemplateClass)){ return $this->json(1,'无此信息,请核对之后再操作!'); }else{ $this->data['item'] = $InvitationTemplateClass; if($InvitationTemplateClass['parent_id'] > 0){ $parent = MInvitationTemplateClass::getById($InvitationTemplateClass['parent_id']); $this->data['parent'] = $parent; } return $this->view(); } } /** * 添加 */ public function add() { if($this->request->isPost()){ $item = input('post.item/a'); try { validate(VMInvitationTemplateClass::class)->check($item); MInvitationTemplateClass::create($item); return $this->json(); } catch (ValidateException $e) { return $this->json(2, $e->getError()); } } $parentId = input('param.parent_id/d',0); if($parentId > 0){ $parent = MInvitationTemplateClass::getById($parentId); $this->data['parent'] = $parent; } $this->data['parentId'] = $parentId; return $this->view(); } /** * 列表(全部) */ public function index() { $list = MInvitationTemplateClass::getListTree(); $this->data['items'] = $list; return $this->view(); } }