Initial commit: 投注游戏平台初始化

This commit is contained in:
li
2026-02-25 01:26:58 +08:00
commit 77ca2cc8b3
275 changed files with 237479 additions and 0 deletions
+407
View File
@@ -0,0 +1,407 @@
<?php
namespace Plugins\WxGCode\Controllers\Admin;
use App\Core\PluginBaseController;
class WxGCodeController extends PluginBaseController {
protected $pluginManager;
protected $db;
public function __construct() {
global $pluginManager;
$this->pluginManager = $pluginManager;
$this->db = $this->pluginManager->getDB();
}
public function index() {
$this->checkLogin(); // 登录保护
// 获取配置信息(带默认值,避免空值)
$settings = $this->db->get('wxgcode_settings', '*') ?: []; // 如果没有数据,返回空数组
// 传数据给视图(统一封装在data中)
$this->renderPluginView('WxGCode', 'Admin/index.php', [
'data' => [
'settings' => '', // 配置信息
'domain' => '' // 带协议的完整域名
],
'title' => '扫一扫管理中心'
]);
}
public function list() {
$this->checkLogin(); // 登录保护
// 获取分页参数
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$pageSize = isset($_GET['page_size']) ? max(1, min(100, intval($_GET['page_size']))) : 10;
$offset = ($page - 1) * $pageSize;
// 1. 获取总记录数
$totalItems = $this->db->count('wxgcode_list', '*');
// 2. 获取当前页数据
$shortlinks = $this->db->select('wxgcode_list', '*', [
'ORDER' => ['id' => 'DESC'],
'LIMIT' => [$offset, $pageSize]
]);
// 3. 计算分页信息
$totalPages = max(1, ceil($totalItems / $pageSize));
// 4. 返回JSON格式数据
header('Content-Type: application/json; charset=utf-8');
echo json_encode([
'success' => true,
'data' => [
'items' => $shortlinks,
'current_page' => $page,
'total_pages' => $totalPages,
'total_items' => $totalItems,
'page_size' => $pageSize,
'has_prev' => $page > 1,
'has_next' => $page < $totalPages
]
], JSON_UNESCAPED_UNICODE);
exit;
}
public function get($id) {
$this->checkLogin();
// 设置响应内容类型为JSON
header('Content-Type: application/json');
$row = $this->db->get('wxgcode_list', '*', ['id' => $id]);
if ($row) {
// 成功响应,包含状态和数据
echo json_encode([
'success' => true,
'data' => $row
]);
} else {
echo json_encode([
'success' => false,
'message' => '无效的ID或记录不存在'
]);
}
exit;
}
public function update() {
// 设置JSON响应头
header('Content-Type: application/json');
$this->checkLogin();
// 获取表单数据(使用前端页面对应的字段名)
$id = intval($_POST['id'] ?? 0);
$name = trim($_POST['name'] ?? '');
$wx_group_name = trim($_POST['wx_group_name'] ?? '');
$qrcode_url = trim($_POST['qrcode_url'] ?? '');
$code = trim($_POST['code'] ?? '');
$max_scans = intval($_POST['max_scans'] ?? 0);
$max_members = intval($_POST['max_members'] ?? 0);
$description = trim($_POST['description'] ?? '');
$status = isset($_POST['status']) ? 1 : 0;
try {
// 数据验证
if (empty($name)) {
http_response_code(400);
echo json_encode([
'success' => false,
'message' => '请输入活码名称'
]);
exit;
}
if (empty($wx_group_name)) {
http_response_code(400);
echo json_encode([
'success' => false,
'message' => '请输入微信群名称'
]);
exit;
}
if (empty($qrcode_url)) {
http_response_code(400);
echo json_encode([
'success' => false,
'message' => '请输入群二维码URL'
]);
exit;
}
if (empty($code)) {
http_response_code(400);
echo json_encode([
'success' => false,
'message' => '活码编码不能为空'
]);
exit;
}
// 有ID则更新
if ($id > 0) {
$data = [
'name' => $name,
'wx_group_name' => $wx_group_name,
'qrcode_url' => $qrcode_url,
'code' => $code,
'max_scans' => $max_scans,
'max_members' => $max_members,
'description' => $description,
'status' => $status,
'updated_at' => date('Y-m-d H:i:s')
];
$result = $this->db->update('wxgcode_list', $data, ['id' => $id]);
if ($result) {
$qrcode = $this->db->get('wxgcode_list', '*', ['id' => $id]);
echo json_encode([
'success' => true,
'message' => '更新成功',
'data' => $qrcode
]);
} else {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => '更新失败,请稍后重试'
]);
}
}
// 无ID则新增
else {
// 检查编码是否已存在
$exists = $this->db->has('wxgcode_list', ['code' => $code]);
if ($exists) {
http_response_code(400);
echo json_encode([
'success' => false,
'message' => '活码编码已存在,请更换'
]);
exit;
}
$insertId = $this->db->insert('wxgcode_list', [
'name' => $name,
'wx_group_name' => $wx_group_name,
'qrcode_url' => $qrcode_url,
'code' => $code,
'max_scans' => $max_scans,
'max_members' => $max_members,
'description' => $description,
'status' => $status,
'total_views' => 0, // 对应前端显示的访问量
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
if ($insertId) {
echo json_encode([
'success' => true,
'message' => '创建成功',
'data' => [
'id' => $insertId,
'name' => $name,
'wx_group_name' => $wx_group_name,
'qrcode_url' => $qrcode_url,
'code' => $code,
'max_scans' => $max_scans,
'max_members' => $max_members,
'description' => $description,
'status' => $status,
'total_views' => 0
]
]);
} else {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => '创建失败,请稍后重试'
]);
}
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => '操作失败: ' . $e->getMessage()
]);
}
exit;
}
public function settings() {
$this->checkLogin();
header('Content-Type: application/json');
$id = intval($_REQUEST['id'] ?? 0);
try {
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// 获取第一条设置记录
$row = $this->db->get('wxgcode_settings', '*');
if ($row) {
echo json_encode([
'success' => true,
'data' => $row
]);
} else {
echo json_encode([
'success' => false,
'message' => '设置不存在'
]);
}
} else if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 获取提交数据
$wechat_name = trim($_POST['wechat_name'] ?? '');
$wechat_account = trim($_POST['wechat_account'] ?? '');
$appid = trim($_POST['appid'] ?? '');
$appsecret = trim($_POST['appsecret'] ?? '');
$token = trim($_POST['token'] ?? '');
$encoding_aes_key = trim($_POST['encoding_aes_key'] ?? '');
$qrcode_url = trim($_POST['qrcode_url'] ?? '');
$wechat_type = trim($_POST['wechat_type'] ?? 'service');
$status = isset($_POST['status']) ? 1 : 0;
// 验证必填项
if (empty($wechat_name) || empty($wechat_account) || empty($appid) || empty($appsecret)) {
echo json_encode([
'success' => false,
'message' => '公众号名称、原始ID、AppID和AppSecret为必填项'
]);
exit;
}
if ($id > 0) {
// 更新
$data = [
'wechat_name' => $wechat_name,
'wechat_account' => $wechat_account,
'appid' => $appid,
'appsecret' => $appsecret,
'token' => $token,
'encoding_aes_key' => $encoding_aes_key,
'qrcode_url' => $qrcode_url,
'wechat_type' => $wechat_type,
'status' => $status,
'updated_at' => date('Y-m-d H:i:s')
];
$result = $this->db->update('wxgcode_settings', $data, ['id' => $id]);
if ($result) {
$setting = $this->db->get('wxgcode_settings', '*', ['id' => $id]);
echo json_encode([
'success' => true,
'message' => '设置更新成功',
'data' => $setting
]);
} else {
echo json_encode([
'success' => false,
'message' => '更新失败或数据无变化'
]);
}
} else {
// 新增
$insertId = $this->db->insert('wxgcode_settings', [
'wechat_name' => $wechat_name,
'wechat_account' => $wechat_account,
'appid' => $appid,
'appsecret' => $appsecret,
'token' => $token,
'encoding_aes_key' => $encoding_aes_key,
'qrcode_url' => $qrcode_url,
'wechat_type' => $wechat_type,
'status' => 1,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
if ($insertId) {
$setting = $this->db->get('wxgcode_settings', '*');
echo json_encode([
'success' => true,
'message' => '设置创建成功',
'data' => $setting
]);
} else {
echo json_encode([
'success' => false,
'message' => '创建失败,请稍后重试'
]);
}
}
} else {
http_response_code(405);
echo json_encode([
'success' => false,
'message' => '只支持 GET 和 POST 请求'
]);
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => '操作失败: ' . $e->getMessage()
]);
}
exit;
}
public function delete($id) {
$this->checkLogin();
header('Content-Type: application/json');
try {
// 检查是否存在
$row = $this->db->get('wxgcode_list', '*', ['id' => $id]);
if (!$row) {
echo json_encode([
'success' => false,
'message' => '要删除的记录不存在'
]);
exit;
}
// 执行删除
$result = $this->db->delete('wxgcode_list', ['id' => $id]);
if ($result) {
echo json_encode([
'success' => true,
'message' => '记录已删除'
]);
} else {
echo json_encode([
'success' => false,
'message' => '删除失败,请稍后再试'
]);
}
} catch (Exception $e) {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => '删除失败: ' . $e->getMessage()
]);
}
exit;
}
}
+129
View File
@@ -0,0 +1,129 @@
<?php
namespace Plugins\WxGCode\Controllers\Web;
use App\Core\WebBaseController;
class WxGCodeController extends WebBaseController {
protected $pluginManager;
protected $db;
public function __construct() {
global $pluginManager;
$this->pluginManager = $pluginManager;
$this->db = $this->pluginManager->getDB();
}
public function index($code) {
$code = $code;
include __DIR__ . '/../../Views/Web/index.php';
}
public function get($code) {
// 设置响应内容类型为JSON
header('Content-Type: application/json');
// 验证code格式 (假设code是字母数字组合)
if (empty($code) || !preg_match('/^[A-Za-z0-9]+$/', $code)) {
echo json_encode([
'success' => false,
'message' => '无效的活码编码'
]);
exit;
}
// 查询有效的活码记录 (只查询启用状态的)
$row = $this->db->get('wxgcode_list', '*', [
'AND' => [
'code' => $code,
'status' => 1 // 只显示启用状态的活码
]
]);
if ($row) {
// 记录访问量
$this->increaseViewCount($row['id']);
// 检查是否需要切换到备用活码 (如果当前活码达到最大扫码次数)
if ($row['max_scans'] > 0 && $row['total_scans'] >= $row['max_scans'] && !empty($row['backup_id'])) {
$backupRow = $this->db->get('wxgcode_list', '*', [
'AND' => [
'id' => $row['backup_id'],
'status' => 1
]
]);
if ($backupRow) {
$row = $backupRow;
}
}
// 格式化数据
if (isset($row['created_at'])) {
$row['created_at'] = date('Y-m-d', strtotime($row['created_at']));
}
// 返回活码信息
echo json_encode([
'success' => true,
'data' => $row
]);
} else {
echo json_encode([
'success' => false,
'message' => '活码不存在或已被禁用'
]);
}
exit;
}
/**
* 增加活码访问量
* @param int $id 活码ID
*/
protected function increaseViewCount($id) {
// 增加扫码次数
$this->db->update('wxgcode_list', [
'total_views[+]' => 1
], ['id' => $id]);
}
/**
* 刷新二维码
* @param string $code 活码编码
*/
public function refreshQrcode($code) {
header('Content-Type: application/json');
$row = $this->db->get('wxgcode_list', ['id', 'qrcode_url', 'code'], [
'AND' => [
'code' => $code,
'status' => 1
]
]);
if ($row) {
// 这里可以添加调用微信接口生成新二维码的逻辑
// 示例:$newQrcodeUrl = $this->generateNewQrcode($row['id']);
// 简单模拟刷新(实际项目中应替换为真实逻辑)
$newQrcodeUrl = $row['qrcode_url'] . '?t=' . time();
// 更新数据库中的二维码URL
$this->db->update('wxgcode_list', [
'qrcode_url' => $newQrcodeUrl,
'update_time' => date('Y-m-d H:i:s')
], ['id' => $row['id']]);
echo json_encode([
'success' => true,
'data' => [
'qrcode_url' => $newQrcodeUrl
]
]);
} else {
echo json_encode([
'success' => false,
'message' => '刷新失败,活码不存在或已被禁用'
]);
}
exit;
}
}
+1
View File
@@ -0,0 +1 @@
<?php exit();?>{"expire_time":1754852111,"access_token":"95_rXIZ_RT-sfFba-lFUL3IyhSMbDb32bqoFpYzJCYhxLI-7HzWv6v6GqxIfn2V_8qJ1nkGFA-9RtcLNdiEIWHGwNFuuUZntG8CM_OmB4F2tb0teS2QI8EArS4VlOAZRSbAEANFG"}
+1
View File
@@ -0,0 +1 @@
<?php exit();?>{"expire_time":1754852112,"jsapi_ticket":"LIKLckvwlJT9cWIhEQTwfMAhJFYw3_TwrJw6wWpdGhRHwl7AqiNprXTimrsImS13T-xXctQR4na76SuT9Pkxgg"}
+150
View File
@@ -0,0 +1,150 @@
<?php
namespace Plugins\WxGCode\Controllers\lib;
class JSSDK {
private $appId;
private $appSecret;
// 缓存文件路径(使用绝对路径避免问题)
private $cacheDir;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
// 初始化缓存目录(与jssdk.php同目录)
$this->cacheDir = dirname(__FILE__) . '/';
// 确保缓存目录可写
$this->checkCacheDir();
}
// 检查缓存目录是否存在且可写
private function checkCacheDir() {
if (!is_dir($this->cacheDir)) {
mkdir($this->cacheDir, 0755, true);
}
if (!is_writable($this->cacheDir)) {
throw new \Exception("缓存目录不可写:{$this->cacheDir}");
}
}
// 检查并创建缓存文件
private function checkCacheFile($filename) {
$filePath = $this->cacheDir . $filename;
// 如果文件不存在则创建并初始化
if (!file_exists($filePath)) {
$initialData = json_encode([
'expire_time' => 0,
'access_token' => '',
'jsapi_ticket' => ''
]);
$this->set_php_file($filename, $initialData);
}
return $filePath;
}
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
// 动态获取当前URL
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 按ASCII码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
$signature = sha1($string);
return [
"appId" => $this->appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
];
}
private function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
private function getJsApiTicket() {
// 先检查并创建缓存文件
$this->checkCacheFile("jsapi_ticket.php");
$data = json_decode($this->get_php_file("jsapi_ticket.php"));
// 处理空数据或过期情况
if (empty($data) || $data->expire_time < time()) {
$accessToken = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
if (isset($res->ticket)) {
$data = new \stdClass(); // 初始化空对象
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $res->ticket;
$this->set_php_file("jsapi_ticket.php", json_encode($data));
} else {
throw new \Exception("获取jsapi_ticket失败: " . json_encode($res));
}
}
return $data->jsapi_ticket ?? '';
}
private function getAccessToken() {
// 先检查并创建缓存文件
$this->checkCacheFile("access_token.php");
$data = json_decode($this->get_php_file("access_token.php"));
// 处理空数据或过期情况
if (empty($data) || $data->expire_time < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$res = json_decode($this->httpGet($url));
if (isset($res->access_token)) {
$data = new \stdClass(); // 初始化空对象
$data->expire_time = time() + 7000;
$data->access_token = $res->access_token;
$this->set_php_file("access_token.php", json_encode($data));
} else {
throw new \Exception("获取access_token失败: " . json_encode($res));
}
}
return $data->access_token ?? '';
}
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
private function get_php_file($filename) {
$filePath = $this->cacheDir . $filename;
return trim(substr(file_get_contents($filePath), 15));
}
private function set_php_file($filename, $content) {
$filePath = $this->cacheDir . $filename;
$fp = fopen($filePath, "w");
fwrite($fp, "<?php exit();?>" . $content);
fclose($fp);
}
}