Wxapp.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. use think\facade\Log;
  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 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($storeId = null)
  36. {
  37. return (new static)->find($storeId);
  38. }
  39. /**
  40. * 从缓存中获取小程序信息
  41. * @param null $storeId
  42. * @return array
  43. * @throws BaseException
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public static function getWxappCache($storeId = null)
  49. {
  50. // 小程序id
  51. is_null($storeId) && $storeId = static::$storeId;
  52. Log::error("storeId:".$storeId);
  53. if(is_null($storeId)){
  54. $storeId = 10001;
  55. }
  56. if (!$data = Cache::get("wxapp_{$storeId}")) {
  57. // 获取小程序详情, 解除hidden属性
  58. $detail = self::detail($storeId);
  59. empty($detail) && throwError('未找到当前小程序信息');
  60. // 写入缓存
  61. $data = $detail->hidden([])->toArray();
  62. Cache::tag('cache')->set("wxapp_{$storeId}", $data);
  63. }
  64. Log::error("data:".json_encode($data));
  65. return $data;
  66. }
  67. }