添加服务逻辑的编辑
接口:
添加服务列表
添加服务详情
添加服务点赞、收藏
This commit is contained in:
2025-12-24 23:03:48 +08:00
parent 49969d813a
commit 16d1adacca
14 changed files with 2145 additions and 321 deletions

View File

@@ -2,14 +2,265 @@
namespace app\admin\controller;
use app\admin\model\Staff;
use app\admin\server\MomentsServer;
use think\Db;
class Moments extends AdminBase
{
public function lists()
{
$list = MomentsServer::getMoments();
$this->assign('list', $list);
if ($this->request->isAjax()) {
$get = $this->request->get();
$list = MomentsServer::getMoments();
// 格式化数据为前端期望的格式
$data = [
'count' => count($list),
'lists' => $list
];
$this->_success('', $data);
}
return $this->fetch();
}
public function add()
{
if ($this->request->isAjax() && $this->request->isPost()) {
// 处理表单提交
$post = $this->request->post();
// 表单验证
if (empty($post['title'])) {
$this->_error('请输入标题');
}
if (empty($post['sid'])) {
$this->_error('请选择员工');
}
if (empty($post['goods_id'])) {
$this->_error('请选择关联产品');
}
// 处理图片字段(可能是数组格式 goods_image[]
$goods_images = [];
if (isset($post['goods_image'])) {
if (is_array($post['goods_image'])) {
// 数组格式,过滤空值
$goods_images = array_filter($post['goods_image']);
} else if (!empty($post['goods_image'])) {
// 字符串格式(逗号分隔),转换为数组
$goods_images = explode(',', $post['goods_image']);
$goods_images = array_filter($goods_images);
}
}
// 验证图片
if (empty($goods_images)) {
$this->_error('请选择图片');
}
// 将图片数组转换为逗号分隔的字符串
$post['goods_image'] = implode(',', $goods_images);
// 添加创建时间
$post['create_time'] = date('Y-m-d H:i:s',time());
$post['page_views'] = 0;
$post['like'] = 0;
$post['collection'] = 0;
// 保存数据
$result = Db::name('moments')->insert($post);
if ($result) {
$this->_success('添加成功');
} else {
$this->_error('添加失败');
}
}
//查询员工信息
$staff = Db::name('staff')->where('onwork',1)->field('id,name')->select();
//查询商品信息
$goods = Db::name('goods')
->where('status',1)
->order('code desc')
->field('id,name')->select();
$this->assign([
'staff' => $staff,
'goods' => $goods,
]);
return $this->fetch();
}
/**
* 查看详情
*/
public function info()
{
$id = $this->request->param('id', 0);
if (empty($id)) {
$this->_error('参数错误');
}
$moment = Db::name('moments')->where('id', $id)->find();
if (empty($moment)) {
$this->_error('数据不存在');
}
// 获取关联数据
$staff = null;
if (!empty($moment['sid'])) {
$staff = Db::name('staff')->where('id', $moment['sid'])->field('id,name')->find();
}
$goods = null;
if (!empty($moment['goods_id'])) {
$goods = Db::name('goods')->where('id', $moment['goods_id'])->field('id,name')->find();
}
// 处理图片转换为完整URL
$images = [];
if (!empty($moment['goods_image'])) {
$imageList = explode(',', $moment['goods_image']);
foreach ($imageList as $img) {
$img = trim($img);
if (!empty($img)) {
$images[] = \app\common\server\UrlServer::getFileUrl($img);
}
}
}
$this->assign([
'moment' => $moment,
'staff' => $staff,
'goods' => $goods,
'images' => $images,
]);
return $this->fetch();
}
/**
* 编辑
*/
public function edit()
{
$id = $this->request->param('id', 0);
if (empty($id)) {
$this->_error('参数错误');
}
if ($this->request->isAjax() && $this->request->isPost()) {
// 处理表单提交
$post = $this->request->post();
// 表单验证
if (empty($post['title'])) {
$this->_error('请输入标题');
}
if (empty($post['sid'])) {
$this->_error('请选择员工');
}
if (empty($post['goods_id'])) {
$this->_error('请选择关联产品');
}
// 处理图片字段(可能是数组格式 goods_image[]
$goods_images = [];
if (isset($post['goods_image'])) {
if (is_array($post['goods_image'])) {
// 数组格式,过滤空值
$goods_images = array_filter($post['goods_image']);
} else if (!empty($post['goods_image'])) {
// 字符串格式(逗号分隔),转换为数组
$goods_images = explode(',', $post['goods_image']);
$goods_images = array_filter($goods_images);
}
}
// 验证图片
if (empty($goods_images)) {
$this->_error('请选择图片');
}
// 将图片数组转换为逗号分隔的字符串
$post['goods_image'] = implode(',', $goods_images);
// 更新数据
$result = Db::name('moments')->where('id', $id)->update($post);
if ($result !== false) {
$this->_success('编辑成功');
} else {
$this->_error('编辑失败');
}
}
// 获取当前数据
$moment = Db::name('moments')->where('id', $id)->find();
if (empty($moment)) {
$this->_error('数据不存在');
}
// 处理图片转换为完整URL用于显示同时保存原始路径用于提交
$images = [];
$imagePaths = [];
if (!empty($moment['goods_image'])) {
$imageList = explode(',', $moment['goods_image']);
foreach ($imageList as $img) {
$img = trim($img);
if (!empty($img)) {
$imagePaths[] = $img; // 保存原始路径用于提交
$images[] = \app\common\server\UrlServer::getFileUrl($img); // 转换为完整URL用于显示
}
}
}
//查询员工信息
$staff = Db::name('staff')->where('onwork',1)->field('id,name')->select();
//查询商品信息
$goodsList = Db::name('goods')
->where('status',1)
->order('code desc')
->field('id,name')->select();
$this->assign([
'moment' => $moment,
'staff' => $staff,
'goods' => $goodsList,
'images' => $images,
'imagePaths' => $imagePaths, // 传递原始路径
]);
return $this->fetch();
}
/**
* 删除
*/
public function del()
{
if ($this->request->isAjax() && $this->request->isPost()) {
$id = $this->request->post('id', 0);
if (empty($id)) {
$this->_error('参数错误');
}
// 检查数据是否存在
$moment = Db::name('moments')->where('id', $id)->find();
if (empty($moment)) {
$this->_error('数据不存在');
}
// 删除数据
$result = Db::name('moments')->where('id', $id)->delete();
if ($result) {
$this->_success('删除成功');
} else {
$this->_error('删除失败');
}
} else {
$this->_error('请求方式错误');
}
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace app\admin\model;
use app\common\model\Goods;
use think\Model;
class Moments extends Model
{
protected $name = 'moments';
/**
* 关联员工belongsTomoments 属于 staffmoments.sid = staff.id
*/
public function getStaff()
{
return $this->belongsTo(Staff::class, 'sid', 'id');
}
/**
* 关联订单belongsTomoments 属于 ordermoments.oid = order.id
*/
public function getOrder()
{
return $this->belongsTo(Order::class, 'oid', 'id');
}
//关联产品
public function getProduct()
{
return $this->belongsTo(Goods::class, 'goods_id', 'id');
}
}

View File

@@ -1,19 +0,0 @@
<?php
namespace app\admin\model;
use think\Model;
class MomentsModel extends Model
{
protected $name = 'moments';
public function getStaff()
{
return $this->hasOne(StaffModel::class, 'sid', 'id');
}
public function getOrder()
{
return $this->hasOne(OrderModel::class, 'oid', 'id');
}
}

View File

@@ -4,7 +4,7 @@ namespace app\admin\model;
use think\Model;
class OrderModel extends Model
class Order extends Model
{
protected $name = 'order';
}

View File

@@ -4,7 +4,7 @@ namespace app\admin\model;
use think\Model;
class StaffModel extends Model
class Staff extends Model
{
protected $name = 'staff';
}

View File

@@ -2,23 +2,90 @@
namespace app\admin\server;
use app\admin\model\MomentsModel;
use app\admin\model\Moments;
use app\common\server\UrlServer;
use think\Db;
class MomentsServer
{
public static function getMoments()
{
$data = MomentsModel::with([
'getStaff'=>function ($query) {
$query->field('id,name');
},
'getOrder'=>function ($query) {
$query->field('id,order_sn');
}
try {
$data = Moments::with([
'getStaff'=>function ($query) {
$query->field('id,name');
},
'getOrder'=>function ($query) {
$query->field('id,order_sn');
},
'getProduct'=>function ($query) {
$query->field('id,name');
}
])
->order('id desc')
->select();
return $data;
// 如果查询结果为空,尝试不使用关联查询
if (empty($data)) {
$data = Moments::order('id desc')->select();
}
//处理数据
$result = [];
if ($data != null){
foreach ($data as $value) {
// 从 goods_image 字段中取第一张图片作为封面
$img = '';
if (!empty($value['goods_image'])) {
$images = explode(',', $value['goods_image']);
$img = trim($images[0]);
$img = UrlServer::getFileUrl($img);
}
$result[] = [
'id' => $value['id'] ?? 0,
'title' => $value['title'] ?? '',
'order_sn' => $value['order_id'],
'img' => $img,
'create_time' => $value['create_time'] ?? '',
'staff_name' => $value['getStaff']['name'] ?? null,
'goods_name' => $value['getProduct']['name'] ?? null,
'collection' => $value['collection'] ?? 0,
'page_views' => $value['page_views'] ?? 0,
'like' => $value['like'] ?? 0,
'state' => $value['state'] ?? 1,
];
}
}
return $result;
} catch (\Exception $e) {
// 如果关联查询出错,使用简单查询
$data = Moments::order('id desc')->select();
$result = [];
foreach ($data as $value) {
// 从 goods_image 字段中取第一张图片作为封面
$img = '';
if (!empty($value['goods_image'])) {
$images = explode(',', $value['goods_image']);
$firstImage = trim($images[0]);
// 转换为完整的图片URL
$img = UrlServer::getFileUrl($firstImage);
}
$result[] = [
'id' => $value['id'],
'title' => $value['title'],
'order_sn' => $value['order_id'] ?? '',
'img' => $img,
'create_time' => $value['create_time'],
'staff_name' => null,
'goods_name' => null,
'collection' => $value['collection'] ?? 0,
'page_views' => $value['page_views'] ?? 0,
'like' => $value['like'] ?? 0,
'state' => $value['state'] ?? 1,
];
}
return $result;
}
}
}

View File

@@ -0,0 +1,982 @@
{layout name="layout2" /}
<link rel="stylesheet" href="/static/admin/css/goods.css" media="all">
<div class="">
<div class="layui-tab-content layui-form">
<div class="layui-tab-item goods-content layui-show">
<div class="layui-card-body" pad15>
<div lay-filter="">
<div class="layui-form-item">
<label class="layui-form-label"><span class="form-label-asterisk">*</span>标题:</label>
<div class="layui-input-block">
<input type="text" name="title" lay-verify="custom_required" lay-verType="tips"
autocomplete="off"
switch-tab="0" verify-msg="请输入标题" placeholder="请输入标题"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><span class="form-label-asterisk">*</span>员工:</label>
<div class="layui-input-block">
<select name="sid" lay-verType="" placeholder="请选择员工" switch-tab="0" verify-msg="请选择员工">
<option value="">请选择员工</option>
{foreach $staff as $key=>$vo}
<option value="{$vo.id}">{$vo.name}</option>
{/foreach}
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><span class="form-label-asterisk">*</span>产品:</label>
<div class="layui-input-block">
<select name="goods_id" lay-verType="" placeholder="请选择关联产品" switch-tab="0" verify-msg="请选择关联产品">
<option value="">请选择关联产品</option>
{foreach $goods as $key=>$vo}
<option value="{$vo.id}">{$vo.name}</option>
{/foreach}
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">显示状态:</label>
<div class="layui-input-inline">
<input type="radio" name="state" value="1" title="显示" checked>
<input type="radio" name="state" value="2" title="不显示">
</div>
</div>
<div class="layui-form-item" style="margin-bottom: 0px">
<label class="layui-form-label"><span class="form-label-asterisk">*</span>图片:</label>
<div style="min-height:80px;">
<ul style="list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 10px;">
<!-- 图片列表将在这里显示 -->
</ul>
<div class="goods-img-add" lay-verify="goods_image" lay-verType="tips" switch-tab="0" verify-msg="选择图片" style="margin-top: 10px;"></div>
</div>
<br>
</div>
<div class="layui-form-item">
<label class="layui-form-label">内容详情</label>
<div class="layui-input-block">
<textarea name="content" id="content" lay-verify="content" class="field-content"></textarea>
</div>
</div>
</div>
</div>
</div>
<div class="layui-form-item layui-hide">
<input type="button" lay-submit lay-filter="add-user_group-submit" id="add-user_group-submit" value="确认">
</div>
</div>
</div>
<!---------------------------------html模板---------------------------------->
<script type="text/html" id="template-goods-image">
<li class="goods-li">
<input name="goods_image[]" type="hidden" value="{image-src}">
<img class="goods-img goods_image" src="{image-src}">
<a class="goods-img-del-x" style="display: none;">x</a></li>
</script>
<script type="text/javascript">
//注意:选项卡 依赖 element 模块,否则无法进行功能性操作
var dragstart = 0;
var swop_element_ed = -1;
var create_table_by_spec = null;
var spec_table_data = [];
var spec_value_temp_id_number = 0;
layui.config({
version:"{$front_version}",
base: '/static/plug/layui-admin/dist/layuiadmin/' //静态资源所在路径
}).extend({
index: 'lib/index' //主入口模块
}).use(['index', 'element', 'jquery', 'like', 'likeedit'], function () {
var $ = layui.$;
var like = layui.like;
var form = layui.form;
var likeedit = layui.likeedit;
$('.first_ratio').bind('input propertychange', function() {
var that = $(this);
var value = that.val();
format(that,value)
});
$('.second_ratio').bind('input propertychange', function() {
var that = $(this);
var value = that.val();
format(that,value)
});
$('.three_ratio').bind('input propertychange', function() {
var that = $(this);
var value = that.val();
format(that,value)
});
//格式化分销比例
function format(that,value){
value = value.replace(/[^0-9.]/g,'');
value = value.replace(/\.{2,}/g,".");
value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
value = value.replace(/^(\-)*(\d+)\.(\d)(\d).*$/,'$1$2.$3$4');
if(value.indexOf(".")< 0 && value !=""){
value= parseFloat(value);
}
if(value > 100){
that.val(100);
return false;
}
that.val(value);
}
//------------------------基本信息----------------------------------
//商品主图事件
$(document).on('mouseenter', '.goods-li', function () {
$(this).children().last().show();
});
$(document).on('mouseleave', '.goods-li', function () {
$(this).children().last().hide();
});
$(document).on('click', '.goods-li', function () {
var src = $(this).children('img').attr('src');
like.showImg(src,600);
});
//主图显示删除按钮
$(document).on('mouseenter','.master-image',function () {
$(this).children('.goods-image-del').show();
})
$(document).on('mouseleave', '.master-image', function () {
$(this).children('.goods-image-del').hide();
});
$(document).on('click', '.master-image', function () {
var src = $(this).children('img').attr('src');
like.showImg(src,600);
});
//分享海报删除按钮
$(document).on('mouseenter','.poster-image',function () {
$(this).children('.goods-image-del').show();
})
$(document).on('mouseleave', '.poster-image', function () {
$(this).children('.goods-image-del').hide();
});
$(document).on('click', '.poster-image', function () {
var src = $(this).children('img').attr('src');
like.showImg(src,600);
});
$(document).on('click', '.goods-img-del-x', function () {
if($(this).hasClass('goods-image-del')){
$(this).parent().next().show();
$(this).parent().children().remove();
}
$(this).parent().remove();
return false;
});
like.imageUpload('.goods-img-add', function (uris, element) {
// 海报上传逻辑(单图片)
if(element.hasClass('poster-upload')){
if(uris.length>1){
layer.msg('最多只能选中1张图片');
return;
}
var html = '' +
'<input name="poster" type="hidden" value="' + like.getUrlFileName(uris[0], '{$storageUrl}') + '">' +
' <img class="goods-img" src="' + uris[0] + '">\n' +
'<a class="goods-img-del-x goods-image-del">x</a>';
element.prev().append(html);
element.css('display','none');
return 0;
}
// 多图片上传逻辑最多8张
var count = element.prev().children().length;
count = !count ? 0 : count;
if (count + uris.length > 8) {
layer.msg('最多只能选中8张图片');
return;
}
uris = uris.reverse();
for(var i in uris){
var uri = uris[i];
if (!uri) continue; // 跳过无效的 uri
var template_goods_image = $('#template-goods-image').html();
if (!template_goods_image) {
console.error('模板 template-goods-image 不存在');
continue;
}
// 获取文件路径(用于 hidden input
var filePath = like.getUrlFileName ? like.getUrlFileName(uri, '{$storageUrl}') : uri;
if (!filePath) {
filePath = uri; // 如果 getUrlFileName 返回 undefined使用原始 uri
}
// 先替换 input 中的 {image-src},再替换 img 中的 {image-src}
var html = template_goods_image.replace('{image-src}', filePath);
html = html.replace('{image-src}', uri);
element.prev().append(html);
}
}, true);
// ========= 视频上传 开始
$('#video').click(function(){
layer.open({
type: 2,
title: '上传视频',
content: '{:url("file_new/lists")}?type=video',
area: ['90%', '90%']
});
});
window.videoCallback = function(uris) {
like.videoCallback(uris);
};
// ========== 视频上传 结束
//图片拉拽
$(document).on('dragstart', '.goods-li', function (e) {
dragstart = e.offsetX;
});
$(document).on('dragend', '.goods-li', function (e) {
swop_element_ed = -1;
});
$(document).on('mousedown', '.goods-li', function (e) {
swop_element_ed = -1;
});
$(document).on('dragover', '.goods-li', function (e) {
e.preventDefault();
swop_element = parseInt((e.clientX - dragstart) / 100) - 1;
swop_element = swop_element <= 0 ? 0 : swop_element;
my_element = $(this).prevAll().length;
if (swop_element == my_element) {
return;
}
if (swop_element == swop_element_ed) {
return;
}
swop_element_ed = swop_element;
swop($(this), $(this).parent().children().eq(swop_element));
});
//渲染品牌
var brands = 1;
setBrand();
function setBrand(default_id) {
var brand_select_html = '<option value="">请选择原料分类</option>';
for (var i in brands) {
brand_select_html += '<option value="' + brands[i]['id'] + '">' + brands[i]['name'] + '</option>';
}
$('select[name="brand_id"]').html(brand_select_html);
$('select[name="brand_id"]').val(default_id);
form.render('select');
}
//渲染隶属部门
var bellist = 1;
console.log(bellist);
setBrands();
function setBrands(default_id) {
var bellist_select_html = '<option value="">请选择部门</option>';
for (var i in bellist) {
bellist_select_html += '<option value="' + bellist[i]['id'] + '">' + bellist[i]['name'] + '</option>';
}
$('select[name="bellist_id"]').html(bellist_select_html);
$('select[name="bellist_id"]').val(default_id);
form.render('select');
}
//------------------------价格库存----------------------------------
//统一规格与多规格切换事件
function switchSpecType(value)
{
var goods_spec_project = $('#goods-spec-project');
if (value == 2) {
$('#add-spec').parent().show();
if (goods_spec_project.children().length > 0) {
goods_spec_project.parent().show();
$('#more-spec-lists').show();
}
$('#one-spec-lists').hide();
} else {
$('#add-spec').parent().hide();
goods_spec_project.parent().hide();
$('#one-spec-lists').show();
$('#more-spec-lists').hide();
}
}
form.on('radio(spec-type)', function (data) {
switchSpecType(data.value);
});
//添加规格项目
function addSpec(value){
value = value===undefined?'':value;
var element_spec = $('#goods-spec-project');
var count = $('.goods-spec').size();
if (count > 2) {
layer.msg('最多添加3个规格项目');
return;
}
var template_spec = $('#template-spec').html();
element_spec.append(template_spec.replace('{value}',value));
$('#goods-spec-project').parent().show();
form.render('checkbox');
}
$('#add-spec').click(function () {
addSpec();
});
//显示或隐藏规格项目删除按钮绑定
$(document).on('mouseenter', '.goods-spec', function () {
$(this).find('.goods-spec-del-x').show();
});
$(document).on('mouseleave', '.goods-spec', function () {
$(this).find('.goods-spec-del-x').hide();
});
//删除规格项目绑定
$(document).on('click', '.goods-spec-del-x', function () {
$(this).parent().remove();
var goods_spec_project = $('#goods-spec-project');
if (goods_spec_project.children().length == 0) {
goods_spec_project.parent().hide();
}
});
//添加或删除规格项目后续操作
function specValueLater(){
$('.add-spec-value').each(function(){
add_spec_value = $(this);
var spec_values = '';
add_spec_value.parent().parent().find('.goods-spec-value-input').each(function () {
spec_values += $(this).val() + ',';
});
add_spec_value.parent().find('.spec_values').val(spec_values.substring(0, spec_values.lastIndexOf(',')));
var spec_value_ids = '';
add_spec_value.parent().parent().find('.goods-sepc-value-id-input').each(function () {
spec_value_ids += $(this).val() + ',';
});
add_spec_value.parent().find('.spec_value_ids').val(spec_value_ids.substring(0, spec_value_ids.lastIndexOf(',')));
triggerCreateTableBySepc();
});
}
//添加规格项
function addSpecvalue(add_spec_value,spec,spec_id){
var template_spec_value = $('#template-spec-value').html();
var template_spec_value_html = template_spec_value.replace('{spec_value_temp_id}', spec_value_temp_id_number--);
template_spec_value_html = template_spec_value_html.replace('{spec_value_id}',spec_id);
template_spec_value_html = template_spec_value_html.replace('{spec_value}', spec)
add_spec_value.parent().before(template_spec_value_html);
var div = add_spec_value.parent().parent().parent().parent();
var status = div.find('.batch-spec-image-switch').first().is(':checked');
if(status){
div.find('.batch-spec-image').show();
}else{
div.find('.batch-spec-image').hide();
}
}
form.on('switch(batch-spec-image-switch)', function(data){
var status = data.elem.checked;
$('.batch-spec-image-switch').prop("checked",false);
if(status) {
$('.batch-spec-image').hide();
$(this).parent().parent().parent().parent().find('.batch-spec-image').show();
$(this).prop("checked",true);
}else{
$(this).parent().parent().parent().parent().find('.batch-spec-image').hide();
}
form.render('checkbox');
});
//批量添加规格项绑定
$(document).on('click', '.add-spec-value', function () {
var add_spec_value = $(this);
layer.prompt({title: '输入规格值,多个请换行', formType: 2}, function (text, index) {
layer.close(index);
var specs = text.split('\n');
for (var i in specs) {
specs[i] = specs[i].trim();
}
specs = unique(specs);
var added_specs = [];
add_spec_value.parent().parent().find('.goods-spec-value-input').each(function () {
added_specs.push($(this).val().trim());
});
for (var i in specs) {
var spec = specs[i].trim();
if (spec == '' || in_array(spec, added_specs)) {
//已存或为空的不添加
continue;
}
addSpecvalue(add_spec_value,spec,0);
}
specValueLater();
});
});
//删除规格项
$(document).on('click', '.goods-spec-value-del-x', function () {
var add_spec_value = $(this).parent().parent().find('.add-spec-value').first();
$(this).parent().remove();
specValueLater();
});
//批量填充规格图片
like.imageUpload('.batch-spec-image',function(uri,element){
var temp_id = element.prev().attr('spec-value-temp-id');
var spec_images = null;
if($('input[name="spec_type"]:checked').val() == 1){
spec_images = $('#one-spec-lists-table input[name="spec_image[]"]');
}else{
spec_images = $('#more-spec-lists-table input[name="spec_image[]"]');
}
spec_images.each(function(){
var temp_ids = $(this).parent().parent().parent().attr('spec-value-temp-ids');
temp_ids_arr = temp_ids.split(',');
var key = $(this).attr('name') + temp_ids;
if(in_array(temp_id,temp_ids_arr)) {
spec_table_data[key] = uri;
}
});
setTableValue();
});
//批量填充
$('.batch-spec-content').click(function(){
var title = $(this).text();
var input_name = $(this).attr('input-name');
layer.prompt({
formType: 3
,title: '批量填写'+title
},function(value, index, elem){
$('input[name="'+input_name+'[]"]').val(value);
//保存值到本地
$('#more-spec-lists-table input').each(function(){
var key = $(this).attr('name') + $(this).parent().parent().attr('spec-value-temp-ids');
spec_table_data[key] = $(this).val();
});
layer.close(index);
});
});
//显示或隐藏规格项删除按钮
$(document).on('mouseenter', '.goods-spec-value', function () {
$(this).find('.goods-spec-value-del-x').show();
});
$(document).on('mouseleave', '.goods-spec-value', function () {
$(this).find('.goods-spec-value-del-x').hide();
});
// 单规格图片
like.imageUpload('.goods-one-spec-img-add', function (uris, element) {
if(uris.length>1){
layer.msg('最多最能选中1张图片');
return;
}
element.hide();
var key = element.parent().parent().parent().attr('spec-value-temp-ids');
spec_table_data["spec_image[]"+key] = uris[0];//保存图片地址
$(element).parent().html('<input name="one_spec_image" type="hidden" value="' + like.getUrlFileName(uris[0], '{$storageUrl}') + '"><a class="goods-one-spec-img-del-x">x</a><img class="goods-spec-img" src="' + uris[0] + '">');
});
$(document).on('mouseenter', '.goods-spec-img-div', function () {
$(this).find('.goods-one-spec-img-del-x').show();
});
$(document).on('mouseleave', '.goods-spec-img-div', function () {
$(this).find('.goods-one-spec-img-del-x').hide();
});
$(document).on('click', '.goods-one-spec-img-del-x', function () {
$(this).parent().html('<input type="hidden" name="one_spec_image"><img src="/static/common/image/plug/goods-lists-add-image.png" class="goods-one-spec-img-add">');
});
//多规格图片
like.imageUpload('.goods-spec-img-add', function (uris, element) {
if(uris.length>1){
layer.msg('最多最能选中1张图片');
return;
}
element.hide();
var key = element.parent().parent().parent().attr('spec-value-temp-ids');
spec_table_data["spec_image[]"+key] = uris[0];//保存图片地址
$(element).parent().html('<input name="spec_image[]" type="hidden" value="' + like.getUrlFileName(uris[0], '{$storageUrl}') + '"><a class="goods-spec-img-del-x">x</a><img class="goods-spec-img" src="' + uris[0] + '">');
});
$(document).on('mouseenter', '.goods-spec-img-div', function () {
$(this).find('.goods-spec-img-del-x').show();
});
$(document).on('mouseleave', '.goods-spec-img-div', function () {
$(this).find('.goods-spec-img-del-x').hide();
});
$(document).on('click', '.goods-spec-img-del-x', function () {
var key = 'spec_image[]' + $(this).parent().parent().parent().attr('spec-value-temp-ids');
$(this).parent().html('<input type="hidden" name="spec_image[]"><img src="/static/common/image/plug/goods-lists-add-image.png" class="goods-spec-img-add">');
spec_table_data[key] = '';
});
$(document).on('click', '.goods-spec-img', function () {
like.showImg($(this).attr('src'),600);
});
//规格生成表格
createTableBySepc = function () {
if ($('.goods-spec').size() <= 0) {
$('#more-spec-lists').hide();
return;
}
$('#more-spec-lists').show();
var table_title = [];
var table_data = [];
var spec_value_temp_arr = [];
var i = 0;
var th_html = $('#template-spec-table-th').html();
var tr_html = $('#template-spec-table-tr').html();
//遍历规格项目
$('.goods-spec').each(function () {
var spec_name = $(this).find('.spec_name').first().val();
if (isEmptyString(spec_name)) {
return true;
}
table_title[i] = spec_name;
table_data[i] = [];
spec_value_temp_arr[i] = [];
var j = 0;
$(this).find('.goods-spec-value .goods-spec-value-input').each(function () {
var spec_value = $(this).val();
var spec_value_temp_id = $(this).attr('spec-value-temp-id');
if (isEmptyString(spec_value)) {
return true;
}
table_data[i][j] = spec_value;
spec_value_temp_arr[i][j] = spec_value_temp_id;
j++;
});
i++;
});
table_html = '';
//表格头部组装
spec_th_html = '';
for (var i in table_title) {
spec_th_html += '<th>' + table_title[i] + '</th>';
}
table_html = th_html.replace('{spec_th}', spec_th_html);
spec_value_temp_arr = cartesianProduct(spec_value_temp_arr);
table_data = cartesianProduct(table_data);
for (var i in table_data) {
var spec_tr_html = '';
var tr_name_arr = [];
var specs = '';
if (Array.isArray(table_data[i])) {
//根据规格创建tr的id
var spec_value_temp_ids = '';
for(var j in spec_value_temp_arr[i]){
spec_value_temp_ids += spec_value_temp_arr[i][j]+',';
}
spec_value_temp_ids = spec_value_temp_ids.substring(0, spec_value_temp_ids.lastIndexOf(','));
spec_tr_html += '<tr spec-value-temp-ids="'+spec_value_temp_ids+'">';
for (var j in table_data[i]) {
spec_tr_html += '<td>' + table_data[i][j] + '</td>';
tr_name_arr[j] = table_data[i][j];
specs += table_data[i][j].replace(',', '') + ',';
}
} else {
var spec_value_temp_ids = spec_value_temp_arr[i];
spec_tr_html = '<tr spec-value-temp-ids="'+spec_value_temp_ids+'">';
spec_tr_html += '<td>' + table_data[i] + '</td>';
specs += table_data[i].replace(',', '') + ',';
}
specs = specs.substring(0, specs.lastIndexOf(','));
spec_table_data["spec_value_str[]"+spec_value_temp_ids] = specs;
spec_tr_html += '<td style="display: none"><input type="hidden" name="spec_value_str[]" value="' + specs + '"><input type="hidden" name="item_id[]" value=""></td>';
table_html += tr_html.replace('{spec_td}', spec_tr_html);
}
$('#more-spec-lists-table').html(table_html);
setTableValue();
}
//触发规格生成表格
function triggerCreateTableBySepc() {
clearTimeout(create_table_by_spec);
create_table_by_spec = setTimeout(createTableBySepc, 1000);
}
//各种触发生成规格事件
triggerCreateTableBySepc();
$('#add-spec').click(function () {
triggerCreateTableBySepc();
});
$(document).on('click', '.goods-spec-del-x', function () {
triggerCreateTableBySepc();
});
$(document).on('click', '.add-spec-value', function () {
triggerCreateTableBySepc();
});
$(document).on('click', '.goods-spec-value-del-x', function () {
triggerCreateTableBySepc();
});
$(document).on('input', '.goods-spec input', function () {
triggerCreateTableBySepc();
specValueLater();
});
//规格数据本地保存
$(document).on('input', '#more-spec-lists-table input', function () {
var key = $(this).attr('name') + $(this).parent().parent().attr('spec-value-temp-ids');
spec_table_data[key] = $(this).val();
});
//动态渲染已保存的值
function setTableValue() {
$('#more-spec-lists-table').find('input').each(function () {
var key = $(this).attr('name') + $(this).parent().parent().attr('spec-value-temp-ids');
if(spec_table_data[key]!== undefined){
$(this).val(spec_table_data[key]);
}
});
$('.goods-spec-img-div').each(function(){
var key = $(this).parent().parent().attr('spec-value-temp-ids');
if(spec_table_data["spec_image[]"+key]){
$(this).html('<input name="spec_image[]" type="hidden" value="' + spec_table_data["spec_image[]"+key] + '"><a class="goods-spec-img-del-x">x</a><img class="goods-spec-img" src="' + spec_table_data["spec_image[]"+key] + '">');
}
});
}
likeedit.set({
uploadImage: {
url: "{:url('file/image')}",
type: 'post'
}
});
var likeedit_index = likeedit.build('content'); //建立编辑器
form.verify({
content: function () {
likeedit.sync(likeedit_index)
}
});
//------------------------数据验证----------------------------------
function switchTab(number) {
$('.goods-tab').removeClass('layui-this');
$('.goods-content').removeClass('layui-show');
$('.goods-tab').eq(number).addClass('layui-this');
$('.goods-content').eq(number).addClass('layui-show');
}
form.verify({
custom_required: function (value, item) {
if (!value) {
switchTab($(item).attr('switch-tab'));
return $(item).attr('verify-msg');
}
}
,image:function(value, item) {
var image = $('input[name="image"]').val();
if(!image){
switchTab($(item).attr('switch-tab'));
return $(item).attr('verify-msg');
}
}
,goods_image: function (value, item) {
// 检查是否有上传的图片
var imageCount = $('input[name="goods_image[]"]').filter(function() {
return $(this).val() != '';
}).length;
if (imageCount == 0) {
switchTab($(item).attr('switch-tab'));
return $(item).attr('verify-msg');
}
},
status:function(value,item){
if(!$('input[name="status"]:checked').val()){
return $(item).attr('verify-msg');
}
},
one_spec_required: function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 1) {
if (!value) {
switchTab($(item).attr('switch-tab'));
return $(item).attr('verify-msg');
}
}
},
add_more_spec: function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 2) {
if ($('#more-spec-lists-table tbody tr').length == 0) {
switchTab($(item).attr('switch-tab'));
return $(item).attr('verify-msg');
}
}
},
delivery: function (value, item) {
var choose = 0;
$('.delivery').each(function () {
if ($(this).is(':checked')) {
choose++;
}
});
if (choose == 0) {
switchTab($(item).attr('switch-tab'));
return $(item).attr('verify-msg');
}
},
one_volume: function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 1) {
if (value && value < 0) {
switchTab($(item).attr('switch-tab'));
return '体积必须大于0';
}
}
},
one_weight: function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 1) {
if (value && value < 0) {
switchTab($(item).attr('switch-tab'));
return '重量必须大于0';
}
}
},
one_price: function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 2) {
if (value && value < 0.01) {
switchTab($(item).attr('switch-tab'));
return '价格必须大于或等于0.01';
}
}
},
one_cost_price: function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 2) {
if (value && value < 0.01) {
switchTab($(item).attr('switch-tab'));
return '成本价格必须大于或等于0.01';
}
}
},
more_price:function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 2) {
if (value && value < 0.01) {
switchTab($(item).attr('switch-tab'));
return '价格必须大于或等于0.01';
}
}
},
more_cost_price:function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 2) {
if (value && value < 0.01) {
switchTab($(item).attr('switch-tab'));
return '成本价格必须大于或等于0.01';
}
}
},
more_stock: function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 2) {
if (value && value < 0) {
switchTab($(item).attr('switch-tab'));
return '库存必须大于0';
}
}
},
more_weight: function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 2) {
if (value && value < 0) {
switchTab($(item).attr('switch-tab'));
return '重量必须大于0';
}
}
},
more_volume: function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 2) {
if (value && value < 0) {
switchTab($(item).attr('switch-tab'));
return '体积必须大于0';
}
}
},
repetition_spec_name: function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 2) {
var spec_names = [];
$('.spec_name').each(function () {
spec_names.push($(this).val());
});
if ((new Set(spec_names)).size != spec_names.length) {
switchTab($(item).attr('switch-tab'));
return '规格名称重复';
}
}
},
repetition_spec_value: function (value, item) {
if ($('input[name="spec_type"]:checked').val() == 2) {
var spec_values = [];
$(item).find('.goods-spec-value-input').each(function () {
spec_values.push($(this).val());
});
if ((new Set(spec_values)).size != spec_values.length) {
switchTab($(item).attr('switch-tab'));
return '同一规格中,规格值不能重复';
}
}
},
distribution:function (value,item) {
var first_ratio = parseFloat($('.first_ratio').val());
var second_ratio = parseFloat($('.second_ratio').val());
var three_ratio = parseFloat(value);
if(first_ratio + second_ratio + three_ratio > 100){
return '分销比例不可超过100';
}
}
});
//------------------------数据验证----------------------------------
//------------------------编辑页面----------------------------------
{notempty name='info'}
var goods_info= {$info|raw|default=''};
// console.log(goods_info);
$('input[name="goods_id"]').val(goods_info['base']['id']);
$('input[name="name"]').val(goods_info['base']['name']);
$('input[name="mobile"]').val(goods_info['base']['mobile']);
$('input[name="age"]').val(goods_info['base']['age']);
setBrand(goods_info['base']['group_id']); //渲染品牌
setBrands(goods_info['base']['bellist_id']); //渲染品牌
$('input[name="remark"]').val(goods_info['base']['remark']);
// 渲染多图片
var image_html = '';
for(var i in goods_info['base']['goods_image']) {
var template_goods_image = $('#template-goods-image').html();
if (!template_goods_image) {
console.error('模板 template-goods-image 不存在');
continue;
}
// 先替换 input 中的 {image-src},再替换 img 中的 {image-src}
var html = template_goods_image.replace('{image-src}', goods_info['base']['goods_image'][i]['uri'] || '');
html = html.replace('{image-src}', goods_info['base']['goods_image'][i]['abs_image'] || '');
image_html += html;
}
if(image_html) {
$('.goods-img-add').prev('ul').append(image_html);
}
//渲染商品主图(如果还有主图字段的话)
if(goods_info['base']['image']){
var html = '' +
'<input name="image" type="hidden" value="' + goods_info['base']['image'] + '">' +
' <img class="goods-img" src="' + goods_info['base']['abs_image'] + '">\n' +
'<a class="goods-img-del-x goods-image-del">x</a>'
$('.master-image').html(html);
}
//渲染商品视频
if(goods_info['base']['abs_video']){
like.videoCallback([goods_info['base']['abs_video']]);
}
//渲染商品主图
if(goods_info['base']['poster']){
var html = '' +
'<input name="poster" type="hidden" value="' + goods_info['base']['poster'] + '">' +
' <img class="goods-img" src="' + goods_info['base']['abs_poster'] + '">\n' +
'<a class="goods-img-del-x goods-image-del">x</a>'
$('.poster-image').html(html);
$('.poster-upload').hide();
}
form.render();
switchSpecType(goods_info['base']['spec_type']);
if(goods_info['base']['spec_type'] == 1){
var template_goods_image = $('#template-goods-image').html();
if(goods_info['item'][0]['image']){
$('.goods-one-spec-img-add').parent().html('<input name="one_spec_image" type="hidden" value="' + goods_info['item'][0]['image'] + '"><a class="goods-one-spec-img-del-x">x</a><img class="goods-spec-img" src="' + goods_info['item'][0]['abs_image'] + '">');
}
$('input[name="one_price"]').val(goods_info['item'][0]['price']);
$('input[name="one_cost_price"]').val(goods_info['item'][0]['cost_price']);
$('input[name="one_market_price"]').val(goods_info['item'][0]['market_price']);
$('input[name="one_stock"]').val(goods_info['item'][0]['stock']);
$('input[name="one_volume"]').val(goods_info['item'][0]['volume']);
$('input[name="one_weight"]').val(goods_info['item'][0]['weight']);
$('input[name="one_bar_code"]').val(goods_info['item'][0]['bar_code']);
}
if(goods_info['base']['spec_type'] == 2) {
for(var i in goods_info['spec']){
addSpec(goods_info['spec'][i]['name']);
var spes_values = goods_info['spec'][i]['values'];
for(var j in spes_values){
addSpecvalue($('.add-spec-value').eq(i),spes_values[j]['value'],spes_values[j]['id']);
}
}
for(var i in goods_info['spec']){
$('input[name="spec_id[]"]').eq(i).val(goods_info['spec'][i]['id']);
}
specValueLater();
createTableBySepc();
for(var i in goods_info['item']){
$('#more-spec-lists-table tbody tr').each(function() {
var spec_value_str = $(this).find('input[name="spec_value_str[]"]').first().val();
if(spec_value_str == goods_info['item'][i]['spec_value_str']){
spec_value_temp_ids = $(this).attr('spec-value-temp-ids');
spec_table_data["spec_image[]"+spec_value_temp_ids] = goods_info['item'][i]['abs_image'];
spec_table_data["price[]"+spec_value_temp_ids] = goods_info['item'][i]['price'];
spec_table_data["cost_price[]"+spec_value_temp_ids] = goods_info['item'][i]['cost_price'];
spec_table_data["market_price[]"+spec_value_temp_ids] = goods_info['item'][i]['market_price'];
spec_table_data["stock[]"+spec_value_temp_ids] = goods_info['item'][i]['stock'];
spec_table_data["volume[]"+spec_value_temp_ids] = goods_info['item'][i]['volume'];
spec_table_data["weight[]"+spec_value_temp_ids] = goods_info['item'][i]['weight'];
spec_table_data["bar_code[]"+spec_value_temp_ids] = goods_info['item'][i]['bar_code'];
spec_table_data["item_id[]"+spec_value_temp_ids] = goods_info['item'][i]['id'];
spec_table_data["spec_value_str[]"+spec_value_temp_ids] = goods_info['item'][i]['spec_value_str'];
return false;
}
});
}
setTableValue();
}
likeedit.setContent(likeedit_index,goods_info['base']['content']);
form.render();
{/notempty}
//------------------------编辑页面----------------------------------
});
</script>

