123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949 |
- <?php
- namespace app\admin\controller;
- use app\admin\library\ShortUrl;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use app\main\constants\AdminConstants;
- use app\main\constants\PayConstants;
- use app\main\service\AdminService;
- use app\common\service\CardFlipService;
- use GuzzleHttp\Client as Http;
- use think\Config;
- use think\Db;
- /**
- * 活动管理
- *
- * @icon fa fa-circle-o
- */
- class Activity extends Backend
- {
-
- /**
- * Activity模型对象
- */
- protected $model = null;
- protected $activity_type = 0;
- protected $business_line = 0;
- //平台活动
- private const ACTIVITY_TYPE_PLATFORM = 0;
- //渠道自定义活动
- private const ACTIVIYT_TYPE_CHANNEL = 1;
- //赠币活动
- private const ACTIVIYT_TYPE_GIVE = 3;
- //APP活动
- private const ACTIVITY_TYPE_APP = 2;
- public $noNeedRight = ['addchannel','index_ajax','index_give_activity','index_channel_activity'];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Activity');
- $this->view->assign("statusList", $this->model->getStatusList());
- $this->view->assign("popStatusList", $this->model->getPopStatusList());
- $this->view->assign("popRangeList", $this->model->getPopRangeList());
- $this->assignconfig("popRangeList", $this->model->getPopRangeList());
- $this->activity_type = intval($this->request->param('type') ?? 0);
- $this->assign('activity_type',$this->activity_type);
- $this->assignconfig('activity_type',$this->activity_type);
- $this->business_line = PayConstants::BUSINESS_WECHAT;
- if (in_array($this->activity_type, [2])) {
- $this->business_line = PayConstants::BUSINESS_APP;
- }
- $this->assignconfig('business_line', $this->business_line);
- }
- /**
- * 生成短链
- * @return \think\response\Json
- */
- public function sorturl(){
- $ids = empty($_GET['ids'])?'':$_GET['ids'];
- $error = json(['error'=>1]);
- if(!$ids){
- return $error;
- }
- $admin_id = $this->auth->id;
- $param = [];
- $param['aid'] = $ids;
- $param['admin_id'] = $admin_id;
- /**
- * 跳转短链生成格式
- */
- $channel_id = $this->auth->agent_id ? $this->auth->agent_id : $this->auth->channel_id;
- $jmp_url = getCurrentDomain($channel_id,'/s/' . $ids);
- $shorturl = new ShortUrl();
- $hasRecode = model('activity_shortlink')->where($param)->find();
- if(!$param['qq'] = $shorturl->tencent($channel_id,$jmp_url)){
- return $error;
- }
- if(!$param['sina'] = $shorturl->sina($jmp_url)){
- return $error;
- }
- $param['updatetime'] = time();
- if($hasRecode){ //update
- $result = model('activity_shortlink')->update($param,['aid'=>$ids,'admin_id'=>$admin_id]);
- }else{ //insert
- $param['createtime'] = time();
- $result = model('activity_shortlink')->insert($param);
- }
- if($result){
- return json(['error'=>0,'data'=>$param]);
- }else{
- return $error;
- }
- }
- /**
- * 查看
- */
- public function index()
- {
- $give_tab = 1;
- $activity_config = Config::get('site.activity_config');
- $activity_give_config = Config::get('site.activity_give_config');
- if($this->auth->agent_id || $this->group == 3){
- if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
- $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
- }
- }
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- $this->assignconfig('time',$this->time);
- $this->assign('time',$this->time);
- $this->assign('group',$this->group);
- $today = date('Ymd',$this->time);
- /**
- * 跳转短链生成格式
- */
- if($this->group == 4){
- if($this->auth->agent_id){
- $channel_id = $this->auth->agent_id;
- }else{
- $channel_id = $this->auth->channel_id;
- }
- }
- if($this->group == 3){
- $channel_id = $this->auth->channel_id;
- }
- $distribute = 0;
- $adminExtend = model('admin_extend')->where(['admin_id'=>$this->auth->id])->find();
- if(!empty($adminExtend->distribute) && $adminExtend->distribute == '1' ){
- $distribute = 1;
- }
- $this->assign('distribute',$distribute);
- $activityShowRechargePage = 0;
- if($this->group<3 || $this->group==6) {
- 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)
- ->where('type',$this->activity_type)
- ->count();
- $list = $this->model
- ->join('activity_collect c','activity.id=c.aid and c.createdate='.$today.' and c.admin_id=0','left')
- ->join('activity_collect_all a','activity.id=a.aid and a.admin_id=0','left')
- ->field('activity.*,c.money,a.money as allmoney')
- ->where($where)
- ->where('activity.type',$this->activity_type)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- if ($list && in_array($this->activity_type, [0, 2])) {
- //平台或APP判断是否已经设置资源
- foreach ($list as &$item) {
- $resourceModel = model("Resource")->field("id")->where('activity_id', 'eq', $item['id'])->find();
- $item['resource_id'] = 0;
- if ($resourceModel) {
- $item['resource_id'] = $resourceModel['id'];
- }
- }
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }else{
- if($this->group == 3 || ($this->group == 4 && $distribute==1)){
- $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channel_id);
- $activityShowRechargePage = $adminConfig['activity_show_recharge_page'];
- // $adminConfig = model('AdminConfig')->get($this->auth->id);
- if ($adminConfig['give_kandian'] == 0) {
- $give_tab = 0;
- }
- $ac_map = [];
- $this->assign('give_tab', $give_tab);
- //活动状态
- if($this->activity_type == self::ACTIVITY_TYPE_PLATFORM){
- $ac_map['activity.status'] = '1';
- }
- if($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL || $this->activity_type == self::ACTIVIYT_TYPE_GIVE){ //渠道自定义活动 & 赠币活动
- //渠道商处理
- if($this->group == AdminConstants::ADMIN_GROUP_ID_CHANNEL){
- $ac_map['activity.admin_id'] = $this->auth->id;
- }
- //配号代理商
- if($this->group == AdminConstants::ADMIN_GROUP_ID_AGENT && $distribute==1){
- $ac_map['activity.admin_id'] = ['in',"{$this->auth->id},{$this->auth->channel_id}"];
- }elseif($this->group == AdminConstants::ADMIN_GROUP_ID_AGENT){
- //代理商
- $ac_map['activity.admin_id'] = $this->auth->channel_id;
- }
- }
- //不展示系统活动
- $ac_map['is_system'] = ['eq', 0];
- $res = model('activity')
- ->join('activity_collect c','activity.id=c.aid and c.createdate='.$today.' and c.admin_id='.$this->auth->id,'left')
- ->join('activity_collect_all a','activity.id=a.aid and a.admin_id='.$this->auth->id,'left')
- ->join('activity_shortlink s','activity.id= s.aid and s.admin_id='.$this->auth->id,'left')
- ->where($ac_map)
- ->where('activity.id <> 100')
- ->where('activity.type',$this->activity_type)
- ->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')
- ->order('activity.createtime','desc')
- ->paginate(10,false,['query'=>['type'=>$this->activity_type]]);
- }else{ //平台活动
- $res = model('activity')
- ->join('activity_collect c','activity.id=c.aid and c.createdate='.$today.' and c.admin_id='.$this->auth->id,'left')
- ->join('activity_collect_all a','activity.id=a.aid and a.admin_id='.$this->auth->id,'left')
- ->where('activity.status','1')
- ->where('activity.id <> 100')
- ->where('activity.type',$this->activity_type)
- ->field('activity.*,c.money,a.money as allmoney,c.recharge_count,a.recharge_count as arecharge_count')
- ->order('activity.createtime','desc')
- ->paginate(10,false,['query'=>['type'=>$this->activity_type]]);
- }
- $rangeTitleArr = $this->model->getPopRangeList();
- foreach ($res as $index => $vo) {
- $vo['url'] = '';
- //弹窗位置
- $popRangeStr = '';
- if ($vo['pop_range'] || $vo['pop_range'] == '0') {
- $rangeArr = explode(',', $vo['pop_range']);
- foreach ($rangeArr as $k) {
- $popRangeStr .= $rangeTitleArr[$k] .' ';
- }
- } else {
- $popRangeStr = '-';
- }
- $vo['pop_range_txt'] = $popRangeStr;
- if ($vo['endtime'] > time()) {
- switch ($vo['type']) {
- case 0:
- $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id']);
- break;
- case 1:
- $activity_config = Config::get('site.activity_config');
- if (isset($activity_config['config'][$vo['config_id']]) && $activity_config['config'][$vo['config_id']]['status'] == 1) {
- $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id'] . '/r/' . $activity_config['config'][$vo['config_id']]['resource_id']);
- }
- break;
- case 3:
- $activity_give_config = Config::get('site.activity_give_config');
- if (isset($activity_give_config['config'][$vo['config_id']]) && $activity_give_config['config'][$vo['config_id']]['status'] == 1) {
- $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id'] . '/r/' . $activity_give_config['config'][$vo['config_id']]['resource_id']);
- }
- break;
- }
- }
- $res[$index] = $vo;
- }
- if ($this->request->isAjax()) {
- return json($res);
- }
- $subscrip = model('ChannelSpecialManage')->isWhite('subscribe', $this->auth->id);
- $camUrl = CardFlipService::instance()->checkAuth($this->auth->id);
- $this->assign('camUrl',$camUrl);
- $this->assign('subscrip',$subscrip);
- $this->assign('activity_config',$activity_config);
- $this->assign('activity_give_config',$activity_give_config);
- $this->assign('channel_id',$channel_id);
- $this->assign('admin_id',$this->auth->id);
- $this->assign('res',$res);
- $this->assign('activity_show_recharge_page', $activityShowRechargePage);
- return $this->view->fetch();
- }
- }
- /**
- * 活动列表(所有活动)
- * @return \think\response\Json
- */
- public function index_ajax()
- {
- list($where, $whereFilter, $ac_map) = $this->_buildCommonCondition();
- $res = model('activity')//渠道自定义活动 + 赠币活动
- ->where('activity.id <> 100')
- ->where($where)
- ->where($whereFilter)
- ->where($ac_map)
- ->order('activity.createtime', 'desc')
- ->select();
- $respt = model('activity')//平台活动
- ->where('activity.id <> 100')
- ->where('activity.type',self::ACTIVITY_TYPE_PLATFORM)
- ->where($where)
- ->where($whereFilter)
- ->order('activity.createtime', 'desc')
- ->select();
- $res = array_merge($respt, $res);
- $return = $this->_formatActivityUrl($res);
- if ($this->request->isAjax()) {
- return json($return);
- }
- }
- /**
- * 活动列表(渠道自定义活动 & 平台活动)
- * @return \think\response\Json
- */
- public function index_channel_activity()
- {
- list($where, $whereFilter, $ac_map) = $this->_buildCommonCondition();
- $res = model('activity')//渠道自定义活动
- ->where('activity.id <> 100')
- ->where('activity.type',self::ACTIVIYT_TYPE_CHANNEL)
- ->where($where)
- ->where($whereFilter)
- ->where($ac_map)
- ->order('activity.createtime', 'desc')
- ->select();
- $respt = model('activity')//平台活动
- ->where('activity.id <> 100')
- ->where('activity.type',self::ACTIVITY_TYPE_PLATFORM)
- ->where($where)
- ->where($whereFilter)
- ->order('activity.createtime', 'desc')
- ->select();
- $res = array_merge($respt, $res);
- $return = $this->_formatActivityUrl($res);
- if ($this->request->isAjax()) {
- return json($return);
- }
- }
- /**
- * 赠币活动列表
- * @return \think\response\Json
- */
- public function index_give_activity()
- {
- list($where, $whereFilter, $ac_map) = $this->_buildCommonCondition();
- $res = model('activity')
- ->where('activity.id <> 100')
- ->where('activity.type',self::ACTIVIYT_TYPE_GIVE)
- ->where($where)
- ->where($whereFilter)
- ->where($ac_map)
- ->order('activity.createtime', 'desc')
- ->select();
- $return = $this->_formatActivityUrl($res);
- if ($this->request->isAjax()) {
- return json($return);
- }
- }
- /**
- * 格式化活动链接
- * @param $res
- * @return array
- */
- private function _formatActivityUrl($res){
- $return = [];
- if ($res) {
- $channel_id = $this->auth->agent_id ? $this->auth->agent_id : $this->auth->channel_id;
- foreach ($res as $index => $vo) {
- if ($vo['endtime'] > time()) {
- switch ($vo['type']) {
- case 0:
- $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id']);
- $return[] = $vo;
- break;
- case 1:
- $activity_config = Config::get('site.activity_config');
- if (isset($activity_config['config'][$vo['config_id']]) && $activity_config['config'][$vo['config_id']]['status'] == 1) {
- $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id'] . '/r/' . $activity_config['config'][$vo['config_id']]['resource_id']);
- $return[] = $vo;
- }
- break;
- case 3:
- $activity_give_config = Config::get('site.activity_give_config');
- if (isset($activity_give_config['config'][$vo['config_id']]) && $activity_give_config['config'][$vo['config_id']]['status'] == 1) {
- $vo['url'] = getCurrentDomain($channel_id, '/s/' . $vo['id'] . '/r/' . $activity_give_config['config'][$vo['config_id']]['resource_id']);
- $return[] = $vo;
- }
- break;
- }
- }
- }
- }
- return $return;
- }
- /**
- * 构造活动查询列表查询条件
- * @return array
- */
- private function _buildCommonCondition()
- {
- $distribute = 0;
- $adminExtend = model('admin_extend')->where(['admin_id' => $this->auth->id])->find();
- if (!empty($adminExtend->distribute) && $adminExtend->distribute == '1') {
- $distribute = 1;
- }
- $ac_map = ['activity.status' => '1'];
- //渠道商处理
- if ($this->group == AdminConstants::ADMIN_GROUP_ID_CHANNEL) {
- $ac_map['activity.admin_id'] = $this->auth->id;
- }
- //配号代理商
- if ($this->group == AdminConstants::ADMIN_GROUP_ID_AGENT && $distribute == 1) {
- $ac_map['activity.admin_id'] = ['in', "{$this->auth->id},{$this->auth->channel_id}"];
- } elseif ($this->group == AdminConstants::ADMIN_GROUP_ID_AGENT) {
- //代理商
- $ac_map['activity.admin_id'] = $this->auth->channel_id;
- }
- list($whereFilter, $sort, $order, $offset, $limit) = $this->buildparams();
- $where = [];
- if ($this->request->request('name')) {
- $where['name'] = ['LIKE', '%' . $this->request->request('name') . '%'];
- }
- return [$where, $whereFilter, $ac_map];
- }
- public function config(){
- if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
- $map = ['group'=>'activity','name'=>'activity_config'];
- $config_res = model('Config')->where($map)->value('value');
- } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE){
- $map = ['group'=>'activity','name'=>'activity_give_config'];
- $config_res = model('Config')->where($map)->value('value');
- }else{
- $config_res = [];
- $this->error("错误的请求");
- }
- if($this->request->isAjax()){
- $params = $this->request->param();
- if(!isset($params['config'])){
- $this->error('参数错误');
- }
- $config = $params['config'];
- unset($params['config']);
- foreach($config['id'] as $key => $id){
- if(isset($config['name'][$key]) && isset($config['resource_id'][$key]) && isset($config['resource_id'][$key])){
- $params['config'][$id] = [
- 'name' => $config['name'][$key],
- 'resource_id' => $config['resource_id'][$key],
- 'status' => $config['status'][$key],
- 'limit_day' => $config['limit_day'][$key]
- ];
- }
- }
- if(model('Config')->where($map)->find()){
- $is_ok = model('Config')->where($map)->update(['value'=>json_encode($params)]);
- }else{
- if($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL){
- $is_ok = model('Config')->insert(array_merge($map,[
- 'content'=>'活动自定义配置',
- 'value'=>json_encode($params),
- 'type' => 'array',
- ]));
- } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
- $is_ok = model('Config')->insert(array_merge($map,[
- 'content'=>'赠币活动自定义配置',
- 'value'=>json_encode($params),
- 'type' => 'array'
- ]));
- }else{
- $is_ok = false;
- }
- }
- $redis = Redis::instance();
- $redis->del('site');
- if($is_ok !== false){
- $this->success('操作成功');
- }else{
- $this->error('操作失败');
- }
- }
- $this->assign('configRes',json_decode($config_res,true));
- return $this->view->fetch();
- }
- public function changestatus(){
- $id = $_POST['id'];
- $status = $_POST['status'];
- $row = $this->model->where('id', 'eq', $id)->find();
- $res = Db::table('activity')->where(['id'=>$id])->update(['status'=>$status]);
- //删除前台弹窗redis
- if ($row['type'] == self::ACTIVIYT_TYPE_CHANNEL) {
- $popKey = "S-P-T-C:N:{$row['admin_id']}";
- Redis::instance()->del($popKey);
- } elseif ($row['type'] == self::ACTIVIYT_TYPE_GIVE) {
- $popKey = "S-P-T-G:N:{$row['admin_id']}";
- Redis::instance()->del($popKey);
- } elseif ($this->activity_type == self::ACTIVITY_TYPE_PLATFORM) {
- $popKey = "S-P-T-P:N:";
- Redis::instance()->del($popKey);
- }
- return $res;
- }
- public function changepopstatus(){
- $id = $_POST['id'];
- $status = $_POST['status'];
- $row = $this->model->where('id', 'eq', $id)->find();
- $res = Db::table('activity')->where(['id'=>$id])->update(['pop_status'=>$status]);
- //删除前台弹窗redis
- if ($row['type'] == self::ACTIVIYT_TYPE_CHANNEL) {
- $popKey = "S-P-T-C:N:{$row['admin_id']}";
- Redis::instance()->del($popKey);
- } elseif ($row['type'] == self::ACTIVIYT_TYPE_GIVE) {
- $popKey = "S-P-T-G:N:{$row['admin_id']}";
- Redis::instance()->del($popKey);
- } elseif ($this->activity_type == self::ACTIVITY_TYPE_PLATFORM) {
- $popKey = "S-P-T-P:N:";
- Redis::instance()->del($popKey);
- }
- return $res;
- }
- public function showrechargepage()
- {
- if ($this->request->isPost()) {
- $id = $_POST['id'];
- $status = $_POST['status'];
- $iStatus = $status == 'on' ? 1 : 0;
- $res = $this->model->where(['id' => $id])->update(['show_for_recharge_page' => $iStatus]);
- return $res;
- }
- return false;
- }
- /**
- * 添加
- */
- public function add()
- {
- $redis = Redis::instance();
- if($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL){
- $activity_config = Config::get('site.activity_config');
- } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE){
- $activity_config = Config::get('site.activity_give_config');
- }else{
- $activity_config = [];
- }
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- $params['status'] = 1;//自定义活动 状态永远是正常
- if (!in_array($this->activity_type, [self::ACTIVIYT_TYPE_CHANNEL, self::ACTIVIYT_TYPE_GIVE])) {
- //非自定义活动或赠币活动
- if (in_array($this->group, [3, 4, 7, 8])) {
- //这些角色不能创建
- $this->error("没有权限!");
- }
- }
- $redisKeyPrefix = 'AT-M:';
- if (in_array($this->group, [7, 8])) {
- //VIP或VIP创建模板
- if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL || $this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
- $params['admin_id'] = $this->auth->id;
- $config_id = $params['config_id'] ?? null;
- if(!$config_id){
- $this->error("活动金额不能为空!");
- }
- //渠道商自定义活动限制活动时间
- $vali_time = strtotime($params['endtime']) - strtotime($params['starttime']);
- //config_id来判断活动时间
- if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
- $map = ['group'=>'activity','name'=>'activity_config'];
- $config_res = model('Config')->where($map)->value('value');
- } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE){
- $map = ['group'=>'activity','name'=>'activity_give_config'];
- $config_res = model('Config')->where($map)->value('value');
- }
- $configArr = json_decode($config_res,true);
- $source = $configArr['config'][$params['config_id']] ?? [];
- if ($source && isset($source['limit_day']) && $source['limit_day']) {
- if($vali_time > (intval($source['limit_day'])*86400)){
- $this->error("活动时间间隔不能大于 {$source['limit_day']} 天");
- }
- } else {
- if($vali_time > (intval($activity_config['time_interval'])*86400)){
- $this->error("活动时间间隔不能大于 {$activity_config['time_interval']} 天");
- }
- }
- }
- } else {
- //检查渠道商和配号代理商
- if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL || $this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
- if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
- $redisKeyPrefix = 'AT-M:';
- } else if ($this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
- $redisKeyPrefix = 'AT-M-G:';
- }
- $count = intval($redis->hget($redisKeyPrefix.date('Ym'),$this->auth->id));
- $limited_number = intval($activity_config['limited_number'] ?? 0);
- if($limited_number <= $count){
- $this->error("当月只能创建 {$limited_number} 条活动");
- }
- $params['admin_id'] = $this->auth->id;
- $config_id = $params['config_id'] ?? null;
- if(!$config_id){
- $this->error("活动金额不能为空!");
- }
- //渠道商自定义活动限制活动时间
- $vali_time = strtotime($params['endtime']) - strtotime($params['starttime']);
- //config_id来判断活动时间
- if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
- $map = ['group'=>'activity','name'=>'activity_config'];
- $config_res = model('Config')->where($map)->value('value');
- } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE){
- $map = ['group'=>'activity','name'=>'activity_give_config'];
- $config_res = model('Config')->where($map)->value('value');
- }
- $configArr = json_decode($config_res,true);
- $source = $configArr['config'][$params['config_id']] ?? [];
- if ($source && isset($source['limit_day']) && $source['limit_day']) {
- if($vali_time > (intval($source['limit_day'])*86400)){
- $this->error("活动时间间隔不能大于 {$source['limit_day']} 天");
- }
- } else {
- if($vali_time > (intval($activity_config['time_interval'])*86400)){
- $this->error("活动时间间隔不能大于 {$activity_config['time_interval']} 天");
- }
- }
- }
- }
- $params['type'] = $this->activity_type;
- if ($this->dataLimit)
- {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- if($params['starttime']>=$params['endtime']){
- $this->error('活动开始时间不能大于活动结束时间');
- }
- $params['popstarttime'] = $params['starttime'];
- $params['popendtime'] = $params['endtime'];
- if (isset($params['pop_range']) && !empty($params['pop_range'])) {
- $params['pop_range'] = implode(',', $params['pop_range']);
- }
- 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);
- }
- $id = $this->model->allowField(true)->save($params);
- if ($id !== false)
- {
- $redis->hIncrBy($redisKeyPrefix.date('Ym'),$this->auth->id,1);
- $redis->expire($redisKeyPrefix.date('Ym'),31*86400);
- //删除前台弹窗redis
- if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
- $popKey = "S-P-T-C:N:{$this->auth->id}";
- $redis->del($popKey);
- } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
- $popKey = "S-P-T-G:N:{$this->auth->id}";
- $redis->del($popKey);
- } elseif ($this->activity_type == self::ACTIVITY_TYPE_PLATFORM) {
- $popKey = "S-P-T-P:N:";
- Redis::instance()->del($popKey);
- }
- $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(isset($activity_config['config']) && $activity_config['config']){
- $activity_config['config'] = array_filter($activity_config['config'],function($val){
- if(intval($val['status'])){
- return true;
- }
- });
- }
- $this->assign('activity_config',$activity_config);
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = NULL)
- {
- if($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL){
- $activity_config = Config::get('site.activity_config');
- } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE){
- $activity_config = Config::get('site.activity_give_config');
- }else{
- $activity_config = [];
- }
- $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)
- {
- if($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL || $this->activity_type == self::ACTIVIYT_TYPE_GIVE){ //渠道自定义活动 || 赠币活动
- //渠道商自定义活动限制活动时间
- $vali_time = strtotime($params['endtime']) - strtotime($params['starttime']);
- //资源自定义时间
- $source = $activity_config['config'][$row['config_id']] ?? [];
- if ($source && isset($source['limit_day']) && $source['limit_day']) {
- if($vali_time > (intval($source['limit_day'])*86400)){
- $this->error("活动时间间隔不能大于 {$source['limit_day']} 天");
- }
- } else {
- if($vali_time > (intval($activity_config['time_interval'])*86400)){
- $this->error("活动时间间隔不能大于 {$activity_config['time_interval']} 天");
- }
- }
- $time = time();
- if($row['endtime'] < $time){
- $this->error('已结束的活动不能编辑');
- }elseif($row['starttime'] < $time && $row['endtime'] > $time){
- $this->error('进行中的活动不能编辑');
- }
- }
- if($params['starttime']>=$params['endtime']){
- $this->error('活动开始时间不能大于活动结束时间');
- }
- $params['popstarttime'] = $params['starttime'];
- $params['popendtime'] = $params['endtime'];
- if (isset($params['pop_range']) && !empty($params['pop_range'])) {
- $params['pop_range'] = implode(',', $params['pop_range']);
- }
- 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)
- {
- //清理缓存
- $key1 = 'A-I:'.$ids;
- Redis::instance()->del($key1);
- $key2 = 'A-R:'.$ids;
- Redis::instance()->del($key2);
- //删除前台弹窗redis
- if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
- $popKey = "S-P-T-C:N:{$this->auth->id}";
- Redis::instance()->del($popKey);
- } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
- $popKey = "S-P-T-G:N:{$this->auth->id}";
- Redis::instance()->del($popKey);
- } elseif ($this->activity_type == self::ACTIVITY_TYPE_PLATFORM) {
- $popKey = "S-P-T-P:N:";
- Redis::instance()->del($popKey);
- }
- //判断是否是VIP活动 如果是需要同步更新
- $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();
- if ($slaveRows) {
- unset($params['id']);
- foreach ($slaveRows as $slaveRow) {
- $slaveAdminId = $slaveRow['slave_admin_id'];
- $slaveActivity = $this->model->get($slaveRow['slave_activity_id']);
- $slaveActivity->allowField(true)->save($params);
- $key1 = 'A-I:'.$slaveActivity['id'];
- Redis::instance()->del($key1);
- $key2 = 'A-R:'.$slaveActivity['id'];
- Redis::instance()->del($key2);
- //删除前台弹窗redis
- if ($this->activity_type == self::ACTIVIYT_TYPE_CHANNEL) {
- $popKey = "S-P-T-C:N:{$slaveAdminId}";
- Redis::instance()->del($popKey);
- } elseif ($this->activity_type == self::ACTIVIYT_TYPE_GIVE) {
- $popKey = "S-P-T-G:N:{$slaveAdminId}";
- Redis::instance()->del($popKey);
- }
- }
- }
- $this->success();
- }
- else
- {
- $this->error($row->getError());
- }
- }
- catch (\think\exception\PDOException $e)
- {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- if(isset($activity_config['config']) && $activity_config['config']){
- $activity_config['config'] = array_filter($activity_config['config'],function($val){
- if(intval($val['status'])){
- return true;
- }
- });
- }
- $this->assign('activity_config',$activity_config);
- //弹窗位置转换
- $popRangeSelected = [];
- if (strlen($row['pop_range']) > 0) {
- $popRangeSelected = explode(',', $row['pop_range']);
- }
- $this->view->assign("row", $row);
- $this->view->assign("popRangeSelected", $popRangeSelected);
- return $this->view->fetch();
- }
- public function select()
- {
- $params = $this->request->get();
- $type = $params['type'] ?? 0;
- $status = $params['status'] ?? '1';
- if ($this->request->isAjax()) {
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $maps['activity.type'] = ['eq', $type];
- $maps['activity.status'] = ['eq', $status];
- $list = [];
- $total = model('activity')
- ->where($maps)
- ->where($where)
- ->order('activity.createtime', 'desc')
- ->count();
- if ($total) {
- $list = model('activity')
- ->where($maps)
- ->where($where)
- ->order('activity.createtime', 'desc')
- ->limit($offset, $limit)
- ->select();
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- $this->assignconfig('type', $type);
- return $this->view->fetch();
- }
- /**
- * 设置弹窗位置
- * @return int|string
- * @throws \think\Exception
- * @throws \think\exception\PDOException
- */
- public function set_pop_range()
- {
- $id = $_POST['id'];
- $status = $_POST['status'];
- $pop_range = $_POST['pop_range'];
- $pop_ispay = $_POST['pop_ispay'];
- $row = $this->model->where('id', 'eq', $id)->find();
- //需要验证权限
- if (in_array($this->group, [3, 4, 7, 8])) {
- if ($row['admin_id'] != $this->auth->id) {
- $this->error(__('You have no permission'));
- }
- }
- $res = Db::table('activity')->where(['id'=>$id])->update(['pop_status'=>$status, 'pop_range' => $pop_range, 'pop_ispay' => $pop_ispay ]);
- //删除前台弹窗redis
- if ($row['type'] == self::ACTIVIYT_TYPE_CHANNEL) {
- $popKey = "S-P-T-C:N:{$row['admin_id']}";
- Redis::instance()->del($popKey);
- } elseif ($row['type'] == self::ACTIVIYT_TYPE_GIVE) {
- $popKey = "S-P-T-G:N:{$row['admin_id']}";
- Redis::instance()->del($popKey);
- } elseif ($this->activity_type == self::ACTIVITY_TYPE_PLATFORM) {
- $popKey = "S-P-T-P:N:";
- Redis::instance()->del($popKey);
- }
- return $res;
- }
- }
|