152 lines
6.5 KiB
PHP
152 lines
6.5 KiB
PHP
<?php
|
|
|
|
namespace app\api\model;
|
|
|
|
use think\Db;
|
|
use think\Model;
|
|
|
|
class UserTopLine extends Model
|
|
{
|
|
//获取最大头条输入金额
|
|
public function get_top_line_integral($uid)
|
|
{
|
|
$key_title_arr = ['top_line_min_price', 'top_line_step_price', 'top_line_sustain_time','top_line_reset_price_time'];
|
|
$config = Db::name('config')->where('key_title', 'in', $key_title_arr)->column('key_value', 'key_title');
|
|
$map = [];
|
|
$info = Db::name('user_top_line')->where($map)->order('add_time', 'desc')->find();
|
|
$pay_integral = $config['top_line_min_price'];
|
|
if(!empty($info)) {
|
|
if($info['add_time'] + $config['top_line_reset_price_time'] > time()) {
|
|
$pay_integral = $info['pay_integral'] + $config['top_line_step_price'];
|
|
}
|
|
}
|
|
$data = [
|
|
'pay_integral' => $pay_integral,
|
|
'step_integral' => $config['top_line_step_price']
|
|
];
|
|
return ['code' => 200, 'msg' => '获取数据成功', 'data' => $data];
|
|
}
|
|
//发布头条
|
|
public function publish_top_line($uid, $pay_integral, $content, $rid)
|
|
{
|
|
//维禁判断
|
|
if(!empty($content)) {
|
|
$result = model('Tencent')->content_moderation('Text', $content);
|
|
if($result['code'] != 200) {
|
|
return ['code' => 201, 'msg' => '内容存在违禁词!', 'data' => null];
|
|
}
|
|
}
|
|
|
|
$user_info = Db::name('user')->field('uid,integral,contribution_level')->where('uid', $uid)->find();
|
|
if(empty($user_info)) {
|
|
return ['code' => 201, 'msg' => '用户信息不存在', 'data' => null];
|
|
}
|
|
if($pay_integral <= 0) {
|
|
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
|
|
}
|
|
if($user_info['integral'] < $pay_integral) {
|
|
return ['code' => 201, 'msg' => '钻石不足', 'data' => null];
|
|
}
|
|
|
|
//持续时间
|
|
$key_title_arr = ['top_line_min_price', 'top_line_step_price', 'top_line_sustain_time','top_line_reset_price_time'];
|
|
$config = Db::name('config')->where('key_title', 'in', $key_title_arr)->column('key_value', 'key_title');
|
|
|
|
$last_user_top_line_info = Db::name('user_top_line')->order('add_time', 'desc')->find();
|
|
if($last_user_top_line_info) {
|
|
if(($last_user_top_line_info['add_time'] + $config['top_line_reset_price_time']) >= time()) {
|
|
if($pay_integral <= $last_user_top_line_info['pay_integral']) {
|
|
return ['code' => 201, 'msg' => '抢头条金额不能小于上一次', 'data' => null];
|
|
}
|
|
} else {
|
|
$pay_integral = $config['top_line_min_price'];
|
|
}
|
|
}
|
|
$top_line_sustain_time = $config['top_line_sustain_time'];
|
|
Db::startTrans();
|
|
try {
|
|
$reslut = model('admin/User')->change_user_money_by_uid($uid, -$pay_integral, 2, 52, "抢头条", $uid, 0);
|
|
if ($reslut['code'] != 200) {
|
|
Db::rollback();
|
|
return ['code' => 201, 'msg' => $reslut['msg'], 'data' => null];
|
|
}
|
|
$data = [
|
|
'uid' => $uid,
|
|
'pay_integral' => $pay_integral,
|
|
'base64_content' => base64_encode($content),
|
|
'over_time' => time() + $top_line_sustain_time,
|
|
'add_time' => time(),
|
|
'update_time' => time(),
|
|
'rid' => $rid
|
|
];
|
|
Db::name('user_top_line')->insert($data);
|
|
|
|
Db::commit();
|
|
$list = $this->_get_user_top_line_list();
|
|
$push_data = [];
|
|
$push_data['code'] = 323;
|
|
$push_data['msg'] = "抢头条";
|
|
$push_data['data'] = $list;
|
|
model('api/WebSocketPush')->send_to_all($push_data);
|
|
return ['code' => 200, 'msg' => '开启成功', 'data' => null];
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
// 回滚事务
|
|
dump($e);
|
|
return ['code' => 201, 'msg' => '开启失败', 'data' => null];
|
|
}
|
|
}
|
|
|
|
//获取表白墙列表
|
|
public function get_user_top_line_list($page, $page_limit)
|
|
{
|
|
$list = $this->_get_user_top_line_list();
|
|
return ['code' => 200, 'msg' => '获取数据成功', 'data' => $list];
|
|
|
|
}
|
|
|
|
private function _get_user_top_line_list()
|
|
{
|
|
$map = [];
|
|
$map[] = ['a.over_time', '>=', time()];
|
|
$info = Db::name('user_top_line')->alias('a')
|
|
->join('user b', 'a.uid = b.uid', 'left')
|
|
->where($map)
|
|
->field('a.base64_content,a.uid,b.base64_nick_name,b.head_pic,b.sex,a.rid,a.pay_integral,a.over_time')
|
|
->order('id', 'desc')->find();
|
|
if(empty($info)) {
|
|
$info['uid'] = 0;
|
|
$info['content'] = '';
|
|
$info['head_pic'] = '';
|
|
$info['over_time'] = 0;
|
|
$info['nick_name'] = '';
|
|
$info['pay_integral'] = '';
|
|
} else {
|
|
$info['nick_name'] = mb_convert_encoding(base64_decode($info['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
$info['head_pic'] = localpath_to_netpath($info['head_pic']);
|
|
$info['content'] = mb_convert_encoding(base64_decode($info['base64_content']), 'UTF-8', 'UTF-8');
|
|
$info['over_time'] = $info['over_time'] - time();
|
|
}
|
|
// $level_list = Db::name('user_level')->field('image,type,level')->where(['is_delete' => 1])->select();
|
|
// $charm_level_arr = [];
|
|
// $contribution_level_arr = [];
|
|
// foreach($level_list as $val) {
|
|
// $image = localpath_to_netpath($val['image']);
|
|
// $level = $val['level'];
|
|
// $type = $val['type'];
|
|
// if($type == 1) {
|
|
// $charm_level_arr[$level] = $image;
|
|
// }elseif($type == 2) {
|
|
// $contribution_level_arr[$level] = $image;
|
|
// }
|
|
// }
|
|
// foreach($list as &$val) {
|
|
// $val['nick_name'] = mb_convert_encoding(base64_decode($val['base64_nick_name']), 'UTF-8', 'UTF-8');
|
|
// $val['head_pic'] = localpath_to_netpath($val['head_pic']);
|
|
// $val['charm_level_image'] = $charm_level_arr[$val['charm_level']] ?? '';
|
|
// $val['contribution_level_image'] = $contribution_level_arr[$val['contribution_level']] ?? '';
|
|
// $val['content'] = mb_convert_encoding(base64_decode($val['base64_content']), 'UTF-8', 'UTF-8');
|
|
// }
|
|
return $info;
|
|
}
|
|
} |