<?php
|
|
namespace app\operations\model\app;
|
|
use app\common\model\app\App as AppModel;
|
use think\facade\Cache;
|
use app\common\library\fbpay\FbPay; //by yj
|
|
/**
|
* 应用模型
|
*/
|
class App extends AppModel
|
{
|
/**
|
* 更新应用设置
|
*/
|
public function edit($data)
|
{
|
$this->startTrans();
|
try {
|
// 删除app缓存
|
self::deleteCache();
|
$where['app_id'] = self::$app_id;
|
|
$count = $this->count($where);
|
// 更新小程序设置
|
if ($count > 0) {
|
self::update($data, $where);
|
}
|
if ($count == 0) {
|
$data['app_id'] = self::$app_id;
|
self::create($data);
|
}
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
}
|
|
public function count($where)
|
{
|
return $this->where($where)->count();
|
}
|
|
/**
|
* 删除app缓存
|
*/
|
public static function deleteCache()
|
{
|
return Cache::delete('app_' . self::$app_id);
|
}
|
|
/**
|
* 保存支付设置
|
*/
|
public function editPay($data){
|
$this->startTrans();
|
try {
|
$data['pay_type'] = json_encode($data['pay_type']);
|
if (!empty($data['cert_pem']) && !empty($data['key_pem'])) {
|
// 写入微信支付证书文件
|
$this->writeCertPemFiles($data['cert_pem'], $data['key_pem']);
|
}
|
$this->save($data);
|
|
//如果是付呗接口,检查参数配置 by yj
|
if($data['apitype'] && empty($data['is_use_plus'])) {
|
$app = FbPay::getFbPayApp(self::$app_id, 'wx');
|
$FbPay = new FbPay($app);
|
$res = $FbPay->wxconfig();
|
if($res != true) {
|
$this->error = $res;
|
$this->rollback();
|
return false;
|
}
|
}
|
|
// 更新缓存 by yj 2022.12.23
|
Cache::tag('cache')->set('app_' . self::$app_id, $data);
|
|
$this->commit();
|
return true;
|
} catch (\Exception $e) {
|
$this->error = $e->getMessage();
|
$this->rollback();
|
return false;
|
}
|
}
|
|
/**
|
* 写入cert证书文件
|
*/
|
private function writeCertPemFiles($cert_pem = '', $key_pem = '')
|
{
|
// 证书目录
|
$filePath = root_path() . 'runtime/cert/app/' . self::$app_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;
|
}
|
|
}
|