FufenService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/11/28
  6. * Time: 上午10:05
  7. */
  8. namespace app\main\service;
  9. use app\common\library\Redis;
  10. use app\main\constants\CacheConstants;
  11. use think\Config;
  12. use think\Request;
  13. /**
  14. * Class FufenService
  15. * @package app\main\service
  16. */
  17. class FufenService extends BaseService
  18. {
  19. /**
  20. * @var FufenService
  21. */
  22. protected static $self = NULL;
  23. /**
  24. * @return FufenService
  25. */
  26. public static function instance()
  27. {
  28. if (self::$self == NULL) {
  29. self::$self = new self();
  30. }
  31. return self::$self;
  32. }
  33. /**
  34. * @return \app\common\model\FufenGroup
  35. */
  36. public function getFufenGroupModel()
  37. {
  38. return model('FufenGroup');
  39. }
  40. /**
  41. * 获取支付id
  42. * @param $user_id
  43. * @param $channel_id
  44. * @return \app\main\model\object\ReturnObject
  45. */
  46. public function getWxpayId($user_id, $channel_id)
  47. {
  48. $cacheWx = CacheConstants::getChannelWxpayIds($channel_id);
  49. $wxpayList = Redis::instance()->get($cacheWx);
  50. if ($wxpayList === false) {
  51. $wxpayList = $this->getFufenGroupModel()->where('find_in_set(' . $channel_id . ', channel_ids)')->where('status', '1')->value('wxpay_ids');
  52. if (!$wxpayList) {
  53. $wxpayList = '';
  54. }
  55. Redis::instance()->set($cacheWx, $wxpayList);
  56. }
  57. if (!$wxpayList) {
  58. return $this->setData(false)->getReturn();
  59. } else {
  60. $wxpayList = explode(',', $wxpayList);
  61. }
  62. $cache = CacheConstants::getWxpayFunfen($user_id);
  63. $wxUsed = Redis::instance()->sMembers($cache);
  64. $left = array_diff($wxpayList, $wxUsed);
  65. $wxpayId = false;
  66. if ($left) {
  67. $wxpayId = array_pop($left);
  68. }
  69. return $this->setData($wxpayId)->getReturn();
  70. }
  71. /**
  72. * 检查域名是否为复粉支付域名
  73. * @param $host
  74. * @return \app\main\model\object\ReturnObject
  75. */
  76. public function checkWxpayHostIsFufen($host)
  77. {
  78. $pay_hosts = OfficialAccountsService::instance()->getWxpayModel()->getInfoByHost($host);
  79. if ($pay_hosts) {
  80. return $this->setData((bool)$pay_hosts['fufen'])->getReturn();
  81. }
  82. return $this->setData(false)->getReturn();
  83. }
  84. /**
  85. * 获取支付域名
  86. * @param $user_id
  87. * @param $channel_id
  88. * @return \app\main\model\object\ReturnObject
  89. */
  90. public function getPayUrl($user_id, $channel_id)
  91. {
  92. $wxpayId = $this->getWxpayId($user_id, $channel_id)->data;
  93. if (!$wxpayId) {
  94. $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channel_id);
  95. $menuophost = $adminConfig['menuophost'] ?? null;
  96. $menuwxpay_host = $adminConfig['menuwxpay_host'] ?? null;
  97. if ($menuophost && $menuwxpay_host && $menuophost == getCurrentOphost()) {
  98. $payHost = $adminConfig['menuwxpay_host'];
  99. } else {
  100. $payHost = $adminConfig['wxpay_pay_host'];
  101. }
  102. } else {
  103. $wxpay = OfficialAccountsService::instance()->getWxpayModel()->getInfoById($wxpayId);
  104. $payHost = $wxpay['pay_host'];
  105. }
  106. $payHost = Config::get('site.scheme') . '://' . $payHost;
  107. return $this->setData($payHost)->getReturn();
  108. }
  109. /**
  110. * 清除支付相关的渠道缓存
  111. * @param $wxpay_id
  112. * @return \app\main\model\object\ReturnObject
  113. */
  114. public function clearChannelCacheByWxpayId($wxpay_id)
  115. {
  116. $fufenGroup = $this->getFufenGroupModel()->where('find_in_set(' . $wxpay_id . ', wxpay_ids)')->find();
  117. if ($fufenGroup) {
  118. $channel_ids = explode(',', $fufenGroup['channel_ids']);
  119. $wxpay_ids = explode(',', $fufenGroup['wxpay_ids']);
  120. $update_ids = array_diff($wxpay_ids, [$wxpay_id]);
  121. $update_ids_str = implode(',', $update_ids);
  122. $this->getFufenGroupModel()->update(['wxpay_ids' => $update_ids_str], ['id' => $fufenGroup['id']]);
  123. foreach ($channel_ids as $channel_id) {
  124. Redis::instance()->del(CacheConstants::getChannelWxpayIds($channel_id));
  125. }
  126. }
  127. return $this->setData(true)->getReturn();
  128. }
  129. /**
  130. * 检测测试参数
  131. * @return \app\main\model\object\ReturnObject
  132. */
  133. public function checkTestParams()
  134. {
  135. $valid = false;
  136. if ($t = Request::instance()->get('t')) {
  137. if (!$test = Config::get('app_test')) {
  138. $test = 'test';
  139. }
  140. if ($t == substr(md5($test), 4, 2)) {
  141. $valid = true;
  142. }
  143. }
  144. return $this->setData($valid)->getReturn();
  145. }
  146. }