Channel.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Area;
  4. use app\admin\model\ChannelClosing;
  5. use app\common\controller\Backend;
  6. use redis\RedLock;
  7. use think\Db;
  8. use think\Exception;
  9. use think\exception\PDOException;
  10. use think\exception\ValidateException;
  11. /**
  12. * 推广渠道
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Channel extends Backend
  17. {
  18. /**
  19. * Channel模型对象
  20. * @var \app\admin\model\Channel
  21. */
  22. protected $model = null;
  23. /**
  24. * Channel模型对象
  25. * @var ChannelClosing
  26. */
  27. protected $channelClosingModel;
  28. public function _initialize()
  29. {
  30. parent::_initialize();
  31. $this->model = new \app\admin\model\Channel;
  32. $this->channelClosingModel = new ChannelClosing();
  33. }
  34. protected $noNeedRight = ["closing", "detail"];
  35. private function fetchWhere()
  36. {
  37. $admin = $this->auth->getUserInfo();
  38. if (!$admin)
  39. $this->error("error");
  40. $p_where = [];
  41. if (\E_ADMIN_TYPE::Store === $admin["type"]) {
  42. } else if (\E_ADMIN_TYPE::Agency === $admin["type"]) {
  43. array_push($p_where, ["city_code", "in", is_null($admin["city_codes"]) ? [] : explode(",", $admin["city_codes"])]);
  44. }
  45. return $p_where;
  46. }
  47. /**
  48. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  49. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  50. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  51. */
  52. /**
  53. * 查看
  54. */
  55. public function index()
  56. {
  57. //当前是否为关联查询
  58. $this->relationSearch = false;
  59. //设置过滤方法
  60. $this->request->filter(['strip_tags', 'trim']);
  61. $c_where = $this->fetchWhere();
  62. if ($this->request->isAjax()) {
  63. //如果发送的来源是Selectpage,则转发到Selectpage
  64. if ($this->request->request('keyField')) {
  65. return $this->c_selectpage($c_where);
  66. }
  67. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  68. $query = $this->model
  69. ->where($where);
  70. foreach ($c_where as $item) {
  71. $query->where($item[0], $item[1], $item[2]);
  72. }
  73. $list = $query
  74. ->order($sort, $order)
  75. ->paginate($limit);
  76. // foreach ($list as $row) {
  77. // $row->visible(['id', 'promotioner_name', 'promotioner_mobile', 'city_name', 'description', 'key', 'download_qrcode_image', 'register_qrcode_image', 'download_config', 'register_config', 'order_config', 'download_count', 'register_count', 'order_count', 'updatetime']);
  78. // }
  79. $result = array("total" => $list->total(), "rows" => $list->items());
  80. return json($result);
  81. }
  82. return $this->view->fetch();
  83. }
  84. public function add()
  85. {
  86. if (false === $this->request->isPost()) {
  87. return $this->view->fetch();
  88. }
  89. $params = $this->request->post('row/a');
  90. if (empty($params)) {
  91. $this->error(__('Parameter %s can not be empty', ''));
  92. }
  93. $params = $this->preExcludeFields($params);
  94. $area = (new Area())->where("id", $params["area_id"])->find();
  95. if (!$area) $this->error("选择的区域不存在");
  96. if (isset($params["starttime"])) {
  97. $params["starttime"] = strtotime($params["starttime"]);
  98. }
  99. if (isset($params["endtime"])) {
  100. $params["endtime"] = strtotime($params["endtime"]);
  101. }
  102. $params["city_code"] = $area["area_code"];
  103. $params["city_name"] = $area["mergename"];
  104. $params["key"] = md5(rand(0, 9999) . md5($params["city_code"] . time() . rand(0, 9999)));
  105. $download_url = qrcodeBase64("https://pbh5.xunsoftware.com/index/index", ["channel_key" => $params["key"]]);
  106. $register_url = qrcodeBase64("https://pbh5.xunsoftware.com/pages/user/login", ["channel_key" => $params["key"]]);
  107. $params["download_qrcode_image"] = $download_url;
  108. $params["register_qrcode_image"] = $register_url;
  109. if (is_null($params["download_qrcode_image"]) || is_null($params["register_qrcode_image"]))
  110. $this->error("生成二维码失败");
  111. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  112. $params[$this->dataLimitField] = $this->auth->id;
  113. }
  114. $result = false;
  115. Db::startTrans();
  116. try {
  117. //是否采用模型验证
  118. if ($this->modelValidate) {
  119. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  120. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  121. $this->model->validateFailException()->validate($validate);
  122. }
  123. $result = $this->model->allowField(true)->save($params);
  124. Db::commit();
  125. } catch (ValidateException | PDOException | Exception $e) {
  126. Db::rollback();
  127. $this->error($e->getMessage());
  128. }
  129. if ($result === false) {
  130. $this->error(__('No rows were inserted'));
  131. }
  132. $this->success();
  133. }
  134. public function edit($ids = null)
  135. {
  136. $row = $this->model->get($ids);
  137. if (!$row) {
  138. $this->error(__('No Results were found'));
  139. }
  140. $adminIds = $this->getDataLimitAdminIds();
  141. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  142. $this->error(__('You have no permission'));
  143. }
  144. if (false === $this->request->isPost()) {
  145. $this->view->assign('row', $row);
  146. return $this->view->fetch();
  147. }
  148. $params = $this->request->post('row/a');
  149. if (empty($params)) {
  150. $this->error(__('Parameter %s can not be empty', ''));
  151. }
  152. $params = $this->preExcludeFields($params);
  153. if (isset($params["starttime"])) {
  154. $params["starttime"] = strtotime($params["starttime"]);
  155. }
  156. if (isset($params["endtime"])) {
  157. $params["endtime"] = strtotime($params["endtime"]);
  158. }
  159. $result = false;
  160. Db::startTrans();
  161. try {
  162. //是否采用模型验证
  163. if ($this->modelValidate) {
  164. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  165. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  166. $row->validateFailException()->validate($validate);
  167. }
  168. $result = $row->allowField(true)->save($params);
  169. Db::commit();
  170. } catch (ValidateException | PDOException | Exception $e) {
  171. Db::rollback();
  172. $this->error($e->getMessage());
  173. }
  174. if (false === $result) {
  175. $this->error(__('No rows were updated'));
  176. }
  177. $this->success();
  178. }
  179. // 清算
  180. public function closing()
  181. {
  182. $id = $this->request->param("id");
  183. if (!$id)
  184. $this->error("ID 不存在!");
  185. $channel = $this->model->get($id);
  186. if (!$channel)
  187. $this->error("渠道不存在!");
  188. $total_amount = (($channel["download_config"] * $channel["download_count"])
  189. + ($channel["register_config"] * $channel["register_count"])
  190. + ($channel["order_config"] * $channel["order_count"]));
  191. if ($total_amount <= 0)
  192. $this->error("总金额为0,无法结算");
  193. $redLock = new RedLock();
  194. $lock = $redLock->lock("channel:closing:{$id}");
  195. if (!is_array($lock))
  196. $this->error("稍后再试");
  197. Db::startTrans();
  198. try {
  199. $this->model->update(
  200. [
  201. "download_count" => 0,
  202. "register_count" => 0,
  203. "order_count" => 0
  204. ],
  205. ["id" => $id]
  206. );
  207. $this->channelClosingModel->save([
  208. "channel_id" => $id,
  209. "download_config" => $channel["download_config"],
  210. "register_config" => $channel["register_config"],
  211. "order_config" => $channel["order_config"],
  212. "download_count" => $channel["download_count"],
  213. "register_count" => $channel["register_count"],
  214. "order_count" => $channel["order_count"],
  215. "total_amount" => fixed2Float($total_amount),
  216. "createtime" => time(),
  217. "updatetime" => time()
  218. ]);
  219. Db::commit();
  220. } catch (Exception $e) {
  221. Db::rollback();
  222. $this->error($e->getMessage());
  223. } finally {
  224. $redLock->unlock($lock);
  225. }
  226. $this->success("结算成功!");
  227. }
  228. public function detail($id = null)
  229. {
  230. $this->assign("rows", $this->channelClosingModel->where("channel_id", $id)->order("createtime", "desc")->limit(0, 100)->select());
  231. return $this->fetch();
  232. }
  233. }