Files
amb_wechatapp/application/admin/controller/config/WxMiniProgram.php
T

96 lines
2.5 KiB
PHP

<?php
namespace app\admin\controller\config;
use app\common\controller\Backend;
use think\Cache;
/**
* 微信小程序配置管理
*/
class WxMiniProgram extends Backend
{
protected $model = null;
protected $configName = 'wxMiniProgram';
public function _initialize()
{
parent::_initialize();
\app\common\model\fill\Handle::check('config_system', $this->configName);
}
/**
* 获取配置
* @return mixed
*/
public function getConfig()
{
$data = \app\common\model\config\System::getConfig($this->configName);
return $this->success("获取成功", $data);
}
/**
* 刷新Token
* @return mixed
*/
public function refreshToken()
{
$token = $this->generateRandomString(32);
$this->saveConfig('msg_push_token', $token);
return $this->success("刷新成功", ['msg_push_token' => $token]);
}
/**
* 刷新EncodingAESKey
* @return mixed
*/
public function refreshAESKey()
{
$aesKey = $this->generateRandomString(43);
$this->saveConfig('msg_push_aes_key', $aesKey);
return $this->success("刷新成功", ['msg_push_aes_key' => $aesKey]);
}
/**
* 保存配置字段
* @param string $fieldName 字段名
* @param string $value 值
*/
private function saveConfig($fieldName, $value)
{
$config = \app\common\model\config\System::load($this->configName);
if (!$config) {
$this->error("配置不存在");
}
foreach ($config as &$item) {
if ($item['name'] === $fieldName) {
$item['value'] = $value;
break;
}
}
$model = new \app\admin\model\config\System();
$row = $model->where(['name' => $this->configName])->find();
if ($row) {
$row->value = json_encode(array_values($config), JSON_UNESCAPED_UNICODE);
$row->save();
Cache::rm(\app\common\constant\cache\Keys::SYSTEM_CONFIG($this->configName));
}
}
/**
* 生成随机字符串
* @param int $length 长度
* @return string
*/
private function generateRandomString($length)
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$result = '';
for ($i = 0; $i < $length; $i++) {
$result .= $chars[random_int(0, strlen($chars) - 1)];
}
return $result;
}
}