Component.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Models\OpenPlat;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 授权方信息以及刷新令牌
  6. *
  7. */
  8. class Component extends Model
  9. {
  10. // 与模型关联的表名
  11. protected $table = 'openplat_component';
  12. // 是否主动维护时间戳
  13. public $timestamps = false;
  14. // 定义时间戳字段名
  15. // const CREATED_AT = 'insert_time';
  16. // const UPDATED_AT = 'update_time';
  17. public function _inintlaze()
  18. {
  19. parent::_inintlaze();
  20. }
  21. /**
  22. * 添加数据
  23. *
  24. */
  25. public function add($data)
  26. {
  27. // 时间
  28. $data['insert_time'] = time();
  29. $data['update_time'] = time();
  30. // 写入数据表
  31. $id = $this->query()->insertGetId($data);
  32. // 如果操作失败
  33. if (!$id) return 0;
  34. // 更新缓存
  35. $this->getList(true);
  36. // 返回结果
  37. return $id;
  38. }
  39. /**
  40. * 添加数据
  41. *
  42. */
  43. public function edit($id, $data)
  44. {
  45. // 更新时间
  46. $data['update_time'] = time();
  47. // 写入数据表
  48. $result = $this->query()->where(['id' => $id])->update($data);
  49. // 如果操作失败
  50. if (!$result) return 0;
  51. // 更新缓存
  52. $this->getList(true);
  53. // 返回结果
  54. return $id;
  55. }
  56. /**
  57. * 获取列表
  58. * @param Bool $force 是否强制更新
  59. *
  60. */
  61. public function getList($force = false)
  62. {
  63. // 结果数据
  64. $list = $force ? [] : cache('manager:openplat:component:list');
  65. // 不存在数据
  66. if (!$list) {
  67. // 从数据库获取数据
  68. $data = $this->query()->where([['status', '=', 0]])->get(['authorizer_appid as appid', 'authorizer_refresh_token as refresh_token', 'company_id', 'func_scope', 'status', 'insert_time', 'update_time'])->toArray();
  69. // 循环处理数据
  70. $list = [];
  71. // 进行更新
  72. foreach ($data as $value) {
  73. // 重组数据
  74. $list[$value['appid']] = $value;
  75. }
  76. // 存起来
  77. cache(['manager:openplat:component:list' => $list]);
  78. }
  79. // 返回结果
  80. return $list;
  81. }
  82. /**
  83. * 获取配置平台对应的应用数据
  84. *
  85. * @param int $appid appid
  86. * @param string $field 指定字段
  87. *
  88. */
  89. public function getOne($appid, $field = '')
  90. {
  91. // 获取列表数据
  92. $list = $this->getList();
  93. // 获取数据
  94. $one = isset($list[$appid]) ? $list[$appid] : [];
  95. // 返回值
  96. return empty($field) ? $one : (isset($one[$field]) ? $one[$field] : null);
  97. }
  98. }