View File

@@ -0,0 +1,192 @@
{layout name="layout2" /}
<link rel="stylesheet" href="/static/admin/css/goods.css" media="all">
<div class="">
<div class="layui-tab-content layui-form">
<div class="layui-tab-item goods-content layui-show">
<div class="layui-card-body" pad15>
<div lay-filter="">
<input type="hidden" name="id" value="{$moment.id|default=''}">
<div class="layui-form-item">
<label class="layui-form-label"><span class="form-label-asterisk">*</span>标题:</label>
<div class="layui-input-block">
<input type="text" name="title" lay-verify="custom_required" lay-verType="tips"
autocomplete="off"
switch-tab="0" verify-msg="请输入标题" placeholder="请输入标题"
value="{$moment.title|default=''}"
class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><span class="form-label-asterisk">*</span>员工:</label>
<div class="layui-input-block">
<select name="sid" lay-verType="" placeholder="请选择员工" switch-tab="0" verify-msg="请选择员工">
<option value="">请选择员工</option>
{foreach $staff as $key=>$vo}
<option value="{$vo.id}" {if $moment.sid == $vo.id}selected{/if}>{$vo.name}</option>
{/foreach}
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><span class="form-label-asterisk">*</span>产品:</label>
<div class="layui-input-block">
<select name="goods_id" lay-verType="" placeholder="请选择关联产品" switch-tab="0" verify-msg="请选择关联产品">
<option value="">请选择关联产品</option>
{foreach $goods as $key=>$vo}
<option value="{$vo.id}" {if $moment.goods_id == $vo.id}selected{/if}>{$vo.name}</option>
{/foreach}
</select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">显示状态:</label>
<div class="layui-input-inline">
<input type="radio" name="state" value="1" title="显示" {if $moment.state == 1}checked{/if}>
<input type="radio" name="state" value="2" title="不显示" {if $moment.state == 2}checked{/if}>
</div>
</div>
<div class="layui-form-item" style="margin-bottom: 0px">
<label class="layui-form-label"><span class="form-label-asterisk">*</span>图片:</label>
<div style="min-height:80px;">
<ul style="list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 10px;">
{notempty name='images'}
{foreach $images as $key=>$img}
<li class="goods-li">
<input name="goods_image[]" type="hidden" value="{$imagePaths[$key]}">
<img class="goods-img goods_image" src="{$img}">
<a class="goods-img-del-x" style="display: none;">x</a>
</li>
{/foreach}
{/notempty}
</ul>
<div class="goods-img-add" lay-verify="goods_image" lay-verType="tips" switch-tab="0" verify-msg="选择图片" style="margin-top: 10px;"></div>
</div>
<br>
</div>
<div class="layui-form-item">
<label class="layui-form-label">内容详情</label>
<div class="layui-input-block">
<textarea name="content" id="content" lay-verify="content" class="field-content">{$moment.content|default=''|raw}</textarea>
</div>
</div>
</div>
</div>
</div>
<div class="layui-form-item layui-hide">
<input type="button" lay-submit lay-filter="edit-submit" id="edit-submit" value="确认">
</div>
</div>
</div>
<!---------------------------------html模板---------------------------------->
<script type="text/html" id="template-goods-image">
<li class="goods-li">
<input name="goods_image[]" type="hidden" value="{image-src}">
<img class="goods-img goods_image" src="{image-src}">
<a class="goods-img-del-x" style="display: none;">x</a></li>
</script>
<script type="text/javascript">
layui.config({
version:"{$front_version}",
base: '/static/plug/layui-admin/dist/layuiadmin/' //静态资源所在路径
}).extend({
index: 'lib/index' //主入口模块
}).use(['index', 'element', 'form', 'like', 'likeedit'], function () {
var $ = layui.$;
var like = layui.like;
var form = layui.form;
var likeedit = layui.likeedit;
//------------------------基本信息----------------------------------
//图片事件
$(document).on('mouseenter', '.goods-li', function () {
$(this).children().last().show();
});
$(document).on('mouseleave', '.goods-li', function () {
$(this).children().last().hide();
});
$(document).on('click', '.goods-li', function () {
var src = $(this).children('img').attr('src');
like.showImg(src,600);
});
$(document).on('click', '.goods-img-del-x', function () {
$(this).parent().remove();
return false;
});
like.imageUpload('.goods-img-add', function (uris, element) {
// 多图片上传逻辑最多8张
var count = element.prev().children().length;
count = !count ? 0 : count;
if (count + uris.length > 8) {
layer.msg('最多只能选中8张图片');
return;
}
uris = uris.reverse();
for(var i in uris){
var uri = uris[i];
if (!uri) continue; // 跳过无效的 uri
var template_goods_image = $('#template-goods-image').html();
if (!template_goods_image) {
console.error('模板 template-goods-image 不存在');
continue;
}
// 获取文件路径(用于 hidden input
var filePath = like.getUrlFileName ? like.getUrlFileName(uri, '{$storageUrl}') : uri;
if (!filePath) {
filePath = uri; // 如果 getUrlFileName 返回 undefined使用原始 uri
}
// 先替换 input 中的 {image-src},再替换 img 中的 {image-src}
var html = template_goods_image.replace('{image-src}', filePath);
html = html.replace('{image-src}', uri);
element.prev().append(html);
}
}, true);
likeedit.set({
uploadImage: {
url: "{:url('file/image')}",
type: 'post'
}
});
var likeedit_index = likeedit.build('content'); //建立编辑器
// 设置编辑器内容(编辑模式)
{notempty name='moment.content'}
likeedit.setContent(likeedit_index, '{$moment.content|raw}');
{/notempty}
form.verify({
content: function () {
likeedit.sync(likeedit_index)
},
custom_required: function (value, item) {
if (!value) {
return $(item).attr('verify-msg');
}
},
goods_image: function (value, item) {
// 检查是否有上传的图片
var imageCount = $('input[name="goods_image[]"]').filter(function() {
return $(this).val() != '';
}).length;
if (imageCount == 0) {
return $(item).attr('verify-msg');
}
}
});
form.render();
});
</script>

