ChannelService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. * @param $id 渠道ID
  148. * @param $params []
  149. * name:渠道名称 可选
  150. * nickname:渠道昵称 可选
  151. * pay_merchant_id: 支付通道 可选
  152. * distribution_manages_id: 管理员 可选
  153. * phone:电话 可选
  154. * person_in_charge_name:负责人 可选
  155. * remark:备注 可选
  156. * is_enabled: 是否开通 0:未审核; 1:审核通过
  157. * @return mixed
  158. */
  159. public static function updateChannelData($id, $params = [])
  160. {
  161. return Channel::updateChannelData($id, $params);
  162. }
  163. /**
  164. * 获取渠道列表
  165. * @param $params []
  166. * channel_id:渠道ID 可选
  167. * channel_name:渠道名称 可选
  168. * search_name: 搜索名称
  169. * start_date:开始时间 可选
  170. * end_date:结束时间 可选
  171. * is_enabled: 是否开通 0:未审核; 1:审核通过
  172. * distribution_manages_id: 管理员 可选
  173. * @param $isAll
  174. * @return mixed
  175. */
  176. public static function getChannelList($params = [], $isAll = '')
  177. {
  178. return Channel::getChannelList($params, $isAll);
  179. }
  180. /**
  181. * 获取当前渠道经理下所有渠道ID
  182. * @param $distribution_manages_id
  183. * @return mixed
  184. */
  185. public static function getChannelIdList($distribution_manages_id = '')
  186. {
  187. return Channel::getChannelIdList($distribution_manages_id);
  188. }
  189. public static function getSourceName($source)
  190. {
  191. if (config('common.tonglianpay') == $source) {
  192. return "通联支付";
  193. } else if (config('common.lianlianpay') == $source) {
  194. return "连连支付";
  195. } else if (config('common.officialpay') == $source) {
  196. return "官方支付";
  197. }
  198. return "";
  199. }
  200. static function getDefault($channel_user_id)
  201. {
  202. return Channel::getDefault($channel_user_id);
  203. }
  204. /**
  205. * 通过渠道ID、用户ID获取渠道
  206. */
  207. static function getUserChannel($distribution_channle_id, $channel_user_id)
  208. {
  209. return Channel::getUserChannel($distribution_channle_id, $channel_user_id);
  210. }
  211. /**
  212. * 获取用户下所有渠道ID
  213. * @param $channel_user_id
  214. */
  215. static function getUserChannelIds($channel_user_id)
  216. {
  217. return Channel::getUserChannelIds($channel_user_id);
  218. }
  219. static function getUserChannelIdsV2($channel_user_id)
  220. {
  221. return Channel::where('channel_user_id', $channel_user_id)->get()->pluck('id')->toArray();
  222. }
  223. /**
  224. * 设置站点别名
  225. */
  226. static function setChannelSiteNickName($distribution_channle_id, $nick_name)
  227. {
  228. return Channel::where('id', $distribution_channle_id)->update(['site_nick_name' => $nick_name]);
  229. }
  230. /**
  231. * 通过DistributionManagesId获取
  232. */
  233. static function getByDistributionManagesId($distribution_manage_id)
  234. {
  235. return Channel::getByDistributionManagesId($distribution_manage_id);
  236. }
  237. /**
  238. * 更新外站开关
  239. * @return mixed
  240. */
  241. static function updateDistributionOuterSiteSwitch($distribution_channel_id,$status)
  242. {
  243. return Channel::updateDistributionOuterSiteSwitch($distribution_channel_id,$status);
  244. }
  245. /**
  246. * 更新老年站点开关
  247. * @return mixed
  248. */
  249. static function updateDistributionOldUserSiteSwitch($distribution_channel_id,$status)
  250. {
  251. return Channel::updateDistributionOldUserSiteSwitch($distribution_channel_id,$status);
  252. }
  253. /**
  254. * 获取渠道信息
  255. * @return mixed
  256. */
  257. static function getDistributionChannel($distribution_channel_id)
  258. {
  259. return Channel::getDistributionChannel($distribution_channel_id);
  260. }
  261. /**
  262. * 获取渠道信息
  263. * @return mixed
  264. */
  265. static function getDistributionChannelSwitchByCategory($distribution_channel_id,$category)
  266. {
  267. return DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id,$category);
  268. }
  269. /**
  270. * 获取渠道信息
  271. * @return mixed
  272. */
  273. static function getDistributionChannelSwitchByCategoryAndCompany($company_id,$category)
  274. {
  275. return DistributionSelfDefineConfig::getDistributionChannelSwitchByCategoryAndCompany($company_id,$category);
  276. }
  277. // 判断渠道的某个分类的 按照公司的权限
  278. static function check_channel_company_priv($distribution_channel_id,$category){
  279. $distribution = self::getDistributionChannel($distribution_channel_id);
  280. $channel_user_id = isset($distribution->channel_user_id)?$distribution->channel_user_id:'';
  281. $channel_user = ChannelUserService::getById($channel_user_id);
  282. $company_id = isset($channel_user->company_id)?$channel_user->company_id:'';
  283. \Log::info('check_channel_company_priv:category:'.$category.' distribution_channel_id:'.$distribution_channel_id.' channel_user_id:'.$channel_user_id.' company_id:'.$company_id);
  284. return self::getDistributionChannelSwitchByCategoryAndCompany($company_id,$category);
  285. }
  286. // 判断渠道的某个分类的 按照登录账号的权限
  287. static function check_channel_account_priv($distribution_channel_id,$category){
  288. $distribution = self::getDistributionChannel($distribution_channel_id);
  289. $channel_user_id = isset($distribution->channel_user_id)?$distribution->channel_user_id:'';
  290. $channel_user = ChannelUserService::getById($channel_user_id);
  291. $account = isset($channel_user->phone)?$channel_user->phone:'';
  292. \Log::info('check_channel_company_priv:category:'.$category.' distribution_channel_id:'.$distribution_channel_id.' channel_user_id:'.$channel_user_id.' account:'.$account);
  293. return self::getDistributionChannelSwitchByCategoryAndAccount($account,$category);
  294. }
  295. static function getDistributionChannelSwitchByCategoryAndAccount($account,$category)
  296. {
  297. return DistributionSelfDefineConfig::getDistributionChannelSwitchByCategoryAndAccount($account,$category);
  298. }
  299. /**
  300. * 根据站点属性,转换对应的回复链接
  301. */
  302. static function convertChannelReplyUrl($is_outer_site,$appid,$openid,$content,$fromwhere='')
  303. {
  304. if(!$is_outer_site) return $content;
  305. $matchs = [];
  306. $from_where_str = '&fromwhere='.$fromwhere;
  307. \Log::Info('convertChannelReplyUrl_before:appid'.$appid.' openid:'.$openid.' is_outer_site:'.$is_outer_site.' fromwhere:'.$fromwhere);
  308. $new_content = $content;
  309. // \Log::Info('$content');\Log::Info($content);
  310. // 图文模式
  311. if(is_array($new_content)){
  312. foreach ($new_content as $key=>$one_content){
  313. if(isset($new_content[$key]['url']) && !empty($new_content[$key]['url'])){
  314. if(strpos($new_content[$key]['url'],'?') > -1){
  315. $new_content[$key]['url'] = $new_content[$key]['url'].'&appid='.$appid.'&openid='.$openid.$from_where_str;
  316. }else{
  317. $new_content[$key]['url'] = $new_content[$key]['url'].'?appid='.$appid.'&openid='.$openid.$from_where_str;
  318. }
  319. }
  320. }
  321. }
  322. // 文字模式
  323. else{
  324. preg_match_all('/href=\"(.*)\"/u', $content, $matchs1);
  325. $matchs1 = isset($matchs1[1])?$matchs1[1]:[];
  326. preg_match_all('/href=\'(.*)\'/u', $content, $matchs2);
  327. $matchs2 = isset($matchs2[1])?$matchs2[1]:[];
  328. $matchs_all = array_merge($matchs1,$matchs2);
  329. if(!empty($matchs_all)){
  330. foreach($matchs_all as $match){
  331. $new_url = $url = $match;
  332. if(strpos($url,'?') > -1){
  333. $new_url = $url.'&appid='.$appid.'&openid='.$openid.$from_where_str;
  334. }else{
  335. $new_url = $url.'?appid='.$appid.'&openid='.$openid.$from_where_str;
  336. }
  337. // 小链接可能多次被替换,所以一定要引号结尾
  338. $new_content = str_replace($url.'"',$new_url.'"',$new_content);
  339. $new_content = str_replace($url."'",$new_url."'",$new_content);
  340. }
  341. }
  342. }
  343. // \Log::Info('convertChannelReplyUrl_after:appid'.$appid.' openid:'.$openid);
  344. // \Log::Info('$new_content');\Log::Info($new_content);
  345. return $new_content;
  346. }
  347. static function getContentByUrl($url)
  348. {
  349. $ch = curl_init();
  350. $timeout = 10; // set to zero for no timeout
  351. curl_setopt($ch, CURLOPT_URL, $url);
  352. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  353. 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');
  354. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  355. $html = curl_exec($ch);
  356. return $html;
  357. }
  358. public static function getChannelCompanyInfo($distribution_channel_id){
  359. return Channel::select(DB::raw("companies.*"))
  360. ->leftjoin('channel_users','channel_users.id','=','distribution_channels.channel_user_id')
  361. ->leftjoin('companies','companies.id','=','channel_users.company_id')
  362. ->where('distribution_channels.id',$distribution_channel_id)
  363. ->first();
  364. }
  365. public static function getChannelCompanySex($distribution_channel_id){
  366. $company = Channel::select(DB::raw("companies.*"))
  367. ->leftjoin('channel_users','channel_users.id','=','distribution_channels.channel_user_id')
  368. ->leftjoin('companies','companies.id','=','channel_users.company_id')
  369. ->where('distribution_channels.id',$distribution_channel_id)
  370. ->first();
  371. // 1男 2女 3混合
  372. $sex = isset($company->fans_gender)?$company->fans_gender:'3';
  373. return $sex;
  374. }
  375. //根据站点id获取公司id 强关页面需要用
  376. public static function getCompanyIdByDistributionchannelid(int $distribution_channel_id){
  377. $company = Channel::select('channel_users.company_id')
  378. ->join('channel_users','channel_users.id','=','distribution_channels.channel_user_id')
  379. ->where('distribution_channels.id',$distribution_channel_id)
  380. ->first();
  381. if($company && $company->company_id){
  382. return $company->company_id;
  383. }
  384. return 0;
  385. }
  386. }