| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- namespace app\admin\controller\store;
- use app\admin\model\Area;
- use app\api\service\BDService;
- use app\common\controller\Backend;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- /**
- * 球房管理
- *
- * @icon fa fa-circle-o
- */
- class Store extends Backend
- {
- protected $noNeedRight = ["fetchWhere", "profit_rate"];
- /**
- * Store模型对象
- * @var \app\admin\model\store\Store
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\store\Store;
- $this->assignconfig(["admin" => $this->auth->getUserInfo()]);
- $this->assign("is_edit_name", $this->auth->getUserInfo()["type"] == \E_IDENTITY_TYPE::Platform);
- $this->view->assign("statusList", $this->model->getStatusList());
- }
- private function fetchWhere()
- {
- $admin = $this->auth->getUserInfo();
- if (!$admin)
- $this->error("error");
- $p_where = [];
- if (\E_ADMIN_TYPE::Store === $admin["type"]) {
- array_push($p_where, ["id", "=", $admin["store_id"]]);
- }
- 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']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->c_selectpage($this->fetchWhere());
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $query = $this->model
- ->where($where);
- foreach ($this->fetchWhere() as $item) {
- $query->where($item[0], $item[1], $item[2]);
- }
- $list = $query->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
- $row->visible(['id', 'name', 'legal_person_name', 'invite_qr_code', 'profit_amount', 'photo_images', 'tags', 'star', 'phone', 'email', 'area_city', 'address', 'status', 'hot', 'sort', 'updatetime', 'city_code']);
- }
- $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);
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- if(mb_strlen($params["area_city"]) > 0) {
- $area_city = join(',', explode("/", $params["area_city"]));
- } else {
- $area_city = "湖南省,长沙市,长沙县";
- }
- $area = (new Area())->where("mergename", "中国,{$area_city}")->find();
- if (!$area)
- $this->error("地区选择错误!");
- $params["county_code"] = $area["area_code"];
- $params["city_code"] = (int)($area["area_code"] / 100) * 100;
- $params["p_code"] = (int)($area["area_code"] / 1000) * 1000;
- if ((($area["area_code"] / 100) - (int)($area["area_code"] / 100)) == 0) {
- $this->error("请选择区/县");
- }
- $params["lng"] = $area["lng"];
- $params["lat"] = $area["lat"];
- $s_res = BDService::geocoding($params["address"] ?? '');
- if ($s_res) {
- $params["lng"] = $s_res->data()["lng"];
- $params["lat"] = $s_res->data()["lat"];
- }
- $params["status"] = $this->auth->getUserInfo()["type"] == \E_IDENTITY_TYPE::Platform ? \E_BASE_STATUS::Normal : \E_BASE_STATUS::Checking;
- $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);
- }
- $store_id = $this->model->allowField(true)->insertGetId($params);
- $qr_code = \qrcodeBase64("https://pbh5.xunsoftware.com/pages/user/login", ["invite_store_id" => $store_id]);
- $this->model->update([
- "invite_qr_code" => $qr_code
- ], ["id" => $store_id]);
- $result = true;
- 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)
- {
- $admin = $this->auth->getUserInfo();
- $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,
- 'admin_type' => $admin["type"]
- ]);
- 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_city = join(',', explode("/", $params["area_city"]));
- $area = (new Area())->where("mergename", "中国,{$area_city}")->find();
- if (!$area)
- $this->error("地区选择错误!");
- $params["lng"] = $area["lng"];
- $params["lat"] = $area["lat"];
- $params["county_code"] = $area["area_code"];
- $params["city_code"] = (int)($area["area_code"] / 100) * 100;
- $params["p_code"] = (int)($area["area_code"] / 1000) * 1000;
- if ((($area["area_code"] / 100) - (int)($area["area_code"] / 100)) == 0) {
- $this->error("请选择区/县");
- }
- $s_res = BDService::geocoding($params["address"] ?? '');
- if ($s_res) {
- $params["lng"] = $s_res->data()["lng"];
- $params["lat"] = $s_res->data()["lat"];
- }
- if ($admin["type"] !== \E_IDENTITY_TYPE::Platform) {
- unset($params["status"]);
- }
- $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);
- }
- if (null === $row["invite_qr_code"]) {
- $params["invite_qr_code"] = \qrcodeBase64("https://pbh5.xunsoftware.com/pages/user/login", ["invite_store_id" => $ids]);
- }
- $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 profit_rate($ids = null)
- {
- $admin = $this->auth->getUserInfo();
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- if (false === $this->request->isPost()) {
- $this->view->assign([
- 'row' => $row,
- 'admin_type' => $admin["type"]
- ]);
- return $this->view->fetch();
- }
- $params = $this->request->param();
- $platform_profit_rate = isset($params["platform_profit_rate"]) ? $params["platform_profit_rate"] : $row["platform_profit_rate"];
- $agency_profit_rate = isset($params["agency_profit_rate"]) ? $params["agency_profit_rate"] : $row["agency_profit_rate"];
- $this->model->update([
- "platform_profit_rate" => $platform_profit_rate,
- "agency_profit_rate" => $agency_profit_rate
- ], ["id" => $row["id"]]);
- $this->success();
- }
- }
|