ChannelService.php 15 KB

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