Wxapp.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace app\common\model;
  3. use app\common\exception\BaseException;
  4. use think\Cache;
  5. use think\Db;
  6. /**
  7. * 微信小程序模型
  8. * Class Wxapp
  9. * @package app\common\model
  10. */
  11. class Wxapp extends BaseModel
  12. {
  13. protected $name = 'wxapp';
  14. /**
  15. * 小程序导航
  16. * @return \think\model\relation\HasOne
  17. */
  18. public function navbar()
  19. {
  20. return $this->hasOne('WxappNavbar');
  21. }
  22. /**
  23. * 小程序页面
  24. * @return \think\model\relation\HasOne
  25. */
  26. public function diyPage()
  27. {
  28. return $this->hasOne('WxappPage');
  29. }
  30. /**
  31. * 获取小程序信息
  32. * @param null $wxapp_id
  33. * @return static|null
  34. * @throws \think\exception\DbException
  35. */
  36. public static function detail($wxapp_id = null)
  37. {
  38. return self::get($wxapp_id ?: []);
  39. }
  40. /**
  41. * 从缓存中获取小程序信息
  42. * @param null $wxapp_id
  43. * @return mixed|null|static
  44. * @throws BaseException
  45. * @throws \think\exception\DbException
  46. */
  47. public static function getWxappCache($wxapp_id = null)
  48. {
  49. if (is_null($wxapp_id)) {
  50. $self = new static();
  51. $wxapp_id = $self::$wxapp_id;
  52. }
  53. if (!$data = Cache::get('wxapp_' . $wxapp_id)) {
  54. $data = self::detail($wxapp_id);
  55. if (empty($data)) throw new BaseException(['msg' => '未找到当前小程序信息']);
  56. Cache::tag('cache')->set('wxapp_' . $wxapp_id, $data);
  57. }
  58. return $data;
  59. }
  60. }