Wxapp.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 cores\BaseModel;
  14. use think\facade\Cache;
  15. use app\common\exception\BaseException;
  16. /**
  17. * 微信小程序模型
  18. * Class Wxapp
  19. * @package app\common\model
  20. */
  21. class Wxapp extends BaseModel
  22. {
  23. // 定义表名
  24. protected $name = 'wxapp';
  25. // 定义主键名
  26. protected $pk = 'id';
  27. /**
  28. * 获取小程序信息
  29. * @param int|null $storeId
  30. * @return array|static|null
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public static function detail(?int $storeId = null)
  36. {
  37. $storeId = $storeId ?: static::$storeId;
  38. return (new static)->where(['store_id' => $storeId])->find();
  39. }
  40. /**
  41. * 从缓存中获取小程序信息
  42. * @param int|null $storeId
  43. * @return array
  44. * @throws BaseException
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public static function getWxappCache(?int $storeId = null)
  50. {
  51. // 小程序id
  52. is_null($storeId) && $storeId = static::$storeId;
  53. if (!$data = Cache::get("wxapp_{$storeId}")) {
  54. // 获取小程序详情, 解除hidden属性
  55. $detail = self::detail($storeId);
  56. empty($detail) && throwError('未找到当前小程序信息');
  57. // 写入缓存
  58. $data = $detail->hidden([])->toArray();
  59. Cache::tag('cache')->set("wxapp_{$storeId}", $data);
  60. }
  61. return $data;
  62. }
  63. }