Wxapp.php 1.5 KB

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