model = new \app\admin\model\Channel; $this->channelClosingModel = new ChannelClosing(); } protected $noNeedRight = ["closing", "detail"]; private function fetchWhere() { $admin = $this->auth->getUserInfo(); if (!$admin) $this->error("error"); $p_where = []; if (\E_ADMIN_TYPE::Store === $admin["type"]) { } else if (\E_ADMIN_TYPE::Agency === $admin["type"]) { array_push($p_where, ["city_code", "in", is_null($admin["city_codes"]) ? [] : explode(",", $admin["city_codes"])]); } return $p_where; } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //当前是否为关联查询 $this->relationSearch = false; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); $c_where = $this->fetchWhere(); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->c_selectpage($c_where); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $query = $this->model ->where($where); foreach ($c_where as $item) { $query->where($item[0], $item[1], $item[2]); } $list = $query ->order($sort, $order) ->paginate($limit); // foreach ($list as $row) { // $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']); // } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } public function add() { if (false === $this->request->isPost()) { return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); $area = (new Area())->where("id", $params["area_id"])->find(); if (!$area) $this->error("选择的区域不存在"); if (isset($params["starttime"])) { $params["starttime"] = strtotime($params["starttime"]); } if (isset($params["endtime"])) { $params["endtime"] = strtotime($params["endtime"]); } $params["city_code"] = $area["area_code"]; $params["city_name"] = $area["mergename"]; $params["key"] = md5(rand(0, 9999) . md5($params["city_code"] . time() . rand(0, 9999))); $download_url = qrcodeBase64("https://pbh5.xunsoftware.com/index/index", ["channel_key" => $params["key"]]); $register_url = qrcodeBase64("https://pbh5.xunsoftware.com/pages/user/login", ["channel_key" => $params["key"]]); $params["download_qrcode_image"] = $download_url; $params["register_qrcode_image"] = $register_url; if (is_null($params["download_qrcode_image"]) || is_null($params["register_qrcode_image"])) $this->error("生成二维码失败"); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException()->validate($validate); } $result = $this->model->allowField(true)->save($params); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('No rows were inserted')); } $this->success(); } public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } if (false === $this->request->isPost()) { $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); if (isset($params["starttime"])) { $params["starttime"] = strtotime($params["starttime"]); } if (isset($params["endtime"])) { $params["endtime"] = strtotime($params["endtime"]); } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException()->validate($validate); } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } // 清算 public function closing() { $id = $this->request->param("id"); if (!$id) $this->error("ID 不存在!"); $channel = $this->model->get($id); if (!$channel) $this->error("渠道不存在!"); $total_amount = (($channel["download_config"] * $channel["download_count"]) + ($channel["register_config"] * $channel["register_count"]) + ($channel["order_config"] * $channel["order_count"])); if ($total_amount <= 0) $this->error("总金额为0,无法结算"); $redLock = new RedLock(); $lock = $redLock->lock("channel:closing:{$id}"); if (!is_array($lock)) $this->error("稍后再试"); Db::startTrans(); try { $this->model->update( [ "download_count" => 0, "register_count" => 0, "order_count" => 0 ], ["id" => $id] ); $this->channelClosingModel->save([ "channel_id" => $id, "download_config" => $channel["download_config"], "register_config" => $channel["register_config"], "order_config" => $channel["order_config"], "download_count" => $channel["download_count"], "register_count" => $channel["register_count"], "order_count" => $channel["order_count"], "total_amount" => fixed2Float($total_amount), "createtime" => time(), "updatetime" => time() ]); Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } finally { $redLock->unlock($lock); } $this->success("结算成功!"); } public function detail($id = null) { $this->assign("rows", $this->channelClosingModel->where("channel_id", $id)->order("createtime", "desc")->limit(0, 100)->select()); return $this->fetch(); } }