RechargeChannel.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Models;
  3. use App\Services\Payment\PaymentProviderCatalog;
  4. use App\Services\Payment\PaymentProviderService;
  5. use App\Constants\Util;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. class RechargeChannel extends BaseModel
  8. {
  9. use SoftDeletes;
  10. protected $table = 'recharge_channel';
  11. protected $fillable = ['id', 'data_type', 'from', 'key', 'name', 'type', 'rate', 'min', 'max', 'fixed', 'sort', 'status', 'logo'];
  12. protected $hidden = [];
  13. public function getLogoAttribute($value): string
  14. {
  15. return $value ? (string)Util::ensureUrl($value) : '';
  16. }
  17. public function scopeAvailable($query)
  18. {
  19. $query->where('status', 1);
  20. $statuses = PaymentProviderService::statuses();
  21. foreach (PaymentProviderCatalog::codes() as $code) {
  22. if (($statuses[$code] ?? 0) === 1) continue;
  23. foreach ([1, 2] as $direction) {
  24. $types = PaymentProviderCatalog::channelTypesForProvider($code, $direction);
  25. if ($types === []) continue;
  26. $query->whereNot(fn ($part) => $part->where('data_type', $direction)->whereIn('type', $types));
  27. }
  28. }
  29. return $query;
  30. }
  31. public function getFixedAttribute($value)
  32. {
  33. return $value ? explode(',', $value) : null;
  34. }
  35. public static function getChannel($data_type, $type = [])
  36. {
  37. $query = self::where('status', 1)->where('data_type', $data_type);
  38. if ($type) {
  39. $query = $query->whereIn('type', $type);
  40. }
  41. $statuses = PaymentProviderService::statuses();
  42. $channel = $query->orderBy('sort')->orderBy('id')->get(['type', 'name', 'from', 'data_type', 'status'])
  43. ->map(function ($row) use ($statuses) {
  44. $data = $row->toArray() + PaymentProviderCatalog::channelDisplay($row->toArray(), $statuses);
  45. return $data + ['disabled' => $data['company_status'] === 0,
  46. 'disabled_reason' => $data['company_status'] === 0 ? '支付公司已禁用' : ''];
  47. })->all();
  48. $channel = array_column($channel, null, 'type');
  49. return array_values($channel);
  50. }
  51. //获取充值通道
  52. public static function product($from = '')
  53. {
  54. $where['status'] = 1;
  55. $where['data_type'] = 1;
  56. if($from){
  57. $where['from'] = $from;
  58. }
  59. $list = self::where($where)->available()->orderBy('sort', 'asc')->get()->toArray();
  60. return array_column($list, null, 'key');
  61. }
  62. public static function getFormatChannel($data_type, $recharge_channel_group_id = 1)
  63. {
  64. $recharge_channel_group_id = $recharge_channel_group_id > 0 ? $recharge_channel_group_id : 1;
  65. $query = self::where('data_type', $data_type)->available();
  66. $field = 'recharge_type'; //充值类型
  67. if ($data_type == 2) {
  68. //提现类型
  69. $field = 'withdraw_type';
  70. } elseif ($data_type == 3) {
  71. //提现类型
  72. $field = 'activity_type';
  73. }
  74. $group = RechargeChannelGroup::query()->find($recharge_channel_group_id);
  75. $types = $group ? (array)$group->{$field} : [];
  76. if (!$types) {
  77. return [];
  78. }
  79. $query = $query->whereIn('type', $types);
  80. $fields = ['id', 'data_type','name','type', 'min','max','fixed','rate'];
  81. if ((int)$data_type === 1) $fields[] = 'logo';
  82. $product = $query->orderBy('sort', 'asc')->orderBy('id')->select($fields)->get()->toArray();
  83. $list = [];
  84. foreach($product as $key => $pv) {
  85. if (isset($list[$pv['type']]['config'])) {
  86. $config = $list[$pv['type']]['config'];
  87. if (empty($config['range'])) {
  88. $config['range'][] = $config;
  89. }
  90. if ($pv['min'] < $config['min']) {
  91. $config['min'] = $pv['min'];
  92. }
  93. if ($pv['max'] > $config['max']) {
  94. $config['max'] = $pv['max'];
  95. }
  96. if ($pv['rate'] < $config['rate']) {
  97. $config['max_rate'] = $config['rate'];
  98. $config['min_rate'] = $pv['rate'];
  99. }
  100. if ($pv['rate'] > $config['rate']) {
  101. $config['max_rate'] = $pv['rate'];
  102. }
  103. $config['range'][] = $pv;
  104. } else {
  105. $config = $pv;
  106. }
  107. $list[$pv['type']] = [
  108. 'key' => $key,
  109. 'label' => lang($pv['name']),
  110. 'value' => $pv['type'],
  111. 'config' => $config ?? [],
  112. ];
  113. if ((int)$data_type === 1) $list[$pv['type']]['logo'] = (string)($config['logo'] ?? '');
  114. }
  115. $list = array_column($list, null, 'key');
  116. return array_values($list);
  117. }
  118. //校验是否支持此提现方式
  119. public static function checkWithdrawChannel($type, $recharge_channel_group_id = 1) {
  120. $recharge_channel_group_id = $recharge_channel_group_id > 0 ? $recharge_channel_group_id : 1;
  121. $group = RechargeChannelGroup::query()->find($recharge_channel_group_id);
  122. $withdrawTypes = $group ? (array)$group->withdraw_type : [];
  123. if (!in_array($type, $withdrawTypes, true)) {
  124. return false;
  125. }
  126. return self::query()->where('data_type', 2)->where('type', $type)
  127. ->available()->orderBy('sort')->first() ?: false;
  128. }
  129. //校验是否支持此充值方式
  130. public static function checkRechargeChannel($type, $recharge_channel_group_id = 1) {
  131. $recharge_channel_group_id = $recharge_channel_group_id > 0 ? $recharge_channel_group_id : 1;
  132. $group = RechargeChannelGroup::query()->find($recharge_channel_group_id);
  133. $rechargeTypes = $group ? (array)$group->recharge_type : [];
  134. if (!in_array($type, $rechargeTypes, true)) {
  135. return false;
  136. }
  137. return self::query()->where('data_type', 1)->where('type', $type)
  138. ->available()->orderBy('sort')->first() ?: false;
  139. }
  140. }