model = model('SubscripActivity'); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法 * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //设置过滤方法 $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(); $total = $this->model ->where($where) ->order($sort, $order) ->count(); $popArr = $this->model->getPopSpaceList(); $list = $this->model ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); if(!empty($list)){ foreach($list as $k=>$v){ $spaceArr = explode(',',$v['pop_space']); $spaceStr = ''; if(!empty($spaceArr)) foreach($spaceArr as $vv){ $spaceStr .=isset($popArr[$vv]) ? $popArr[$vv].',' : ''; } $list[$k]['pop_space'] = trim($spaceStr,','); //判断活动是否开启 $list[$k]['is_begin'] = time() > strtotime($v['begin_date']) ? 1 : 0; } } $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) { /* * 已经弃用,如果为了兼容老版可取消注释 foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; } */ if ($this->dataLimit) { $params[$this->dataLimitField] = $this->auth->id; } if($params['begin_date'] <= date('Ymd',time())){ $this->error('活动开始日期不能小于当前日期'); } $params['pop_space'] = implode(',',$params['pop_space']); if(!$params['pop_space']){ $this->error('请选择弹窗位置'); } $params['end_date'] = date('Ymd',strtotime($params['begin_date'])+$params['days']*3600*24); $isHave = $this->model->get(['begin_date'=>$params['begin_date'],'state'=>1]); if (!empty($isHave)){ $this->error('当前日期已经有活动'); } 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); } $result = $this->model->allowField(true)->save($params); $params['id'] = $this->model->id; $params['created_at'] = $this->model->created_at; $params['updated_at'] = $this->model->updated_at; if ($result !== false) { $this->delActRedis($params); $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', '')); } // $goods_list = CampaignService::instance()->getCampaignGoods(); $goods_list = model('Goods')->getGoodsList(PayConstants::BUSINESS_WECHAT, PayConstants::GOODS_CATEGORY_CAMPAIGN, PayConstants::GOODS_TYPE_VIP); $this->assign('goods_list', $goods_list); return $this->view->fetch(); } /** * 编辑 */ 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) { /* * 已经弃用,如果为了兼容老版可取消注释 foreach ($params as $k => &$v) { $v = is_array($v) ? implode(',', $v) : $v; } */ $params['pop_space'] = implode(',',$params['pop_space']); if(!$params['pop_space']){ $this->error('请选择弹窗位置'); } $params['id'] = $ids; if($params['begin_date'] <= date('Ymd',time())){ $this->error('活动开始日期不能小于当前日期'); } $params['end_date'] = date('Ymd',strtotime($params['begin_date'])+$params['days']*3600*24); $isHave = $this->model->where('id','<>',$ids)->where(['begin_date'=>$params['begin_date'],'state'=>1])->select(); if (!empty($isHave)){ $this->error('当前日期已经有活动'); } 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) { $this->delActRedis($params); $this->success(); } else { $this->error($row->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } $row['chapter_name'] = model('Book')::getChapterInfo($row['book_id'], $row['chapter_id'])['data']['name']; $this->view->assign("row", $row); return $this->view->fetch(); } /** * 更新redis * @param $params */ public function delActRedis($params) { $redisPre = ActivityConstants::SUBSCRIP_ACTIVITY; Redis::instance()->del($redisPre.$params['begin_date']); Redis::instance()->del($redisPre.$params['id']); return; } public function ajaxLapse() { $id = $this->request->param('id',0); $state = $this->request->param('state',0); if ( !$id || !$state ){ $this->error('操作失败'); } $state = $state == 2 ? 1 : 2; $obj = $this->model->find($id); if ( $obj->begin_date <= date('Ymd',time()) ){ $this->error('活动已经开始,不可操作'); } $isHave = $this->model->get(['begin_date'=>$obj->begin_date,'state'=>1]); if ( !empty($isHave) && $state == 1 ){ $this->error('当前日期已经有活动'); } $obj->state=$state; $obj->save(); $this->success('操作成功'); } public function upload() { $aid = $this->request->param('aid'); $begin_date = $this->request->param('date'); if ($this->request->isPost()) { $filePath = ROOT_PATH . DS . 'public' . DS . $this->request->param('file'); $list = file($filePath); $list = array_map(function($v){ $v = trim($v); return $v; },$list); $count = 0; if (!empty($list)){ $redisPre = ActivityConstants::SUBSCRIP_USER_AUTH; foreach ($list as $user) { if ($count%5000 == 0){ sleep(1); } $redisKey = $redisPre.$user; Redis::instance()->sadd($redisKey,$aid); Redis::instance()->expire($redisKey,3600*24); $count++; } } if ($count){ $this->success(); } else { $this->error('未查询到可上传信息'); } } $this->view->assign('aid',$aid); $this->view->assign('begin_date',$begin_date); return $this->view->fetch(); } public function detail() { $id = $this->request->param('id'); $row = $this->model->get(['id'=>$id]); if (empty($row)){ $this->error('参数错误'); } $row['chapter_name'] = '-'; if ($row['chapter_id']){ $chapter_name = model('Book')::getChapterInfo($row['book_id'], $row['chapter_id']); $row['chapter_name'] = $chapter_name['data']['name'] ?? '-'; } $row['book_name'] = model('Book')->where('id',$row['book_id'])->find()['name'] ?? $row['book_id']; if ($row['pop_space']){ $arr = [ 1=>'个人中心', 2=>'最近阅读', 3=>'书城首页' ]; $row['pop_space_text'] = ''; $pop_arr = explode(',',$row['pop_space']); if (!empty($pop_arr)){ foreach ($pop_arr as $k =>$v){ $row['pop_space_text'] .= $arr[$v] ? $arr[$v].',' : ''; } } $row['pop_space_text'] = trim($row['pop_space_text'],','); } $this->view->assign('row',$row); return $this->view->fetch(); } }