123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2019/1/22
- * Time: 下午5:50
- */
- namespace app\common\service;
- use app\admin\library\Auth;
- use app\common\constants\AppConstants;
- use app\common\constants\ErrorCodeConstants;
- use app\common\library\Redis;
- use app\common\model\AdminConfig;
- use app\common\model\AuthGroup;
- use app\common\model\object\ReferralObject;
- use app\common\model\Referral;
- use think\Log;
- /**
- * Class ReferralService
- * @package app\common\service
- */
- class ReferralService extends BaseService
- {
- // 代理商 | 配号代理商
- const GROUP_DLS = 4;
- // 渠道商
- const GROUP_QDS = 3;
- /**
- * @var ReferralService
- */
- private static $self;
- /**
- * @return $this|ReferralService
- */
- public static function instance()
- {
- if (self::$self == NULL) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * @return Referral
- */
- public function getReferralModel()
- {
- return model('Referral');
- }
- /**
- * @return AuthGroupAccess
- */
- public function getAuthGroupAccessModel()
- {
- return model('AuthGroupAccess');
- }
- /**
- * @return AdminExtend
- */
- public function getAdminExtendModel()
- {
- return model('AdminExtend');
- }
- /**
- * 获取更新
- * @param $params
- * @return \app\common\model\object\ReturnObject
- */
- public function editReferral($params)
- {
- $oReferral = new ReferralObject();
- $oReferral->bind($params);
- $oReferral->share_image = ResourceService::instance()->getRandomImage()->data;
- $oReferral->share_title = ResourceService::instance()->getRandomTitle()->data;
- $exists = [
- 'id' => $oReferral->id,
- // 'admin_id' => $oReferral->admin_id,
- ];
- if (!$this->getReferralModel()->where($exists)->count()) {
- return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->getReturn();
- }
- $this->getReferralModel()->update($oReferral->toArray());
- $key = 'RI:N:' . $oReferral->id;
- Redis::instance()->del($key);
- return $this->getReturn();
- }
- public function addReferral($params)
- {
- $oReferral = new ReferralObject();
- $oReferral->bind($params);
- $oReferral->createtime = $oReferral->updatetime = time();
- $oReferral->share_image = ResourceService::instance()->getRandomImage()->data;
- $oReferral->share_title = ResourceService::instance()->getRandomTitle()->data;
- if($oReferral->book_id){
- $book = BookService::instance()->getBookModel()->where('id', '=', $oReferral->book_id)->find();
- if($book->getData('state') == 0 || $book->getData('cansee') == 0){
- return $this->setCode(ErrorCodeConstants::PERMISSION_DENY)->setMsg('抱歉,本书已下架,无法生成推广链接~')->getReturn();
- }
- }
- $insert = $oReferral->toArray();
- $referralId = $this->getReferralModel()->insertGetId($insert);
- if(!$referralId){
- return $this->setCode(ErrorCodeConstants::DB_ERROR_UPDATE)->setMsg('数据库执行错误')->getReturn();
- }
- return $this->setData($referralId)->getReturn();
- }
- public function get189Url($referral_id, $default_url, $channel_id='')
- {
- //$channel_id = $this->getChannelIdByReferralId($referral_id);
- $domain = AppConstants::getRandomFrontDomain();
- //$base_url = "http://www.189.cn/login/skip/ecs.do?method=skip&platNo=10001&toStUrl=http://bj.189.cn@";
- //$base_url = "https://mp.weixinbridge.com/mp/wapredirect?url=http://";
- //$base_url = "http://grouproam.qq.com:443/cgi-bin/httpconn?htcmd=0x6ff0080&u=http://";
- $base_url = '';
- if(!$domain){
- $default_url = str_replace(["https://", "http://"], ["", ""], $default_url);
- $url = $base_url . $default_url;
- }else{
- //$url = $base_url . $domain . "/referral.html?t=" . $referral_id . "&type=code&channel_id=".$channel_id;
- $url = $base_url . $domain . "/referral.html?t=" . $referral_id . "&type=code";
- }
- Log::info('189url:'.$url);
- return $this->setData($url)->getReturn();
- }
- public function get189Urlv2($referral_id, $default_url, $channel_id='')
- {
- //$channel_id = $this->getChannelIdByReferralId($referral_id);
- $domain = AppConstants::getRandomFrontDomain();
- //$base_url = "http://www.189.cn/login/skip/ecs.do?method=skip&platNo=10001&toStUrl=http://bj.189.cn@";
- //$base_url = "https://mp.weixinbridge.com/mp/wapredirect?url=http://";
- $base_url = "http://grouproam.qq.com:443/cgi-bin/httpconn?htcmd=0x6ff0080&u=";
- if (!$domain) {
- $default_url = str_replace(["https://", "http://"], ["", ""], $default_url);
- $url = $base_url . urlencode("http://" . $default_url);
- } else {
- //$url = $base_url . $domain . "/referral.html?t=" . $referral_id . "&type=code&channel_id=".$channel_id;
- $url = $base_url . urlencode("http://" . $domain . "/referral.html?t=" . $referral_id . "&type=code");
- }
- Log::info('189url:'.$url);
- return $this->setData($url)->getReturn();
- }
- public function get189Urlv3($referral_id, $default_url, $channel_id='')
- {
- $domain = AppConstants::getRandomWechatDomain();
- $base_url = '';
- if(!$domain){
- $default_url = str_replace(["https://", "http://"], ["", ""], $default_url);
- $url = $base_url . $default_url;
- }else{
- $appid = $domain['appid'] ?? '';
- $redirect_uri = $domain['domain']."/referral.html?t=" . $referral_id . "&type=code" ?? '';
- $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";
- }
- Log::info('189url:'.$url);
- return $this->setData($url)->getReturn();
- }
- public function getSkinUrl($referral_id, $default_url, $channel_id='')
- {
- //$channel_id = $this->getChannelIdByReferralId($referral_id);
- $domain = AppConstants::getRandomFrontDomain();
- Log::info('skinUrl:domain:'.$domain);
- $base_url = "";
- if(!$domain){
- $default_url = str_replace(["https://", "http://"], ["", ""], $default_url);
- $url = $base_url . $default_url;
- }else{
- //$url = $base_url . $domain . "/referral.html?t=" . $referral_id . "&type=code&channel_id=".$channel_id;
- $url = $base_url . $domain . "/referral.html?t=" . $referral_id . "&type=code";
- }
- Log::info('skinUrl:'.$url);
- return $url;
- }
- /**
- * 通过Referral_id ,获取ChannelID
- * @param $referral_id
- * @return int
- */
- public function getChannelIdByReferralId($referral_id)
- {
- $channel_id = '';
- $referralInfo = $this->getReferralModel()->getone($referral_id, false);
- $referral_admin_id = $referralInfo['admin_id'];
- $group_id = $this->getAuthGroupAccessModel()->getGroupId($referral_admin_id);
- if($group_id == self::GROUP_DLS){
- $qds_info = $this ->getAdminExtendModel()->getInfo($referral_admin_id);
- if($qds_info['distribute']){
- // 配号代理商
- $channel_id = $referral_admin_id;
- } else{
- $channel_id = $qds_info['create_by'];
- }
- }
- if($group_id == self::GROUP_QDS){
- $channel_id = $referral_admin_id;
- }
- return $channel_id;
- }
- }
|