This repository has been archived on 2024-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
yoshop/source/application/common/model/Wxapp.php

61 lines
1.5 KiB
PHP
Raw Normal View History

2020-04-25 22:20:29 +08:00
<?php
namespace app\common\model;
use think\Cache;
2020-04-25 22:34:57 +08:00
use app\common\exception\BaseException;
2020-04-25 22:20:29 +08:00
/**
* 微信小程序模型
* Class Wxapp
* @package app\common\model
*/
class Wxapp extends BaseModel
{
protected $name = 'wxapp';
/**
* 小程序页面
* @return \think\model\relation\HasOne
*/
public function diyPage()
{
return $this->hasOne('WxappPage');
}
/**
* 获取小程序信息
2020-04-25 22:53:20 +08:00
* @param int|null $wxappId
2020-04-25 22:20:29 +08:00
* @return static|null
* @throws \think\exception\DbException
*/
2020-04-25 22:53:20 +08:00
public static function detail($wxappId = null)
2020-04-25 22:20:29 +08:00
{
2020-04-25 22:53:20 +08:00
return static::get($wxappId ?: []);
2020-04-25 22:20:29 +08:00
}
/**
* 从缓存中获取小程序信息
2020-04-25 22:48:24 +08:00
* @param int|null $wxappId 小程序id
* @return array $data
2020-04-25 22:20:29 +08:00
* @throws BaseException
2020-04-25 22:48:24 +08:00
* @throws \think\Exception
2020-04-25 22:20:29 +08:00
* @throws \think\exception\DbException
*/
2020-04-25 22:48:24 +08:00
public static function getWxappCache($wxappId = null)
2020-04-25 22:20:29 +08:00
{
2020-04-25 22:48:24 +08:00
// 小程序id
is_null($wxappId) && $wxappId = static::$wxapp_id;
if (!$data = Cache::get("wxapp_{$wxappId}")) {
// 获取小程序详情, 解除hidden属性
$detail = self::detail($wxappId)->hidden([], true);
if (empty($detail)) throw new BaseException(['msg' => '未找到当前小程序信息']);
// 写入缓存
$data = $detail->toArray();
Cache::tag('cache')->set("wxapp_{$wxappId}", $data);
2020-04-25 22:20:29 +08:00
}
return $data;
}
}