ReferralService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/1/22
  6. * Time: 下午5:50
  7. */
  8. namespace app\common\service;
  9. use app\admin\library\Auth;
  10. use app\common\constants\AppConstants;
  11. use app\common\constants\ErrorCodeConstants;
  12. use app\common\library\Redis;
  13. use app\common\model\AdminConfig;
  14. use app\common\model\AuthGroup;
  15. use app\common\model\object\ReferralObject;
  16. use app\common\model\Referral;
  17. use think\Log;
  18. /**
  19. * Class ReferralService
  20. * @package app\common\service
  21. */
  22. class ReferralService extends BaseService
  23. {
  24. // 代理商 | 配号代理商
  25. const GROUP_DLS = 4;
  26. // 渠道商
  27. const GROUP_QDS = 3;
  28. /**
  29. * @var ReferralService
  30. */
  31. private static $self;
  32. /**
  33. * @return $this|ReferralService
  34. */
  35. public static function instance()
  36. {
  37. if (self::$self == NULL) {
  38. self::$self = new self();
  39. }
  40. return self::$self;
  41. }
  42. /**
  43. * @return Referral
  44. */
  45. public function getReferralModel()
  46. {
  47. return model('Referral');
  48. }
  49. /**
  50. * @return AuthGroupAccess
  51. */
  52. public function getAuthGroupAccessModel()
  53. {
  54. return model('AuthGroupAccess');
  55. }
  56. /**
  57. * @return AdminExtend
  58. */
  59. public function getAdminExtendModel()
  60. {
  61. return model('AdminExtend');
  62. }
  63. /**
  64. * 获取更新
  65. * @param $params
  66. * @return \app\common\model\object\ReturnObject
  67. */
  68. public function editReferral($params)
  69. {
  70. $oReferral = new ReferralObject();
  71. $oReferral->bind($params);
  72. $oReferral->share_image = ResourceService::instance()->getRandomImage()->data;
  73. $oReferral->share_title = ResourceService::instance()->getRandomTitle()->data;
  74. $exists = [
  75. 'id' => $oReferral->id,
  76. // 'admin_id' => $oReferral->admin_id,
  77. ];
  78. if (!$this->getReferralModel()->where($exists)->count()) {
  79. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->getReturn();
  80. }
  81. $this->getReferralModel()->update($oReferral->toArray());
  82. $key = 'RI:N:' . $oReferral->id;
  83. Redis::instance()->del($key);
  84. return $this->getReturn();
  85. }
  86. public function addReferral($params)
  87. {
  88. $oReferral = new ReferralObject();
  89. $oReferral->bind($params);
  90. $oReferral->createtime = $oReferral->updatetime = time();
  91. $oReferral->share_image = ResourceService::instance()->getRandomImage()->data;
  92. $oReferral->share_title = ResourceService::instance()->getRandomTitle()->data;
  93. if($oReferral->book_id){
  94. $book = BookService::instance()->getBookModel()->where('id', '=', $oReferral->book_id)->find();
  95. if($book->getData('state') == 0 || $book->getData('cansee') == 0){
  96. return $this->setCode(ErrorCodeConstants::PERMISSION_DENY)->setMsg('抱歉,本书已下架,无法生成推广链接~')->getReturn();
  97. }
  98. }
  99. $insert = $oReferral->toArray();
  100. $referralId = $this->getReferralModel()->insertGetId($insert);
  101. if(!$referralId){
  102. return $this->setCode(ErrorCodeConstants::DB_ERROR_UPDATE)->setMsg('数据库执行错误')->getReturn();
  103. }
  104. return $this->setData($referralId)->getReturn();
  105. }
  106. public function get189Url($referral_id, $default_url, $channel_id='')
  107. {
  108. //$channel_id = $this->getChannelIdByReferralId($referral_id);
  109. $domain = AppConstants::getRandomFrontDomain();
  110. //$base_url = "http://www.189.cn/login/skip/ecs.do?method=skip&platNo=10001&toStUrl=http://bj.189.cn@";
  111. //$base_url = "https://mp.weixinbridge.com/mp/wapredirect?url=http://";
  112. //$base_url = "http://grouproam.qq.com:443/cgi-bin/httpconn?htcmd=0x6ff0080&u=http://";
  113. $base_url = '';
  114. if(!$domain){
  115. $default_url = str_replace(["https://", "http://"], ["", ""], $default_url);
  116. $url = $base_url . $default_url;
  117. }else{
  118. //$url = $base_url . $domain . "/referral.html?t=" . $referral_id . "&type=code&channel_id=".$channel_id;
  119. $url = $base_url . $domain . "/referral.html?t=" . $referral_id . "&type=code";
  120. }
  121. Log::info('189url:'.$url);
  122. return $this->setData($url)->getReturn();
  123. }
  124. public function get189Urlv2($referral_id, $default_url, $channel_id='')
  125. {
  126. //$channel_id = $this->getChannelIdByReferralId($referral_id);
  127. $domain = AppConstants::getRandomFrontDomain();
  128. //$base_url = "http://www.189.cn/login/skip/ecs.do?method=skip&platNo=10001&toStUrl=http://bj.189.cn@";
  129. //$base_url = "https://mp.weixinbridge.com/mp/wapredirect?url=http://";
  130. $base_url = "http://grouproam.qq.com:443/cgi-bin/httpconn?htcmd=0x6ff0080&u=";
  131. if (!$domain) {
  132. $default_url = str_replace(["https://", "http://"], ["", ""], $default_url);
  133. $url = $base_url . urlencode("http://" . $default_url);
  134. } else {
  135. //$url = $base_url . $domain . "/referral.html?t=" . $referral_id . "&type=code&channel_id=".$channel_id;
  136. $url = $base_url . urlencode("http://" . $domain . "/referral.html?t=" . $referral_id . "&type=code");
  137. }
  138. Log::info('189url:'.$url);
  139. return $this->setData($url)->getReturn();
  140. }
  141. public function get189Urlv3($referral_id, $default_url, $channel_id='')
  142. {
  143. $domain = AppConstants::getRandomWechatDomain();
  144. $base_url = '';
  145. if(!$domain){
  146. $default_url = str_replace(["https://", "http://"], ["", ""], $default_url);
  147. $url = $base_url . $default_url;
  148. }else{
  149. $appid = $domain['appid'] ?? '';
  150. $redirect_uri = $domain['domain']."/referral.html?t=" . $referral_id . "&type=code" ?? '';
  151. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri=http://{$redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
  152. }
  153. Log::info('189url:'.$url);
  154. return $this->setData($url)->getReturn();
  155. }
  156. public function getSkinUrl($referral_id, $default_url, $channel_id='')
  157. {
  158. //$channel_id = $this->getChannelIdByReferralId($referral_id);
  159. $domain = AppConstants::getRandomFrontDomain();
  160. Log::info('skinUrl:domain:'.$domain);
  161. $base_url = "";
  162. if(!$domain){
  163. $default_url = str_replace(["https://", "http://"], ["", ""], $default_url);
  164. $url = $base_url . $default_url;
  165. }else{
  166. //$url = $base_url . $domain . "/referral.html?t=" . $referral_id . "&type=code&channel_id=".$channel_id;
  167. $url = $base_url . $domain . "/referral.html?t=" . $referral_id . "&type=code";
  168. }
  169. Log::info('skinUrl:'.$url);
  170. return $url;
  171. }
  172. /**
  173. * 通过Referral_id ,获取ChannelID
  174. * @param $referral_id
  175. * @return int
  176. */
  177. public function getChannelIdByReferralId($referral_id)
  178. {
  179. $channel_id = '';
  180. $referralInfo = $this->getReferralModel()->getone($referral_id, false);
  181. $referral_admin_id = $referralInfo['admin_id'];
  182. $group_id = $this->getAuthGroupAccessModel()->getGroupId($referral_admin_id);
  183. if($group_id == self::GROUP_DLS){
  184. $qds_info = $this ->getAdminExtendModel()->getInfo($referral_admin_id);
  185. if($qds_info['distribute']){
  186. // 配号代理商
  187. $channel_id = $referral_admin_id;
  188. } else{
  189. $channel_id = $qds_info['create_by'];
  190. }
  191. }
  192. if($group_id == self::GROUP_QDS){
  193. $channel_id = $referral_admin_id;
  194. }
  195. return $channel_id;
  196. }
  197. }