Guidemanage.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2019/8/9
  6. * Time: 14:05
  7. */
  8. namespace app\admin\controller;
  9. use app\common\controller\Backend;
  10. use app\common\library\Redis;
  11. use app\main\service\AdminService;
  12. class Guidemanage extends Backend
  13. {
  14. protected $model = null;
  15. public function _initialize()
  16. {
  17. parent::_initialize(); // TODO: Change the autogenerated stub
  18. $this->model = model('GuideManage');
  19. }
  20. /*public function add()
  21. {
  22. if ($this->request->isPost())
  23. {
  24. $params = $this->request->post("row/a");
  25. if ($params){
  26. try{
  27. $this->model->save($params);
  28. $this->success();
  29. }catch (\Exception $exception){
  30. $this->error('渠道号不能重复添加');
  31. }
  32. }
  33. }
  34. return $this->view->fetch();
  35. }*/
  36. /**
  37. * 添加
  38. */
  39. public function add()
  40. {
  41. if ($this->request->isPost())
  42. {
  43. $params = $this->request->post("row/a");
  44. if ($params)
  45. {
  46. /*
  47. * 已经弃用,如果为了兼容老版可取消注释
  48. foreach ($params as $k => &$v)
  49. {
  50. $v = is_array($v) ? implode(',', $v) : $v;
  51. }
  52. */
  53. if ($this->dataLimit)
  54. {
  55. $params[$this->dataLimitField] = $this->auth->id;
  56. }
  57. try
  58. {
  59. //是否采用模型验证
  60. if ($this->modelValidate)
  61. {
  62. $name = basename(str_replace('\\', '/', get_class($this->model)));
  63. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  64. $this->model->validate($validate);
  65. }
  66. $admin = AdminService::instance()->getAdminExtendModel()->getInfo($params['channel_id']);
  67. if (!$admin) {
  68. $this->error('渠道信息有误');
  69. }
  70. if (!$admin['benefit_app']) {
  71. $this->error('请先配置分成比例');
  72. }
  73. $result = $this->model->allowField(true)->save($params);
  74. $redis = Redis::instance();
  75. $redis_key = 'APPGUIDE:' . $params['channel_id'];
  76. $redis->del($redis_key);
  77. if ($result !== false)
  78. {
  79. $this->success();
  80. }
  81. else
  82. {
  83. $this->error($this->model->getError());
  84. }
  85. }
  86. catch (\think\exception\PDOException $e)
  87. {
  88. $this->error('添加失败,渠道号可能已经存在');
  89. //$this->error($e->getMessage());
  90. }
  91. }
  92. $this->error(__('Parameter %s can not be empty', ''));
  93. }
  94. return $this->view->fetch();
  95. }
  96. /**
  97. * 编辑
  98. */
  99. public function edit($ids = NULL)
  100. {
  101. $row = $this->model->get($ids);
  102. if (!$row)
  103. $this->error(__('No Results were found'));
  104. $adminIds = $this->getDataLimitAdminIds();
  105. if (is_array($adminIds))
  106. {
  107. if (!in_array($row[$this->dataLimitField], $adminIds))
  108. {
  109. $this->error(__('You have no permission'));
  110. }
  111. }
  112. if ($this->request->isPost())
  113. {
  114. $params = $this->request->post("row/a");
  115. if ($params)
  116. {
  117. /*
  118. * 已经弃用,如果为了兼容老版可取消注释
  119. foreach ($params as $k => &$v)
  120. {
  121. $v = is_array($v) ? implode(',', $v) : $v;
  122. }
  123. */
  124. try
  125. {
  126. //是否采用模型验证
  127. if ($this->modelValidate)
  128. {
  129. $name = basename(str_replace('\\', '/', get_class($this->model)));
  130. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  131. $row->validate($validate);
  132. }
  133. $result = $row->allowField(true)->save($params);
  134. $redis = Redis::instance();
  135. $redis_key = 'APPGUIDE:' . $params['channel_id'];
  136. $redis->del($redis_key);
  137. if ($result !== false)
  138. {
  139. $this->success();
  140. }
  141. else
  142. {
  143. $this->error($row->getError());
  144. }
  145. }
  146. catch (\think\exception\PDOException $e)
  147. {
  148. $this->error($e->getMessage());
  149. }
  150. }
  151. $this->error(__('Parameter %s can not be empty', ''));
  152. }
  153. $this->view->assign("row", $row);
  154. return $this->view->fetch();
  155. }
  156. /**
  157. * 删除
  158. */
  159. public function del($ids = "")
  160. {
  161. if ($ids)
  162. {
  163. $pk = $this->model->getPk();
  164. $adminIds = $this->getDataLimitAdminIds();
  165. if (is_array($adminIds))
  166. {
  167. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  168. }
  169. $list = $this->model->where($pk, 'in', $ids)->select();
  170. $count = 0;
  171. foreach ($list as $k => $v)
  172. {
  173. $count += $v->delete();
  174. $redis = Redis::instance();
  175. $redis_key = 'APPGUIDE:' . $v['channel_id'];
  176. $redis->del($redis_key);
  177. }
  178. if ($count)
  179. {
  180. $this->success();
  181. }
  182. else
  183. {
  184. $this->error(__('No rows were deleted'));
  185. }
  186. }
  187. $this->error(__('Parameter %s can not be empty', 'ids'));
  188. }
  189. }