179 lines
5.4 KiB
PHP
179 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\wxapp;
|
|
|
|
use app\common\controller\Backend;
|
|
use think\Cache;
|
|
|
|
/**
|
|
* 微信小程序消息推送配置管理
|
|
*/
|
|
class MessagePush extends Backend
|
|
{
|
|
protected $model = null;
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
\app\common\model\fill\Handle::check('config_system', 'wxMiniProgram');
|
|
\app\common\model\fill\Handle::check('config_system', 'wxMiniProgramMsgPush');
|
|
}
|
|
|
|
/**
|
|
* 获取消息推送配置
|
|
* @return mixed
|
|
*/
|
|
public function getConfig()
|
|
{
|
|
$wxConfig = \app\common\model\config\System::getConfig('wxMiniProgram') ?: [];
|
|
$msgPushConfig = \app\common\model\config\System::getConfig('wxMiniProgramMsgPush') ?: [];
|
|
|
|
$msgPushToken = isset($msgPushConfig['msg_push_token']) ? $msgPushConfig['msg_push_token'] : '';
|
|
$msgPushAesKey = isset($msgPushConfig['msg_push_aes_key']) ? $msgPushConfig['msg_push_aes_key'] : '';
|
|
|
|
if (empty($msgPushToken)) {
|
|
$msgPushToken = $this->generateRandomString(32);
|
|
$this->saveConfigField('msg_push_token', $msgPushToken);
|
|
}
|
|
|
|
if (empty($msgPushAesKey)) {
|
|
$msgPushAesKey = $this->generateRandomString(43);
|
|
$this->saveConfigField('msg_push_aes_key', $msgPushAesKey);
|
|
}
|
|
|
|
$data = [
|
|
'msg_push_url' => $this->getMsgPushUrl(),
|
|
'msg_push_token' => $msgPushToken,
|
|
'msg_push_aes_key' => $msgPushAesKey,
|
|
];
|
|
|
|
return $this->success("获取成功", $data);
|
|
}
|
|
|
|
/**
|
|
* 修复配置(用默认结构重建,保留已有字段的值)
|
|
* 当数据库中的配置字段损坏时使用
|
|
* @return mixed
|
|
*/
|
|
public function repairConfig()
|
|
{
|
|
$configName = 'wxMiniProgramMsgPush';
|
|
$default = \app\common\model\fill\Handle::getSystemDefaultConfig($configName);
|
|
if (!$default) {
|
|
return $this->error('默认配置不存在');
|
|
}
|
|
|
|
$model = new \app\admin\model\config\System();
|
|
$dbRow = $model->where(['name' => $configName])->find();
|
|
if (!$dbRow) {
|
|
return $this->error('配置记录不存在');
|
|
}
|
|
|
|
$existConfig = json_decode($dbRow->value, true) ?: [];
|
|
$existValues = [];
|
|
foreach ($existConfig as $item) {
|
|
if (isset($item['name']) && isset($item['value'])) {
|
|
$existValues[$item['name']] = $item['value'];
|
|
}
|
|
}
|
|
|
|
foreach ($default as &$item) {
|
|
if (isset($existValues[$item['name']])) {
|
|
$item['value'] = $existValues[$item['name']];
|
|
}
|
|
}
|
|
|
|
$dbRow->value = json_encode(array_values($default), JSON_UNESCAPED_UNICODE);
|
|
$dbRow->save();
|
|
Cache::rm(\app\common\constant\cache\Keys::SYSTEM_CONFIG($configName));
|
|
|
|
return $this->success('修复成功');
|
|
}
|
|
|
|
/**
|
|
* 获取消息推送回调地址
|
|
* @return string
|
|
*/
|
|
private function getMsgPushUrl()
|
|
{
|
|
global $_W;
|
|
if (\app\common\library\Platform::getSystemType() == 'single') {
|
|
return $_W['siteroot'] . '/index.php?i=' . $_W['uniacid'] . '&route=callback/app.wxapp/notify';
|
|
} else {
|
|
return $_W['siteroot'] . '/app/index.php?i=' . $_W['uniacid'] . '&c=entry&m=tuzi_v1&do=index&route=callback/app.wxapp/notify';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 刷新Token
|
|
* @return mixed
|
|
*/
|
|
public function refreshToken()
|
|
{
|
|
$token = $this->generateRandomString(32);
|
|
$this->saveConfigField('msg_push_token', $token);
|
|
return $this->success("刷新成功", ['msg_push_token' => $token]);
|
|
}
|
|
|
|
/**
|
|
* 刷新EncodingAESKey
|
|
* @return mixed
|
|
*/
|
|
public function refreshAESKey()
|
|
{
|
|
$aesKey = $this->generateRandomString(43);
|
|
$this->saveConfigField('msg_push_aes_key', $aesKey);
|
|
return $this->success("刷新成功", ['msg_push_aes_key' => $aesKey]);
|
|
}
|
|
|
|
/**
|
|
* 保存配置字段值
|
|
* @param string $fieldName 字段名
|
|
* @param string $value 值
|
|
*/
|
|
private function saveConfigField($fieldName, $value)
|
|
{
|
|
$configName = 'wxMiniProgramMsgPush';
|
|
$config = \app\common\model\config\System::load($configName);
|
|
if (!$config) {
|
|
$this->error("配置不存在");
|
|
}
|
|
|
|
$found = false;
|
|
foreach ($config as &$item) {
|
|
if (isset($item['name']) && $item['name'] === $fieldName) {
|
|
$item['value'] = $value;
|
|
$found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$found) {
|
|
$this->error("字段不存在");
|
|
}
|
|
|
|
$model = new \app\admin\model\config\System();
|
|
$row = $model->where(['name' => $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($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;
|
|
}
|
|
}
|