Activity.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\library\ShortUrl;
  4. use app\common\controller\Backend;
  5. use app\common\library\Redis;
  6. use app\main\constants\AdminConstants;
  7. use app\main\constants\PayConstants;
  8. use app\main\service\AdminService;
  9. use app\common\service\CardFlipService;
  10. use GuzzleHttp\Client as Http;
  11. use think\Config;
  12. use think\Db;
  13. /**
  14. * 活动管理
  15. *
  16. * @icon fa fa-circle-o
  17. */
  18. class Activity extends Backend
  19. {
  20. /**
  21. * Activity模型对象
  22. */
  23. protected $model = null;
  24. protected $activity_type = 0;
  25. protected $business_line = 0;
  26. //平台活动
  27. private const ACTIVITY_TYPE_PLATFORM = 0;
  28. //渠道自定义活动
  29. private const ACTIVIYT_TYPE_CHANNEL = 1;
  30. //赠币活动
  31. private const ACTIVIYT_TYPE_GIVE = 3;
  32. //APP活动
  33. private const ACTIVITY_TYPE_APP = 2;
  34. public $noNeedRight = ['addchannel','index_ajax','index_give_activity','index_channel_activity'];
  35. public function _initialize()
  36. {
  37. parent::_initialize();
  38. $this->model = model('Activity');
  39. $this->view->assign("statusList", $this->model->getStatusList());
  40. $this->view->assign("popStatusList", $this->model->getPopStatusList());
  41. $this->view->assign("popRangeList", $this->model->getPopRangeList());
  42. $this->assignconfig("popRangeList", $this->model->getPopRangeList());
  43. $this->activity_type = intval($this->request->param('type') ?? 0);
  44. $this->assign('activity_type',$this->activity_type);
  45. $this->assignconfig('activity_type',$this->activity_type);
  46. $this->business_line = PayConstants::BUSINESS_WECHAT;
  47. if (in_array($this->activity_type, [2])) {
  48. $this->business_line = PayConstants::BUSINESS_APP;
  49. }
  50. $this->assignconfig('business_line', $this->business_line);
  51. }
  52. /**
  53. * 生成短链
  54. * @return \think\response\Json
  55. */
  56. public function sorturl(){
  57. $ids = empty($_GET['ids'])?'':$_GET['ids'];
  58. $error = json(['error'=>1]);
  59. if(!$ids){
  60. return $error;
  61. }
  62. $admin_id = $this->auth->id;
  63. $param = [];
  64. $param['aid'] = $ids;
  65. $param['admin_id'] = $admin_id;
  66. /**
  67. * 跳转短链生成格式
  68. */
  69. $channel_id = $this->auth->agent_id ? $this->auth->agent_id : $this->auth->channel_id;
  70. $jmp_url = getCurrentDomain($channel_id,'/s/' . $ids);
  71. $shorturl = new ShortUrl();
  72. $hasRecode = model('activity_shortlink')->where($param)->find();
  73. if(!$param['qq'] = $shorturl->tencent($channel_id,$jmp_url)){
  74. return $error;
  75. }
  76. if(!$param['sina'] = $shorturl->sina($jmp_url)){
  77. return $error;
  78. }
  79. $param['updatetime'] = time();
  80. if($hasRecode){ //update
  81. $result = model('activity_shortlink')->update($param,['aid'=>$ids,'admin_id'=>$admin_id]);
  82. }else{ //insert
  83. $param['createtime'] = time();
  84. $result = model('activity_shortlink')->insert($param);
  85. }
  86. if($result){
  87. return json(['error'=>0,'data'=>$param]);
  88. }else{
  89. return $error;
  90. }
  91. }
  92. /**
  93. * 查看
  94. */
  95. public function index()
  96. {
  97. $give_tab = 1;
  98. $activity_config = Config::get('site.activity_config');
  99. $activity_give_config = Config::get('site.activity_give_config');
  100. if($this->auth->agent_id || $this->group == 3){
  101. if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
  102. $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
  103. }
  104. }
  105. //设置过滤方法
  106. $this->request->filter(['strip_tags']);
  107. $this->assignconfig('time',$this->time);
  108. $this->assign('time',$this->time);
  109. $this->assign('group',$this->group);
  110. $today = date('Ymd',$this->time);
  111. /**
  112. * 跳转短链生成格式
  113. */
  114. if($this->group == 4){
  115. if($this->auth->agent_id){
  116. $channel_id = $this->auth->agent_id;
  117. }else{
  118. $channel_id = $this->auth->channel_id;
  119. }
  120. }
  121. if($this->group == 3){
  122. $channel_id = $this->auth->channel_id;
  123. }
  124. $distribute = 0;
  125. $adminExtend = model('admin_extend')->where(['admin_id'=>$this->auth->id])->find();
  126. if(!empty($adminExtend->distribute) && $adminExtend->distribute == '1' ){
  127. $distribute = 1;
  128. }
  129. $this->assign('distribute',$distribute);
  130. $activityShowRechargePage = 0;
  131. if($this->group<3 || $this->group==6) {
  132. if ($this->request->isAjax()) {
  133. //如果发送的来源是Selectpage,则转发到Selectpage
  134. if ($this->request->request('pkey_name')) {
  135. return $this->selectpage();
  136. }
  137. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  138. $total = $this->model
  139. ->where($where)
  140. ->where('type',$this->activity_type)
  141. ->count();
  142. $list = $this->model
  143. ->join('activity_collect c','activity.id=c.aid and c.createdate='.$today.' and c.admin_id=0','left')
  144. ->join('activity_collect_all a','activity.id=a.aid and a.admin_id=0','left')
  145. ->field('activity.*,c.money,a.money as allmoney')
  146. ->where($where)
  147. ->where('activity.type',$this->activity_type)
  148. ->order($sort, $order)
  149. ->limit($offset, $limit)
  150. ->select();
  151. if ($list && in_array($this->activity_type, [0, 2])) {
  152. //平台或APP判断是否已经设置资源
  153. foreach ($list as &$item) {
  154. $resourceModel = model("Resource")->field("id")->where('activity_id', 'eq', $item['id'])->find();
  155. $item['resource_id'] = 0;
  156. if ($resourceModel) {
  157. $item['resource_id'] = $resourceModel['id'];
  158. }
  159. }
  160. }
  161. $result = array("total" => $total, "rows" => $list);
  162. return json($result);
  163. }
  164. return $this->view->fetch();
  165. }else{
  166. if($this->group == 3 || ($this->group == 4 && $distribute==1)){
  167. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channel_id);
  168. $activityShowRechargePage = $adminConfig['activity_show_recharge_page'];
  169. // $adminConfig = model('AdminConfig')->get($this->auth->id);
  170. if ($adminConfig['give_kandian'] == 0) {
  171. $give_tab = 0;
  172. }
  173. $ac_map = [];
  174. $this->assign('give_tab', $give_tab);
  175. //活动状态
  176. if($this->activity_type == self::ACTIVITY_TYPE_PLATFORM){
  177. $ac_map['activity.status'] = '1';
  178. }
  179. if($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL || $this->activity_type == self::ACTIVIYT_TYPE_GIVE){ //渠道自定义活动 & 赠币活动
  180. //渠道商处理
  181. if($this->group == AdminConstants::ADMIN_GROUP_ID_CHANNEL){
  182. $ac_map['activity.admin_id'] = $this->auth->id;
  183. }
  184. //配号代理商
  185. if($this->group == AdminConstants::ADMIN_GROUP_ID_AGENT && $distribute==1){
  186. $ac_map['activity.admin_id'] = ['in',"{$this->auth->id},{$this->auth->channel_id}"];
  187. }elseif($this->group == AdminConstants::ADMIN_GROUP_ID_AGENT){
  188. //代理商
  189. $ac_map['activity.admin_id'] = $this->auth->channel_id;
  190. }
  191. }
  192. //不展示系统活动
  193. $ac_map['is_system'] = ['eq', 0];
  194. $res = model('activity')
  195. ->join('activity_collect c','activity.id=c.aid and c.createdate='.$today.' and c.admin_id='.$this->auth->id,'left')
  196. ->join('activity_collect_all a','activity.id=a.aid and a.admin_id='.$this->auth->id,'left')
  197. ->join('activity_shortlink s','activity.id= s.aid and s.admin_id='.$this->auth->id,'left')
  198. ->where($ac_map)
  199. ->where('activity.id <> 100')
  200. ->where('activity.type',$this->activity_type)
  201. ->field('activity.*,c.money,a.money as allmoney,s.qq as qq, s.sina as sina,c.recharge_count,a.recharge_count as arecharge_count')
  202. ->order('activity.createtime','desc')
  203. ->paginate(10,false,['query'=>['type'=>$this->activity_type]]);
  204. }else{ //平台活动
  205. $res = model('activity')
  206. ->join('activity_collect c','activity.id=c.aid and c.createdate='.$today.' and c.admin_id='.$this->auth->id,'left')
  207. ->join('activity_collect_all a','activity.id=a.aid and a.admin_id='.$this->auth->id,'left')
  208. ->where('activity.status','1')
  209. ->where('activity.id <> 100')
  210. ->where('activity.type',$this->activity_type)
  211. ->field('activity.*,c.money,a.money as allmoney,c.recharge_count,a.recharge_count as arecharge_count')
  212. ->order('activity.createtime','desc')
  213. ->paginate(10,false,['query'=>['type'=>$this->activity_type]]);
  214. }
  215. $rangeTitleArr = $this->model->getPopRangeList();
  216. foreach ($res as $index => $vo) {
  217. $vo['url'] = '';
  218. //弹窗位置
  219. $popRangeStr = '';
  220. if ($vo['pop_range'] || $vo['pop_range'] == '0') {
  221. $rangeArr = explode(',', $vo['pop_range']);
  222. foreach ($rangeArr as $k) {
  223. $popRangeStr .= $rangeTitleArr[$k] .' ';
  224. }
  225. } else {
  226. $popRangeStr = '-';
  227. }
  228. $vo['pop_range_txt'] = $popRangeStr;
  229. if ($vo['endtime'] > time()) {
  230. switch ($vo['type']) {
  231. case 0:
  232. $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id']);
  233. break;
  234. case 1:
  235. $activity_config = Config::get('site.activity_config');
  236. if (isset($activity_config['config'][$vo['config_id']]) && $activity_config['config'][$vo['config_id']]['status'] == 1) {
  237. $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id'] . '/r/' . $activity_config['config'][$vo['config_id']]['resource_id']);
  238. }
  239. break;
  240. case 3:
  241. $activity_give_config = Config::get('site.activity_give_config');
  242. if (isset($activity_give_config['config'][$vo['config_id']]) && $activity_give_config['config'][$vo['config_id']]['status'] == 1) {
  243. $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id'] . '/r/' . $activity_give_config['config'][$vo['config_id']]['resource_id']);
  244. }
  245. break;
  246. }
  247. }
  248. $res[$index] = $vo;
  249. }
  250. if ($this->request->isAjax()) {
  251. return json($res);
  252. }
  253. $subscrip = model('ChannelSpecialManage')->isWhite('subscribe', $this->auth->id);
  254. $camUrl = CardFlipService::instance()->checkAuth($this->auth->id);
  255. $this->assign('camUrl',$camUrl);
  256. $this->assign('subscrip',$subscrip);
  257. $this->assign('activity_config',$activity_config);
  258. $this->assign('activity_give_config',$activity_give_config);
  259. $this->assign('channel_id',$channel_id);
  260. $this->assign('admin_id',$this->auth->id);
  261. $this->assign('res',$res);
  262. $this->assign('activity_show_recharge_page', $activityShowRechargePage);
  263. return $this->view->fetch();
  264. }
  265. }
  266. /**
  267. * 活动列表(所有活动)
  268. * @return \think\response\Json
  269. */
  270. public function index_ajax()
  271. {
  272. list($where, $whereFilter, $ac_map) = $this->_buildCommonCondition();
  273. $res = model('activity')//渠道自定义活动 + 赠币活动
  274. ->where('activity.id <> 100')
  275. ->where($where)
  276. ->where($whereFilter)
  277. ->where($ac_map)
  278. ->order('activity.createtime', 'desc')
  279. ->select();
  280. $respt = model('activity')//平台活动
  281. ->where('activity.id <> 100')
  282. ->where('activity.type',self::ACTIVITY_TYPE_PLATFORM)
  283. ->where($where)
  284. ->where($whereFilter)
  285. ->order('activity.createtime', 'desc')
  286. ->select();
  287. $res = array_merge($respt, $res);
  288. $return = $this->_formatActivityUrl($res);
  289. if ($this->request->isAjax()) {
  290. return json($return);
  291. }
  292. }
  293. /**
  294. * 活动列表(渠道自定义活动 & 平台活动)
  295. * @return \think\response\Json
  296. */
  297. public function index_channel_activity()
  298. {
  299. list($where, $whereFilter, $ac_map) = $this->_buildCommonCondition();
  300. $res = model('activity')//渠道自定义活动
  301. ->where('activity.id <> 100')
  302. ->where('activity.type',self::ACTIVIYT_TYPE_CHANNEL)
  303. ->where($where)
  304. ->where($whereFilter)
  305. ->where($ac_map)
  306. ->order('activity.createtime', 'desc')
  307. ->select();
  308. $respt = model('activity')//平台活动
  309. ->where('activity.id <> 100')
  310. ->where('activity.type',self::ACTIVITY_TYPE_PLATFORM)
  311. ->where($where)
  312. ->where($whereFilter)
  313. ->order('activity.createtime', 'desc')
  314. ->select();
  315. $res = array_merge($respt, $res);
  316. $return = $this->_formatActivityUrl($res);
  317. if ($this->request->isAjax()) {
  318. return json($return);
  319. }
  320. }
  321. /**
  322. * 赠币活动列表
  323. * @return \think\response\Json
  324. */
  325. public function index_give_activity()
  326. {
  327. list($where, $whereFilter, $ac_map) = $this->_buildCommonCondition();
  328. $res = model('activity')
  329. ->where('activity.id <> 100')
  330. ->where('activity.type',self::ACTIVIYT_TYPE_GIVE)
  331. ->where($where)
  332. ->where($whereFilter)
  333. ->where($ac_map)
  334. ->order('activity.createtime', 'desc')
  335. ->select();
  336. $return = $this->_formatActivityUrl($res);
  337. if ($this->request->isAjax()) {
  338. return json($return);
  339. }
  340. }
  341. /**
  342. * 格式化活动链接
  343. * @param $res
  344. * @return array
  345. */
  346. private function _formatActivityUrl($res){
  347. $return = [];
  348. if ($res) {
  349. $channel_id = $this->auth->agent_id ? $this->auth->agent_id : $this->auth->channel_id;
  350. foreach ($res as $index => $vo) {
  351. if ($vo['endtime'] > time()) {
  352. switch ($vo['type']) {
  353. case 0:
  354. $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id']);
  355. $return[] = $vo;
  356. break;
  357. case 1:
  358. $activity_config = Config::get('site.activity_config');
  359. if (isset($activity_config['config'][$vo['config_id']]) && $activity_config['config'][$vo['config_id']]['status'] == 1) {
  360. $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id'] . '/r/' . $activity_config['config'][$vo['config_id']]['resource_id']);
  361. $return[] = $vo;
  362. }
  363. break;
  364. case 3:
  365. $activity_give_config = Config::get('site.activity_give_config');
  366. if (isset($activity_give_config['config'][$vo['config_id']]) && $activity_give_config['config'][$vo['config_id']]['status'] == 1) {
  367. $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id'] . '/r/' . $activity_give_config['config'][$vo['config_id']]['resource_id']);
  368. $return[] = $vo;
  369. }
  370. break;
  371. }
  372. }
  373. }
  374. }
  375. return $return;
  376. }
  377. /**
  378. * 构造活动查询列表查询条件
  379. * @return array
  380. */
  381. private function _buildCommonCondition()
  382. {
  383. $distribute = 0;
  384. $adminExtend = model('admin_extend')->where(['admin_id' => $this->auth->id])->find();
  385. if (!empty($adminExtend->distribute) && $adminExtend->distribute == '1') {
  386. $distribute = 1;
  387. }
  388. $ac_map = ['activity.status' => '1'];
  389. //渠道商处理
  390. if ($this->group == AdminConstants::ADMIN_GROUP_ID_CHANNEL) {
  391. $ac_map['activity.admin_id'] = $this->auth->id;
  392. }
  393. //配号代理商
  394. if ($this->group == AdminConstants::ADMIN_GROUP_ID_AGENT && $distribute == 1) {
  395. $ac_map['activity.admin_id'] = ['in', "{$this->auth->id},{$this->auth->channel_id}"];
  396. } elseif ($this->group == AdminConstants::ADMIN_GROUP_ID_AGENT) {
  397. //代理商
  398. $ac_map['activity.admin_id'] = $this->auth->channel_id;
  399. }
  400. list($whereFilter, $sort, $order, $offset, $limit) = $this->buildparams();
  401. $where = [];
  402. if ($this->request->request('name')) {
  403. $where['name'] = ['LIKE', '%' . $this->request->request('name') . '%'];
  404. }
  405. return [$where, $whereFilter, $ac_map];
  406. }
  407. public function config(){
  408. if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
  409. $map = ['group'=>'activity','name'=>'activity_config'];
  410. $config_res = model('Config')->where($map)->value('value');
  411. } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE){
  412. $map = ['group'=>'activity','name'=>'activity_give_config'];
  413. $config_res = model('Config')->where($map)->value('value');
  414. }else{
  415. $config_res = [];
  416. $this->error("错误的请求");
  417. }
  418. if($this->request->isAjax()){
  419. $params = $this->request->param();
  420. if(!isset($params['config'])){
  421. $this->error('参数错误');
  422. }
  423. $config = $params['config'];
  424. unset($params['config']);
  425. foreach($config['id'] as $key => $id){
  426. if(isset($config['name'][$key]) && isset($config['resource_id'][$key]) && isset($config['resource_id'][$key])){
  427. $params['config'][$id] = [
  428. 'name' => $config['name'][$key],
  429. 'resource_id' => $config['resource_id'][$key],
  430. 'status' => $config['status'][$key],
  431. 'limit_day' => $config['limit_day'][$key]
  432. ];
  433. }
  434. }
  435. if(model('Config')->where($map)->find()){
  436. $is_ok = model('Config')->where($map)->update(['value'=>json_encode($params)]);
  437. }else{
  438. if($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL){
  439. $is_ok = model('Config')->insert(array_merge($map,[
  440. 'content'=>'活动自定义配置',
  441. 'value'=>json_encode($params),
  442. 'type' => 'array',
  443. ]));
  444. } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
  445. $is_ok = model('Config')->insert(array_merge($map,[
  446. 'content'=>'赠币活动自定义配置',
  447. 'value'=>json_encode($params),
  448. 'type' => 'array'
  449. ]));
  450. }else{
  451. $is_ok = false;
  452. }
  453. }
  454. $redis = Redis::instance();
  455. $redis->del('site');
  456. if($is_ok !== false){
  457. $this->success('操作成功');
  458. }else{
  459. $this->error('操作失败');
  460. }
  461. }
  462. $this->assign('configRes',json_decode($config_res,true));
  463. return $this->view->fetch();
  464. }
  465. public function changestatus(){
  466. $id = $_POST['id'];
  467. $status = $_POST['status'];
  468. $row = $this->model->where('id', 'eq', $id)->find();
  469. $res = Db::table('activity')->where(['id'=>$id])->update(['status'=>$status]);
  470. //删除前台弹窗redis
  471. if ($row['type'] == self::ACTIVIYT_TYPE_CHANNEL) {
  472. $popKey = "S-P-T-C:N:{$row['admin_id']}";
  473. Redis::instance()->del($popKey);
  474. } elseif ($row['type'] == self::ACTIVIYT_TYPE_GIVE) {
  475. $popKey = "S-P-T-G:N:{$row['admin_id']}";
  476. Redis::instance()->del($popKey);
  477. } elseif ($this->activity_type == self::ACTIVITY_TYPE_PLATFORM) {
  478. $popKey = "S-P-T-P:N:";
  479. Redis::instance()->del($popKey);
  480. }
  481. return $res;
  482. }
  483. public function changepopstatus(){
  484. $id = $_POST['id'];
  485. $status = $_POST['status'];
  486. $row = $this->model->where('id', 'eq', $id)->find();
  487. $res = Db::table('activity')->where(['id'=>$id])->update(['pop_status'=>$status]);
  488. //删除前台弹窗redis
  489. if ($row['type'] == self::ACTIVIYT_TYPE_CHANNEL) {
  490. $popKey = "S-P-T-C:N:{$row['admin_id']}";
  491. Redis::instance()->del($popKey);
  492. } elseif ($row['type'] == self::ACTIVIYT_TYPE_GIVE) {
  493. $popKey = "S-P-T-G:N:{$row['admin_id']}";
  494. Redis::instance()->del($popKey);
  495. } elseif ($this->activity_type == self::ACTIVITY_TYPE_PLATFORM) {
  496. $popKey = "S-P-T-P:N:";
  497. Redis::instance()->del($popKey);
  498. }
  499. return $res;
  500. }
  501. public function showrechargepage()
  502. {
  503. if ($this->request->isPost()) {
  504. $id = $_POST['id'];
  505. $status = $_POST['status'];
  506. $iStatus = $status == 'on' ? 1 : 0;
  507. $res = $this->model->where(['id' => $id])->update(['show_for_recharge_page' => $iStatus]);
  508. return $res;
  509. }
  510. return false;
  511. }
  512. /**
  513. * 添加
  514. */
  515. public function add()
  516. {
  517. $redis = Redis::instance();
  518. if($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL){
  519. $activity_config = Config::get('site.activity_config');
  520. } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE){
  521. $activity_config = Config::get('site.activity_give_config');
  522. }else{
  523. $activity_config = [];
  524. }
  525. if ($this->request->isPost())
  526. {
  527. $params = $this->request->post("row/a");
  528. if ($params)
  529. {
  530. $params['status'] = 1;//自定义活动 状态永远是正常
  531. if (!in_array($this->activity_type, [self::ACTIVIYT_TYPE_CHANNEL, self::ACTIVIYT_TYPE_GIVE])) {
  532. //非自定义活动或赠币活动
  533. if (in_array($this->group, [3, 4, 7, 8])) {
  534. //这些角色不能创建
  535. $this->error("没有权限!");
  536. }
  537. }
  538. $redisKeyPrefix = 'AT-M:';
  539. if (in_array($this->group, [7, 8])) {
  540. //VIP或VIP创建模板
  541. if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL || $this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
  542. $params['admin_id'] = $this->auth->id;
  543. $config_id = $params['config_id'] ?? null;
  544. if(!$config_id){
  545. $this->error("活动金额不能为空!");
  546. }
  547. //渠道商自定义活动限制活动时间
  548. $vali_time = strtotime($params['endtime']) - strtotime($params['starttime']);
  549. //config_id来判断活动时间
  550. if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
  551. $map = ['group'=>'activity','name'=>'activity_config'];
  552. $config_res = model('Config')->where($map)->value('value');
  553. } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE){
  554. $map = ['group'=>'activity','name'=>'activity_give_config'];
  555. $config_res = model('Config')->where($map)->value('value');
  556. }
  557. $configArr = json_decode($config_res,true);
  558. $source = $configArr['config'][$params['config_id']] ?? [];
  559. if ($source && isset($source['limit_day']) && $source['limit_day']) {
  560. if($vali_time > (intval($source['limit_day'])*86400)){
  561. $this->error("活动时间间隔不能大于 {$source['limit_day']} 天");
  562. }
  563. } else {
  564. if($vali_time > (intval($activity_config['time_interval'])*86400)){
  565. $this->error("活动时间间隔不能大于 {$activity_config['time_interval']} 天");
  566. }
  567. }
  568. }
  569. } else {
  570. //检查渠道商和配号代理商
  571. if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL || $this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
  572. if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
  573. $redisKeyPrefix = 'AT-M:';
  574. } else if ($this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
  575. $redisKeyPrefix = 'AT-M-G:';
  576. }
  577. $count = intval($redis->hget($redisKeyPrefix.date('Ym'),$this->auth->id));
  578. $limited_number = intval($activity_config['limited_number'] ?? 0);
  579. if($limited_number <= $count){
  580. $this->error("当月只能创建 {$limited_number} 条活动");
  581. }
  582. $params['admin_id'] = $this->auth->id;
  583. $config_id = $params['config_id'] ?? null;
  584. if(!$config_id){
  585. $this->error("活动金额不能为空!");
  586. }
  587. //渠道商自定义活动限制活动时间
  588. $vali_time = strtotime($params['endtime']) - strtotime($params['starttime']);
  589. //config_id来判断活动时间
  590. if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
  591. $map = ['group'=>'activity','name'=>'activity_config'];
  592. $config_res = model('Config')->where($map)->value('value');
  593. } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE){
  594. $map = ['group'=>'activity','name'=>'activity_give_config'];
  595. $config_res = model('Config')->where($map)->value('value');
  596. }
  597. $configArr = json_decode($config_res,true);
  598. $source = $configArr['config'][$params['config_id']] ?? [];
  599. if ($source && isset($source['limit_day']) && $source['limit_day']) {
  600. if($vali_time > (intval($source['limit_day'])*86400)){
  601. $this->error("活动时间间隔不能大于 {$source['limit_day']} 天");
  602. }
  603. } else {
  604. if($vali_time > (intval($activity_config['time_interval'])*86400)){
  605. $this->error("活动时间间隔不能大于 {$activity_config['time_interval']} 天");
  606. }
  607. }
  608. }
  609. }
  610. $params['type'] = $this->activity_type;
  611. if ($this->dataLimit)
  612. {
  613. $params[$this->dataLimitField] = $this->auth->id;
  614. }
  615. if($params['starttime']>=$params['endtime']){
  616. $this->error('活动开始时间不能大于活动结束时间');
  617. }
  618. $params['popstarttime'] = $params['starttime'];
  619. $params['popendtime'] = $params['endtime'];
  620. if (isset($params['pop_range']) && !empty($params['pop_range'])) {
  621. $params['pop_range'] = implode(',', $params['pop_range']);
  622. }
  623. try
  624. {
  625. //是否采用模型验证
  626. if ($this->modelValidate)
  627. {
  628. $name = basename(str_replace('\\', '/', get_class($this->model)));
  629. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  630. $this->model->validate($validate);
  631. }
  632. $id = $this->model->allowField(true)->save($params);
  633. if ($id !== false)
  634. {
  635. $redis->hIncrBy($redisKeyPrefix.date('Ym'),$this->auth->id,1);
  636. $redis->expire($redisKeyPrefix.date('Ym'),31*86400);
  637. //删除前台弹窗redis
  638. if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
  639. $popKey = "S-P-T-C:N:{$this->auth->id}";
  640. $redis->del($popKey);
  641. } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
  642. $popKey = "S-P-T-G:N:{$this->auth->id}";
  643. $redis->del($popKey);
  644. } elseif ($this->activity_type == self::ACTIVITY_TYPE_PLATFORM) {
  645. $popKey = "S-P-T-P:N:";
  646. Redis::instance()->del($popKey);
  647. }
  648. $this->success();
  649. }
  650. else
  651. {
  652. $this->error($this->model->getError());
  653. }
  654. }
  655. catch (\think\exception\PDOException $e)
  656. {
  657. $this->error($e->getMessage());
  658. }
  659. }
  660. $this->error(__('Parameter %s can not be empty', ''));
  661. }
  662. if(isset($activity_config['config']) && $activity_config['config']){
  663. $activity_config['config'] = array_filter($activity_config['config'],function($val){
  664. if(intval($val['status'])){
  665. return true;
  666. }
  667. });
  668. }
  669. $this->assign('activity_config',$activity_config);
  670. return $this->view->fetch();
  671. }
  672. /**
  673. * 编辑
  674. */
  675. public function edit($ids = NULL)
  676. {
  677. if($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL){
  678. $activity_config = Config::get('site.activity_config');
  679. } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE){
  680. $activity_config = Config::get('site.activity_give_config');
  681. }else{
  682. $activity_config = [];
  683. }
  684. $row = $this->model->get($ids);
  685. if (!$row)
  686. $this->error(__('No Results were found'));
  687. $adminIds = $this->getDataLimitAdminIds();
  688. if (is_array($adminIds))
  689. {
  690. if (!in_array($row[$this->dataLimitField], $adminIds))
  691. {
  692. $this->error(__('You have no permission'));
  693. }
  694. }
  695. if ($this->request->isPost())
  696. {
  697. $params = $this->request->post("row/a");
  698. if ($params)
  699. {
  700. if($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL || $this->activity_type == self::ACTIVIYT_TYPE_GIVE){ //渠道自定义活动 || 赠币活动
  701. //渠道商自定义活动限制活动时间
  702. $vali_time = strtotime($params['endtime']) - strtotime($params['starttime']);
  703. //资源自定义时间
  704. $source = $activity_config['config'][$row['config_id']] ?? [];
  705. if ($source && isset($source['limit_day']) && $source['limit_day']) {
  706. if($vali_time > (intval($source['limit_day'])*86400)){
  707. $this->error("活动时间间隔不能大于 {$source['limit_day']} 天");
  708. }
  709. } else {
  710. if($vali_time > (intval($activity_config['time_interval'])*86400)){
  711. $this->error("活动时间间隔不能大于 {$activity_config['time_interval']} 天");
  712. }
  713. }
  714. $time = time();
  715. if($row['endtime'] < $time){
  716. $this->error('已结束的活动不能编辑');
  717. }elseif($row['starttime'] < $time && $row['endtime'] > $time){
  718. $this->error('进行中的活动不能编辑');
  719. }
  720. }
  721. if($params['starttime']>=$params['endtime']){
  722. $this->error('活动开始时间不能大于活动结束时间');
  723. }
  724. $params['popstarttime'] = $params['starttime'];
  725. $params['popendtime'] = $params['endtime'];
  726. if (isset($params['pop_range']) && !empty($params['pop_range'])) {
  727. $params['pop_range'] = implode(',', $params['pop_range']);
  728. }
  729. try
  730. {
  731. //是否采用模型验证
  732. if ($this->modelValidate)
  733. {
  734. $name = basename(str_replace('\\', '/', get_class($this->model)));
  735. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  736. $row->validate($validate);
  737. }
  738. $result = $row->allowField(true)->save($params);
  739. if ($result !== false)
  740. {
  741. //清理缓存
  742. $key1 = 'A-I:'.$ids;
  743. Redis::instance()->del($key1);
  744. $key2 = 'A-R:'.$ids;
  745. Redis::instance()->del($key2);
  746. //删除前台弹窗redis
  747. if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
  748. $popKey = "S-P-T-C:N:{$this->auth->id}";
  749. Redis::instance()->del($popKey);
  750. } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
  751. $popKey = "S-P-T-G:N:{$this->auth->id}";
  752. Redis::instance()->del($popKey);
  753. } elseif ($this->activity_type == self::ACTIVITY_TYPE_PLATFORM) {
  754. $popKey = "S-P-T-P:N:";
  755. Redis::instance()->del($popKey);
  756. }
  757. //判断是否是VIP活动 如果是需要同步更新
  758. $slaveRows = model("VipActivityRelation")->field("slave_activity_id, slave_admin_id")->where('master_admin_id', 'eq', $this->auth->id)->where('master_activity_id', 'eq', $ids)->select();
  759. if ($slaveRows) {
  760. unset($params['id']);
  761. foreach ($slaveRows as $slaveRow) {
  762. $slaveAdminId = $slaveRow['slave_admin_id'];
  763. $slaveActivity = $this->model->get($slaveRow['slave_activity_id']);
  764. $slaveActivity->allowField(true)->save($params);
  765. $key1 = 'A-I:'.$slaveActivity['id'];
  766. Redis::instance()->del($key1);
  767. $key2 = 'A-R:'.$slaveActivity['id'];
  768. Redis::instance()->del($key2);
  769. //删除前台弹窗redis
  770. if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
  771. $popKey = "S-P-T-C:N:{$slaveAdminId}";
  772. Redis::instance()->del($popKey);
  773. } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
  774. $popKey = "S-P-T-G:N:{$slaveAdminId}";
  775. Redis::instance()->del($popKey);
  776. }
  777. }
  778. }
  779. $this->success();
  780. }
  781. else
  782. {
  783. $this->error($row->getError());
  784. }
  785. }
  786. catch (\think\exception\PDOException $e)
  787. {
  788. $this->error($e->getMessage());
  789. }
  790. }
  791. $this->error(__('Parameter %s can not be empty', ''));
  792. }
  793. if(isset($activity_config['config']) && $activity_config['config']){
  794. $activity_config['config'] = array_filter($activity_config['config'],function($val){
  795. if(intval($val['status'])){
  796. return true;
  797. }
  798. });
  799. }
  800. $this->assign('activity_config',$activity_config);
  801. //弹窗位置转换
  802. $popRangeSelected = [];
  803. if (strlen($row['pop_range']) > 0) {
  804. $popRangeSelected = explode(',', $row['pop_range']);
  805. }
  806. $this->view->assign("row", $row);
  807. $this->view->assign("popRangeSelected", $popRangeSelected);
  808. return $this->view->fetch();
  809. }
  810. public function select()
  811. {
  812. $params = $this->request->get();
  813. $type = $params['type'] ?? 0;
  814. $status = $params['status'] ?? '1';
  815. if ($this->request->isAjax()) {
  816. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  817. $maps['activity.type'] = ['eq', $type];
  818. $maps['activity.status'] = ['eq', $status];
  819. $list = [];
  820. $total = model('activity')
  821. ->where($maps)
  822. ->where($where)
  823. ->order('activity.createtime', 'desc')
  824. ->count();
  825. if ($total) {
  826. $list = model('activity')
  827. ->where($maps)
  828. ->where($where)
  829. ->order('activity.createtime', 'desc')
  830. ->limit($offset, $limit)
  831. ->select();
  832. }
  833. $result = array("total" => $total, "rows" => $list);
  834. return json($result);
  835. }
  836. $this->assignconfig('type', $type);
  837. return $this->view->fetch();
  838. }
  839. /**
  840. * 设置弹窗位置
  841. * @return int|string
  842. * @throws \think\Exception
  843. * @throws \think\exception\PDOException
  844. */
  845. public function set_pop_range()
  846. {
  847. $id = $_POST['id'];
  848. $status = $_POST['status'];
  849. $pop_range = $_POST['pop_range'];
  850. $pop_ispay = $_POST['pop_ispay'];
  851. $row = $this->model->where('id', 'eq', $id)->find();
  852. //需要验证权限
  853. if (in_array($this->group, [3, 4, 7, 8])) {
  854. if ($row['admin_id'] != $this->auth->id) {
  855. $this->error(__('You have no permission'));
  856. }
  857. }
  858. $res = Db::table('activity')->where(['id'=>$id])->update(['pop_status'=>$status, 'pop_range' => $pop_range, 'pop_ispay' => $pop_ispay ]);
  859. //删除前台弹窗redis
  860. if ($row['type'] == self::ACTIVIYT_TYPE_CHANNEL) {
  861. $popKey = "S-P-T-C:N:{$row['admin_id']}";
  862. Redis::instance()->del($popKey);
  863. } elseif ($row['type'] == self::ACTIVIYT_TYPE_GIVE) {
  864. $popKey = "S-P-T-G:N:{$row['admin_id']}";
  865. Redis::instance()->del($popKey);
  866. } elseif ($this->activity_type == self::ACTIVITY_TYPE_PLATFORM) {
  867. $popKey = "S-P-T-P:N:";
  868. Redis::instance()->del($popKey);
  869. }
  870. return $res;
  871. }
  872. }