PayConfigService.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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, $field='', $cache=true)
  37. {
  38. $cacheKey = "caches:temp:payConfig:channel_{$channel}".($field? '_'.md5($field):'');
  39. $data = RedisCache::get($cacheKey);
  40. if($data && $cache){
  41. return $data;
  42. }
  43. $field = $field? $field : 'app_id,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. }