Activity.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace app\admin\controller\subscrip;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\common\service\CampaignService;
  6. use app\main\constants\ActivityConstants;
  7. use app\main\constants\PayConstants;
  8. use think\db;
  9. /**
  10. * 订阅活动列管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Activity extends Backend
  15. {
  16. /**
  17. * SubscripActivity模型对象
  18. */
  19. protected $model = null;
  20. protected $noNeedRight = ['ajaxlapse','upload','detail'];
  21. protected $noNeedLogin = ['ajaxlapse','upload','detail'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('SubscripActivity');
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags']);
  39. if ($this->request->isAjax())
  40. {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('pkey_name'))
  43. {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $total = $this->model
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->count();
  51. $popArr = $this->model->getPopSpaceList();
  52. $list = $this->model
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->limit($offset, $limit)
  56. ->select();
  57. if(!empty($list)){
  58. foreach($list as $k=>$v){
  59. $spaceArr = explode(',',$v['pop_space']);
  60. $spaceStr = '';
  61. if(!empty($spaceArr)) foreach($spaceArr as $vv){
  62. $spaceStr .=isset($popArr[$vv]) ? $popArr[$vv].',' : '';
  63. }
  64. $list[$k]['pop_space'] = trim($spaceStr,',');
  65. //判断活动是否开启
  66. $list[$k]['is_begin'] = time() > strtotime($v['begin_date']) ? 1 : 0;
  67. }
  68. }
  69. $result = array("total" => $total, "rows" => $list);
  70. return json($result);
  71. }
  72. return $this->view->fetch();
  73. }
  74. /**
  75. * 添加
  76. */
  77. public function add()
  78. {
  79. if ($this->request->isPost())
  80. {
  81. $params = $this->request->post("row/a");
  82. if ($params)
  83. {
  84. /*
  85. * 已经弃用,如果为了兼容老版可取消注释
  86. foreach ($params as $k => &$v)
  87. {
  88. $v = is_array($v) ? implode(',', $v) : $v;
  89. }
  90. */
  91. if ($this->dataLimit)
  92. {
  93. $params[$this->dataLimitField] = $this->auth->id;
  94. }
  95. if($params['begin_date'] <= date('Ymd',time())){
  96. $this->error('活动开始日期不能小于当前日期');
  97. }
  98. $params['pop_space'] = implode(',',$params['pop_space']);
  99. if(!$params['pop_space']){
  100. $this->error('请选择弹窗位置');
  101. }
  102. $params['end_date'] = date('Ymd',strtotime($params['begin_date'])+$params['days']*3600*24);
  103. $isHave = $this->model->get(['begin_date'=>$params['begin_date'],'state'=>1]);
  104. if (!empty($isHave)){
  105. $this->error('当前日期已经有活动');
  106. }
  107. try
  108. {
  109. //是否采用模型验证
  110. if ($this->modelValidate)
  111. {
  112. $name = basename(str_replace('\\', '/', get_class($this->model)));
  113. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  114. $this->model->validate($validate);
  115. }
  116. $result = $this->model->allowField(true)->save($params);
  117. $params['id'] = $this->model->id;
  118. $params['created_at'] = $this->model->created_at;
  119. $params['updated_at'] = $this->model->updated_at;
  120. if ($result !== false)
  121. {
  122. $this->delActRedis($params);
  123. $this->success();
  124. }
  125. else
  126. {
  127. $this->error($this->model->getError());
  128. }
  129. }
  130. catch (\think\exception\PDOException $e)
  131. {
  132. $this->error($e->getMessage());
  133. }
  134. }
  135. $this->error(__('Parameter %s can not be empty', ''));
  136. }
  137. // $goods_list = CampaignService::instance()->getCampaignGoods();
  138. $goods_list = model('Goods')->getGoodsList(PayConstants::BUSINESS_WECHAT, PayConstants::GOODS_CATEGORY_CAMPAIGN, PayConstants::GOODS_TYPE_VIP);
  139. $this->assign('goods_list', $goods_list);
  140. return $this->view->fetch();
  141. }
  142. /**
  143. * 编辑
  144. */
  145. public function edit($ids = NULL)
  146. {
  147. $row = $this->model->get($ids);
  148. if (!$row)
  149. $this->error(__('No Results were found'));
  150. $adminIds = $this->getDataLimitAdminIds();
  151. if (is_array($adminIds))
  152. {
  153. if (!in_array($row[$this->dataLimitField], $adminIds))
  154. {
  155. $this->error(__('You have no permission'));
  156. }
  157. }
  158. if ($this->request->isPost())
  159. {
  160. $params = $this->request->post("row/a");
  161. if ($params)
  162. {
  163. /*
  164. * 已经弃用,如果为了兼容老版可取消注释
  165. foreach ($params as $k => &$v)
  166. {
  167. $v = is_array($v) ? implode(',', $v) : $v;
  168. }
  169. */
  170. $params['pop_space'] = implode(',',$params['pop_space']);
  171. if(!$params['pop_space']){
  172. $this->error('请选择弹窗位置');
  173. }
  174. $params['id'] = $ids;
  175. if($params['begin_date'] <= date('Ymd',time())){
  176. $this->error('活动开始日期不能小于当前日期');
  177. }
  178. $params['end_date'] = date('Ymd',strtotime($params['begin_date'])+$params['days']*3600*24);
  179. $isHave = $this->model->where('id','<>',$ids)->where(['begin_date'=>$params['begin_date'],'state'=>1])->select();
  180. if (!empty($isHave)){
  181. $this->error('当前日期已经有活动');
  182. }
  183. try
  184. {
  185. //是否采用模型验证
  186. if ($this->modelValidate)
  187. {
  188. $name = basename(str_replace('\\', '/', get_class($this->model)));
  189. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  190. $row->validate($validate);
  191. }
  192. $result = $row->allowField(true)->save($params);
  193. if ($result !== false)
  194. {
  195. $this->delActRedis($params);
  196. $this->success();
  197. }
  198. else
  199. {
  200. $this->error($row->getError());
  201. }
  202. }
  203. catch (\think\exception\PDOException $e)
  204. {
  205. $this->error($e->getMessage());
  206. }
  207. }
  208. $this->error(__('Parameter %s can not be empty', ''));
  209. }
  210. $row['chapter_name'] = model('Book')::getChapterInfo($row['book_id'], $row['chapter_id'])['data']['name'];
  211. $this->view->assign("row", $row);
  212. return $this->view->fetch();
  213. }
  214. /**
  215. * 更新redis
  216. * @param $params
  217. */
  218. public function delActRedis($params)
  219. {
  220. $redisPre = ActivityConstants::SUBSCRIP_ACTIVITY;
  221. Redis::instance()->del($redisPre.$params['begin_date']);
  222. Redis::instance()->del($redisPre.$params['id']);
  223. return;
  224. }
  225. public function ajaxLapse()
  226. {
  227. $id = $this->request->param('id',0);
  228. $state = $this->request->param('state',0);
  229. if ( !$id || !$state ){
  230. $this->error('操作失败');
  231. }
  232. $state = $state == 2 ? 1 : 2;
  233. $obj = $this->model->find($id);
  234. if ( $obj->begin_date <= date('Ymd',time()) ){
  235. $this->error('活动已经开始,不可操作');
  236. }
  237. $isHave = $this->model->get(['begin_date'=>$obj->begin_date,'state'=>1]);
  238. if ( !empty($isHave) && $state == 1 ){
  239. $this->error('当前日期已经有活动');
  240. }
  241. $obj->state=$state;
  242. $obj->save();
  243. $this->success('操作成功');
  244. }
  245. public function upload()
  246. {
  247. $aid = $this->request->param('aid');
  248. $begin_date = $this->request->param('date');
  249. if ($this->request->isPost()) {
  250. $filePath = ROOT_PATH . DS . 'public' . DS . $this->request->param('file');
  251. $list = file($filePath);
  252. $list = array_map(function($v){
  253. $v = trim($v);
  254. return $v;
  255. },$list);
  256. $count = 0;
  257. if (!empty($list)){
  258. $redisPre = ActivityConstants::SUBSCRIP_USER_AUTH;
  259. foreach ($list as $user) {
  260. if ($count%5000 == 0){
  261. sleep(1);
  262. }
  263. $redisKey = $redisPre.$user;
  264. Redis::instance()->sadd($redisKey,$aid);
  265. Redis::instance()->expire($redisKey,3600*24);
  266. $count++;
  267. }
  268. }
  269. if ($count){
  270. $this->success();
  271. } else {
  272. $this->error('未查询到可上传信息');
  273. }
  274. }
  275. $this->view->assign('aid',$aid);
  276. $this->view->assign('begin_date',$begin_date);
  277. return $this->view->fetch();
  278. }
  279. public function detail()
  280. {
  281. $id = $this->request->param('id');
  282. $row = $this->model->get(['id'=>$id]);
  283. if (empty($row)){
  284. $this->error('参数错误');
  285. }
  286. $row['chapter_name'] = '-';
  287. if ($row['chapter_id']){
  288. $chapter_name = model('Book')::getChapterInfo($row['book_id'], $row['chapter_id']);
  289. $row['chapter_name'] = $chapter_name['data']['name'] ?? '-';
  290. }
  291. $row['book_name'] = model('Book')->where('id',$row['book_id'])->find()['name'] ?? $row['book_id'];
  292. if ($row['pop_space']){
  293. $arr = [
  294. 1=>'个人中心',
  295. 2=>'最近阅读',
  296. 3=>'书城首页'
  297. ];
  298. $row['pop_space_text'] = '';
  299. $pop_arr = explode(',',$row['pop_space']);
  300. if (!empty($pop_arr)){
  301. foreach ($pop_arr as $k =>$v){
  302. $row['pop_space_text'] .= $arr[$v] ? $arr[$v].',' : '';
  303. }
  304. }
  305. $row['pop_space_text'] = trim($row['pop_space_text'],',');
  306. }
  307. $this->view->assign('row',$row);
  308. return $this->view->fetch();
  309. }
  310. }