Wxapp.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\model;
  13. use think\facade\Cache;
  14. use app\common\exception\BaseException;
  15. /**
  16. * 微信小程序模型
  17. * Class Wxapp
  18. * @package app\common\model
  19. */
  20. class Wxapp extends BaseModel
  21. {
  22. // 定义表名
  23. protected $name = 'wxapp';
  24. // 定义主键名
  25. protected $pk = 'id';
  26. /**
  27. * 获取小程序信息
  28. * @param null $storeId
  29. * @return array|static|null
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public static function detail($storeId = null)
  35. {
  36. return (new static)->find($storeId);
  37. }
  38. /**
  39. * 从缓存中获取小程序信息
  40. * @param null $storeId
  41. * @return array
  42. * @throws BaseException
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public static function getWxappCache($storeId = null)
  48. {
  49. // 小程序id
  50. is_null($storeId) && $storeId = static::$storeId;
  51. if (!$data = Cache::get("wxapp_{$storeId}")) {
  52. // 获取小程序详情, 解除hidden属性
  53. $detail = self::detail($storeId);
  54. empty($detail) && throwError('未找到当前小程序信息');
  55. // 写入缓存
  56. $data = $detail->hidden([])->toArray();
  57. Cache::tag('cache')->set("wxapp_{$storeId}", $data);
  58. }
  59. return $data;
  60. }
  61. }