修改插件里的show.htm代码和Codehighlighting.php代码
show.htm
<link rel="stylesheet" href="/weapp/Codehighlighting/template/skin/css/prism.css" />
<script src="/weapp/Codehighlighting/template/skin/js/prism.js"></script>
<style type="text/css">
pre.noselect,pre.noselect code{user-select: none;-webkit-user-select: none;-moz-user-select: none;-ms-user-select: none;}
.prism-scroll,.prism-scroll code{height:{$data['maxheight']}px !important;overflow:hidden !important;box-sizing:border-box;}
.prism-scroll{position: relative;}
.prism-scroll .bg_CodeBtn{display:flex;justify-content: center;align-items: center;position: absolute;bottom:-8px;left:0;height:50%;width:100%;z-index:999;background-image: linear-gradient(-180deg, rgba(255, 255, 255, 0) 0%, #fff 100%);}
.prism-scroll .bg_CodeBtn .More_CodeBtn{display:block;height:50px;background:#333333;width:50px;border-radius:50px;cursor: pointer;}
.prism-scroll .bg_CodeBtn .More_CodeBtn svg{width:50px;height:50px;transform: rotate(90deg);}
</style>
<script type="text/javascript">
(function() {
if (window._prismFinalApplied) return;
window._prismFinalApplied = true;
// 检测前端登录状态(通过 Cookie)
function isUserLogin() {
return document.cookie.indexOf('users_id=') !== -1;
}
function highlightAllCodeBlocks() {
if (typeof Prism === 'undefined') return;
var blocks = document.querySelectorAll('pre code');
blocks.forEach(function(block) {
if (block.classList.contains('prism-highlighted')) return;
var pre = block.parentNode;
var classes = pre.className + ' ' + block.className;
var langMatch = classes.match(/language-(\w+)/);
if (langMatch) {
var lang = langMatch[1];
if (Prism.languages[lang]) {
Prism.highlightElement(block);
block.classList.add('prism-highlighted');
return;
}
}
Prism.highlightElement(block);
block.classList.add('prism-highlighted');
});
}
function enhanceCodeBlocks() {
$("pre").each(function() {
if ($(this).data('enhanced')) return;
$(this).data('enhanced', true);
var prev = $(this).prev();
var btn = '<svg class="Copy_CodeBtn" fill="#fff" style="margin-right:8px;vertical-align: middle;cursor: pointer;" width="16" height="16" viewBox="0 0 11.199999809265137 11.199999809265137" class="cursor-pointer flex items-center tongyi-design-highlighter-copy-btn" data-spm-anchor-id="5176.28103460.0.i14.297c5d27C86h6a"><g><path d="M11.2,1.6C11.2,0.716344,10.4837,0,9.6,0L4.8,0C3.91634,0,3.2,0.716344,3.2,1.6L4.16,1.6Q4.16,1.3349,4.34745,1.14745Q4.5349,0.96,4.8,0.96L9.6,0.96Q9.8651,0.96,10.0525,1.14745Q10.24,1.3349,10.24,1.6L10.24,6.4Q10.24,6.6651,10.0525,6.85255Q9.8651,7.04,9.6,7.04L9.6,8C10.4837,8,11.2,7.28366,11.2,6.4L11.2,1.6ZM0,4L0,9.6C0,10.4837,0.716344,11.2,1.6,11.2L7.2,11.2C8.08366,11.2,8.8,10.4837,8.8,9.6L8.8,4C8.8,3.11634,8.08366,2.4,7.2,2.4L1.6,2.4C0.716344,2.4,0,3.11634,0,4ZM1.14745,10.0525Q0.96,9.8651,0.96,9.6L0.96,4Q0.96,3.7349,1.14745,3.54745Q1.3349,3.36,1.6,3.36L7.2,3.36Q7.4651,3.36,7.65255,3.54745Q7.84,3.7349,7.84,4L7.84,9.6Q7.84,9.8651,7.65255,10.0525Q7.4651,10.24,7.2,10.24L1.6,10.24Q1.3349,10.24,1.14745,10.0525Z" data-spm-anchor-id="5176.28103460.0.i12.297c5d27C86h6a"></path></g></svg>';
if(prev.hasClass("prism-show-language")) {
prev.find("div").prepend(btn);
}else{
$(this).before('<div class="prism-show-language"><div class="prism-show-language-label" data-language="default">' + btn + '</div>');
}
{if $data['heightopen']}
if($(this).height() >= {$data['maxheight']}){
$(this).addClass('prism-scroll');
$(this).append('<div class="bg_CodeBtn"><span class="More_CodeBtn"><svg viewBox="0 0 100 100"><polyline fill="none" points="42,32 60,50 42,68" stroke-linecap="round" stroke-linejoin="round" stroke-width="6" stroke="#fff"></polyline></svg></span></div>');
}
{/if}
});
}
function bindEvents() {
if (window._prismEventsBound) return;
window._prismEventsBound = true;
{if $data['heightopen']}
$(document).on('click', '.More_CodeBtn', function() {
var $pre = $(this).closest('pre');
if ($pre.hasClass("prism-scroll")) {
$pre.removeClass('prism-scroll').find('.bg_CodeBtn').remove();
}
});
{/if}
$(document).on('click', '.Copy_CodeBtn', function() {
// 双重校验:如果后台限制了会员复制,但前端判断未登录则强制跳转
{if $data['copy']}
// 后台允许复制(可能是未开启会员复制,或已登录)
// 但是,如果后台开启了会员复制,但用户实际未登录(防御性代码),仍要拦截
{if $data['useropen']}
// 后台开启了会员复制,需要前端再校验一次登录状态
if (!isUserLogin()) {
alert("请先登录!");
var loginUrl = "{eyou:diyurl type='login' /}";
var currentPage = encodeURIComponent(window.location.href);
var separator = loginUrl.indexOf('?') !== -1 ? '&' : '?';
window.location.href = loginUrl + separator + 'referurl=' + currentPage;
return;
}
{/if}
// 执行复制
var $codeBlock = $(this).closest('.prism-show-language').next('pre');
if ($codeBlock.length) {
var codeContent = $codeBlock.find('code').text();
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(codeContent).then(function() {
alert("复制成功!");
}).catch(function() {
alert("复制失败,请手动复制");
});
} else {
var tempInput = $("<input>");
$("body").append(tempInput);
tempInput.val(codeContent).select();
document.execCommand("copy");
tempInput.remove();
alert("复制成功!");
}
} else {
alert("未找到代码块");
}
{else}
// 后台开启了会员复制且用户未登录,提示登录
alert("请先登录!");
var loginUrl = "{eyou:diyurl type='login' /}";
var currentPage = encodeURIComponent(window.location.href);
var separator = loginUrl.indexOf('?') !== -1 ? '&' : '?';
window.location.href = loginUrl + separator + 'referurl=' + currentPage;
{/if}
});
{if $data['copy']}
$(document).on('copy', 'pre', function(e) { e.preventDefault(); });
$(document).on('contextmenu', 'pre', function(e) { e.preventDefault(); });
$(document).on('selectstart', 'pre', function(e) { e.preventDefault(); });
$(document).on('keydown', function(e) {
if ((e.ctrlKey || e.metaKey) && e.key === 'a') {
e.preventDefault();
}
});
{/if}
}
function preparePreTags() {
$('pre').each(function() {
if ($(this).find('code').length === 0) {
$(this).html('<code>' + $(this).html() + '</code>');
}
});
}
$(function() {
preparePreTags();
enhanceCodeBlocks();
bindEvents();
setTimeout(highlightAllCodeBlocks, 100);
var observer = new MutationObserver(function(mutations) {
var needsHighlight = false;
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length) needsHighlight = true;
});
if (needsHighlight) {
setTimeout(function() {
preparePreTags();
enhanceCodeBlocks();
highlightAllCodeBlocks();
}, 50);
}
});
observer.observe(document.body, { childList: true, subtree: true });
});
})();
</script>Codehighlighting.php代码
<?php
/**
* 易优CMS
* ============================================================================
* 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
* 网站地址: http://www.eyoucms.com
* ----------------------------------------------------------------------------
* 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
* ============================================================================
* Author: 小虎哥 <1105415366@qq.com>
* Date: 2018-06-28
*/
namespace weapp\Codehighlighting\controller;
use think\Db;
use app\common\controller\Weapp;
class Codehighlighting extends Weapp
{
private $weappInfo;
public function __construct(){
parent::__construct();
$this->weappInfo = $this->getWeappInfo();
$this->assign('weappInfo', $this->weappInfo);
}
public function doc()
{
return $this->fetch('doc');
}
public function index()
{
if (IS_POST) {
$post = input('post.');
$post['maxheight'] = intval($post['maxheight']);
if(empty($post['maxheight'])) $this->error('限制高度不能为空!');
$data = [
'data' => json_encode($post, true),
'tag_weapp' => 1,
'update_time' => getTime(),
];
$result = Db::name('weapp')->where(['code'=>'Codehighlighting'])->update($data);
if (!empty($result)) {
$this->success('操作成功');
}
$this->error("操作失败!");
}
$data = Db::name('weapp')->where(['code'=>'Codehighlighting'])->getField('data');
$data = json_decode($data, true);
$this->assign('data', $data);
return $this->fetch('index');
}
/**
* 前台钩子输出
*/
public function show($params = null){
// 获取插件配置
$weapp = Db::name('weapp')->where('code', 'Codehighlighting')->find();
$data = !empty($weapp['data']) ? json_decode($weapp['data'], true) : [];
// 默认值
if (!isset($data['useropen'])) $data['useropen'] = 0;
if (!isset($data['heightopen'])) $data['heightopen'] = 0;
if (!isset($data['maxheight'])) $data['maxheight'] = 350;
// 判断会员复制权限(使用易优CMS的 is_login 函数,返回用户ID,未登录返回0)
$userid = function_exists('is_login') ? is_login() : 0;
if (empty($userid) && session('users_id')) {
$userid = session('users_id');
}
// 核心逻辑:
// 如果后台开启会员复制(useropen=1),则只有登录用户(userid>0)才能复制
// 如果后台未开启,则所有人都可以复制
if (!empty($data['useropen'])) {
$data['copy'] = ($userid > 0) ? true : false;
} else {
$data['copy'] = true;
}
$this->assign('data', $data);
echo $this->fetch('show');
}
}