ChannelService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/12/2
  6. * Time: 15:56
  7. */
  8. namespace App\Modules\Channel\Services;
  9. use App\Modules\OfficialAccount\Models\DistributionSelfDefineConfig;
  10. use App\Modules\Channel\Models\Channel;
  11. use App\Modules\Finance\Services\FinanceService;
  12. use App\Modules\SendOrder\Services\SendOrderService;
  13. use App\Modules\Trade\Services\PayMerchantService;
  14. use DB;
  15. class ChannelService
  16. {
  17. /**
  18. * 获取所有渠道
  19. * @param array $params
  20. * @return mixed
  21. */
  22. static function getAllChannels($params = [])
  23. {
  24. return Channel::getAllChannels($params);
  25. }
  26. /**
  27. * 根据手机号码获取
  28. * @return mixed
  29. */
  30. static function getByPhone($phone)
  31. {
  32. return Channel::getByPhone($phone);
  33. }
  34. /**
  35. * 根据名称获取id
  36. * @param $nickName
  37. * @return mixed
  38. */
  39. static function getIdByNickName($nickName)
  40. {
  41. return Channel::getIdByNickName($nickName);
  42. }
  43. /**
  44. * 根据账号ID获取渠道列表
  45. * @return mixed
  46. */
  47. static function getByChannelUserId($channel_user_id)
  48. {
  49. return Channel::getByChannelUserId($channel_user_id);
  50. }
  51. /**
  52. * 删除站点
  53. * @param unknown_type $distribution_channel_id
  54. * @param unknown_type $channel_user_id
  55. */
  56. static function deleteChannel($distribution_channel_id, $channel_user_id)
  57. {
  58. return Channel::deleteChannel($distribution_channel_id, $channel_user_id);
  59. }
  60. /**
  61. * 根据渠道id获取
  62. * @return mixed
  63. */
  64. static function getById($id)
  65. {
  66. return Channel::getById($id);
  67. }
  68. /**
  69. * 根据渠道id获取渠道名称
  70. * @return mixed
  71. */
  72. static function getChannelNameById($id)
  73. {
  74. if (empty($id)) {
  75. return '';
  76. }
  77. return Channel::getChannelNameById($id);
  78. }
  79. /**
  80. * 根据渠道id获取渠道用户id
  81. * @return mixed
  82. */
  83. static function getUserIdById($id)
  84. {
  85. if (empty($id)) {
  86. return 0;
  87. }
  88. return Channel::getUserIdById($id);
  89. }
  90. /**
  91. * 根据渠道id获取渠道昵称
  92. * @param $id
  93. * @return string
  94. */
  95. static function getChannelNicknameById($id)
  96. {
  97. if (empty($id)) {
  98. return "";
  99. }
  100. $result = Channel::getChannelNicknameById($id);
  101. if (empty($result)) {
  102. return "";
  103. }
  104. return $result['nickname'];
  105. }
  106. /**
  107. * 根据渠道id获取渠道昵称
  108. * @param $id
  109. * @return string
  110. */
  111. static function getChannelCompanyNameById($id)
  112. {
  113. if (empty($id)) {
  114. return "";
  115. }
  116. $result = Channel::getChannelCompanyNameById($id);
  117. return $result;
  118. }
  119. /**
  120. * 获取渠道对应的支付渠道
  121. * @param $id
  122. * @return string
  123. */
  124. static function getChannelBankSourceById($id)
  125. {
  126. //TODO 获取渠道对应的支付渠道
  127. $channel = self::getById($id);
  128. if (!empty($channel) && $channel['pay_merchant_id'] > 0) {
  129. $payMerchant = PayMerchantService::getPayMerchantSingle($channel['pay_merchant_id']);
  130. if (!empty($payMerchant) && !empty($payMerchant['source'])) {
  131. return $payMerchant['source'];
  132. }
  133. }
  134. return config('common.tonglianpay');
  135. }
  136. /**
  137. * 创建渠道
  138. * @param array $params ['password','name','pay_type','nickname','latest_login_ip','latest_login_time','remark','register_ip','channel_user_id']
  139. * @return object
  140. */
  141. static function createChannel($params)
  142. {
  143. return Channel::createChannel($params);
  144. }
  145. /**
  146. * 获取渠道统计数据
  147. */
  148. static function getChannelData()
  149. {
  150. //获取所有的渠道
  151. $channels = Channel::getAllChannels();
  152. //获取所有渠道账户余额总额(可提现金额总额)
  153. $avaliableBalanceAll = FinanceService::getChannelAccountBalanceAll();
  154. //所有充值总则
  155. // $chargeAllAmount = TradeSevices::getChannelPriceCountAll();
  156. if (empty($channels)) {
  157. foreach ($channels as $channelItem) {
  158. $channelId = $channelItem->id;
  159. //获取渠道下的账户余额
  160. $channelAvaliableBalance = FinanceService::getChannelAccountBalance($channelId);
  161. //获取渠道下的充值总额
  162. // $channelChargeAllAmount = Trade::getChannelPriceCountSingle($channelId);
  163. //获取渠道下的总派单数
  164. $sendOrderCount = SendOrderService::getPromotionCountByChannelId($channelId);
  165. }
  166. }
  167. }
  168. /**
  169. * 修改渠道密码
  170. * @param string $phone 手机号码
  171. * @param string $password 加密后密码
  172. * @return boolen
  173. */
  174. static function modifyPassword($phone, $password)
  175. {
  176. return Channel::modifyPassword($phone, $password);
  177. }
  178. /**
  179. * 获取渠道列表
  180. * @param $id 渠道ID
  181. * @param $params []
  182. * name:渠道名称 可选
  183. * nickname:渠道昵称 可选
  184. * pay_merchant_id: 支付通道 可选
  185. * distribution_manages_id: 管理员 可选
  186. * phone:电话 可选
  187. * person_in_charge_name:负责人 可选
  188. * remark:备注 可选
  189. * is_enabled: 是否开通 0:未审核; 1:审核通过
  190. * @return mixed
  191. */
  192. public static function updateChannelData($id, $params = [])
  193. {
  194. return Channel::updateChannelData($id, $params);
  195. }
  196. /**
  197. * 获取渠道列表
  198. * @param $params []
  199. * channel_id:渠道ID 可选
  200. * channel_name:渠道名称 可选
  201. * search_name: 搜索名称
  202. * start_date:开始时间 可选
  203. * end_date:结束时间 可选
  204. * is_enabled: 是否开通 0:未审核; 1:审核通过
  205. * distribution_manages_id: 管理员 可选
  206. * @param $isAll
  207. * @return mixed
  208. */
  209. public static function getChannelList($params = [], $isAll = '')
  210. {
  211. return Channel::getChannelList($params, $isAll);
  212. }
  213. /**
  214. * 获取当前渠道经理下所有渠道ID
  215. * @param $distribution_manages_id
  216. * @return mixed
  217. */
  218. public static function getChannelIdList($distribution_manages_id = '')
  219. {
  220. return Channel::getChannelIdList($distribution_manages_id);
  221. }
  222. public static function getSourceName($source)
  223. {
  224. if (config('common.tonglianpay') == $source) {
  225. return "通联支付";
  226. } else if (config('common.lianlianpay') == $source) {
  227. return "连连支付";
  228. } else if (config('common.officialpay') == $source) {
  229. return "官方支付";
  230. }
  231. return "";
  232. }
  233. static function getDefault($channel_user_id)
  234. {
  235. return Channel::getDefault($channel_user_id);
  236. }
  237. /**
  238. * 通过渠道ID、用户ID获取渠道
  239. */
  240. static function getUserChannel($distribution_channle_id, $channel_user_id)
  241. {
  242. return Channel::getUserChannel($distribution_channle_id, $channel_user_id);
  243. }
  244. /**
  245. * 获取用户下所有渠道ID
  246. * @param $channel_user_id
  247. */
  248. static function getUserChannelIds($channel_user_id)
  249. {
  250. return Channel::getUserChannelIds($channel_user_id);
  251. }
  252. static function getUserChannelIdsV2($channel_user_id)
  253. {
  254. return Channel::where('channel_user_id', $channel_user_id)->get()->pluck('id')->toArray();
  255. }
  256. /**
  257. * 设置站点别名
  258. */
  259. static function setChannelSiteNickName($distribution_channle_id, $nick_name)
  260. {
  261. return Channel::where('id', $distribution_channle_id)->update(['site_nick_name' => $nick_name]);
  262. }
  263. /**
  264. * 通过DistributionManagesId获取
  265. */
  266. static function getByDistributionManagesId($distribution_manage_id)
  267. {
  268. return Channel::getByDistributionManagesId($distribution_manage_id);
  269. }
  270. /**
  271. * 更新外站开关
  272. * @return mixed
  273. */
  274. static function updateDistributionOuterSiteSwitch($distribution_channel_id,$status)
  275. {
  276. return Channel::updateDistributionOuterSiteSwitch($distribution_channel_id,$status);
  277. }
  278. /**
  279. * 更新老年站点开关
  280. * @return mixed
  281. */
  282. static function updateDistributionOldUserSiteSwitch($distribution_channel_id,$status)
  283. {
  284. return Channel::updateDistributionOldUserSiteSwitch($distribution_channel_id,$status);
  285. }
  286. /**
  287. * 获取渠道信息
  288. * @return mixed
  289. */
  290. static function getDistributionChannel($distribution_channel_id)
  291. {
  292. return Channel::getDistributionChannel($distribution_channel_id);
  293. }
  294. /**
  295. * 获取渠道信息
  296. * @return mixed
  297. */
  298. static function getDistributionChannelSwitchByCategory($distribution_channel_id,$category)
  299. {
  300. return DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id,$category);
  301. }
  302. /**
  303. * 获取渠道信息
  304. * @return mixed
  305. */
  306. static function getDistributionChannelSwitchByCategoryAndCompany($company_id,$category)
  307. {
  308. return DistributionSelfDefineConfig::getDistributionChannelSwitchByCategoryAndCompany($company_id,$category);
  309. }
  310. // 判断渠道的某个分类的 按照公司的权限
  311. static function check_channel_company_priv($distribution_channel_id,$category){
  312. $distribution = self::getDistributionChannel($distribution_channel_id);
  313. $channel_user_id = isset($distribution->channel_user_id)?$distribution->channel_user_id:'';
  314. $channel_user = ChannelUserService::getById($channel_user_id);
  315. $company_id = isset($channel_user->company_id)?$channel_user->company_id:'';
  316. \Log::info('check_channel_company_priv:category:'.$category.' distribution_channel_id:'.$distribution_channel_id.' channel_user_id:'.$channel_user_id.' company_id:'.$company_id);
  317. return self::getDistributionChannelSwitchByCategoryAndCompany($company_id,$category);
  318. }
  319. // 判断渠道的某个分类的 按照登录账号的权限
  320. static function check_channel_account_priv($distribution_channel_id,$category){
  321. $distribution = self::getDistributionChannel($distribution_channel_id);
  322. $channel_user_id = isset($distribution->channel_user_id)?$distribution->channel_user_id:'';
  323. $channel_user = ChannelUserService::getById($channel_user_id);
  324. $account = isset($channel_user->phone)?$channel_user->phone:'';
  325. \Log::info('check_channel_company_priv:category:'.$category.' distribution_channel_id:'.$distribution_channel_id.' channel_user_id:'.$channel_user_id.' account:'.$account);
  326. return self::getDistributionChannelSwitchByCategoryAndAccount($account,$category);
  327. }
  328. static function getDistributionChannelSwitchByCategoryAndAccount($account,$category)
  329. {
  330. return DistributionSelfDefineConfig::getDistributionChannelSwitchByCategoryAndAccount($account,$category);
  331. }
  332. /**
  333. * 根据站点属性,转换对应的回复链接
  334. */
  335. static function convertChannelReplyUrl($is_outer_site,$appid,$openid,$content,$fromwhere='',$keyword='')
  336. {
  337. if(!$is_outer_site) return $content;
  338. $matchs = [];
  339. $from_where_str = '&fromwhere='.$fromwhere;
  340. if ($keyword) $keyword = '&fromtype='.$keyword;
  341. \Log::Info('convertChannelReplyUrl_before:appid'.$appid.' openid:'.$openid.' is_outer_site:'.$is_outer_site.' fromwhere:'.$fromwhere);
  342. $new_content = $content;
  343. // \Log::Info('$content');\Log::Info($content);
  344. // 图文模式
  345. if(is_array($new_content)){
  346. foreach ($new_content as $key=>$one_content){
  347. if(isset($new_content[$key]['url']) && !empty($new_content[$key]['url'])){
  348. if(strpos($new_content[$key]['url'],'?') > -1){
  349. $new_content[$key]['url'] = $new_content[$key]['url'].'&appid='.$appid.'&openid='.$openid.$from_where_str;
  350. }else{
  351. $new_content[$key]['url'] = $new_content[$key]['url'].'?appid='.$appid.'&openid='.$openid.$from_where_str;
  352. }
  353. }
  354. }
  355. }
  356. // 文字模式
  357. else{
  358. // preg_match_all('/href=\"(.*)\"/u', $content, $matchs1);
  359. // $matchs1 = isset($matchs1[1])?$matchs1[1]:[];
  360. // preg_match_all('/href=\'(.*)\'/u', $content, $matchs2);
  361. // $matchs2 = isset($matchs2[1])?$matchs2[1]:[];
  362. // $matchs_all = array_merge($matchs1,$matchs2);
  363. //
  364. preg_match_all("/<a(s*[^>]+s*)href=([\"|']?)([^\"'>\s]+)([\"|']?)/ies",$content,$matchs);
  365. $matchs_all = isset($matchs[3])?$matchs[3]:[];
  366. \Log::info('$matchs_all');\Log::info($matchs_all);
  367. if(!empty($matchs_all)){
  368. foreach($matchs_all as $match){
  369. $match = str_replace(array('\r','\n'),"",$match);// 兼容换行
  370. $match = trim($match);
  371. $new_url = $url = $match;
  372. if(strpos($url,'?') > -1){
  373. $new_url = $url.'&appid='.$appid.'&openid='.$openid.$from_where_str.$keyword;
  374. }else{
  375. $new_url = $url.'?appid='.$appid.'&openid='.$openid.$from_where_str.$keyword;
  376. }
  377. // 小链接可能多次被替换,所以一定要引号结尾
  378. $new_content = str_replace($url.'\n"',$new_url.'"',$new_content);
  379. $new_content = str_replace($url."\n'",$new_url."'",$new_content);
  380. $new_content = str_replace($url.'"',$new_url.'"',$new_content);
  381. $new_content = str_replace($url."'",$new_url."'",$new_content);
  382. }
  383. }
  384. }
  385. \Log::Info('convertChannelReplyUrl_after:appid'.$appid.' openid:'.$openid);
  386. \Log::Info('$new_content');\Log::Info($new_content);
  387. return $new_content;
  388. }
  389. static function getContentByUrl($url)
  390. {
  391. $ch = curl_init();
  392. $timeout = 10; // set to zero for no timeout
  393. curl_setopt($ch, CURLOPT_URL, $url);
  394. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  395. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36');
  396. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  397. $html = curl_exec($ch);
  398. return $html;
  399. }
  400. public static function getChannelCompanyInfo($distribution_channel_id){
  401. return Channel::select(DB::raw("companies.*"))
  402. ->leftjoin('channel_users','channel_users.id','=','distribution_channels.channel_user_id')
  403. ->leftjoin('companies','companies.id','=','channel_users.company_id')
  404. ->where('distribution_channels.id',$distribution_channel_id)
  405. ->first();
  406. }
  407. public static function getChannelCompanySex($distribution_channel_id){
  408. $company = Channel::select(DB::raw("companies.*"))
  409. ->leftjoin('channel_users','channel_users.id','=','distribution_channels.channel_user_id')
  410. ->leftjoin('companies','companies.id','=','channel_users.company_id')
  411. ->where('distribution_channels.id',$distribution_channel_id)
  412. ->first();
  413. // 1男 2女 3混合
  414. $sex = isset($company->fans_gender)?$company->fans_gender:'3';
  415. return $sex;
  416. }
  417. }