78 lines
1.9 KiB
PHP
78 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace app\store\model;
|
|
|
|
use think\Cache;
|
|
use app\common\model\Wxapp as WxappModel;
|
|
|
|
/**
|
|
* 微信小程序模型
|
|
* Class Wxapp
|
|
* @package app\store\model
|
|
*/
|
|
class Wxapp extends WxappModel
|
|
{
|
|
/**
|
|
* 更新小程序设置
|
|
* @param $data
|
|
* @return bool
|
|
* @throws \think\exception\PDOException
|
|
*/
|
|
public function edit($data)
|
|
{
|
|
$this->startTrans();
|
|
try {
|
|
// 删除wxapp缓存
|
|
self::deleteCache();
|
|
// 写入微信支付证书文件
|
|
$this->writeCertPemFiles($data['cert_pem'], $data['key_pem']);
|
|
// 更新小程序设置
|
|
$this->allowField(true)->save($data);
|
|
$this->commit();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
$this->error = $e->getMessage();
|
|
$this->rollback();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 写入cert证书文件
|
|
* @param string $cert_pem
|
|
* @param string $key_pem
|
|
* @return bool
|
|
*/
|
|
private function writeCertPemFiles($cert_pem = '', $key_pem = '')
|
|
{
|
|
if (empty($cert_pem) || empty($key_pem)) {
|
|
return false;
|
|
}
|
|
// 证书目录
|
|
$filePath = APP_PATH . 'common/library/wechat/cert/' . self::$wxapp_id . '/';
|
|
// 目录不存在则自动创建
|
|
if (!is_dir($filePath)) {
|
|
mkdir($filePath, 0755, true);
|
|
}
|
|
// 写入cert.pem文件
|
|
if (!empty($cert_pem)) {
|
|
file_put_contents($filePath . 'cert.pem', $cert_pem);
|
|
}
|
|
// 写入key.pem文件
|
|
if (!empty($key_pem)) {
|
|
file_put_contents($filePath . 'key.pem', $key_pem);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除wxapp缓存
|
|
* @return bool
|
|
*/
|
|
public static function deleteCache()
|
|
{
|
|
return Cache::rm('wxapp_' . self::$wxapp_id);
|
|
}
|
|
|
|
}
|