GuardController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * 守护
  4. */
  5. namespace app\admin\controller;
  6. use cmf\controller\AdminBaseController;
  7. use think\facade\Db;
  8. class GuardController extends AdminbaseController {
  9. protected function getTypes($k=''){
  10. $type=array(
  11. '1'=>'普通守护',
  12. '2'=>'尊贵守护',
  13. );
  14. if($k===''){
  15. return $type;
  16. }
  17. return isset($type[$k])?$type[$k]:'';
  18. }
  19. protected function getLengthtype($k=''){
  20. $length=array(
  21. '0'=>'天',
  22. '1'=>'月',
  23. '2'=>'年',
  24. );
  25. if($k===''){
  26. return $length;
  27. }
  28. return isset($length[$k])?$length[$k]:'';
  29. }
  30. protected function getLengthtime($k=''){
  31. $type=array(
  32. '0'=>60*60*24,
  33. '1'=>60*60*24*30,
  34. '2'=>60*60*24*365,
  35. );
  36. if($k===''){
  37. return $type;
  38. }
  39. return isset($type[$k])?$type[$k]:0;
  40. }
  41. function index(){
  42. $lists = Db::name("guard")
  43. ->order("list_order asc")
  44. ->paginate(20);
  45. $page = $lists->render();
  46. $this->assign('lists', $lists);
  47. $this->assign("page", $page);
  48. $this->assign("type_a", $this->getTypes());
  49. $this->assign("length_type_a", $this->getLengthtype());
  50. return $this->fetch();
  51. }
  52. function del(){
  53. $id = $this->request->param('id', 0, 'intval');
  54. $rs = DB::name('guard')->where("id={$id}")->delete();
  55. if(!$rs){
  56. $this->error("删除失败!");
  57. }
  58. $action="删除守护:{$id}";
  59. setAdminLog($action);
  60. $this->resetcache();
  61. $this->success("删除成功!",url("guard/index"));
  62. }
  63. //排序
  64. public function listOrder() {
  65. $model = DB::name('guard');
  66. parent::listOrders($model);
  67. $action="更新守护排序";
  68. setAdminLog($action);
  69. $this->resetcache();
  70. $this->success("排序更新成功!");
  71. }
  72. function add(){
  73. $this->assign('type_a', $this->getTypes());
  74. $this->assign('length_type_a', $this->getLengthtype());
  75. return $this->fetch();
  76. }
  77. function addPost(){
  78. if ($this->request->isPost()) {
  79. $data = $this->request->param();
  80. $name=$data['name'];
  81. if($name==""){
  82. $this->error("请输入名称");
  83. }
  84. $coin=intval($data['coin']);
  85. if($coin=="" || $coin<1){
  86. $this->error("请输入有效价格");
  87. }
  88. $length=intval($data['length']);
  89. if($length=="" || $length<1){
  90. $this->error("请输入有效时长");
  91. }
  92. $length_type=$data['length_type'];
  93. $data['addtime']=time();
  94. $data['uptime']=time();
  95. $data['length_time']=$length * $this->getLengthtime($length_type);
  96. $id = DB::name('guard')->insertGetId($data);
  97. if(!$id){
  98. $this->error("添加失败!");
  99. }
  100. $action="添加守护:{$id}";
  101. setAdminLog($action);
  102. $this->resetcache();
  103. $this->success("添加成功!");
  104. }
  105. }
  106. function edit(){
  107. $id = $this->request->param('id', 0, 'intval');
  108. $data=Db::name('guard')
  109. ->where("id={$id}")
  110. ->find();
  111. if(!$data){
  112. $this->error("信息错误");
  113. }
  114. $this->assign('data', $data);
  115. $this->assign('type_a', $this->getTypes());
  116. $this->assign('length_type_a', $this->getLengthtype());
  117. return $this->fetch();
  118. }
  119. function editPost(){
  120. if ($this->request->isPost()) {
  121. $data = $this->request->param();
  122. $name=$data['name'];
  123. if($name==""){
  124. $this->error("请输入名称");
  125. }
  126. $coin=intval($data['coin']);
  127. if($coin=="" || $coin<1){
  128. $this->error("请输入有效价格");
  129. }
  130. $length=intval($data['length']);
  131. if($length=="" || $length<1){
  132. $this->error("请输入有效时长");
  133. }
  134. $length_type=$data['length_type'];
  135. $data['uptime']=time();
  136. $data['length_time']=$length * $this->getLengthtime($length_type);
  137. $rs = DB::name('guard')->update($data);
  138. if($rs===false){
  139. $this->error("修改失败!");
  140. }
  141. $action="修改守护:{$data['id']}";
  142. setAdminLog($action);
  143. $this->resetcache();
  144. $this->success("修改成功!");
  145. }
  146. }
  147. function resetCache(){
  148. $key='guard_list';
  149. $list= DB::name('guard')
  150. ->field('id,name,name_en,type,coin')
  151. ->order('list_order asc')
  152. ->select();
  153. if($list){
  154. setcaches($key,$list);
  155. }else{
  156. delcache($key);
  157. }
  158. return 1;
  159. }
  160. }