DataApiConfig.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Redis;
  4. use app\main\service\GdtService;
  5. use think\Log;
  6. use think\Model;
  7. class DataApiConfig extends Model
  8. {
  9. // 表名
  10. protected $table = 'data_api_config';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = 'int';
  13. // 定义时间戳字段名
  14. protected $createTime = 'createtime';
  15. protected $updateTime = 'updatetime';
  16. // 追加属性
  17. protected $append = [
  18. 'is_all_text',
  19. 'status_text'
  20. ];
  21. public function getIsAllList()
  22. {
  23. return ['0' => '否','1' => '是'];
  24. }
  25. public function getStatusList()
  26. {
  27. return ['0' => __('Status 0'),'1' => __('Status 1')];
  28. }
  29. public function getIsAllTextAttr($value, $data)
  30. {
  31. $value = $value ? $value : $data['is_all'];
  32. $list = $this->getIsAllList();
  33. return isset($list[$value]) ? $list[$value] : '';
  34. }
  35. public function getStatusTextAttr($value, $data)
  36. {
  37. $value = $value ? $value : $data['status'];
  38. $list = $this->getStatusList();
  39. return isset($list[$value]) ? $list[$value] : '';
  40. }
  41. /**
  42. * 拉取API配置
  43. * @param $client_id
  44. * @param bool $fromDb
  45. * @return array|mixed
  46. * @throws \think\Exception
  47. */
  48. public function getInfo($client_id, $fromDb = false)
  49. {
  50. $redisKey = "DATAAPI:".$client_id;
  51. if ($fromDb == false) {
  52. $data = Redis::instance()->get($redisKey);
  53. if ($data) {
  54. return json_decode($data, true);
  55. } else {
  56. $fromDb = true;
  57. }
  58. }
  59. if ($fromDb) {
  60. $row = $this->where('client_id', 'eq', $client_id)->find();
  61. if ($row) {
  62. $data = $row->toArray();
  63. } else {
  64. $data = [];
  65. }
  66. Redis::instance()->set($redisKey, json_encode($data), 3600);
  67. return $data;
  68. }
  69. return [];
  70. }
  71. }