123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use think\Controller;
- use think\Log;
- use think\Request;
- /**
- * 活动资源管理
- *
- * @icon fa fa-circle-o
- */
- class Resource extends Backend
- {
- /**
- * @var \app\common\model\Resource
- */
- protected $model = null;
- /**
- * @var int
- */
- protected $aid = 0;
- protected $resource_type = null;
- protected $business_line = 0;
- protected $admin_id;
- public $noNeedRight =['select'];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Resource');
- $this->view->assign("showTypeList", $this->model->getShowTypeList());
- $this->view->assign("typeList", $this->model->getTypeList());
- $this->view->assign("statusList", $this->model->getStatusList());
- $this->view->assign("popRangeList", $this->model->getPopRangeList());
- $this->view->assign("classifyList", $this->model->getClassifyList());
- $this->aid = $this->request->get('aid');
- if ($this->request->has('resource_type')) {
- $this->resource_type = intval($this->request->get('resource_type'));
- }
- $this->assign('aid', $this->aid);
- $this->assign('resource_type', $this->resource_type ?? '');
- $this->assignconfig('aid', $this->aid);
- $this->assignconfig('resource_type', $this->resource_type ?? '');
- //业务线
- if ($this->request->has('business_line')) {
- $this->business_line = intval($this->request->get('business_line'));
- }
- $this->assignconfig('business_line', $this->business_line);
- $this->assign('business_line', $this->business_line);
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
- * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- $map['activity_id'] = $this->aid;
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name')) {
- return $this->selectpage();
- }
- if ($this->resource_type !== null) {
- $map['resource_type'] = $this->resource_type;
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->where($map)
- ->count();
- $list = $this->model->alias('r')
- ->join(['goods' => 'g'], 'r.goods_id = g.id', 'LEFT')
- ->field([
- 'r.*',
- 'g.show_type' => 'g_show_type',
- 'g.type' => 'g_type',
- 'g.money' => 'g_money',
- 'g.kandian' => 'g_kandian',
- 'g.free_kandian' => 'g_free_kandian',
- 'g.icon' => 'g_icon',
- 'g.free_day' => 'g_free_day',
- 'g.day' => 'g_day',
- ])
- ->where($where)
- ->where($map)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- public function select(){
- $map['activity_id'] = $this->aid;
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax())
- {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name'))
- {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- if ($this->resource_type !== null) {
- $map['resource_type'] = $this->resource_type;
- }
- $total = $this->model
- ->where($where)
- ->where($map)
- ->count();
- $list = $this->model->alias('r')
- ->join(['goods' => 'g'], 'r.goods_id = g.id', 'LEFT')
- ->field([
- 'r.*',
- 'g.show_type' => 'g_show_type',
- 'g.type' => 'g_type',
- 'g.money' => 'g_money',
- 'g.kandian' => 'g_kandian',
- 'g.free_kandian' => 'g_free_kandian',
- 'g.icon' => 'g_icon',
- 'g.free_day' => 'g_free_day',
- 'g.day' => 'g_day',
- ])
- ->where($where)
- ->where($map)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- if ($this->dataLimit)
- {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- try
- {
- //是否采用模型验证
- if ($this->modelValidate)
- {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
- $this->model->validate($validate);
- }
- if (isset($params['activity_id']) && $params['activity_id'] > 0) {
- //验证平台活动 是否已经添加了资源
- if (model("Resource")->where('activity_id', 'eq', $params['activity_id'])->count() > 0) {
- $this->error("活动仅支持配置一条资源");
- }
- }
- $result = $this->model->allowField(true)->save($params);
- if ($result !== false)
- {
- $this->success();
- }
- else
- {
- $this->error($this->model->getError());
- }
- }
- catch (\think\exception\PDOException $e)
- {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- if (in_array($this->resource_type, [0, 1, 2])) {
- return $this->view->fetch('addgoodsresource');
- } else {
- return $this->view->fetch('add');
- }
- }
- /**
- * 编辑
- */
- 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))
- {
- if (!in_array($row[$this->dataLimitField], $adminIds))
- {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- //$params['warning'] = str_replace("/n","<br/>",$params['warning']);die;
- Log::write($params['warning'],'congcongtest');
- try
- {
- //是否采用模型验证
- if ($this->modelValidate)
- {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
- $row->validate($validate);
- }
- $result = $row->allowField(true)->save($params);
- if ($result !== false)
- {
- //清一下缓存
- $key = 'A-R-R:'.$ids;
- $key2 = 'R-I:'.$ids;
- Redis::instance()->del($key);
- Redis::instance()->del($key2);
- $this->success();
- }
- else
- {
- $this->error($row->getError());
- }
- }
- catch (\think\exception\PDOException $e)
- {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $goods = [
- 'show_type_text' => '',
- 'type_text' => '',
- 'money' => '',
- 'kandian' => '',
- 'free_kandian' => '',
- 'icon' => '',
- 'id' => '',
- 'free_day' => '',
- 'type' => '',
- 'day' => '',
- ];
- if (!empty($row['goods_id'])) {
- $goodsModel = model('goods');
- $goods = $goodsModel->where('id', $row['goods_id'])->find();
- }
- $this->view->assign("goods", $goods);
- $this->view->assign("row", $row);
- if (in_array($this->resource_type, [0, 1])) {
- return $this->view->fetch('editgoodsresource');
- } else {
- return $this->view->fetch('edit');
- }
- }
- }
|