Wxapp.php 1.6 KB

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