You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
415 lines
19 KiB
415 lines
19 KiB
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\controller\Api;
|
|
use think\Validate;
|
|
use app\admin\model\usergroup\Info;
|
|
use app\admin\model\Usergroup as UG;
|
|
use app\admin\model\Order;
|
|
use app\admin\model\Group;
|
|
use app\admin\model\User;
|
|
use think\Db;
|
|
use think\Exception;
|
|
|
|
class Usergroup extends Api{
|
|
|
|
protected $model = null;
|
|
|
|
protected $noNeedRight = '*';
|
|
protected $noNeedLogin = ['getlist','getgroupdetail','getMyingGroup'];
|
|
|
|
|
|
|
|
/**
|
|
* 初始化函数
|
|
* 该函数的目的是在类实例化时进行一些初始化操作
|
|
* 主要包括调用父类的初始化方法以及设置本类操作的模型对象
|
|
*/
|
|
public function _initialize(){
|
|
// 调用父类的_initialize方法,以执行父类的初始化逻辑
|
|
parent::_initialize();
|
|
// 实例化UG模型对象,并将其赋值给当前类的model属性,以便后续操作使用
|
|
$this->model = new UG;
|
|
}
|
|
/**
|
|
* 获取列表数据
|
|
* 本函数用于处理请求,根据提供的筛选条件、排序规则、偏移量和限制数量,
|
|
* 从数据库中获取相应列表,并返回成功获取的数据
|
|
*/
|
|
public function getlist(){
|
|
// 接收并检查是否为POST请求
|
|
$data = $this->request->post();
|
|
if($this->request->post()){
|
|
// 检查是否存在token且不为空,用于身份验证
|
|
$uid = isset($data['token'])&&!empty($data['token'])?$this->auth->id: 0;
|
|
// 解析过滤条件,从请求数据中获取并解码
|
|
$where = [];
|
|
if(isset($data['filter'])&&$data['filter']){
|
|
$where = json_decode(htmlspecialchars_decode($data['filter']),true);
|
|
}
|
|
// 解析排序规则,从请求数据中获取并解码
|
|
$order = [];
|
|
// 获取分页偏移量
|
|
$offset = $data['offset'];
|
|
// 获取分页限制数量
|
|
$limit = $data['limit'];
|
|
// 调用模型的getlist方法,根据条件、排序、偏移量和限制数量获取列表数据
|
|
$list = $this->model->getlist($where, $order, $offset, $limit,$uid);
|
|
// 返回成功消息和获取的列表数据
|
|
return $this->success('获取成功',$list);
|
|
}
|
|
return $this->error('非法请求');
|
|
|
|
}
|
|
/**
|
|
* 创建组局
|
|
*
|
|
* @return mixed 组局发布结果的响应
|
|
*/
|
|
public function createGroup(){
|
|
// 获取用户提交的组局信息
|
|
$data = $this->request->post();
|
|
|
|
// 定义组局信息的验证规则
|
|
$rule = [
|
|
'title' => 'require', // 活动主题必填
|
|
'content' => 'require', // 组局主题描述必填
|
|
'tag' => 'require', // 玩法标签必选
|
|
'start_time' => 'require', // 开始时间必选
|
|
'end_time' => 'require', // 结束时间必选
|
|
'active_address' => 'require', // 活动地点必选
|
|
'people' => 'number|between:2,100', // 参与人数必须是数字且在2到100人之间
|
|
'pay_price' => 'require', // 付费价格必填
|
|
"address_lat" => 'require', // 活动地点纬度必填
|
|
"address_lng" => 'require' // 活动地点经度必填
|
|
];
|
|
|
|
// 定义验证规则对应的错误信息
|
|
$msg = [
|
|
'title.require' => '填写你想组局的活动主题',
|
|
'content.require' => '请添加组局的主题描述',
|
|
'tag.require' => '请选择一个玩法标签',
|
|
'start_time.require' => '请选择开始时间',
|
|
'end_time.require' => '请选择结束时间',
|
|
'active_address.require' => '请选择活动地点',
|
|
'people.require' => '人数要求最少为2人',
|
|
'people.between' => '人数要求最少为2人',
|
|
'address_lat.require' => '活动地点获取失败',
|
|
'address_lng.require' => '活动地点获取失败',
|
|
];
|
|
|
|
// 实例化验证器并应用规则和错误信息
|
|
$validate = new Validate($rule, $msg);
|
|
|
|
// 检查提交的数据是否通过验证
|
|
if(!$validate->check($data)){
|
|
// 如果验证失败,返回错误信息
|
|
return $this->error($validate->getError());
|
|
}
|
|
//检测文字是否含有违禁词
|
|
$min_user = new \app\admin\model\miniprogram\User;
|
|
$min_user_info = $min_user->getbyUser_id($this->auth->id);
|
|
$openid = $min_user_info['openid'];
|
|
|
|
$check = WechatService::security(['openid'=>$openid,'content'=>$data['content']]);
|
|
$cd = $check['result'];
|
|
if($cd['label']!=100) return $this->error("内容含涉嫌违规,请修改后再发布");
|
|
|
|
$check = WechatService::security(['openid'=>$openid,'content'=>$data['title']]);
|
|
$cd = $check['result'];
|
|
if($cd['label']!=100) return $this->error("内容含涉嫌违规,请修改后再发布");
|
|
|
|
// 添加发布者的用户ID和发布时间到数据中
|
|
$data['uid'] = $this->auth->id;
|
|
$data['created'] = time();
|
|
$data['start_time'] = strtotime(date('Y',time())."-".$data['start_time']);
|
|
$data['end_time'] = strtotime(date('Y',time())."-".$data['end_time']);
|
|
// 移除数据中的令牌信息,防止插入数据库
|
|
unset($data['token']);
|
|
|
|
// 尝试插入数据到数据库并获取插入的ID
|
|
if(!$this->model->insertGetId($data)) {
|
|
// 如果插入失败,返回错误信息
|
|
return $this->error("组局发布失败");
|
|
}
|
|
|
|
// 如果一切正常,返回成功信息
|
|
return $this->success("组局发布成功");
|
|
}
|
|
/**
|
|
* 用户加入组局功能
|
|
*
|
|
* @return mixed 返回操作结果,成功或错误信息
|
|
*/
|
|
public function usecreateGroup(){
|
|
// 获取POST请求的数据
|
|
$data = $this->request->post();
|
|
// 检查是否有POST数据提交
|
|
if($this->request->post()){
|
|
// 获取当前用户ID
|
|
$uid = $this->auth->id;
|
|
$nickname = $this->auth->nickname;
|
|
// 获取提交的组局ID
|
|
$id = $data['id'];
|
|
// 查询组局信息,仅查询状态为0或1的组局
|
|
$groupinfo = $this->model->whereIn("status",[0,1])->where('is_del',0)->find($id);
|
|
// 如果找不到组局信息,返回错误提示
|
|
if(!$groupinfo) return $this->error("组局不存在");
|
|
// 判断当前组局是否人数已满
|
|
if($groupinfo->ing_people>=$groupinfo->people){
|
|
return $this->error("人数已满");
|
|
}
|
|
// 判断当前组局是否是自己的,如果是,返回错误提示
|
|
if($groupinfo->uid==$uid){
|
|
return $this->error("不能加入自己的组局哦");
|
|
}
|
|
// 查询用户是否已经加入该组局
|
|
$UGinfo = Info::where("uid",$uid)->where("ugid",$id)->where('status',0)->find();
|
|
// 如果用户已经加入,返回错误提示
|
|
if($UGinfo) return $this->error("已加入改组局");
|
|
// 开启数据库事务
|
|
DB::startTrans();
|
|
try {
|
|
// 准备插入用户组局信息的数据
|
|
$insert = [];
|
|
$insert['ugid'] = $id;
|
|
$insert['uid'] = $uid;
|
|
$insert['create'] = time();
|
|
// 更新组局信息,增加参与人数
|
|
$groupinfo->ing_people = $groupinfo->ing_people + 1;
|
|
// 如果人数达到设定值,更新组局状态为已满
|
|
if($groupinfo->ing_people = $groupinfo->people) $groupinfo->status = 2;
|
|
// 如果成功更新组局信息和插入用户组局信息,提交事务并返回成功提示
|
|
if($groupinfo->save()&&Info::insert($insert)){
|
|
//加入组局消息记录
|
|
$minfo = [
|
|
'uid'=>$groupinfo->uid,
|
|
'error'=>1,
|
|
'create'=>time(),
|
|
'type'=>"system_group",
|
|
'content'=>$groupinfo->title." 开始时间:".$groupinfo->start_time_text,
|
|
'title'=>"{$nickname}加入组局",
|
|
'is_read'=>0
|
|
];
|
|
$message = new \app\admin\model\system\Message;
|
|
$message->addmessage($minfo);
|
|
DB::commit();
|
|
return $this->success("加入成功");
|
|
}
|
|
|
|
throw new Exception("操作繁忙,请歇一会");
|
|
} catch (Exception $e) {
|
|
// 回滚事务
|
|
DB::rollback();
|
|
// 返回错误信息
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
/**
|
|
* 退出组局功能
|
|
* 此函数处理用户退出组局的逻辑,包括验证用户是否已加入组局、组局是否还存在,
|
|
* 以及更新组局和用户状态等数据库操作
|
|
*
|
|
* @return mixed 返回操作结果的JSON响应
|
|
*/
|
|
public function useexitGroup(){
|
|
// 获取POST请求的数据
|
|
$data = $this->request->post();
|
|
// 检查是否有POST数据提交
|
|
if($this->request->post()){
|
|
// 获取当前用户ID
|
|
$uid = $this->auth->id;
|
|
$nickname = $this->auth->nickname;
|
|
// 获取需要退出的组局ID
|
|
$id = $data['id'];
|
|
// 查询组局信息,仅查询状态为0、1、2的组局
|
|
$groupinfo = $this->model->whereIn("status",[0,1,2])->where('is_del',0)->find($id);
|
|
// 如果找不到组局信息,返回错误提示
|
|
if(!$groupinfo) return $this->error("组局已结束,无需退出");
|
|
// 查询用户是否已加入该组局且状态为0
|
|
$uginfo = Info::where("uid",$uid)->where("ugid",$id)->where('status',0)->find();
|
|
// 如果用户未加入组局,返回错误提示
|
|
if(!$uginfo) return $this->error("您还未加入该组局");
|
|
// 开启数据库事务
|
|
DB::startTrans();
|
|
try {
|
|
// 更新用户组局状态为1,表示已退出
|
|
$uginfo->status = 1;
|
|
// 更新组局的参与人数,减去退出的1人
|
|
$groupinfo->ing_people = $groupinfo->ing_people - 1;
|
|
// 更新组局状态为1,表示有人退出
|
|
$groupinfo->status = 1;
|
|
// 如果数据库更新成功,提交事务并返回成功提示
|
|
//加入组局消息记录
|
|
$minfo = [
|
|
'uid'=>$groupinfo->uid,
|
|
'error'=>0,
|
|
'create'=>time(),
|
|
'type'=>"system_group",
|
|
'content'=>$groupinfo->title." 开始时间:".$groupinfo->start_time_text,
|
|
'title'=>"{$nickname}退出组局",
|
|
'is_read'=>0
|
|
];
|
|
$message = new \app\admin\model\system\Message;
|
|
$message->addmessage($minfo);
|
|
if($groupinfo->save()&&$uginfo->save()){
|
|
DB::commit();
|
|
return $this->success("已退出");
|
|
}
|
|
// 如果更新失败,抛出异常
|
|
throw new Exception("操作繁忙,请歇一会");
|
|
} catch (Exception $e) {
|
|
// 事务回滚
|
|
DB::rollback();
|
|
// 返回异常信息作为错误提示
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
// 如果没有POST数据,返回非法请求的错误提示
|
|
return $this->error("非法请求");
|
|
}
|
|
public function getMyingGroup(){
|
|
// 获取POST请求的数据
|
|
// 检查是否有POST数据提交
|
|
if($this->request->post()){
|
|
$data = $this->request->post();
|
|
|
|
$GM = new Group;
|
|
// 获取当前用户ID
|
|
$uid =$data['uid'];
|
|
$myuid = isset($data['token'])&&!empty($data['token'])?$this->auth->id: 0;
|
|
// 查询我正加加入的系统组局
|
|
$orderlist = Order::where("uid",$uid)->whereIn("status",[1,2])->order('pay_time','desc')->select();
|
|
foreach($orderlist as $k => $v){
|
|
//查询是否是达人
|
|
$userinfo = User::get($v['uid']);
|
|
$return = $GM->getlist(['id'=>$v['vid']], [], 1, 10 ,0);
|
|
$orderlist[$k]['groupinfo'] = empty($return['rows'][0])?[]:$return['rows'][0];
|
|
$orderlist[$k]['is_system'] = 1;
|
|
$orderlist[$k]['is_daren'] = $userinfo['is_daren'];
|
|
$orderlist[$k]['avatar'] = $userinfo['avatar'];
|
|
}
|
|
// 查询正在加入的组局
|
|
$uglist = Info::where("uid",$uid)->whereIn("status",[0])->select();
|
|
foreach($uglist as $k => $v){
|
|
$return = $this->model->getgroupdetail($v['ugid'],$myuid);
|
|
if(in_array($return['status'],[0,1,2])){
|
|
$userinfo = User::get($v['uid']);
|
|
$return = $this->model->getlist(['id'=>$v['ugid']], [], 1, 10 ,0);
|
|
$mainuserinfo = User::get($return['rows'][0]['uid']);
|
|
// $return = $this->model->getgroupdetail($v['gid']);
|
|
$uglist[$k]['groupinfo'] = $return['rows'][0];
|
|
$uglist[$k]['is_system'] = 0;
|
|
$uglist[$k]['is_my'] = 0;
|
|
$uglist[$k]['is_daren'] = $mainuserinfo['is_daren'];
|
|
$uglist[$k]['avatar'] = $mainuserinfo['avatar'];
|
|
$uglist[$k]['nickname'] = $mainuserinfo['nickname'];
|
|
}else{
|
|
unset($uglist[$k]);
|
|
}
|
|
}
|
|
// 查询我创建的组局
|
|
$mylist = $this->model->where("uid",$uid)->whereIn("status",[0,1,2])->where('is_del',0)->select();
|
|
foreach($mylist as $ks => $v){
|
|
$userinfo = User::get($v['uid']);
|
|
$return = $this->model->getlist([], [], 1, 10 ,0);
|
|
$us['avatar'] = $userinfo['avatar'];
|
|
$userinfos[] = $us;
|
|
$mylist[$ks]['groupinfo'] = $return['rows'][0];
|
|
$mylist[$ks]['groupinfo']['user_info'] =$userinfos ;
|
|
$mylist[$ks]['is_system'] = 0;
|
|
$mylist[$ks]['is_my'] = 1;
|
|
$mylist[$ks]['nickname'] = $userinfo['nickname'];
|
|
$mylist[$ks]['is_daren'] = $userinfo['is_daren'];
|
|
$mylist[$ks]['avatar'] = $userinfo['avatar'];
|
|
}
|
|
$rerurninfo = array_merge($mylist,$uglist,$orderlist);
|
|
return $this->success("获取成功",$rerurninfo);
|
|
}
|
|
}
|
|
public function getMyhistoyGroup(){
|
|
// 获取POST请求的数据
|
|
// 检查是否有POST数据提交
|
|
if($this->request->post()){
|
|
$data = $this->request->post();
|
|
|
|
$GM = new Group;
|
|
// 获取当前用户ID
|
|
$uid =$this->auth->id;
|
|
$myuid = isset($data['token'])&&!empty($data['token'])?$this->auth->id: 0;
|
|
// 查询我正加加入的系统组局
|
|
$orderlist = Order::where("uid",$uid)->whereIn("status",[1,2])->order('pay_time','desc')->select();
|
|
foreach($orderlist as $k => $v){
|
|
//查询是否是达人
|
|
$userinfo = User::get($v['uid']);
|
|
$return = $GM->getlist(['id'=>$v['vid']], [], 1, 10 ,0);
|
|
$orderlist[$k]['groupinfo'] = $return['rows'][0];
|
|
$orderlist[$k]['is_system'] = 1;
|
|
$orderlist[$k]['is_daren'] = $userinfo['is_daren'];
|
|
$orderlist[$k]['avatar'] = $userinfo['avatar'];
|
|
}
|
|
// 查询正在加入的组局
|
|
$uglist = Info::where("uid",$uid)->whereIn("status",[0])->select();
|
|
foreach($uglist as $k => $v){
|
|
$return = $this->model->getgroupdetail($v['ugid'],$myuid);
|
|
if(in_array($return['status'],[0,1,2])){
|
|
$userinfo = User::get($v['uid']);
|
|
$return = $this->model->getlist(['id'=>$v['ugid']], [], 1, 10 ,0);
|
|
$mainuserinfo = User::get($return['rows'][0]['uid']);
|
|
// $return = $this->model->getgroupdetail($v['gid']);
|
|
$uglist[$k]['groupinfo'] = $return['rows'][0];
|
|
$uglist[$k]['is_system'] = 0;
|
|
$uglist[$k]['is_my'] = 0;
|
|
$uglist[$k]['is_daren'] = $mainuserinfo['is_daren'];
|
|
$uglist[$k]['avatar'] = $mainuserinfo['avatar'];
|
|
$uglist[$k]['nickname'] = $mainuserinfo['nickname'];
|
|
}else{
|
|
unset($uglist[$k]);
|
|
}
|
|
}
|
|
// 查询我创建的组局
|
|
$mylist = $this->model->where("uid",$uid)->whereIn("status",[0,1,2])->where('is_del',0)->select();
|
|
foreach($mylist as $ks => $v){
|
|
$userinfo = User::get($v['uid']);
|
|
$return = $this->model->getlist([], [], 1, 10 ,0);
|
|
$us['avatar'] = $userinfo['avatar'];
|
|
$userinfos[] = $us;
|
|
$mylist[$ks]['groupinfo'] = $return['rows'][0];
|
|
$mylist[$ks]['groupinfo']['user_info'] =$userinfos ;
|
|
$mylist[$ks]['is_system'] = 0;
|
|
$mylist[$ks]['is_my'] = 1;
|
|
$mylist[$ks]['nickname'] = $userinfo['nickname'];
|
|
$mylist[$ks]['is_daren'] = $userinfo['is_daren'];
|
|
$mylist[$ks]['avatar'] = $userinfo['avatar'];
|
|
}
|
|
$rerurninfo = array_merge($mylist,$uglist,$orderlist);
|
|
return $this->success("获取成功",$rerurninfo);
|
|
}
|
|
}
|
|
public function getgroupdetail(){
|
|
$data = $this->request->post();
|
|
if($this->request->post()){
|
|
$uid = isset($data['token'])&&!empty($data['token'])?$this->auth->id:0;
|
|
$return = $this->model->getgroupdetail($data['id'],$uid);
|
|
return $this->success('获取成功',$return);
|
|
}
|
|
}
|
|
public function delGroup(){
|
|
$data = $this->request->post();
|
|
if($this->request->post()){
|
|
$uid = $this->auth->id;
|
|
//查询是否是我发布的组局
|
|
$return = $this->model->get($data['id']);
|
|
if(!$return){
|
|
return $this->error('组局不存在');
|
|
}
|
|
if($return['uid']!=$uid) return $this->error('不是您创建的组局');
|
|
$return->is_del = 1;
|
|
if(!$return->save())return $this->error('删除失败');
|
|
return $this->success('删除成功');
|
|
}
|
|
}
|
|
}
|
|
|
|
|