Hearbook.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\main\service\ClientAppService;
  5. use think\Config;
  6. use think\Controller;
  7. use think\Request;
  8. use think\Exception;
  9. use ZipArchive;
  10. /**
  11. *
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Hearbook extends Backend
  16. {
  17. protected $noNeedRight=['index','add','edit','del','multi','local'];
  18. /**
  19. * Hearbook模型对象
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('Hearbook');
  26. $this->view->assign("isallowedList", $this->model->getIsallowedList());
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 添加
  36. */
  37. public function add()
  38. {
  39. if ($this->request->isPost())
  40. {
  41. $params = $this->request->post("row/a");
  42. if ($params)
  43. {
  44. /*
  45. * 已经弃用,如果为了兼容老版可取消注释
  46. foreach ($params as $k => &$v)
  47. {
  48. $v = is_array($v) ? implode(',', $v) : $v;
  49. }
  50. */
  51. if ($this->dataLimit)
  52. {
  53. $params[$this->dataLimitField] = $this->auth->id;
  54. }
  55. try
  56. {
  57. //是否采用模型验证
  58. if ($this->modelValidate)
  59. {
  60. $name = basename(str_replace('\\', '/', get_class($this->model)));
  61. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  62. $this->model->validate($validate);
  63. }
  64. $result = $this->model->allowField(true)->save($params);
  65. if ($result !== false)
  66. {
  67. ClientAppService::instance()->clearHearBookCache($params['package_title']);
  68. $this->success();
  69. }
  70. else
  71. {
  72. $this->error($this->model->getError());
  73. }
  74. }
  75. catch (\think\exception\PDOException $e)
  76. {
  77. $this->error($e->getMessage());
  78. }
  79. }
  80. $this->error(__('Parameter %s can not be empty', ''));
  81. }
  82. return $this->view->fetch();
  83. }
  84. /**
  85. * 编辑
  86. */
  87. public function edit($ids = NULL)
  88. {
  89. $row = $this->model->get($ids);
  90. if (!$row)
  91. $this->error(__('No Results were found'));
  92. $adminIds = $this->getDataLimitAdminIds();
  93. if (is_array($adminIds))
  94. {
  95. if (!in_array($row[$this->dataLimitField], $adminIds))
  96. {
  97. $this->error(__('You have no permission'));
  98. }
  99. }
  100. if ($this->request->isPost())
  101. {
  102. $params = $this->request->post("row/a");
  103. if ($params)
  104. {
  105. /*
  106. * 已经弃用,如果为了兼容老版可取消注释
  107. foreach ($params as $k => &$v)
  108. {
  109. $v = is_array($v) ? implode(',', $v) : $v;
  110. }
  111. */
  112. try
  113. {
  114. //是否采用模型验证
  115. if ($this->modelValidate)
  116. {
  117. $name = basename(str_replace('\\', '/', get_class($this->model)));
  118. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  119. $row->validate($validate);
  120. }
  121. $result = $row->allowField(true)->save($params);
  122. if ($result !== false)
  123. {
  124. ClientAppService::instance()->clearHearBookCache($params['package_title']);
  125. $this->success();
  126. }
  127. else
  128. {
  129. $this->error($row->getError());
  130. }
  131. }
  132. catch (\think\exception\PDOException $e)
  133. {
  134. $this->error($e->getMessage());
  135. }
  136. }
  137. $this->error(__('Parameter %s can not be empty', ''));
  138. }
  139. $this->view->assign("row", $row);
  140. return $this->view->fetch();
  141. }
  142. /**
  143. * 删除
  144. */
  145. public function del($ids = "")
  146. {
  147. if ($ids)
  148. {
  149. $pk = $this->model->getPk();
  150. $adminIds = $this->getDataLimitAdminIds();
  151. if (is_array($adminIds))
  152. {
  153. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  154. }
  155. $list = $this->model->where($pk, 'in', $ids)->select();
  156. $count = 0;
  157. foreach ($list as $k => $v)
  158. {
  159. ClientAppService::instance()->clearHearBookCache($v['package_title']);
  160. $count += $v->delete();
  161. }
  162. if ($count)
  163. {
  164. $this->success();
  165. }
  166. else
  167. {
  168. $this->error(__('No rows were deleted'));
  169. }
  170. }
  171. $this->error(__('Parameter %s can not be empty', 'ids'));
  172. }
  173. }