123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <?php
- namespace app\admin\controller\referral;
- use app\admin\library\ShortUrl;
- use app\common\controller\Backend;
- use app\common\service\ReferralService;
- use app\common\service\ResourceService;
- use think\Config;
- use think\Controller;
- use think\Request;
- use EasyWeChat\Factory;
- use app\common\library\Redis;
- use Symfony\Component\Cache\Simple\RedisCache;
- use GuzzleHttp\Client as Http;
- /**
- * 导粉推广文案多条
- *
- * @icon fa fa-circle-o
- */
- class Many extends Backend
- {
- /**
- * ReferralMany模型对象
- */
- protected $model = null;
- /**
- * 是否开启Validate验证
- */
- protected $modelValidate = true;
- /**
- * 是否开启数据限制
- * 支持auth/personal
- * 表示按权限判断/仅限个人
- * 默认为禁用,若启用请务必保证表中存在admin_id字段
- */
- protected $dataLimit = 'personal';
- /**
- * Multi方法可批量修改的字段
- */
- protected $multiFields = 'state';
- public function _initialize()
- {
- parent::_initialize();
- set_time_limit(120);
- $this->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();
- }
- }
|