Referral.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wanggb
  5. * Date: 2019/2/21
  6. * Time: 19:00
  7. */
  8. namespace app\api\controller;
  9. use app\common\constants\AppConstants;
  10. use app\common\controller\Api;
  11. use app\common\library\Redis;
  12. use app\common\service\ReferralService;
  13. use app\main\constants\CacheConstants;
  14. use app\main\service\LogService;
  15. use think\Config;
  16. use think\Log;
  17. class Referral extends Api
  18. {
  19. protected $wechatRedisKey = 'WECHATPOOLS:';
  20. public function getreferralshort()
  21. {
  22. Log::info('[ API ] [ REFERRAL ] PARAM: ' . json_encode(var_export($this->request->param(), true),
  23. JSON_UNESCAPED_UNICODE));
  24. $id = $this->request->param('id');
  25. $type = $this->request->param('type');
  26. $channel_id = ReferralService::instance()->getChannelIdByReferralId($id);
  27. $referral = ReferralService::instance()->getReferralModel()->where('id', '=', $id)->find();
  28. Log::info('[ API ] [ REFERRAL ] channel_id::' . $channel_id);
  29. if ($channel_id) {
  30. $referral_url = getCurrentDomain($channel_id, $referral['source_url']);
  31. } else {
  32. $referral_url = $referral['source_url'];
  33. }
  34. Log::info('[ API ] [ REFERRAL ] SourceURL:' . $referral_url);
  35. if ($type == 'code') {
  36. //$wechat_domains = @file_get_contents(ROOT_PATH . 'public/wechat_domains.txt');
  37. /*$redis = Redis::instance();
  38. $wechat_domains = $redis->get($this->wechatRedisKey);
  39. $data = json_decode($wechat_domains, true);
  40. $idx = rand(0, (count($data) - 1));*/
  41. $wechatDomainData = AppConstants::getRandomWechatDomain();
  42. $appid = $wechatDomainData['appid'];
  43. $wechatDomain = $wechatDomainData['domain'];
  44. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=";
  45. $url .= urlencode("http://" . $wechatDomain . "/referral.html?t=" . $id . "&channel_id=" . $channel_id);
  46. $url .= "&response_type=code&scope=snsapi_base&connect_redirect=1#wechat_redirect";
  47. //$url = "/referral.html?t=" . $id . "&channel_id=" . $channel_id;
  48. Log::info('[ API ] [ REFERRAL ] [RESULTURL] ' . $url);
  49. $this->success('返回成功', ['url' => $url]);
  50. } else {
  51. //方案一
  52. /*if ($referral['short_id'] > 0) {
  53. // 开启导粉域名
  54. $jump_url = getCurrentDomain($channel_id, '/t/' . $referral['id']);
  55. $referral_url = replaceShortDomain($jump_url, $referral['short_id']);
  56. Log::info('[ API ] [ REFERRAL ] replaceShortDomain ' . $referral_url);
  57. }*/
  58. //方案二
  59. $referral_url = $this->getGuideDomain($channel_id, $referral['id']);
  60. if(empty($referral_url)){
  61. // 获取业务域名
  62. $referral_url = getCurrentDomain($channel_id, '/t/' . $referral['id']);
  63. }
  64. Log::info('[ API ] [ REFERRAL ] GuideDomain ' . $referral_url);
  65. Log::info('[ API ] [ REFERRAL ] [RESULTURL] ' . $referral_url);
  66. $this->success('返回成功', ['url' => $referral_url]);
  67. }
  68. }
  69. /**
  70. * 获取GuideDomain
  71. * @param $admin_id
  72. * @param $referral_id
  73. * @return string
  74. */
  75. public function getGuideDomain($admin_id, $referral_id)
  76. {
  77. $adminConfigInfo = model('AdminConfig')->getAdminInfoAll($admin_id);
  78. // 短链域名&倒粉域名开关 是否开启/data/www/cps/application/api/controller/Referral.php
  79. if (!empty($adminConfigInfo['guide_domain'])) {
  80. $guide_domain = model('GuideRelation')->getRandGuideHost($admin_id);
  81. return sprintf("%s/t/%d", $guide_domain, $referral_id);
  82. } else {
  83. return '';
  84. }
  85. }
  86. }