原帖https://hostloc.com/thread-1367276-1-1.html
根据原帖修改为php版本
代码index.html
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>6间房图片上传</title>
<link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/2.4.4/weui.min.css">
<style>
body {
background-color: #f1f1f1;
color: #333;
font-family: "Microsoft YaHei", "Hiragino Sans GB", sans-serif;
}
.container {
padding: 20px;
max-width: 600px;
margin: 0 auto;
}
.main-content {
background-color: #fff;
border-radius: 10px;
padding: 30px;
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
#drop-area {
border: 2px dashed #FFC300;
border-radius: 10px;
padding: 40px;
text-align: center;
margin-bottom: 20px;
transition: all 0.3s ease;
min-height: 200px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
#drop-area:hover {
background-color: #FFF9E6;
}
#drop-area.dragover {
background-color: #FFF9E6;
box-shadow: inset 0 0 10px rgba(255,195,0,0.3);
}
.thumb {
width: 80px;
height: 80px;
object-fit: cover;
margin: 5px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.weui-btn {
font-weight: bold;
}
.weui-btn_warn {
background-color: #fff;
color: #FFC300;
border: 1px solid #FFC300;
}
.weui-cell {
background-color: #f9f9f9;
border-radius: 5px;
margin-bottom: 10px;
display: flex;
flex-direction: row;
align-items: center;
}
.weui-cell__bd {
flex: 1;
}
.weui-cell__ft {
margin-left: 10px;
}
.weui-input {
border: none;
background-color: transparent;
padding: 5px 10px;
width: 100%;
box-sizing: border-box;
}
.button-container {
display: flex;
gap: 20px;
}
.button-container .weui-flex__item {
flex: 1;
}
@media (max-width: 480px) {
.container {
padding: 10px;
}
.main-content {
padding: 20px;
}
#drop-area {
padding: 20px;
}
.thumb {
width: 60px;
height: 60px;
}
.weui-cell {
flex-direction: column;
align-items: stretch;
}
.weui-cell__ft {
margin-left: 0;
margin-top: 10px;
}
.weui-btn_mini {
width: 100%;
}
.button-container {
flex-direction: column;
gap: 10px;
}
.button-container .weui-flex__item {
width: 100%;
}
}
.code-tabs {
display: flex;
justify-content: space-around;
margin-bottom: 10px;
}
.code-tab {
padding: 5px 10px;
cursor: pointer;
border-bottom: 2px solid transparent;
}
.code-tab.active {
border-bottom-color: #FFC300;
}
.code-content {
display: none;
}
.code-content.active {
display: block;
}
</style>
</head>
<body>
<div class="container">
<div class="main-content">
<h2 class="weui-panel__hd" style="text-align: center; margin-bottom: 20px;">6间房图片上传</h2>
<div id="drop-area" class="weui-cells weui-cells_form">
<div>
<p>拖放图片到此处或点击选择文件</p>
<p>也可以直接粘贴图片</p>
</div>
<input type="file" id="fileElem" accept="image/*" style="display:none">
<div id="thumb-container"></div>
</div>
<div class="button-container">
<div class="weui-flex__item">
<a href="javascript:" id="upload-btn" class="weui-btn weui-btn_primary">开始上传</a>
</div>
<div class="weui-flex__item">
<a href="javascript:" id="clear-btn" class="weui-btn weui-btn_warn">清除</a>
</div>
</div>
<div id="result-container"></div>
</div>
</div>
<script src="https://res.wx.qq.com/open/libs/weuijs/1.2.1/weui.min.js"></script>
<script>
const dropArea = document.getElementById('drop-area');
const fileElem = document.getElementById('fileElem');
const thumbContainer = document.getElementById('thumb-container');
const resultContainer = document.getElementById('result-container');
const uploadBtn = document.getElementById('upload-btn');
const clearBtn = document.getElementById('clear-btn');
let fileToUpload = null;
uploadBtn.addEventListener('click', () => {
if (!fileToUpload) {
weui.topTips('请选择文件后再上传', 2000);
return;
}
uploadFile();
});
clearBtn.addEventListener('click', () => {
thumbContainer.innerHTML = '';
resultContainer.innerHTML = '';
fileToUpload = null;
weui.toast('已清除', 1000);
});
dropArea.addEventListener('dragover', (event) => {
event.preventDefault();
dropArea.classList.add('dragover');
});
dropArea.addEventListener('dragleave', () => {
dropArea.classList.remove('dragover');
});
dropArea.addEventListener('drop', (event) => {
event.preventDefault();
dropArea.classList.remove('dragover');
const file = event.dataTransfer.files[0];
if (file && file.type.startsWith('image/')) {
handleFile(file);
} else {
weui.topTips('请上传图片文件', 2000);
}
});
dropArea.addEventListener('click', () => {
fileElem.click();
});
fileElem.addEventListener('change', () => {
if (fileElem.files.length > 0) {
handleFile(fileElem.files[0]);
}
});
document.addEventListener('paste', (event) => {
const items = event.clipboardData.items;
for (let i = 0; i < items.length; i++) {
if (items[i].type.indexOf('image') !== -1) {
const blob = items[i].getAsFile();
handleFile(blob);
break;
}
}
});
function handleFile(file) {
fileToUpload = file;
displayThumbnail(file);
}
function displayThumbnail(file) {
const reader = new FileReader();
reader.onloadend = () => {
thumbContainer.innerHTML = '';
const img = document.createElement('img');
img.src = reader.result;
img.classList.add('thumb');
thumbContainer.appendChild(img);
};
reader.readAsDataURL(file);
}
function uploadFile() {
const formData = new FormData();
formData.append('file', fileToUpload);
formData.append('pid', `${1001 + Math.floor(Math.random() * 9)}`);
formData.append('size', 'c6');
formData.append('callbackUrl', 'https://v.6.cn/profile/transferStation.html');
formData.append('callbackFun', `window.parent.${crypto.randomUUID().split('-')[0]}`);
const loadingTip = weui.loading('上传中...');
fetch('upload.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
loadingTip.hide();
if (data.success && data.url) {
weui.toast('上传成功', 1000);
displayResult(data.url, fileToUpload.name);
} else {
weui.topTips('上传失败: ' + (data.message || '未知错误'), 2000);
}
})
.catch(error => {
loadingTip.hide();
weui.topTips('上传失败: ' + error.message, 2000);
});
}
function displayResult(imgUrl, fileName) {
const resultItem = document.createElement('div');
resultItem.classList.add('weui-cell');
const cellBody = document.createElement('div');
cellBody.classList.add('weui-cell__bd');
const codeTabs = document.createElement('div');
codeTabs.classList.add('code-tabs');
['URL', 'UBB', 'Markdown', 'HTML'].forEach((tabName, index) => {
const tab = document.createElement('div');
tab.classList.add('code-tab');
tab.textContent = tabName;
tab.addEventListener('click', () => switchTab(resultItem, index));
codeTabs.appendChild(tab);
});
cellBody.appendChild(codeTabs);
const codeContents = [
imgUrl,
`[img]${imgUrl}[/img]`,
`![${fileName}](${imgUrl})`,
`<img src="${imgUrl}" alt="${fileName}">`
];
codeContents.forEach((content, index) => {
const codeContent = document.createElement('div');
codeContent.classList.add('code-content');
const input = document.createElement('input');
input.type = 'text';
input.value = content;
input.readOnly = true;
input.classList.add('weui-input');
codeContent.appendChild(input);
cellBody.appendChild(codeContent);
});
const copyButton = document.createElement('a');
copyButton.classList.add('weui-btn', 'weui-btn_mini', 'weui-btn_primary');
copyButton.textContent = '复制';
copyButton.addEventListener('click', () => {
const activeInput = resultItem.querySelector('.code-content.active input');
activeInput.select();
document.execCommand('copy');
weui.toast('已复制', 1000);
});
const cellFoot = document.createElement('div');
cellFoot.classList.add('weui-cell__ft');
cellFoot.appendChild(copyButton);
resultItem.appendChild(cellBody);
resultItem.appendChild(cellFoot);
resultContainer.appendChild(resultItem);
switchTab(resultItem, 0);
}
function switchTab(resultItem, activeIndex) {
const tabs = resultItem.querySelectorAll('.code-tab');
const contents = resultItem.querySelectorAll('.code-content');
tabs.forEach((tab, index) => {
if (index === activeIndex) {
tab.classList.add('active');
contents[index].classList.add('active');
} else {
tab.classList.remove('active');
contents[index].classList.remove('active');
}
});
}
</script>
</body>
</html>
upload.php
<?php
header('Content-Type: application/json');
try {
if (!isset($_FILES['file'])) {
throw new Exception('没有接收到文件');
}
$file = $_FILES['file'];
if ($file['error'] !== UPLOAD_ERR_OK) {
throw new Exception('文件上传错误:' . $file['error']);
}
$url = 'https://pic.v.6.cn/api/uploadForGeneral.php';
$pid = '100' . rand(1, 9);
$callbackUrl = 'https://v.6.cn/profile/transferStation.html';
$callbackFun = 'window.parent.' . substr(uniqid(), 0, 8);
$postData = array(
'pid' => $pid,
'size' => 'c6',
'callbackUrl' => $callbackUrl,
'callbackFun' => $callbackFun,
'file' => new CURLFile($file['tmp_name'], $file['type'], $file['name'])
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('CURL错误:' . curl_error($ch));
}
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $headerSize);
if (preg_match('/Location:.*json=(.*)/i', $headers, $matches)) {
$uploadResult = json_decode($matches[1], true);
if ($uploadResult['flag'] === '001') {
echo json_encode([
'success' => true,
'url' => $uploadResult['content']['url']['link']
]);
} else {
throw new Exception('上传服务器返回错误');
}
} else {
throw new Exception('未找到上传结果');
}
curl_close($ch);
} catch (Exception $e) {
echo json_encode([
'success' => false,
'message' => $e->getMessage()
]);
}
测试图片