3 changed files with 138 additions and 0 deletions
@ -0,0 +1,69 @@ |
|||||
|
<?php |
||||
|
declare (strict_types = 1); |
||||
|
|
||||
|
namespace app\controller; |
||||
|
|
||||
|
use app\BaseController; |
||||
|
use app\middleware\CheckAdmin; |
||||
|
use think\exception\ValidateException; |
||||
|
use app\model\Setting as SettingModel; |
||||
|
use think\facade\Request; |
||||
|
|
||||
|
class Setting extends BaseController |
||||
|
{ |
||||
|
|
||||
|
protected $middleware = [CheckAdmin::class]; |
||||
|
|
||||
|
/** |
||||
|
* 返回充值设置 |
||||
|
* @throws \think\db\exception\DataNotFoundException |
||||
|
* @throws \think\db\exception\DbException |
||||
|
* @throws \think\db\exception\ModelNotFoundException |
||||
|
*/ |
||||
|
public function rechargeSetInfo() |
||||
|
{ |
||||
|
$user_recharge_set = SettingModel::settingLoad('user_recharge_set') ?: 0; |
||||
|
$agent_recharge_set = SettingModel::settingLoad('agent_recharge_set') ?: 0; |
||||
|
|
||||
|
return $this->renderSuccess('数据返回成功',[ |
||||
|
[ |
||||
|
'title' => '用户端充值接口', |
||||
|
'type' => 1, |
||||
|
'is_open' => $user_recharge_set |
||||
|
], |
||||
|
[ |
||||
|
'title' => '代理端充值接口', |
||||
|
'type' => 2, |
||||
|
'is_open' => $agent_recharge_set |
||||
|
] |
||||
|
]); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 充值设置接口 |
||||
|
* @return array|void |
||||
|
* @throws \think\db\exception\DataNotFoundException |
||||
|
* @throws \think\db\exception\DbException |
||||
|
* @throws \think\db\exception\ModelNotFoundException |
||||
|
*/ |
||||
|
public function openRecharge() |
||||
|
{ |
||||
|
$param = Request::param(); |
||||
|
|
||||
|
try { |
||||
|
|
||||
|
validate()->rule([ |
||||
|
'is_open|开关状态' => 'require|in:0,1', |
||||
|
'type|开关类型' => 'require|in:1,2' |
||||
|
])->check($param); |
||||
|
|
||||
|
$typeArr = [ 1 => 'user_recharge_set', 2 => 'agent_recharge_set']; |
||||
|
|
||||
|
SettingModel::settingSave($typeArr[$param['type']],$param['is_open']); |
||||
|
|
||||
|
return $this->renderSuccess('设置成功'); |
||||
|
} catch (ValidateException $validateException) { |
||||
|
return $this->renderError($validateException['msg']); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\model; |
||||
|
|
||||
|
use think\facade\Cache; |
||||
|
use think\Model; |
||||
|
|
||||
|
class Setting extends Model |
||||
|
{ |
||||
|
|
||||
|
/** |
||||
|
* 添加设置 |
||||
|
* @param $key |
||||
|
* @param $value |
||||
|
* @return mixed |
||||
|
* @throws \think\db\exception\DataNotFoundException |
||||
|
* @throws \think\db\exception\DbException |
||||
|
* @throws \think\db\exception\ModelNotFoundException |
||||
|
*/ |
||||
|
public static function settingSave($key,$value) |
||||
|
{ |
||||
|
|
||||
|
$Setting = new Setting(); |
||||
|
$edit = $Setting->where('key',$key)->find(); |
||||
|
$value = serialize($value); |
||||
|
if ($edit) { |
||||
|
$edit->value = $value; |
||||
|
$Setting = $edit->save(); |
||||
|
} else { |
||||
|
$Setting = $Setting->save(['key' => $key, 'value' => $value]); |
||||
|
} |
||||
|
|
||||
|
# 添加缓存 |
||||
|
Cache::store('redis')->set($key,$value); |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取缓存 |
||||
|
* @param $key |
||||
|
* @return array|string |
||||
|
* @throws \think\db\exception\DataNotFoundException |
||||
|
* @throws \think\db\exception\DbException |
||||
|
* @throws \think\db\exception\ModelNotFoundException |
||||
|
*/ |
||||
|
public static function settingLoad($key) |
||||
|
{ |
||||
|
# 获取缓存 |
||||
|
$catch = Cache::store('redis')->get($key); |
||||
|
|
||||
|
if (empty($catch)) { |
||||
|
$Setting = new Setting(); |
||||
|
$Setting = $Setting->where('key',$key)->find(); |
||||
|
if (!$Setting) return ''; |
||||
|
$catch = $Setting->value; |
||||
|
# 添加缓存 |
||||
|
Cache::store('redis')->set($key,$catch); |
||||
|
} |
||||
|
|
||||
|
return unserialize($catch); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue