YsBankService.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\YsBankListModel;
  4. use utils\RedisCache;
  5. /**
  6. * 银盛银行卡服务 by wes
  7. * Class YsBankService
  8. * @package app\common\service
  9. */
  10. class YsBankService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new YsBankListModel();
  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 $params 查询参数
  32. * @param int $pageSize
  33. * @param string $field
  34. * @param string $cache
  35. * @return array|mixed
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getList($params, $pageSize = 10, $field = '', $cache=true)
  41. {
  42. $page = request()->post('page', 1);
  43. $cacheKey = "caches:temp:ysBankList:{$page}_{$pageSize}_" . md5(json_encode($params));
  44. $data = RedisCache::get($cacheKey);
  45. if ($data && $cache) {
  46. return $data;
  47. }
  48. $field = $field ? $field : 'id,bank_no,bank_name,bank_sub,bank_type,bank_value,status';
  49. $data = $this->model->where(['status' => 1])->where(function ($query) use ($params) {
  50. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  51. if ($keyword) {
  52. $query->where('bank_name|bank_sub', 'like', "%{$keyword}%");
  53. }
  54. })
  55. ->field($field)
  56. ->order('id asc')
  57. ->page($page, $pageSize)->select();
  58. $data = $data? $data->toArray() : [];
  59. if($data && $cache){
  60. RedisCache::set($cacheKey, $data, 3600);
  61. }
  62. return $data;
  63. }
  64. }