model = model('ReferralMany'); $this->view->assign("shortUrlTypeList", $this->model->getShortUrlTypeList()); $this->view->assign("stateList", $this->model->getStateList()); } /** * 查看 */ public function index() { // 是否开启微信分享开关 $open_wechat_share = Config::get('site.open_wechat_share') ?? 0; $this->assignconfig('open_wechat_share', $open_wechat_share); //设置过滤方法 $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(); $whereArr = ['state' => 1]; $whereArr = function ($query) use ($whereArr) { foreach ($whereArr as $k => $v) { if (is_array($v)) { call_user_func_array([$query, 'where'], $v); } else { $query->where($v); } } }; $total = $this->model ->where($where) ->where($whereArr) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->where($whereArr) ->order($sort, $order) ->limit($offset, $limit) ->select(); foreach ($list as $key => &$item) { $item = $item->toArray(); $item['referral_list'] = model('referral')->with('book')->where('referral.id', 'in', $item['referral_ids'])->select(); if (is_array($item['referral_list'])) { // 加入防封前置域名 foreach ($item['referral_list'] as $r_key => $r_item) { $item['referral_list'][$r_key]['skin_url'] = ReferralService::instance()->getSkinUrl($r_item['id'], $r_item['source_url']); } } if (is_array($item['push_json'])) { foreach ($item['push_json'] as $k => &$it) { $it['agent_list'] = model('Admin')->where('id', 'in', $it['ids'])->select(); } } } $result = array("total" => $total, "rows" => $list); return json($result); } if($this->auth->agent_id || $this->group == 3){ if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){ $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config')); } } 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); } $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', '')); } 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) { 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->success(); } else { $this->error($row->getError()); } } catch (\think\exception\PDOException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } $row['referral_list'] = model('referral')->with('book')->where('referral.id', 'in', $row['referral_ids'])->select(); $this->view->assign("row", $row); return $this->view->fetch(); } /** * 推送 */ public function push() { $id = input('id'); $row = $this->model->get($id); if ($this->request->isPost()) { $rq = input(); $ids = $rq['ids']; if(count($ids) == 1){ if(empty($ids[0])) { $this->error('请至少选择一位代理'); }else{ $idstr = $ids[0]; } }else{ $idstr = implode(',',$ids); } $item = array( 'ids'=>$idstr, 'time'=>date('Y-m-d H:i:s') ); if(!empty($row['push_json'])){ $push_json = $row['push_json']; }else{ $push_json = []; } $push_json[] = $item; //更新 $row['push_json'] = $push_json; // $update = $row->save(); // if($update !== false){ /** * 插入推广链接到代理商 */ // 新增单条链接到代理商 $referral_ids = explode(',',$row['referral_ids']); $newReferralIds = []; foreach ($referral_ids as $key=>$value) { if (!$value) { continue; } $referralModel = model('referral'); $res = $referralModel->get($value); if (empty($res)) { continue; } $params = []; $params['manage_title_id'] = $res['manage_title_id']; $params['manage_cover_id'] = $res['manage_cover_id']; $params['manage_template_id'] = $res['manage_template_id']; $params['manage_preview_id'] = $res['manage_preview_id']; $params['guide_title'] = $res['guide_title']; $params['guide_image'] = $res['guide_image']; $params['guide_sex'] = $res['guide_sex']; $params['guide_idx'] = $res['guide_idx']; $params['book_id'] = $res['book_id']; $params['chapter_id'] = $res['chapter_id']; $params['chapter_name'] = $res['chapter_name']; $params['chapter_idx'] = $res['chapter_idx']; $params['guide_chapter_idx'] = $res['guide_chapter_idx']; $params['name'] = $res['name']; $params['wx_type'] = $res['wx_type']; $params['type'] = $res['type']; $params['push'] = $res['push']; $params['uv'] = 0; $params['money'] = 0; $params['source_url'] = null; $params['short_url_qq'] = null; $params['short_url_weibo'] = null; $params['createtime'] = time(); $params['updatetime'] = time(); $params['share_image'] = ResourceService::instance()->getRandomImage()->data; $params['share_title'] = ResourceService::instance()->getRandomTitle()->data; foreach ($ids as $k=>$v) { if (!$v) { continue; } $params['admin_id'] = $v; $insertId = $referralModel->insertGetId($params); if($extend = model("AdminExtend")->getInfo($v)){ if(isset($extend['distribute']) && $extend['distribute']){ $channel_id = $v; }else{ $channel_id = $this->auth->id; } }else{ $channel_id = $this->auth->id; } if ($insertId !== false) { if ($params['type'] == 2) { //首页 $source_url = getCurrentDomain($channel_id,'?referral_id=' . $insertId); } else { $source_url = getCurrentDomain($channel_id,'/index/book/chapter?book_id=' . $params['book_id'] . '&sid=' . $params['chapter_id'] . '&referral_id=' . $insertId); } $source_url .= '&agent_id=' . $v; $params['source_url'] = $source_url; //绑定域名短链ID及重置生成短链接的源地址,代理商使用渠道短链远吗 $short = model('ShortRelation')->getRandShort($channel_id); if($short){ $jump_url = getCurrentDomain($channel_id,'/t/'.$insertId); $params['short_id'] = $short->id; $short_source_url = replaceShortDomain($jump_url, $short->id); // $params['jmp_url'] = $short_source_url; }else{ $short_source_url = $params['source_url']; } $shorturl = new ShortUrl(); /** * 腾讯短连接生成 */ $params['short_url_qq'] = $shorturl->tencent($channel_id,$short_source_url); /** * 微博短连接生成 */ $params['short_url_weibo'] =$shorturl->sina($short_source_url); $referralModel->save($params, ['id' => $insertId]); $newReferralIds[$v][] = $insertId; $redis = Redis::instance(); $redis->del('RI:N:'.$insertId); } else { $this->error($referralModel->getError()); } } } // 新增多条链接到代理商 $qd_data = array( 'title' => $row['title'], 'short_url_type' => $row['short_url_type'], 'state' => $row['state'], ); $qd_datas = []; foreach($ids as $i){ if(!empty($i)){ $qd_data['referral_ids'] = implode(',', $newReferralIds[$i]); $qd_data['admin_id'] = $i; } $qd_datas[] = $qd_data; } $s = $this->model->saveAll($qd_datas); if($s){ $row->save(); // 更新渠道商多条推送记录 $this->success('操作成功'); }else{ $this->error('推送给代理失败'); } // }else{ // $this->error('操作失败'); // } // exit; } //单条推广链接 $referrals = model('referral')->with('book')->where('referral.id', 'in', $row['referral_ids'])->select(); $row['referral_list'] = $referrals; //可以继续推的代理 $where = array( 'e.create_by'=>$this->auth->id, 'a.group_id'=>4, ); if(!empty($row['push_json'])){ $exists_ids = []; foreach($row['push_json'] as $p){ $ids = explode(',',$p['ids']); foreach ($ids as $key => $id) { if(!empty($id)){ $exists_ids[] = $id; } } } $where['admin.id'] = array("not in",$exists_ids); } $agents = model('admin') ->join("admin_extend e","e.admin_id = admin.id") ->join('auth_group_access a', 'a.uid= admin.id') ->where($where) ->field("admin.id,nickname") ->select(); $this->assign('agents',$agents); $this->assign('row', $row); return $this->view->fetch(); } }