PayConfigService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\PayConfigModel;
  4. use utils\RedisCache;
  5. /**
  6. * 支付配置服务 by wes
  7. * Class PayConfigService
  8. * @package app\common\service
  9. */
  10. class PayConfigService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new PayConfigModel();
  17. }
  18. /**
  19. * 静态化入口
  20. * @return static|null
  21. */
  22. public static function make()
  23. {
  24. if(!self::$instance){
  25. self::$instance = new static();
  26. }
  27. return self::$instance;
  28. }
  29. /**
  30. * 按渠道获取支付配置参数
  31. * @param $channel 渠道
  32. * @param string $field
  33. * @param bool $cache
  34. * @return array|mixed
  35. */
  36. public function getInfoByChannel($channel, $scene=1, $paySource=1, $cache=true)
  37. {
  38. $cacheKey = "caches:temp:payConfig:info:c_{$channel}_{$scene}_{$paySource}";
  39. $data = RedisCache::get($cacheKey);
  40. if($data && $cache){
  41. return $data;
  42. }
  43. $field = 'app_id,title,pay_code,sign_key,scene,pay_source,check_password,api_url,private_key,public_key,app_cert_path,pay_root_cert,pay_cert_path,mchid';
  44. $data = $this->model->where(['channel'=> $channel,'status'=>1])->field($field)->findOrEmpty();
  45. $data = $data? $data->toArray() : [];
  46. if($data && $cache){
  47. RedisCache::set($cacheKey, $data, 7200);
  48. }
  49. return $data;
  50. }
  51. /**
  52. * 获取开启的支付渠道配置
  53. * @param int $scene 支付场景:1-所有,4-商城,6-服务商
  54. * @param int $paySource 支付来源:1-所有,2-安卓,3-苹果
  55. * @param bool $cache
  56. * @return array|mixed
  57. */
  58. public function getChannelList($scene=1, $paySource=1, $cache=true)
  59. {
  60. $cacheKey = "caches:temp:payConfig:channelList:s_{$scene}_{$paySource}";
  61. $data = RedisCache::get($cacheKey);
  62. if($data && $cache){
  63. return $data;
  64. }
  65. $where = ['status'=>1];
  66. if($scene>0){
  67. $where['scene'] = $scene;
  68. }
  69. if($paySource>0){
  70. $where['pay_source'] = $paySource;
  71. }
  72. $field = 'app_id,title,pay_code,sign_key,scene,pay_source,check_password,api_url,private_key,icon,public_key,app_cert_path,pay_root_cert,pay_cert_path,mchid';
  73. $data = $this->model->where($where)->withAttr('icon',function($value){
  74. return getWebUrl().$value;
  75. })->order('sort asc,id asc')->column($field,'channel');
  76. if($data && $cache){
  77. RedisCache::set($cacheKey, $data, 7200);
  78. }
  79. return $data;
  80. }
  81. }