Files
yusheng-php/application/api/model/RoomEmoji.php

78 lines
2.3 KiB
PHP

<?php
namespace app\api\model;
use app\common\controller\Push;
use think\Cache;
use think\Db;
use think\Model;
class RoomEmoji extends Model
{
protected $table = 'vs_room_emoji';
protected $table_type = 'vs_room_emoji_type';
private $redis;
public function __construct()
{
parent::__construct();
$this->redis = \think\Cache::store('redis')->handler();
}
//获取房间表情包类型
public function getRoomEmojiType(){
$list = db($this->table_type)->field('id,type_name')->where(['status'=>1,'deletetime'=>0])->select();
return ['code' => 1, 'msg' => '创建成功', 'data' => $list];
}
//获取房间表情包
public function getRoomEmoji($type_id=0,$pid=0, $isTree = false, $user_id = 0){
$where = [];
if($type_id){
$where['type_id'] = $type_id;
}
if($pid!=""){
$where['pid'] = $pid;
}
$list = db($this->table)
->field('id,pid,type_id,name,image,animate_image')
->where(['status'=>1,'deletetime'=>0])
->where($where)
->order('sort desc,id desc')
->select();
$is_lock = 0;
if ($type_id == 3) { // 专属礼物
$Nobility = model('api/Nobility')->getUserNobilityInfo($user_id);
$power_list_ids = array_column($Nobility['power_list'],'id');
if(!in_array(9,$power_list_ids)){
$is_lock = 1;
}
}
foreach ($list as &$v) {
$v['is_lock'] = $is_lock;
}
if ($isTree && !empty($list) && empty($pid)) {
$list = $this->buildTree($list);
}
return ['code' => 1, 'msg' => '成功', 'data' => $list];
}
/**
* 将扁平数据转换为树状结构
* @param array $data 数据列表
* @param int $pid 父级ID
* @return array
*/
private function buildTree($data, $pid = 0)
{
$tree = [];
foreach ($data as $item) {
if ($item['pid'] == $pid) {
$children = $this->buildTree($data, $item['id']);
if (!empty($children)) {
$item['children'] = $children;
}
$tree[] = $item;
}
}
return $tree;
}
}