Postbackrules.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\main\constants\PostbackConstants;
  6. use app\main\service\AdminService;
  7. use think\Config;
  8. use think\Controller;
  9. use think\Request;
  10. /**
  11. *
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Postbackrules extends Backend
  16. {
  17. /**
  18. * PostbackRules模型对象
  19. */
  20. protected $model = null;
  21. protected $noNeedRight = ['globalconfig'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('Postbackrules');
  26. $this->view->assign("stateList", $this->model->getStateList());
  27. $this->view->assign("typeList", $this->model->getTypeList());
  28. $this->view->assign('type', $this->request->get('type', 'tout'));
  29. $this->assignconfig('type', $this->request->get('type', 'tout'));
  30. }
  31. /**
  32. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  33. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  34. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35. */
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('pkey_name')) {
  46. return $this->selectpage();
  47. }
  48. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  49. $where_u = [
  50. 'type' => $this->request->get('type', 'tout')
  51. ];
  52. $total = $this->model
  53. ->where($where)
  54. ->where($where_u)
  55. ->order($sort, $order)
  56. ->count();
  57. $list = $this->model
  58. ->where($where)
  59. ->where($where_u)
  60. ->order($sort, $order)
  61. ->limit($offset, $limit)
  62. ->select();
  63. $kl_config = Config::get('site.' . $this->request->get('type', 'tout') . '_report_kl');
  64. $kl_config = json_decode($kl_config, true);
  65. foreach ($list as $index => $item) {
  66. if ($list[$index]['money'] == '-1') {
  67. $list[$index]['money'] = Config::get('site.' . $item['type'] . '_report_money') . '(全局配置)';
  68. }
  69. if ($list[$index]['man_val'] == '-1') {
  70. $list[$index]['man_val'] = empty($kl_config) ? '' : $kl_config['man_val'] . '(全局配置)';
  71. }
  72. if ($list[$index]['kou_val'] == '-1') {
  73. $list[$index]['kou_val'] = empty($kl_config) ? '' : $kl_config['kou_val'] . '(全局配置)';
  74. }
  75. }
  76. $result = array("total" => $total, "rows" => $list);
  77. return json($result);
  78. }
  79. return $this->view->fetch();
  80. }
  81. /**
  82. * 添加
  83. */
  84. public function add()
  85. {
  86. if ($this->request->isPost()) {
  87. $params = $this->request->post("row/a");
  88. if ($params) {
  89. $params['type'] = $this->request->get('type', 'tout');
  90. if ($this->dataLimit) {
  91. $params[$this->dataLimitField] = $this->auth->id;
  92. }
  93. try {
  94. //是否采用模型验证
  95. if ($this->modelValidate) {
  96. $name = basename(str_replace('\\', '/', get_class($this->model)));
  97. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  98. $this->model->validate($validate);
  99. }
  100. $result = $this->model->allowField(true)->save($params);
  101. if ($result !== false) {
  102. $this->success();
  103. } else {
  104. $this->error($this->model->getError());
  105. }
  106. } catch (\think\exception\PDOException $e) {
  107. $this->error($e->getMessage());
  108. }
  109. }
  110. $this->error(__('Parameter %s can not be empty', ''));
  111. }
  112. return $this->view->fetch();
  113. }
  114. public function changestate($id, $state)
  115. {
  116. $row = $this->model->where([
  117. 'id' => $id,
  118. 'state' => $state,
  119. ])->find();
  120. if (!$row) {
  121. $this->error('未查询到可更新数据');
  122. }
  123. $update = [
  124. 'updatetime' => time(),
  125. ];
  126. if ($state == PostbackConstants::STATE_SHOW) {
  127. $update['state'] = PostbackConstants::STATE_HIDE;
  128. } else {
  129. $update['state'] = PostbackConstants::STATE_SHOW;
  130. }
  131. $status = $this->model->update($update, ['id' => $id]);
  132. if ($status) {
  133. $this->success();
  134. } else {
  135. $this->error('更新失败');
  136. }
  137. }
  138. /**
  139. * 全局配置
  140. * @param string $type
  141. * @return string
  142. * @throws \think\Exception
  143. */
  144. public function globalconfig($type = 'tout')
  145. {
  146. if (!$this->request->isAjax()) {
  147. $data = model('Config')->whereIn('name', [$type . '_report_money', $type . '_report_kl'])->column('name,value');
  148. $money = $data[$type . '_report_money'] ?? 0;
  149. $kl = $data[$type . '_report_kl'] ?? json_encode(['man_val' => '', 'kou_val' => '']);
  150. $this->view->assign(['money' => $money, 'kl' => json_decode($kl, true)]);
  151. return $this->view->fetch();
  152. } else {
  153. $money = $this->request->request('money', 0);
  154. $man_val = $this->request->request('man_val', '');
  155. $kou_val = $this->request->request('kou_val', '');
  156. $money_field = $type . '_report_money';
  157. $kl_field = $type . '_report_kl';
  158. if (model("Config")->where('name', 'eq', $money_field)->find()) {
  159. model('Config')->update(['value' => $money], ['name' => $money_field]);
  160. } else {
  161. $moneyData = [
  162. 'name' => $money_field,
  163. 'group' => 'postback',
  164. 'title' => '金额阈值',
  165. 'tip' => '金额阈值',
  166. 'type' => 'string',
  167. 'value' => $money,
  168. 'content' => '',
  169. 'rule' => 'required',
  170. 'extend' => '',
  171. ];
  172. model('Config')->allowField(true)->insertGetId($moneyData);
  173. }
  174. if (model("Config")->where('name', 'eq', $kl_field)->find()) {
  175. model('Config')->update(['value' => json_encode(['man_val' => $man_val, 'kou_val' => $kou_val]),], ['name' => $kl_field]);
  176. } else {
  177. $klData = [
  178. 'name' => $kl_field,
  179. 'group' => 'postback',
  180. 'title' => '扣量阈值',
  181. 'tip' => '扣量阈值',
  182. 'type' => 'string',
  183. 'value' => json_encode(['man_val' => $man_val, 'kou_val' => $kou_val]),
  184. 'content' => '',
  185. 'rule' => 'required',
  186. 'extend' => '',
  187. ];
  188. model('Config')->allowField(true)->insertGetId($klData);
  189. }
  190. Redis::instance()->del('site');
  191. $this->success();
  192. }
  193. }
  194. }