后台:
添加服务逻辑的编辑 接口: 添加服务列表 添加服务详情 添加服务点赞、收藏
This commit is contained in:
@@ -23,6 +23,6 @@ class City extends ApiBase
|
||||
public function index()
|
||||
{
|
||||
$array = $this->cityService->list();
|
||||
return $this->_success($array);
|
||||
return $this->_success('成功',$array);
|
||||
}
|
||||
}
|
||||
314
application/api/controller/Moments.php
Normal file
314
application/api/controller/Moments.php
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user