manghe/app/MyController.php
2025-06-22 19:12:07 +08:00

100 lines
2.7 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace app;
use think\App;
use think\exception\ValidateException;
use think\Validate;
use think\Request;
/**
* 控制器基础类
*/
abstract class MyController extends BaseController
{
/**
* 返回封装后的 API 数据到客户端
*/
protected function renderJson($status = 1, $msg = '', $data = [])
{
$platform = request()->header('platform', '');
if ($platform == "APP_ANDROID" || $platform == "APP_IOS") {
// 密钥和加密算法
// $secretKey = 'g6R@9!zB2fL#1cVm'; // 必须是 16/24/32 位
// $cipher = 'AES-128-CBC';
// $iv = substr(md5($secretKey), 0, 16); // IV 长度必须是 16 字节
// 对 $data 进行加密处理
// $dataString = json_encode($data, JSON_UNESCAPED_UNICODE);
// $encrypted = openssl_encrypt($dataString, $cipher, $secretKey, 0, $iv);
return [
'ret' => $status,
// 'payload' => $data,//$encrypted,
'data' => $data,
'note' => $msg,
'ts' => time(), // 当前时间戳
];
}
$request = request();
return compact('status', 'msg', 'data');
}
protected function encryption($data)
{
$secretKey = 'g6R@9!zB2fL#1cVm'; // 必须是 16/24/32 位
$cipher = 'AES-128-CBC';
$iv = substr(md5($secretKey), 0, 16); // IV 长度必须是 16 字节
// 对 $data 进行加密处理
$dataString = json_encode($data, JSON_UNESCAPED_UNICODE);
$encrypted = openssl_encrypt($dataString, $cipher, $secretKey, 0, $iv);
return $encrypted;
}
/**
* 返回操作成功json
*/
protected function renderSuccess($msg = 'success', $data = [])
{
return json($this->renderJson(1, $msg, $data));
}
/**
* 返回操作失败json
*/
protected function renderError($msg = 'error', $data = [], $status = 0)
{
return json($this->renderJson($status, $msg, $data));
}
/**
* 返回layui.table格式的数据
*/
protected function renderTable($msg = 'success', $count = 0, $data = [], $code = 0)
{
return json([
'code' => $code,
'msg' => $msg,
'count' => $count,
'data' => $data
]);
}
/**
* 获取post数据 (数组)
*/
protected function postData($key = null)
{
return $this->request->param(is_null($key) ? '' : $key . '/a');
}
/**
* 获取post数据 (数组)
*/
protected function getData($key = null)
{
return $this->request->get(is_null($key) ? '' : $key);
}
}