Files
mier-php/application/api/model/UserCollectRoom.php
2025-08-11 10:22:05 +08:00

95 lines
3.2 KiB
PHP

<?php
namespace app\api\model;
use think\DB;
use think\Model;
class UserCollectRoom extends Model
{
public function is_collect_room($uid, $rid)
{
$map = [];
$map[] = ['uid', '=', $uid];
$map[] = ['rid', '=', $rid];
$reslut = db::name('user_collect_room')->where($map)->find();
if ($reslut) {
return ['code' => 200, 'msg' => '房间已收藏', 'data' => null];
} else {
return ['code' => 201, 'msg' => '房间未收藏', 'data' => null];
}
}
public function collect_room($uid, $rid)
{
$map = [];
$map[] = ['rid', '=', $rid];
$map[] = ['is_delete', '=', 1];
$room_info = db::name('room')->field('rid,tid,room_micro,room_owner_uid,room_admin_uid')->where($map)->find();
if (empty($room_info)) {
return ['code' => 201, 'msg' => '房间信息不存在', 'data' => null];
}
Db::startTrans();
try {
$data = [];
$data['uid'] = $uid;
$data['rid'] = $rid;
$data['add_time'] = time();
$data['update_time'] = time();
$reslut = db::name('user_collect_room')->insert($data);
if (!$reslut) {
Db::rollback();
return ['code' => 201, 'msg' => '收藏失败', 'data' => null];
}
$map = [];
$map['rid'] = $rid;
$reslut = db::name('room')->where($map)->setInc('collect_num', 1);
if (!$reslut) {
Db::rollback();
return ['code' => 201, 'msg' => '收藏失败', 'data' => null];
}
Db::commit();
return ['code' => 200, 'msg' => "收藏成功", 'data' => null];
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return ['code' => 201, 'msg' => "请重试", 'data' => null];
}
}
//取消收藏
public function un_collect_room($uid, $rid)
{
$map = [];
$map[] = ['rid', '=', $rid];
$map[] = ['is_delete', '=', 1];
$room_info = db::name('room')->field('rid,tid,room_micro,room_owner_uid,room_admin_uid')->where($map)->find();
if (empty($room_info)) {
return ['code' => 201, 'msg' => '房间信息不存在', 'data' => null];
}
Db::startTrans();
try {
$map = [];
$map[] = ['uid', '=', $uid];
$map[] = ['rid', '=', $rid];
$reslut = db::name('user_collect_room')->where($map)->delete();
if (!$reslut) {
Db::rollback();
return ['code' => 201, 'msg' => '收藏失败', 'data' => null];
}
$map = [];
$map['rid'] = $rid;
$reslut = db::name('room')->where($map)->setDec('collect_num', 1);
if (!$reslut) {
Db::rollback();
return ['code' => 201, 'msg' => '收藏失败', 'data' => null];
}
Db::commit();
return ['code' => 200, 'msg' => "收藏成功", 'data' => null];
} catch (\Exception $e) {
// 回滚事务
Db::rollback();
return ['code' => 201, 'msg' => "请重试", 'data' => null];
}
}
}