Many.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace app\admin\controller\referral;
  3. use app\admin\library\ShortUrl;
  4. use app\common\controller\Backend;
  5. use app\common\service\ReferralService;
  6. use app\common\service\ResourceService;
  7. use think\Config;
  8. use think\Controller;
  9. use think\Request;
  10. use EasyWeChat\Factory;
  11. use app\common\library\Redis;
  12. use Symfony\Component\Cache\Simple\RedisCache;
  13. use GuzzleHttp\Client as Http;
  14. /**
  15. * 导粉推广文案多条
  16. *
  17. * @icon fa fa-circle-o
  18. */
  19. class Many extends Backend
  20. {
  21. /**
  22. * ReferralMany模型对象
  23. */
  24. protected $model = null;
  25. /**
  26. * 是否开启Validate验证
  27. */
  28. protected $modelValidate = true;
  29. /**
  30. * 是否开启数据限制
  31. * 支持auth/personal
  32. * 表示按权限判断/仅限个人
  33. * 默认为禁用,若启用请务必保证表中存在admin_id字段
  34. */
  35. protected $dataLimit = 'personal';
  36. /**
  37. * Multi方法可批量修改的字段
  38. */
  39. protected $multiFields = 'state';
  40. public function _initialize()
  41. {
  42. parent::_initialize();
  43. set_time_limit(120);
  44. $this->model = model('ReferralMany');
  45. $this->view->assign("shortUrlTypeList", $this->model->getShortUrlTypeList());
  46. $this->view->assign("stateList", $this->model->getStateList());
  47. }
  48. /**
  49. * 查看
  50. */
  51. public function index()
  52. {
  53. // 是否开启微信分享开关
  54. $open_wechat_share = Config::get('site.open_wechat_share') ?? 0;
  55. $this->assignconfig('open_wechat_share', $open_wechat_share);
  56. //设置过滤方法
  57. $this->request->filter(['strip_tags']);
  58. if ($this->request->isAjax()) {
  59. //如果发送的来源是Selectpage,则转发到Selectpage
  60. if ($this->request->request('pkey_name')) {
  61. return $this->selectpage();
  62. }
  63. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  64. $whereArr = ['state' => 1];
  65. $whereArr = function ($query) use ($whereArr) {
  66. foreach ($whereArr as $k => $v) {
  67. if (is_array($v)) {
  68. call_user_func_array([$query, 'where'], $v);
  69. } else {
  70. $query->where($v);
  71. }
  72. }
  73. };
  74. $total = $this->model
  75. ->where($where)
  76. ->where($whereArr)
  77. ->order($sort, $order)
  78. ->count();
  79. $list = $this->model
  80. ->where($where)
  81. ->where($whereArr)
  82. ->order($sort, $order)
  83. ->limit($offset, $limit)
  84. ->select();
  85. foreach ($list as $key => &$item) {
  86. $item = $item->toArray();
  87. $item['referral_list'] = model('referral')->with('book')->where('referral.id', 'in', $item['referral_ids'])->select();
  88. if (is_array($item['referral_list'])) {
  89. // 加入防封前置域名
  90. foreach ($item['referral_list'] as $r_key => $r_item) {
  91. $item['referral_list'][$r_key]['skin_url'] = ReferralService::instance()->getSkinUrl($r_item['id'], $r_item['source_url']);
  92. }
  93. }
  94. if (is_array($item['push_json'])) {
  95. foreach ($item['push_json'] as $k => &$it) {
  96. $it['agent_list'] = model('Admin')->where('id', 'in', $it['ids'])->select();
  97. }
  98. }
  99. }
  100. $result = array("total" => $total, "rows" => $list);
  101. return json($result);
  102. }
  103. if($this->auth->agent_id || $this->group == 3){
  104. if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
  105. $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
  106. }
  107. }
  108. return $this->view->fetch();
  109. }
  110. /**
  111. * 添加
  112. */
  113. public function add()
  114. {
  115. if ($this->request->isPost()) {
  116. $params = $this->request->post("row/a");
  117. if ($params) {
  118. if ($this->dataLimit) {
  119. $params[$this->dataLimitField] = $this->auth->id;
  120. }
  121. try
  122. {
  123. //是否采用模型验证
  124. if ($this->modelValidate) {
  125. $name = basename(str_replace('\\', '/', get_class($this->model)));
  126. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  127. $this->model->validate($validate);
  128. }
  129. $result = $this->model->allowField(true)->save($params);
  130. if ($result !== false) {
  131. $this->success();
  132. } else {
  133. $this->error($this->model->getError());
  134. }
  135. } catch (\think\exception\PDOException $e) {
  136. $this->error($e->getMessage());
  137. }
  138. }
  139. $this->error(__('Parameter %s can not be empty', ''));
  140. }
  141. return $this->view->fetch();
  142. }
  143. /**
  144. * 编辑
  145. */
  146. public function edit($ids = null)
  147. {
  148. $row = $this->model->get($ids);
  149. if (!$row) {
  150. $this->error(__('No Results were found'));
  151. }
  152. $adminIds = $this->getDataLimitAdminIds();
  153. if (is_array($adminIds)) {
  154. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  155. $this->error(__('You have no permission'));
  156. }
  157. }
  158. if ($this->request->isPost()) {
  159. $params = $this->request->post("row/a");
  160. if ($params) {
  161. try
  162. {
  163. //是否采用模型验证
  164. if ($this->modelValidate) {
  165. $name = basename(str_replace('\\', '/', get_class($this->model)));
  166. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  167. $row->validate($validate);
  168. }
  169. $result = $row->allowField(true)->save($params);
  170. if ($result !== false) {
  171. $this->success();
  172. } else {
  173. $this->error($row->getError());
  174. }
  175. } catch (\think\exception\PDOException $e) {
  176. $this->error($e->getMessage());
  177. }
  178. }
  179. $this->error(__('Parameter %s can not be empty', ''));
  180. }
  181. $row['referral_list'] = model('referral')->with('book')->where('referral.id', 'in', $row['referral_ids'])->select();
  182. $this->view->assign("row", $row);
  183. return $this->view->fetch();
  184. }
  185. /**
  186. * 推送
  187. */
  188. public function push()
  189. {
  190. $id = input('id');
  191. $row = $this->model->get($id);
  192. if ($this->request->isPost()) {
  193. $rq = input();
  194. $ids = $rq['ids'];
  195. if(count($ids) == 1){
  196. if(empty($ids[0])) {
  197. $this->error('请至少选择一位代理');
  198. }else{
  199. $idstr = $ids[0];
  200. }
  201. }else{
  202. $idstr = implode(',',$ids);
  203. }
  204. $item = array(
  205. 'ids'=>$idstr,
  206. 'time'=>date('Y-m-d H:i:s')
  207. );
  208. if(!empty($row['push_json'])){
  209. $push_json = $row['push_json'];
  210. }else{
  211. $push_json = [];
  212. }
  213. $push_json[] = $item;
  214. //更新
  215. $row['push_json'] = $push_json;
  216. // $update = $row->save();
  217. // if($update !== false){
  218. /**
  219. * 插入推广链接到代理商
  220. */
  221. // 新增单条链接到代理商
  222. $referral_ids = explode(',',$row['referral_ids']);
  223. $newReferralIds = [];
  224. foreach ($referral_ids as $key=>$value)
  225. {
  226. if (!$value) {
  227. continue;
  228. }
  229. $referralModel = model('referral');
  230. $res = $referralModel->get($value);
  231. if (empty($res)) {
  232. continue;
  233. }
  234. $params = [];
  235. $params['manage_title_id'] = $res['manage_title_id'];
  236. $params['manage_cover_id'] = $res['manage_cover_id'];
  237. $params['manage_template_id'] = $res['manage_template_id'];
  238. $params['manage_preview_id'] = $res['manage_preview_id'];
  239. $params['guide_title'] = $res['guide_title'];
  240. $params['guide_image'] = $res['guide_image'];
  241. $params['guide_sex'] = $res['guide_sex'];
  242. $params['guide_idx'] = $res['guide_idx'];
  243. $params['book_id'] = $res['book_id'];
  244. $params['chapter_id'] = $res['chapter_id'];
  245. $params['chapter_name'] = $res['chapter_name'];
  246. $params['chapter_idx'] = $res['chapter_idx'];
  247. $params['guide_chapter_idx'] = $res['guide_chapter_idx'];
  248. $params['name'] = $res['name'];
  249. $params['wx_type'] = $res['wx_type'];
  250. $params['type'] = $res['type'];
  251. $params['push'] = $res['push'];
  252. $params['uv'] = 0;
  253. $params['money'] = 0;
  254. $params['source_url'] = null;
  255. $params['short_url_qq'] = null;
  256. $params['short_url_weibo'] = null;
  257. $params['createtime'] = time();
  258. $params['updatetime'] = time();
  259. $params['share_image'] = ResourceService::instance()->getRandomImage()->data;
  260. $params['share_title'] = ResourceService::instance()->getRandomTitle()->data;
  261. foreach ($ids as $k=>$v)
  262. {
  263. if (!$v) {
  264. continue;
  265. }
  266. $params['admin_id'] = $v;
  267. $insertId = $referralModel->insertGetId($params);
  268. if($extend = model("AdminExtend")->getInfo($v)){
  269. if(isset($extend['distribute']) && $extend['distribute']){
  270. $channel_id = $v;
  271. }else{
  272. $channel_id = $this->auth->id;
  273. }
  274. }else{
  275. $channel_id = $this->auth->id;
  276. }
  277. if ($insertId !== false) {
  278. if ($params['type'] == 2) {
  279. //首页
  280. $source_url = getCurrentDomain($channel_id,'?referral_id=' . $insertId);
  281. } else {
  282. $source_url = getCurrentDomain($channel_id,'/index/book/chapter?book_id=' . $params['book_id'] . '&sid=' . $params['chapter_id'] . '&referral_id=' . $insertId);
  283. }
  284. $source_url .= '&agent_id=' . $v;
  285. $params['source_url'] = $source_url;
  286. //绑定域名短链ID及重置生成短链接的源地址,代理商使用渠道短链远吗
  287. $short = model('ShortRelation')->getRandShort($channel_id);
  288. if($short){
  289. $jump_url = getCurrentDomain($channel_id,'/t/'.$insertId);
  290. $params['short_id'] = $short->id;
  291. $short_source_url = replaceShortDomain($jump_url, $short->id);
  292. // $params['jmp_url'] = $short_source_url;
  293. }else{
  294. $short_source_url = $params['source_url'];
  295. }
  296. $shorturl = new ShortUrl();
  297. /**
  298. * 腾讯短连接生成
  299. */
  300. $params['short_url_qq'] = $shorturl->tencent($channel_id,$short_source_url);
  301. /**
  302. * 微博短连接生成
  303. */
  304. $params['short_url_weibo'] =$shorturl->sina($short_source_url);
  305. $referralModel->save($params, ['id' => $insertId]);
  306. $newReferralIds[$v][] = $insertId;
  307. $redis = Redis::instance();
  308. $redis->del('RI:N:'.$insertId);
  309. } else {
  310. $this->error($referralModel->getError());
  311. }
  312. }
  313. }
  314. // 新增多条链接到代理商
  315. $qd_data = array(
  316. 'title' => $row['title'],
  317. 'short_url_type' => $row['short_url_type'],
  318. 'state' => $row['state'],
  319. );
  320. $qd_datas = [];
  321. foreach($ids as $i){
  322. if(!empty($i)){
  323. $qd_data['referral_ids'] = implode(',', $newReferralIds[$i]);
  324. $qd_data['admin_id'] = $i;
  325. }
  326. $qd_datas[] = $qd_data;
  327. }
  328. $s = $this->model->saveAll($qd_datas);
  329. if($s){
  330. $row->save(); // 更新渠道商多条推送记录
  331. $this->success('操作成功');
  332. }else{
  333. $this->error('推送给代理失败');
  334. }
  335. // }else{
  336. // $this->error('操作失败');
  337. // }
  338. // exit;
  339. }
  340. //单条推广链接
  341. $referrals = model('referral')->with('book')->where('referral.id', 'in', $row['referral_ids'])->select();
  342. $row['referral_list'] = $referrals;
  343. //可以继续推的代理
  344. $where = array(
  345. 'e.create_by'=>$this->auth->id,
  346. 'a.group_id'=>4,
  347. );
  348. if(!empty($row['push_json'])){
  349. $exists_ids = [];
  350. foreach($row['push_json'] as $p){
  351. $ids = explode(',',$p['ids']);
  352. foreach ($ids as $key => $id) {
  353. if(!empty($id)){
  354. $exists_ids[] = $id;
  355. }
  356. }
  357. }
  358. $where['admin.id'] = array("not in",$exists_ids);
  359. }
  360. $agents = model('admin')
  361. ->join("admin_extend e","e.admin_id = admin.id")
  362. ->join('auth_group_access a', 'a.uid= admin.id')
  363. ->where($where)
  364. ->field("admin.id,nickname")
  365. ->select();
  366. $this->assign('agents',$agents);
  367. $this->assign('row', $row);
  368. return $this->view->fetch();
  369. }
  370. }