jh-admin/app/model/system/UniAccount.php

104 lines
3.4 KiB
PHP

<?php
namespace app\model\system;
use think\facade\Cache;
use think\model;
class UniAccount extends model
{
/***
* 获取应用配置
* @param $condition
* @param $config_key
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getUniConfig($condition, $config_key)
{
$site_id = isset($condition['site_id']) ? $condition['site_id'] : '';
$appid = isset($condition['appid']) ? $condition['appid'] : '';
if (!$site_id && !$appid) {
return error(-1, 'REQUEST_SITE_ID or REQUEST_APPID_ID');
}
if (empty($config_key)) {
return error(-1, 'REQUEST_CONFIG_KEY');
}
$json_condition = json_encode($condition);
$cache = Cache::get("UNICONFIG_" . $json_condition, "");
if (!empty($cache)) {
Cache::tag("uniConfig")->clear();
return success(0, '', $cache);
}
$info = $this->where($condition)
->json(['value'], true)->order('update_time', 'desc')->find();
if (empty($info)) {
$info = [
'site_id' => '',
'config_key' => $config_key,
'platform_id' => '',
'borrow_id' => '',
'appid' => '',
'original' => '',
'value' => [],
'create_time' => 0,
];
} else {
$info = $info->toArray();
Cache::tag("uniConfig")->set("UNICONFIG_" . $json_condition, $info);
}
return success(0, '成功', $info);
}
/***
* 设置平台参数
* @param $condition
* @param $value
* @param int $original
* @param $app_type
* @param $config_key
* @param int $platform_id
* @param int $borrow_id
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function setUniConfig($condition, $value, $app_type, $config_key, $original = '', $platform_id = 0, $borrow_id = 0)
{
$site_id = isset($condition['site_id']) ? $condition['site_id'] : '';
if ($site_id === '') {
return error(-1, 'REQUEST_SITE_ID');
}
$appid = isset($condition['appid']) ? $condition['appid'] : '';
if (empty($appid)) {
return error(-1, 'REQUEST_APP_ID');
}
if (isset($condition['id']) && is_numeric($condition['id'])) {
$condition = [
'id' => $condition['id']
];
}
$condition['app_type'] = $app_type;
$data['site_id'] = $site_id;
$data['appid'] = $appid;
$data['app_type'] = $app_type;
$data['original'] = $original;
$data['platform_id'] = $platform_id;
$data['borrow_id'] = $borrow_id;
$data['config_key'] = $config_key;
$data['value'] = json_encode($value);
$data['update_time'] = time();
$info = $this->where($condition)->field('id,site_id')->find();
if (empty($info)) {
$res = $this->create($data);
} else if ($info) {
$res = $info->force(true)->save($data);
}
Cache::tag("uniConfig")->clear();
return success(0, '成功', $res);
}
}