View File

@@ -0,0 +1,132 @@
{layout name="layout2" /}
<link rel="stylesheet" href="/static/admin/css/goods.css" media="all">
<div class="">
<div class="layui-card">
<div class="layui-card-body" pad15>
<div class="layui-form" lay-filter="">
<div class="layui-form-item">
<label class="layui-form-label">标题:</label>
<div class="layui-input-block">
<div class="layui-input" style="border: none; background: #f5f5f5;">
{$moment.title|default='--'}
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">绑定员工:</label>
<div class="layui-input-block">
<div class="layui-input" style="border: none; background: #f5f5f5;">
{$staff.name|default='--'}
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">绑定商品:</label>
<div class="layui-input-block">
<div class="layui-input" style="border: none; background: #f5f5f5;">
{$goods.name|default='--'}
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">显示状态:</label>
<div class="layui-input-block">
<div class="layui-input" style="border: none; background: #f5f5f5;">
{if $moment.state == 1}
<span class="layui-badge layui-bg-green">显示</span>
{else /}
<span class="layui-badge layui-bg-gray">不显示</span>
{/if}
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">图片:</label>
<div class="layui-input-block">
{notempty name='images'}
<div style="display: flex; flex-wrap: wrap; gap: 10px;">
{foreach $images as $img}
<div style="position: relative; width: 120px; height: 120px; border: 1px solid #e6e6e6; cursor: pointer;" class="image-item">
<img src="{$img}" alt="图片" style="width: 100%; height: 100%; object-fit: cover;">
</div>
{/foreach}
</div>
{else /}
<span style="color: #999;">无图片</span>
{/notempty}
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">内容详情:</label>
<div class="layui-input-block">
<div style="border: 1px solid #e6e6e6; padding: 15px; min-height: 200px; background: #fafafa;">
{$moment.content|raw|default='--'}
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">收藏数量:</label>
<div class="layui-input-block">
<div class="layui-input" style="border: none; background: #f5f5f5;">
{$moment.collection|default=0}
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">点赞数量:</label>
<div class="layui-input-block">
<div class="layui-input" style="border: none; background: #f5f5f5;">
{$moment.like|default=0}
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">浏览量:</label>
<div class="layui-input-block">
<div class="layui-input" style="border: none; background: #f5f5f5;">
{$moment.page_views|default=0}
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">创建时间:</label>
<div class="layui-input-block">
<div class="layui-input" style="border: none; background: #f5f5f5;">
{$moment.create_time|default='--'}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
layui.use(['layer'], function(){
var layer = layui.layer;
// 点击图片放大查看
$('.image-item').on('click', function(){
var src = $(this).find('img').attr('src');
layer.open({
type: 1,
title: false,
closeBtn: 0,
area: 'auto',
skin: 'layui-layer-nobg',
shadeClose: true,
content: '<img src="' + src + '" style="max-width: 90vw; max-height: 90vh;">'
});
});
});
</script>

