123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Bear
- * Date: 2019/11/28
- * Time: 上午10:05
- */
- namespace app\main\service;
- use app\common\library\Redis;
- use app\main\constants\CacheConstants;
- use think\Config;
- use think\Request;
- /**
- * Class FufenService
- * @package app\main\service
- */
- class FufenService extends BaseService
- {
- /**
- * @var FufenService
- */
- protected static $self = NULL;
- /**
- * @return FufenService
- */
- public static function instance()
- {
- if (self::$self == NULL) {
- self::$self = new self();
- }
- return self::$self;
- }
- /**
- * @return \app\common\model\FufenGroup
- */
- public function getFufenGroupModel()
- {
- return model('FufenGroup');
- }
- /**
- * 获取支付id
- * @param $user_id
- * @param $channel_id
- * @return \app\main\model\object\ReturnObject
- */
- public function getWxpayId($user_id, $channel_id)
- {
- $cacheWx = CacheConstants::getChannelWxpayIds($channel_id);
- $wxpayList = Redis::instance()->get($cacheWx);
- if ($wxpayList === false) {
- $wxpayList = $this->getFufenGroupModel()->where('find_in_set(' . $channel_id . ', channel_ids)')->where('status', '1')->value('wxpay_ids');
- if (!$wxpayList) {
- $wxpayList = '';
- }
- Redis::instance()->set($cacheWx, $wxpayList);
- }
- if (!$wxpayList) {
- return $this->setData(false)->getReturn();
- } else {
- $wxpayList = explode(',', $wxpayList);
- }
- $cache = CacheConstants::getWxpayFunfen($user_id);
- $wxUsed = Redis::instance()->sMembers($cache);
- $left = array_diff($wxpayList, $wxUsed);
- $wxpayId = false;
- if ($left) {
- $wxpayId = array_pop($left);
- }
- return $this->setData($wxpayId)->getReturn();
- }
- /**
- * 检查域名是否为复粉支付域名
- * @param $host
- * @return \app\main\model\object\ReturnObject
- */
- public function checkWxpayHostIsFufen($host)
- {
- $pay_hosts = OfficialAccountsService::instance()->getWxpayModel()->getInfoByHost($host);
- if ($pay_hosts) {
- return $this->setData((bool)$pay_hosts['fufen'])->getReturn();
- }
- return $this->setData(false)->getReturn();
- }
- /**
- * 获取支付域名
- * @param $user_id
- * @param $channel_id
- * @return \app\main\model\object\ReturnObject
- */
- public function getPayUrl($user_id, $channel_id)
- {
- $wxpayId = $this->getWxpayId($user_id, $channel_id)->data;
- if (!$wxpayId) {
- $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($channel_id);
- $menuophost = $adminConfig['menuophost'] ?? null;
- $menuwxpay_host = $adminConfig['menuwxpay_host'] ?? null;
- if ($menuophost && $menuwxpay_host && $menuophost == getCurrentOphost()) {
- $payHost = $adminConfig['menuwxpay_host'];
- } else {
- $payHost = $adminConfig['wxpay_pay_host'];
- }
- } else {
- $wxpay = OfficialAccountsService::instance()->getWxpayModel()->getInfoById($wxpayId);
- $payHost = $wxpay['pay_host'];
- }
- $payHost = Config::get('site.scheme') . '://' . $payHost;
- return $this->setData($payHost)->getReturn();
- }
- /**
- * 清除支付相关的渠道缓存
- * @param $wxpay_id
- * @return \app\main\model\object\ReturnObject
- */
- public function clearChannelCacheByWxpayId($wxpay_id)
- {
- $fufenGroup = $this->getFufenGroupModel()->where('find_in_set(' . $wxpay_id . ', wxpay_ids)')->find();
- if ($fufenGroup) {
- $channel_ids = explode(',', $fufenGroup['channel_ids']);
- $wxpay_ids = explode(',', $fufenGroup['wxpay_ids']);
- $update_ids = array_diff($wxpay_ids, [$wxpay_id]);
- $update_ids_str = implode(',', $update_ids);
- $this->getFufenGroupModel()->update(['wxpay_ids' => $update_ids_str], ['id' => $fufenGroup['id']]);
- foreach ($channel_ids as $channel_id) {
- Redis::instance()->del(CacheConstants::getChannelWxpayIds($channel_id));
- }
- }
- return $this->setData(true)->getReturn();
- }
- /**
- * 检测测试参数
- * @return \app\main\model\object\ReturnObject
- */
- public function checkTestParams()
- {
- $valid = false;
- if ($t = Request::instance()->get('t')) {
- if (!$test = Config::get('app_test')) {
- $test = 'test';
- }
- if ($t == substr(md5($test), 4, 2)) {
- $valid = true;
- }
- }
- return $this->setData($valid)->getReturn();
- }
- }
|