MenuModel.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. use think\Db;
  5. class MenuModel extends Model
  6. {
  7. protected $name = 'auth_rule';
  8. // 开启自动写入时间戳字段
  9. protected $autoWriteTimestamp = true;
  10. /**
  11. * [getMenus 获取全部菜单]
  12. * @author
  13. */
  14. public function getMenus($map,$Nowpage,$limits)
  15. {
  16. return $this->where($map)->page($Nowpage,$limits)->order('id asc')->select()->toArray();
  17. }
  18. /**
  19. * [getAllMenu 获取全部菜单]
  20. * @author
  21. */
  22. public function getAllMenu($map)
  23. {
  24. return $this->where($map)->order('id asc')->select()->toArray ();
  25. }
  26. /**
  27. * [insertMenu 添加菜单]
  28. * @author
  29. */
  30. public function insertMenu($pid,$title,$name,$css,$sort,$status)
  31. {
  32. if(count($pid) == 1){
  33. Db::startTrans();// 启动事务
  34. try{
  35. if(count($name) != count($title) || count($name) != count($css) || count($name) != count($sort)){
  36. return ['code'=>100, 'data' => '','msg'=>'参数格式错误!'];
  37. }
  38. for($i=0;$i<count($name);$i++){
  39. $t = $title[$i];
  40. // $n = strtolower($name[$i]);
  41. $n = $name[$i];
  42. $c = $css[$i];
  43. $id = $pid[0];
  44. $s = $sort[$i];
  45. $param = [
  46. 'pid'=>$id,
  47. 'title'=>$t,
  48. 'name'=>$n,
  49. 'css'=>$c,
  50. 'sort'=>$s,
  51. 'status'=>$status
  52. ];
  53. $list[] = $param;
  54. }
  55. $this->saveAll($list);
  56. writelog('多菜单添加成功',200);
  57. Db::commit();// 提交事务
  58. return ['code' => 200, 'data' => '', 'msg' => '添加菜单成功'];
  59. }catch( \Exception $e){
  60. Db::rollback ();//回滚事务
  61. writelog('多菜单添加失败',100);
  62. return ['code' => 100, 'data' => '', 'msg' => '菜单添加失败'];
  63. }
  64. }else{
  65. Db::startTrans();// 启动事务
  66. try{
  67. if(count($pid) != count($title) || count($pid) != count($name) || count($pid) != count($css) || count($pid) != count($sort)){
  68. return ['code'=>100, 'data' => '','msg'=>'参数格式错误!'];
  69. }
  70. for($i=0;$i<count($pid);$i++){
  71. $t = $title[$i];
  72. // $n = strtolower($name[$i]);
  73. $n = $name[$i];
  74. $c = $css[$i];
  75. $id = $pid[$i];
  76. $s = $sort[$i];
  77. $param = [
  78. 'pid'=>$id,
  79. 'title'=>$t,
  80. 'name'=>$n,
  81. 'css'=>$c,
  82. 'sort'=>$s,
  83. 'status'=>$status
  84. ];
  85. $list[] = $param;
  86. }
  87. $this->saveAll($list);
  88. writelog('多菜单添加成功',200);
  89. Db::commit();// 提交事务
  90. return ['code' => 200, 'data' => '', 'msg' => '添加菜单成功'];
  91. }catch( \Exception $e){
  92. Db::rollback ();//回滚事务
  93. writelog('多菜单添加失败',100);
  94. return ['code' => 100, 'data' => '', 'msg' => '菜单添加失败'];
  95. }
  96. }
  97. }
  98. /**
  99. * [editMenu 编辑菜单]
  100. * @author
  101. */
  102. public function editMenu($param)
  103. {
  104. Db::startTrans();// 启动事务
  105. try{
  106. $this->allowField(true)->save($param, ['id' => $param['id']]);
  107. Db::commit();// 提交事务
  108. writelog('菜单【'.$param['title'].'】编辑成功',200);
  109. return ['code' => 200, 'data' => '', 'msg' => '编辑菜单成功'];
  110. }catch( \Exception $e){
  111. Db::rollback ();//回滚事务
  112. writelog('菜单【'.$param['title'].'】编辑失败',100);
  113. return ['code' => 100, 'data' => '', 'msg' => '编辑菜单失败'];
  114. }
  115. }
  116. /**
  117. * [getOneMenu 根据菜单id获取一条信息]
  118. * @author
  119. */
  120. public function getOneMenu($id)
  121. {
  122. return $this->where('id', $id)->find();
  123. }
  124. /**
  125. * [delMenu 删除菜单]
  126. * @author
  127. */
  128. public function delMenu($id,$param)
  129. {
  130. $title = $this->where('id', $id)->value('title');
  131. Db::startTrans();// 启动事务
  132. try{
  133. // $this->where('id', $id)->delete();
  134. MenuModel::destroy($param);
  135. Db::commit();// 提交事务
  136. $ids = implode(',',$param);
  137. writelog('菜单【'.$title.'】删除成功',200);
  138. return ['code' => 200, 'data' => $ids, 'msg' => '删除菜单成功'];
  139. }catch( \Exception $e){
  140. Db::rollback ();//回滚事务
  141. writelog('菜单【'.$title.'】删除失败',100);
  142. return ['code' => 100, 'data' => '', 'msg' => '删除菜单失败'];
  143. }
  144. }
  145. /**
  146. * batchDelMenu 批量删除菜单
  147. * @param $param
  148. * @return array
  149. */
  150. public function batchDelMenu($param){
  151. Db::startTrans();// 启动事务
  152. try{
  153. MenuModel::destroy($param);
  154. Db::commit();// 提交事务
  155. $ids = implode(',',$param);
  156. writelog('菜单批量删除成功',200);
  157. return ['code' => 200, 'data' => $ids, 'msg' => '批量删除成功'];
  158. }catch( \Exception $e){
  159. Db::rollback ();//回滚事务
  160. writelog('菜单批量删除失败',100);
  161. return ['code' => 100, 'data' => '', 'msg' => '批量删除失败'];
  162. }
  163. }
  164. // /**
  165. // * [ruleSort 菜单排序]
  166. // * @param $param
  167. // * @return array
  168. // */
  169. // public function ruleSort($id,$field,$value){
  170. // Db::startTrans();// 启动事务
  171. // try{
  172. // $this->where(['id' => $id ])->setField($field , $value);
  173. // Db::commit();// 提交事务
  174. // writelog('菜单排序更新成功',200);
  175. // return ['code' => 200,'data' => '', 'msg' => '排序更新成功'];
  176. // }catch( \Exception $e){
  177. // Db::rollback ();//回滚事务
  178. // writelog('菜单排序更新失败',100);
  179. // return ['code' => 100, 'data' => '', 'msg' => '排序更新失败'];
  180. // }
  181. // }
  182. // public function editField($id,$field,$value){
  183. // Db::startTrans();// 启动事务
  184. // try{
  185. // $this->where(['id' => $id ])->setField($field , $value);
  186. // Db::commit();// 提交事务
  187. // writelog('更新字段成功',200);
  188. // return ['code' => 200,'data' => '', 'msg' => '更新字段成功'];
  189. // }catch( \Exception $e){
  190. // Db::rollback ();//回滚事务
  191. // writelog('更新字段失败',100);
  192. // return ['code' => 100, 'data' => '', 'msg' => '更新字段失败'];
  193. // }
  194. // }
  195. /**
  196. * [ruleState 菜单状态]
  197. * @param $param
  198. * @return array
  199. */
  200. public function ruleState($id,$num){
  201. $title = $this->where('id',$id)->value('title');
  202. if($num == 2){
  203. $msg = '禁用';
  204. }else{
  205. $msg = '启用';
  206. }
  207. Db::startTrans();// 启动事务
  208. try{
  209. $this->where('id',$id)->setField(['status'=>$num]);
  210. Db::commit();// 提交事务
  211. writelog('菜单【'.$title.'】'.$msg.'成功',200);
  212. // return ['code' => 200, 'data' => '', 'msg' => '已'.$msg];
  213. }catch( \Exception $e){
  214. Db::rollback ();//回滚事务
  215. writelog('菜单【'.$title.'】'.$msg.'失败',100);
  216. return ['code' => 100, 'data' => '', 'msg' => $msg.'失败'];
  217. }
  218. }
  219. }