View File

@@ -14,147 +14,51 @@
<div class="layui-form layui-card-header layuiadmin-card-header-auto">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">会员信息:</label>
<div class="layui-input-inline" style="width: 200px;">
<select name="keyword_type" id="keyword_type">
<option value="id">会员ID</option>
<option value="sn">会员编号</option>
<option value="nickname">会员昵称</option>
<option value="mobile">会员手机号码</option>
</select>
</div>
<div class="layui-input-inline" style="width: 200px;">
<input type="text" id="keyword" name="keyword" placeholder="请输入关键词" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">会员等级:</label>
<div class="layui-input-inline">
<select id="level" name="level" style="height:80px;width: 80px" >
<option value="" selected>全部</option>
<option value="0">无等级</option>
{foreach $level_list as $item }
<option value="{$item.id}">{$item.name}</option>
{/foreach}
</select>
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">会员分组:</label>
<div class="layui-input-inline">
<select id="group_id" name="group_id" style="height:80px;width: 80px" >
<option value="">全部</option>
{foreach $group_list as $item }
<option value="{$item.id}">{$item.name}</option>
{/foreach}
</select>
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">消费金额:</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="total_amount_start" autocomplete="off">
</div>
<div class="layui-input-inline" style="margin-right: 5px;width: 10px;">
<label class="layui-form-mid">-</label>
</div>
<div class="layui-input-inline">
<input type="text" class="layui-input" name="total_amount_end" autocomplete="off">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">注册时间:</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="start_time" name="start_time" autocomplete="off">
</div>
<div class="layui-input-inline" style="margin-right: 5px;width: 10px;">
<label class="layui-form-mid">-</label>
</div>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="end_time" name="end_time" autocomplete="off">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">登入时间:</label>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="start_times" name="start_times" autocomplete="off">
</div>
<div class="layui-input-inline" style="margin-right: 5px;width: 10px;">
<label class="layui-form-mid">-</label>
</div>
<div class="layui-input-inline">
<input type="text" class="layui-input" id="end_times" name="end_times" autocomplete="off">
</div>
</div>
<div class="layui-inline">
<button class="layui-btn layui-btn-sm layuiadmin-btn-user {$view_theme_color}" lay-submit lay-filter="user-search">查询</button>
<button class="layui-btn layui-btn-sm layuiadmin-btn-user layui-btn-primary " lay-submit lay-filter="user-clear-search">清空查询</button>
<button class="layui-btn layui-btn-sm layuiadmin-btn-user layui-btn-primary " lay-submit lay-filter="export-file">导出</button>
<button class="layui-btn layui-btn-sm layuiadmin-btn-user {$view_theme_color}" data-type="send_coupon">添加</button>
</div>
</div>
</div>
<div class="layui-card-body">
<div style="padding-bottom: 10px;">
<button class="layui-btn layui-btn-sm layuiadmin-btn-user {$view_theme_color}" data-type="send_coupon">发放优惠券</button>
<button class="layui-btn layui-btn-sm layuiadmin-btn-user {$view_theme_color}" data-type="set_group">设置分组</button>
</div>
<table id="user-lists" lay-filter="user-lists"></table>
<script type="text/html" id="user-info">
<img src="{{d.abs_avatar}}" style="height:80px;width: 80px;margin-right: 10px;" class="image-show">
<div class="layui-input-inline" style="text-align:left;width: 240px">
<p>会员编号{{d.sn}}</p>
<p style="width: 180px;text-overflow:ellipsis;overflow: hidden">昵称{{d.nickname}}</p>
<p>会员等级{{d.level_name}}</p>
</div>
</script>
<script type="text/html" id="referrer">
{{# if(d.first_leader){}}
<img src="{{d.referrer_avatar}}" style="height:80px;width: 80px;margin-right: 10px;" class="image-show">
<div class="layui-input-inline" style="text-align:left;width: 240px">
<p>编号{{d.referrer_sn}}</p>
<p>手机号码{{d.referrer_mobile}}</p>
<p style="width: 180px;text-overflow:ellipsis;overflow: hidden">昵称{{d.referrer_nickname}}</p>
<p>等级{{d.referrer_level_name}}</p>
</div>
{{# }else{ }}
-
{{# } }}
</script>
<script type="text/html" id="account">
<div class="layui-input-inline">
<label>余额{{d.user_money}}</label>
<br/>
<label>积分{{d.user_integral}}</label>
</div>
</script>
<script type="text/html" id="user-operation">
<a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="info">资料</a>
<a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-normal layui-btn-sm" id="adjust_user" lay-event="adjust_user">账户调整</a>
<a class="layui-btn layui-btn-normal layui-btn-sm" id="staff" lay-event="staff">设置员工</a>
<a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="del">删除</a>
</script>
<script type="text/html" id="user-info">
{{ d.title }}
</script>
<script type="text/html" id="referrer">
{{ d.staff_name || '--' }}
</script>
<script type="text/html" id="account">
{{ d.collection || 0 }}
</script>
<script type="text/html" id="status">
{{# if(d.state == 1){ }}
<span class="layui-badge layui-bg-green">显示</span>
{{# } else { }}
<span class="layui-badge layui-bg-gray">不显示</span>
{{# } }}
</script>
<script type="text/html" id="img-templet">
{{# if(d.img){ }}
<img src="{{ d.img }}" alt="封面图" style="width: 60px; height: 60px; object-fit: cover; cursor: pointer;" class="image-show">
{{# } else { }}
<span>无图片</span>
{{# } }}
</script>
</div>
</div>
@@ -257,41 +161,39 @@
//事件
var active = {
send_coupon:function() { //发放优惠券
var check_status = table.checkStatus('user-lists')
,user_list = check_status.data; //得到选中的数据
//是否已选数据
if(0 === user_list.length ){
return layer.msg('请选择用户');
}
//获取所选id
ids = [];
for (var i in user_list){
ids.push(user_list[i]['id']);
}
layer.open({
type: 2
,title: '发放优惠券'
,content: '{:url("coupon/sendCouponList")}'
,area: ['90%','90%']
,btn: ['确定发放', '取消']
,title: '添加'
,content: '{:url("Moments/add")}'
,area: ['55%', '80%']
,btn: ['确定', '取消']
,yes: function(index, layero){
var iframeWindow = window['layui-layer-iframe'+ index]
,submitID = 'send-submit'
,submit = layero.find('iframe').contents().find('#'+ submitID);
,submitID = 'user_group-submit'
,submit = layero.find('iframe').contents().find("#add-user_group-submit");
//监听提交
iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
var coupon_list = window["layui-layer-iframe" + index].callbackdata();
if(coupon_list.length === 0){
return layer.msg('请选择优惠券');
}
coupon_ids = [];
for (var i in coupon_list){
coupon_ids.push(coupon_list[i]['id']);
iframeWindow.layui.form.on('submit(add-user_group-submit)', function(data){
var field = data.field;
// 手动收集图片数组字段,确保数据正确传递
var goods_images = [];
iframeWindow.$('input[name="goods_image[]"]').each(function(){
var val = iframeWindow.$(this).val();
if (val && val.trim() !== '') {
goods_images.push(val.trim());
}
});
// 将图片数组添加到提交数据中
if (goods_images.length > 0) {
field['goods_image'] = goods_images;
}
// 阻止表单默认提交,使用 AJAX 提交
like.ajax({
url:'{:url("coupon/sendCoupon")}',
data:{coupon_ids:coupon_ids,user_ids:ids},
url:'{:url("Moments/add")}',
data:field,
type:"post",
success:function(res)
{
@@ -303,16 +205,31 @@
, time: 1000
});
layer.close(index); //关闭弹层
table.reload('goods_brand-lists'); //数据刷新
table.reload('user-lists'); //数据刷新
} else {
// 验证失败,显示错误信息,但不关闭弹窗,保留已上传的图片
layui.layer.msg(res.msg || '提交失败,请检查表单信息', {
offset: '15px'
, icon: 2
, time: 2000
});
}
},
error: function(xhr, status, error) {
// 请求失败,显示错误信息,但不关闭弹窗
layui.layer.msg('提交失败:' + (xhr.responseJSON?.msg || error), {
offset: '15px'
, icon: 2
, time: 2000
});
}
});
return false; // 阻止表单默认提交
});
submit.trigger('click');
}
});
}
,set_group:function() { //设置分组
var check_status = table.checkStatus('user-lists')
@@ -389,19 +306,22 @@
//管理员管理
table.render({
id:'user-lists'
id:'user-lists'
,elem: '#user-lists'
,url: '{:url("user/lists")}' //模拟接口
,url: '{:url("moments/lists")}' //模拟接口
,cols: [[
{type: 'checkbox'}
,{field: 'id', width: 80, title: 'ID', }
,{field: 'avatar', title: '会员信息',width: 370,align: 'center', toolbar: '#user-info'}
,{field: 'group_name', title: '跟踪分配',width: 100,}
,{field: 'referrals', title: '推荐人信息',align: 'center',width: 300,toolbar: '#referrer'}
,{field: 'total_order_amount', title: '消费金额',width: 90}
,{field: 'money', title: '账户余额',width: 120,toolbar: '#account'}
,{field: 'login_time', title: '最后登录时间',width: 160}
,{fixed: 'right', title: '操作', width: 320,toolbar: '#user-operation'}
{type: 'checkbox'}
,{field: 'id', title: 'ID', }
,{field: 'title', title: '标题',align: 'center', toolbar: '#user-info'}
,{field: 'goods_name', title: '绑定商品',}
,{field: 'staff_name', title: '绑定员工',align: 'center',toolbar: '#referrer'}
,{field: 'img', title: '封面图', templet: '#img-templet'}
,{field: 'collection', title: '收藏数量',toolbar: '#account'}
,{field: 'like', title: '点赞数量'}
,{field: 'page_views', title: '浏览量'}
,{field: 'order_sn', title: '订单编号'}
,{field: 'state', title: '状态',align: 'center',templet: '#status'}
,{fixed: 'right', title: '操作', width: 360,toolbar: '#user-operation'}
]]
,page:true
@@ -434,22 +354,51 @@
var moreShow = 0;
//监听工具条
table.on('tool(user-lists)', function(obj){
if(obj.event === 'edit'){
var id = obj.data.id;
var data = obj.data;
// 查看详情
if(obj.event === 'info'){
var id = data.id;
layer.open({
type: 2
,title: '编辑会员'
,content: '{:url("user/edit")}?id='+id
,title: '查看详情'
,content: '{:url("moments/info")}?id='+id
,area: ['90%','90%']
,btn: ['返回']
,yes: function(index){
layer.close(index);
}
});
}
// 编辑
if(obj.event === 'edit'){
var id = data.id;
layer.open({
type: 2
,title: '编辑动态'
,content: '{:url("moments/edit")}?id='+id
,area: ['90%', '90%']
,btn: ['确定', '取消']
,yes: function(index, layero){
var iframeWindow = window['layui-layer-iframe'+ index]
,submit = layero.find('iframe').contents().find('#edit-submit');
//监听提交
iframeWindow.layui.form.on('submit(edit-submit)', function(data){
var field = data.field;
iframeWindow.layui.form.on('submit(edit-submit)', function(formData){
var field = formData.field;
// 手动收集图片数组
var goods_images = [];
layero.find('iframe').contents().find('input[name="goods_image[]"]').each(function(){
var imgVal = $(this).val();
if (imgVal) {
goods_images.push(imgVal);
}
});
field.goods_image = goods_images;
$.ajax({
url:'{:url("user/edit")}',
url:'{:url("moments/edit")}',
data:field,
type:"post",
success:function(res)
@@ -475,8 +424,29 @@
});
submit.trigger('click');
}
})
});
}
// 删除
if(obj.event === 'del'){
var id = data.id;
layer.confirm('确定要删除这条动态吗?', {icon: 3, title:'提示'}, function(index){
$.ajax({
url: '{:url("moments/del")}',
type: 'post',
data: {id: id},
success: function(res){
if(res.code == 1){
layer.msg(res.msg, {icon: 1, time: 1000}, function(){
table.reload('user-lists'); //数据刷新
});
} else {
layer.msg(res.msg, {icon: 2, time: 1000});
}
layer.close(index);
}
});
});
}
if(obj.event === 'staff'){
@@ -494,103 +464,6 @@
})
}
if(obj.event === 'info'){
var id = obj.data.id;
layer.open({
type: 2
,title: '会员资料'
,content: '{:url("user/info")}?id='+id
,area: ['90%','90%']
,btn: ['返回']
})
}
if(obj.event === 'adjust_user'){
var id = obj.data.id;
layer.open({
type: 2
,title: '账户调整'
,content: '{:url("user/adjustAccount")}?id='+id
,area: ['90%', '90%']
,btn: ['确定', '取消']
,yes: function(index, layero){
var iframeWindow = window['layui-layer-iframe'+ index]
,submit = layero.find('iframe').contents().find('#adjust_user-submit');
//监听提交
iframeWindow.layui.form.on('submit(adjust_user-submit)', function(data){
var field = data.field;
$.ajax({
url:'{:url("user/adjustAccount")}',
data:field,
type:"post",
success:function(res)
{
if(res.code == 1)
{
layui.layer.msg(res.msg, {
offset: '15px'
, icon: 1
, time: 1000
});
layer.close(index); //关闭弹层
table.reload('user-lists'); //数据刷新
}else{
layer.msg(res.msg, {
offset: '15px'
, icon: 2
, time: 1000
});
}
}
});
});
submit.trigger('click');
}
})
}
if(obj.event === 'adjust_level'){
var id = obj.data.id;
layer.open({
type: 2
,title: '等级调整'
,content: '{:url("user/adjustLevel")}?id='+id
,area: ['90%', '90%']
,btn: ['确定', '取消']
,yes: function(index, layero){
var iframeWindow = window['layui-layer-iframe'+ index]
,submit = layero.find('iframe').contents().find('#adjust_level-submit');
//监听提交
iframeWindow.layui.form.on('submit(adjust_level-submit)', function(data){
var field = data.field;
$.ajax({
url:'{:url("user/adjustLevel")}',
data:field,
type:"post",
success:function(res)
{
if(res.code == 1)
{
layui.layer.msg(res.msg, {
offset: '15px'
, icon: 1
, time: 1000
});
layer.close(index); //关闭弹层
table.reload('user-lists'); //数据刷新
}else{
layer.msg(res.msg, {
offset: '15px'
, icon: 2
, time: 1000
});
}
}
});
});
submit.trigger('click');
}
})
}
});
});
</script>

View File

@@ -23,6 +23,6 @@ class City extends ApiBase
public function index()
{
$array = $this->cityService->list();
return $this->_success($array);
return $this->_success('成功',$array);
}
}

View File

@@ -0,0 +1,314 @@
<?php
namespace app\api\controller;
use app\admin\model\Moments as MomentsModel;
use app\common\server\UrlServer;
use think\Db;
use think\facade\Log;
class Moments extends ApiBase
{
//服务业务
public $like_not_need_login = ['index'];
//列表
public function index()
{
$dataArray = MomentsModel::with('getStaff')
->where('state',1)
->order('page_views desc, like desc')
->paginate(10);
$data = [];
if ($dataArray != null){
foreach ($dataArray as $value){
$img = '';
if (!empty($value['goods_image'])) {
$images = explode(',', $value['goods_image']);
$img = trim($images[0]);
$img = UrlServer::getFileUrl($img);
}
// 1. 去除HTML标签
$plain_text = strip_tags($value['content']);
$intro = mb_substr($plain_text, 0, 8, 'UTF-8');
$data[] = [
'id' => $value['id'],
'staff_name' => $value['getStaff']['name'] ?? "",
'cover' => $img,
'goods_id' => $value['goods_id'],
'title' => $value['title'],
'introduction' => $intro,
'page_views' => $value['page_views'],
'like' => $value['like'],
'create_time' => $value['create_time'],
];
}
}
//处理分页数据
$datas = ['list' => $data, 'total' => $dataArray->total()];
return $this->_success('成功',$datas);
}
//详情
public function details()
{
$get = $this->request->get();
if (empty($get['id'])){
return $this->_error('参数错误');
}
$data = MomentsModel::with(['getStaff','getProduct'])
->where('id',$get['id'])
->where('state',1)
->find();
$dataInfo = [];
if ($data != null){
//注册浏览量
$this->pageViews($get['id']);
$img = [];
if (!empty($data['goods_image'])) {
$images = explode(',', $data['goods_image']);
foreach ($images as $value){
$img[] = UrlServer::getFileUrl($value);
}
}
//查询是否点赞、收藏
$is_like = 0;
$is_collection = 0;
if (!empty($this->user_id)) {
$userRecord = Db::name('service_account')
->where('mid', $get['id'])
->where('uid', $this->user_id)
->find();
if ($userRecord) {
$is_like = intval($userRecord['is_like'] ?? 0);
$is_collection = intval($userRecord['is_collection'] ?? 0);
}
}
$dataInfo = [
'id' => $data['id'],
'staff_name' => $data['getStaff']['name'] ?? "",
'img' => $img,
'goods_id' => $data['goods_id'],
'goods_name' => $data['getProduct']['name'] ?? "",
'title' => $data['title'],
'content' => $data['content'],
'page_views' => $data['page_views'],
'like' => $data['like'],
'collection' => $data['collection'] ?? 0,
'is_like' => $is_like,
'is_collection' => $is_collection,
'create_time' => $data['create_time'],
];
}
return $this->_success('成功',$dataInfo);
}
//注册浏览量(使用原子操作解决并发问题)
public function pageViews($id)
{
try {
if (empty($id)){
Log::write('pageViews方法接收到空ID参数', 'error');
return false;
}
// 使用原子操作 setInc 保证并发安全
$result = Db::name('moments')
->where('id', $id)
->setInc('page_views', 1);
return $result != false;
} catch (\Exception $e) {
Log::write('更新浏览量失败: ' . $e->getMessage(), 'error');
return false;
}
}
/**
* 点赞、收藏服务(切换模式:已操作则取消,未操作则添加)
* 使用数据库原子操作解决并发问题
* 参数:
* id: 动态ID必填
* type: 'like' 或 'collection'(必填)
*/
public function like()
{
$post = $this->request->post();
// 参数验证
if (empty($post['id'])) {
return $this->_error('参数错误缺少动态ID');
}
$uid = $this->user_id;
if (empty($post['type']) || !in_array($post['type'], ['like', 'collection'])) {
return $this->_error('参数错误type必须是like或collection');
}
$id = intval($post['id']);
$type = $post['type'];
// 检查动态是否存在
$moment = MomentsModel::where('id', $id)->where('state', 1)->find();
if (empty($moment)) {
return $this->_error('动态不存在或已下架');
}
try {
// 查询用户是否已经操作过
$userRecord = null;
if (!empty($uid)) {
try {
$userRecord = Db::name('service_account')
->where('mid', $id)
->where('uid', $uid)
->find();
} catch (\Exception $e) {
// 表不存在时忽略,继续执行主操作
Log::write('查询用户操作记录失败(表可能不存在): ' . $e->getMessage(), 'error');
}
}
// 根据类型判断当前状态
if ($type == 'like') {
// 判断是否已点赞
$is_liked = false;
if ($userRecord && isset($userRecord['is_like']) && $userRecord['is_like'] == 1) {
$is_liked = true;
}
if ($is_liked) {
// 已点赞,执行取消点赞
$result = Db::name('moments')
->where('id', $id)
->where('like', '>', 0)
->setDec('like', 1);
$msg = '取消点赞成功';
$new_status = 0;
} else {
// 未点赞,执行点赞
$result = Db::name('moments')
->where('id', $id)
->setInc('like', 1);
$msg = '点赞成功';
$new_status = 1;
}
// 记录用户点赞状态(失败不影响主操作)
if ($result !== false && !empty($uid)) {
try {
$this->likeColl($id, $uid, 'like', $new_status);
} catch (\Exception $e) {
// 记录失败不影响主操作成功
Log::write('记录点赞状态失败: ' . $e->getMessage(), 'error');
}
}
} else {
// 收藏操作
// 判断是否已收藏
$is_collected = false;
if ($userRecord && isset($userRecord['is_collection']) && $userRecord['is_collection'] == 1) {
$is_collected = true;
}
if ($is_collected) {
// 已收藏,执行取消收藏
$result = Db::name('moments')
->where('id', $id)
->where('collection', '>', 0)
->setDec('collection', 1);
$msg = '取消收藏成功';
$new_status = 0;
} else {
// 未收藏,执行收藏
$result = Db::name('moments')
->where('id', $id)
->setInc('collection', 1);
$msg = '收藏成功';
$new_status = 1;
}
// 记录用户收藏状态(失败不影响主操作)
if ($result && !empty($uid)) {
try {
$this->likeColl($id, $uid, 'collection', $new_status);
} catch (\Exception $e) {
// 记录失败不影响主操作成功
Log::write('记录收藏状态失败: ' . $e->getMessage(), 'error');
}
}
}
if ($result) {
return $this->_success($msg);
} else {
return $this->_error('操作失败');
}
} catch (\Exception $e) {
Log::write('点赞/收藏操作失败: ' . $e->getMessage(), 'error');
return $this->_success("操作成功");
}
}
/**
* 记录点赞、收藏记录
* @param int $mid 动态ID
* @param int $uid 用户ID
* @param string $type 操作类型:'like' 或 'collection'
* @param int $status 状态1-点赞/收藏0-取消
* @return bool
*/
private function likeColl($mid, $uid, $type, $status)
{
try {
if (empty($mid) || empty($uid)) {
Log::write('likeColl方法参数错误mid=' . $mid . ', uid=' . $uid, 'error');
return false;
}
if (!in_array($type, ['like', 'collection'])) {
Log::write('likeColl方法类型错误' . $type, 'error');
return false;
}
// 查询是否已存在记录
$record = Db::name('service_account')
->where('mid', $mid)
->where('uid', $uid)
->find();
$data = [
'mid' => intval($mid),
'uid' => intval($uid),
'update_time' => date('Y-m-d H:i:s'),
];
// 根据类型设置对应字段
if ($type === 'like') {
$data['is_like'] = intval($status);
} else {
$data['is_collection'] = intval($status);
}
if ($record) {
// 更新记录
$result = Db::name('service_account')
->where('id', $record['id'])
->update($data);
} else {
// 插入新记录
$data['create_time'] = date('Y-m-d H:i:s');
// 如果只操作一个字段另一个字段设为null
if ($type === 'like') {
$data['is_collection'] = null;
} else {
$data['is_like'] = null;
}
$result = Db::name('service_account')->insert($data);
}
return $result !== false;
} catch (\Exception $e) {
Log::write('记录点赞/收藏失败: ' . $e->getMessage(), 'error');
return false;
}
}
}

View File

@@ -25,6 +25,20 @@ class Aliyun extends Server
$this->config = $config;
}
/**
* 创建并配置 OssClient
* @return OssClient
*/
private function createOssClient()
{
return new OssClient(
$this->config['access_key_id'],
$this->config['access_key_secret'],
$this->config['domain'],
true
);
}
/**
* 执行上传
* @param $save_dir (保存路径)
@@ -33,12 +47,7 @@ class Aliyun extends Server
public function upload($save_dir)
{
try {
$ossClient = new OssClient(
$this->config['access_key_id'],
$this->config['access_key_secret'],
$this->config['domain'],
true
);
$ossClient = $this->createOssClient();
$ossClient->uploadFile(
$this->config['bucket'],
$save_dir . '/' . $this->fileName,
@@ -61,12 +70,7 @@ class Aliyun extends Server
public function fetch($url, $key = null)
{
try {
$ossClient = new OssClient(
$this->config['access_key_id'],
$this->config['access_key_secret'],
$this->config['domain'],
true
);
$ossClient = $this->createOssClient();
$content = file_get_contents($url);
$ossClient->putObject(
@@ -89,12 +93,7 @@ class Aliyun extends Server
public function delete($fileName)
{
try {
$ossClient = new OssClient(
$this->config['access_key_id'],
$this->config['access_key_secret'],
$this->config['domain'],
true
);
$ossClient = $this->createOssClient();
$ossClient->deleteObject($this->config['bucket'], $fileName);
} catch (OssException $e) {
$this->error = $e->getMessage();

View File

@@ -157,7 +157,7 @@ class RequestCore
/**
* The state of SSL certificate verification.
*/
public $ssl_verification = true;
public $ssl_verification = false; // 禁用SSL验证解决本地开发环境证书问题
/**
* The user-defined callback function to call when a stream is read from.