Guild.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\UserService;
  5. use think\Db;
  6. use think\Exception;
  7. use Redis;
  8. use app\common\service\RoomService;
  9. use app\common\library\Easemob;
  10. /**
  11. * 公会控制器
  12. */
  13. class Guild extends Api
  14. {
  15. protected $noNeedLogin = ['getGuildList'];
  16. protected $noNeedRight = ['*'];
  17. protected $roomTypeArr;
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. $this->roomTypeArr = [1=>"party",2=>"live"];
  22. }
  23. /**
  24. * 获取搜索公会列表
  25. */
  26. public function getGuildList() {
  27. $search = input("search","","string"); //搜索的字段 公会名称/公会ID
  28. $where = [];$whereOr = [];
  29. $where['g.status'] = 1;
  30. if (!empty($search)) {
  31. $whereOr['g.name'] = array('like', '%' . $search . '%');
  32. $whereOr['g.g_id'] = $search;
  33. }
  34. $res = Db::name('guild')->alias('g')->field("g.id,g.g_id,g.user_id,g.party_id,g.name,g.image,g.desc,g.member,
  35. area.province_name,area.name as city_name")
  36. ->join('shopro_area area','g.city_id = area.id','LEFT')
  37. ->where($where)
  38. ->where(function ($query) use($whereOr) {
  39. $query->whereOr($whereOr);
  40. })
  41. ->autopage()
  42. ->select();
  43. $res = list_domain_image($res,['image']);
  44. $this->success("获取成功!",$res);
  45. }
  46. /**
  47. * 获取公会信息-新
  48. */
  49. public function getGuildInfoNew() {
  50. $guild_id = input("guild_id"); //公会ID
  51. $user_id = $this->auth->id;
  52. //结果
  53. $res = [];
  54. //公会信息
  55. $guildInfo = \app\common\model\Guild::field("id,image,name,g_id,user_id,party_id,desc,notice,status,province_id,city_id")->where(["id"=>$guild_id])->find();
  56. if(!$guildInfo) $this->error("公会信息获取失败!");
  57. $guildInfo['province_name'] = '';
  58. $guildInfo['city_name'] = '';
  59. $area_info = Db::name('shopro_area')->where('id',$guildInfo['city_id'])->field('name,province_name')->find();
  60. if($area_info){
  61. $guildInfo['province_name'] = $area_info['province_name'];
  62. $guildInfo['city_name'] = $area_info['name'];
  63. }
  64. // 获取公会成员信息
  65. $guildMemberInfo = \app\common\model\GuildMember::alias("a")
  66. ->field("a.id,a.user_id,a.role,u.nickname,u.avatar,u.gender")
  67. ->join("user u","a.user_id = u.id")
  68. ->where(["a.guild_id"=>$guild_id])->limit(10)->order("a.role","desc")->select();
  69. // 获取公会成员总数
  70. $guildMemberCount = \app\common\model\GuildMember::where(["guild_id"=>$guild_id])->count("id");
  71. // 获取会长信息
  72. $guilderInfo = \app\common\model\User::field("avatar,nickname,u_id")->where(["id"=>$guildInfo->user_id])->find();
  73. // 获取房间信息
  74. $partyInfo = \app\common\model\Party::field("id,party_logo,party_type,is_online,party_name")->where(["id"=>$guildInfo->party_id])->find();
  75. $partyParams = ['party_id' => $guildInfo->party_id];
  76. $roomService = new RoomService();
  77. $partyOnlineRes = $roomService->getPartyUserList($partyParams);
  78. $onlineData = isset($partyOnlineRes['data']) ? $partyOnlineRes['data'] : [];
  79. if (!empty($partyInfo)) {
  80. $partyInfo['user_list'] = isset($onlineData['member_list']) ? $onlineData['member_list'] : [];
  81. $partyInfo['online_num'] = isset($onlineData['online_num']) ? $onlineData['online_num'] : 0;
  82. // 派对类型
  83. $mod = isset($partyInfo["party_type"])?intval($partyInfo["party_type"])%5:1;
  84. $partyInfo["party_type_color"] = $mod == 0?5:$mod;
  85. $partyInfo["type_name"] = '公会房';
  86. }
  87. //获取公会角色 角色:0=成员,1=副会长,2=会长,3=非会员
  88. $guildMember = \app\common\model\GuildMember::field('id,role')->where(['guild_id'=>$guild_id,'user_id'=>$this->auth->id])->find();
  89. if (!empty($guildMember)) {
  90. $guildRole = $guildMember['role'];
  91. }else{
  92. $guildRole = 3;
  93. }
  94. // 返回参数拼接
  95. $res["guildInfo"] = $guildInfo; // 公会基本信息
  96. $res["partyInfo"] = $partyInfo; // 派对基本信息
  97. $res["guildMemberInfo"] = $guildMemberInfo; // 成员列表
  98. $res["guildMemberCount"] = $guildMemberCount; // 成员总数
  99. $res["guilderInfo"] = $guilderInfo; // 会长
  100. $res['guild_role'] = $guildRole; //公会角色
  101. $this->success("获取成功!",$res);
  102. }
  103. /**
  104. * 更新公会基本信息
  105. */
  106. public function guildSaveInfo() {
  107. Db::startTrans();
  108. try {
  109. if($this->auth->is_auth != 2){
  110. $this->error('请先完成实名认证');
  111. }
  112. $guild_id = input("guild_id"); //公会id
  113. $guild_name = input("guild_name"); //公会名
  114. $guild_image = input("guild_image"); //logo
  115. $guild_desc = input("guild_desc"); //公会简介
  116. $guild_notice = input("guild_notice"); //公会公告
  117. $guild_info = input("guild_info"); //公会宣言
  118. $province_id = input("province_id"); //省id
  119. $city_id = input("city_id"); //市id
  120. if(!$guild_name || !$guild_image){
  121. throw new Exception("公会名和logo必填!");
  122. }
  123. $user_id = $this->auth->id;
  124. if (empty($guild_id)) {
  125. //检查
  126. $guildWhere['user_id'] = $user_id;
  127. $guildWhere['status'] = ['in',[0,1]];
  128. $guildData = model('Guild')->where($guildWhere)->find();
  129. if (!empty($guildData)) {
  130. if($guildData['status'] == 1){
  131. throw new Exception('您已创建过公会!');
  132. }else{
  133. throw new Exception('您创建的公会正在审核中!');
  134. }
  135. }
  136. //检查不能是其他家族的成员
  137. $checkmember = model('GuildMember')->where('user_id',$this->auth->id)->find();
  138. if (!empty($checkmember)) {
  139. $this->error('您已经加入了其他公会');
  140. }
  141. //检查删除这个人的所有加入申请
  142. $joinData = model('GuildJoinin')->where('user_id',$this->auth->id)->where('status',0)->find();
  143. if (!empty($joinData)) {
  144. $this->error('您已经申请加入了其他公会,需等待被拒或退出,才能创建公会');
  145. }
  146. //准备创建
  147. $guildInfo = new \app\common\model\Guild();
  148. $ids = \app\common\model\Guild::column("g_id");
  149. $guildInfo->user_id = $user_id;
  150. $guildInfo->g_id = getUinqueId(4, [$ids]);
  151. $guildInfo->status = 0;
  152. //去派对建个房间
  153. $party_info = $this->createParty($user_id,$guild_name,$guild_image,$guild_desc,$guild_notice,8,1,1,$guildInfo['status']);
  154. $guildInfo->party_id = $party_info['party_id'];
  155. $guildInfo->easemob_room_id = $party_info['easemob_room_id'];
  156. } else {
  157. // 获取公会信息
  158. $guildInfo = \app\common\model\Guild::where(["id"=>$guild_id])->find();
  159. // 验证更新条件
  160. if($user_id !== $guildInfo->user_id) throw new Exception("身份验证失败!您不是公会长,无权限更改!");
  161. //去派对建个房间
  162. if(!$guildInfo['party_id']){
  163. $party_info = $this->createParty($user_id,$guild_name,$guild_image,$guild_desc,$guild_notice,8,1,1,$guildInfo['status']);
  164. $guildInfo->party_id = $party_info['party_id'];
  165. $guildInfo->easemob_room_id = $party_info['easemob_room_id'];
  166. }
  167. }
  168. //传参信息
  169. $guild_name && $guildInfo->name = $guild_name;
  170. $guild_image && $guildInfo->image = $guild_image;
  171. $guild_desc && $guildInfo->desc = $guild_desc;
  172. $guild_notice && $guildInfo->notice = $guild_notice;
  173. $guild_info && $guildInfo->info = $guild_info;
  174. $province_id && $guildInfo->province_id = $province_id;
  175. $city_id && $guildInfo->city_id = $city_id;
  176. $res = $guildInfo->save();
  177. if($res !== false) {
  178. if (empty($guild_id)) {
  179. //新公会,会长也是成员之一
  180. $guildMemberData = [
  181. 'guild_id' => $guildInfo->id,
  182. 'user_id' => $user_id,
  183. 'role' => 2, //角色:0=成员,1=副会长,2=会长
  184. // 'sign_type' => 3,//签约类型:1=三个月,2=半年,3=一年
  185. // 'sign_time' => 0,
  186. // 'status' => 1, //状态:1=签约中,2=已解约
  187. 'createtime' => time(),
  188. ];
  189. $guildRes = model('GuildMember')->insertGetId($guildMemberData);
  190. if (!$guildRes) {
  191. throw new Exception('生成成员失败');
  192. }
  193. //更新用户资料
  194. $userWhere['id'] = $user_id;
  195. $user = model('User')->where($userWhere)->find();
  196. if (!empty($user) && $user['guild_id'] != $guildInfo->id) {
  197. $userRes = model('User')->where($userWhere)->update(['is_guild'=>3,'guild_id'=>$guildInfo->id]);
  198. if (!$userRes) {
  199. throw new Exception('用户绑定公会失败');
  200. }
  201. }
  202. }
  203. Db::commit();
  204. $this->success("操作成功!");
  205. } else {
  206. throw new Exception("网络错误,请稍后重试!");
  207. }
  208. } catch (Exception $e) {
  209. Db::rollback();
  210. $this->error($e->getMessage());
  211. }
  212. }
  213. /**
  214. * 创建/进入派对
  215. */
  216. private function createParty($user_id,$party_name,$party_logo,$guild_desc,$guild_notice,$seatnum = 8,$is_public = 1,$room_type = 1,$status = 0) {
  217. $partyModel = new \app\common\model\Party();
  218. // 判断派对是否存在
  219. // sql中不存在派对信息
  220. $party_type = 7; // 派对类型(情感互动,心动点单 等)
  221. $party_notice = $guild_desc; // 派对公告
  222. $party_notice_detail = $guild_notice; // 派对公告详情
  223. $party_ids = $partyModel->column("party_id");
  224. // 创建派对ID (临时ID四位,派对数不超过8999)
  225. $party_id = $this->auth->getUinqueId(4,$party_ids);
  226. if($party_id > 9999) {
  227. $this->error("房间超限,请联系客服");
  228. }
  229. //创建房间,初始化环信
  230. $easemob_room_id = $this->easemob_createroom($party_name,$party_logo,$seatnum,$is_public,$room_type);
  231. //写入派对
  232. $data = [];
  233. $data["user_id"] = $user_id;
  234. $data["room_type"] = $room_type;
  235. $data["party_id"] = $party_id;
  236. $data["party_hot"] = 0;
  237. $data["party_name"] = $party_name;
  238. $data["party_logo"] = $party_logo;
  239. $data["party_type"] = $party_type;
  240. $data["party_notice"] = $party_notice;
  241. $data["party_notice_detail"] = $party_notice_detail;
  242. $data["is_online"] = 1;
  243. $data["status"] = $status;//跟随公会状态
  244. $data["is_recommend"] = 0;
  245. $data["createtime"] = time();
  246. $data["easemob_room_id"] = $easemob_room_id;
  247. if($room_type == 1){
  248. $data["platRate"] = config('site.party_party_platrate');
  249. $data["guilderRate"] = config('site.party_party_guilderrate');
  250. }else{
  251. $data["platRate"] = config('site.party_live_platrate');
  252. $data["guilderRate"] = config('site.party_live_guilderrate');
  253. }
  254. $id = $partyModel->insertGetId($data);
  255. if(!$id) {
  256. $this->error("公会房间创建失败,请稍后重试!");
  257. }
  258. $data["id"] = $id;
  259. $partyInfo = $partyModel->where(["id"=>$id])->find();
  260. $partyInfo["is_new"] = 1;
  261. $partyInfo["type_name"] = '公会房';
  262. //redis
  263. $redis = new Redis();
  264. $redisconfig = config("redis");
  265. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  266. if ($redisconfig['redis_pwd']) {
  267. $redis->auth($redisconfig['redis_pwd']);
  268. }
  269. if($redisconfig['redis_selectdb'] > 0){
  270. $redis->select($redisconfig['redis_selectdb']);
  271. }
  272. // 加入缓存排序,rediskey:派对热度
  273. $redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
  274. // 加入缓存,rediskey:派对详情
  275. $redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($partyInfo));
  276. //更新家族 party_id
  277. $rs = [
  278. 'party_id' => $id,
  279. 'easemob_room_id' => $easemob_room_id,
  280. ];
  281. return $rs;
  282. //声网三个token
  283. // $partyInfo['shengwang_token'] = $this->shengwang_token_private($easemob_room_id);
  284. }
  285. //创建房间,初始化环信
  286. private function easemob_createroom($party_name,$party_logo,$seatnum,$is_public,$room_type){
  287. //[环信]创建聊天室
  288. $easemob = new Easemob();
  289. $easemob_room_id = $easemob->room_createRoom($party_name,$party_name,$this->auth->id);
  290. if(empty($easemob_room_id)){
  291. $this->error('创建房间失败');
  292. }
  293. //[环信]初始化房间,初始化自定义属性
  294. $matedata = [
  295. 'seatnum'=> $seatnum,//麦位数量
  296. 'waitsing_list' => json_encode([]),//已点歌曲列表
  297. 'party_name' => $party_name,//房名字
  298. 'party_logo' => $party_logo,//logo
  299. 'is_public' => $is_public,//是否公开
  300. 'room_type' => $room_type,//房间类型
  301. 'online_user_num' => 0,//当前房间在线的人
  302. 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
  303. 'background' => '', //房间背景
  304. ];
  305. $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$matedata);
  306. //[环信]初始化N个麦位
  307. $seatdata = [];
  308. for($i=0;$i<$seatnum;$i++){
  309. $seat = [
  310. 'charm' => 0, //红心,魅力值
  311. 'isMaster' => false, // 是否是房主
  312. 'headUrl' => '', // 头像
  313. 'userNo' => '', // 座位上用户no
  314. 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
  315. 'name' => '', // 座位上用户昵称
  316. 'seatIndex' => -1, // 座位编号
  317. 'chorusSongCode' => '', // 是否合唱
  318. 'isAudioMuted' => 1, // 是否静音
  319. 'isVideoMuted' => 0, // 是否开启视频
  320. 'checked' => false, // 用于送礼物选择用户
  321. 'isUsed' => true, // 用于送礼物选择用户
  322. 'gender' => 1, //性别
  323. ];
  324. //创建完房间就进入,所以房主直接在麦位0
  325. if($i == 0){
  326. $seat['isMaster'] = true;
  327. $seat['headUrl'] = localpath_to_netpath($this->auth->avatar);
  328. $seat['userNo'] = $this->auth->id;
  329. $seat['rtcUid'] = $this->auth->id;
  330. $seat['name'] = $this->auth->nickname;
  331. $seat['seatIndex'] = $i;
  332. }
  333. $seatdata['seat'.$i] = json_encode($seat);
  334. }
  335. $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$seatdata);
  336. return $easemob_room_id;
  337. }
  338. /**
  339. * 公会签约申请
  340. */
  341. public function guildJoinin() {
  342. $guild_id = input("guild_id"); //公会ID
  343. // 一个身份证号只能申请一次
  344. $user_id = $this->auth->id;
  345. // 实名认证
  346. if($this->auth->is_auth != 2){
  347. $this->error("请完成实名认证!");
  348. }
  349. $check = \app\common\model\Guild::where(["user_id"=>$this->auth->id])->find();
  350. if($check){
  351. if($check['status'] == 1){
  352. $this->error('您已经有自己的公会了');
  353. }elseif($check['status'] == 0){
  354. $this->error('您已经申请了自己的公会了');
  355. }
  356. }
  357. // 搜索签约申请中是否有此身份证号
  358. $idcardfind = \app\common\model\GuildJoinin::where(["user_id"=>$user_id,'guild_id'=>$guild_id,"status"=>0])->find();
  359. if($idcardfind) $this->error("当前账号正在审核,请耐心等待!");
  360. $memberfind = \app\common\model\GuildMember::where(["user_id"=>$user_id])->find();
  361. if($memberfind) $this->error("一个账号只能加入一个公会哟!");
  362. // 添加数据
  363. $data = [];
  364. $data["user_id"] = $user_id;
  365. $data["guild_id"] = $guild_id;
  366. $data["createtime"] = time();
  367. $res = \app\common\model\GuildJoinin::insert($data);
  368. if($res) {
  369. $this->success("申请发送成功!");
  370. } else {
  371. $this->error("网络错误,请稍后重试!");
  372. }
  373. }
  374. /**
  375. * 公会签约申请审核
  376. */
  377. public function guildJoininExamine() {
  378. $joinin_id = input("joinin_id"); // 申请加入的申请ID
  379. $is_pass = input("is_pass"); // 是否通过:1=通过,-1=拒绝
  380. if(!in_array($is_pass,[1,-1]) || !$joinin_id) $this->error("参数错误!");
  381. $user_id = $this->auth->id;
  382. // 获取申请信息
  383. $joininInfo = \app\common\model\GuildJoinin::where(["id"=>$joinin_id])->find();
  384. if(empty($joininInfo)){
  385. $this->error('未找到申请信息');
  386. }
  387. // 获取公会信息
  388. $guildInfo = \app\common\model\Guild::where(["id"=>$joininInfo->guild_id])->find();
  389. // 验证更新条件
  390. if($user_id !== $guildInfo->user_id) $this->error("身份验证失败!您不是公会长,无权限更改!");
  391. //判断重复
  392. $memberfind = \app\common\model\GuildMember::where(["user_id"=>$joininInfo['user_id']])->find();
  393. if($memberfind) $this->error("该用户已有公会!");//包括成员,副会,会长角色
  394. //清理其他公会未审核的数据
  395. if ($is_pass == 1) {
  396. $joinWhere['user_id'] = $joininInfo['user_id'];
  397. $joinWhere['guild_id'] = ['neq',$joininInfo['guild_id']];
  398. $joinWhere['status'] = 0;
  399. $joinData = model('GuildJoinin')->where($joinWhere)->delete();
  400. }
  401. // 计算签约时间段
  402. Db::startTrans();
  403. try{
  404. // 更新申请状态
  405. $joininInfo->status = $is_pass;
  406. $joininInfo->updatetime = time();
  407. $res2 = $joininInfo->save();
  408. if($is_pass == 1) {
  409. // 同意后 加入公会成员
  410. $data = [];
  411. $data["guild_id"] = $guildInfo->id;
  412. $data["user_id"] = $joininInfo->user_id;
  413. $data["role"] = 0;
  414. $data["createtime"] = time();
  415. $res3 = \app\common\model\GuildMember::insert($data);
  416. $res4 = \app\common\model\Guild::where(["id"=>$guildInfo->id])->setInc("member");
  417. if($res2 && $res3 && $res4) {
  418. // +message
  419. \app\common\model\Message::addMessage($joininInfo->user_id,"公会审核通知","恭喜,您的加入公会审核通过啦!");
  420. Db::commit();
  421. $this->success("已审核通过!");
  422. }
  423. } else {
  424. if($res2) {
  425. // +message
  426. \app\common\model\Message::addMessage($joininInfo->user_id,"公会审核通知","您的公会加入申请,会长审核拒绝,没关系,调整姿势再来一遍!");
  427. Db::commit();
  428. $this->success("已审核拒绝!");
  429. }
  430. }
  431. $this->error("网络错误,请稍后重试!");
  432. }catch (ValidateException $e) {
  433. Db::rollback();
  434. $this->error($e->getMessage());
  435. } catch (PDOException $e) {
  436. Db::rollback();
  437. $this->error($e->getMessage());
  438. } catch (Exception $e) {
  439. Db::rollback();
  440. $this->error($e->getMessage());
  441. }
  442. }
  443. /**
  444. * 公会申请加入列表
  445. */
  446. public function guildJoinList() {
  447. $user_id = $this->auth->id;
  448. // 获取公会信息
  449. $guildInfo = model('Guild')->where(["user_id"=>$user_id])->find();
  450. if(!$guildInfo) {
  451. $this->success("获取成功",[]);
  452. }
  453. // 先获取申请列表
  454. $where = [];
  455. $where["m.guild_id"] = $guildInfo->id;
  456. $where["m.status"] = 0;
  457. $result = Db::name('guild_joinin')->alias('m')
  458. ->field("m.id,m.user_id,m.createtime,user.is_online,user.onlinetime,user.nickname,user.avatar")
  459. ->join('user','m.user_id = user.id','LEFT')
  460. ->where($where)->order("createtime","desc")->select();
  461. if($result) {
  462. foreach($result as $k => &$v) {
  463. if ($v['is_online'] == 1) {
  464. $onlineText = '在线';
  465. } else {
  466. $onlineTime = get_last_time($v['onlinetime']);
  467. $onlineText = $onlineTime.'在线';
  468. $limitTime = time() - (3600 * 6);
  469. if ($v['onlinetime'] < $limitTime) {
  470. $onlineText = '离线';
  471. }
  472. }
  473. $v['online_text'] = $onlineText;
  474. unset($v['user']);
  475. }
  476. }
  477. $this->success("获取成功",$result);
  478. }
  479. /**
  480. * 公会成员列表
  481. */
  482. public function guildMember() {
  483. $guild_id = input("guild_id",0,"intval"); //公会ID
  484. if($guild_id <= 0) $this->error("参数错误!");
  485. $where = [];
  486. $where["a.guild_id"] = $guild_id;
  487. // 获取公会成员总数
  488. $guildMemberCount = \app\common\model\GuildMember::alias("a")->where($where)->count("id");
  489. // 获取公会成员列表
  490. $guildMemberList = \app\common\model\GuildMember::alias("a")
  491. ->field("a.id,a.user_id,u.avatar,u.nickname,u.gender,a.role,u.is_online,u.onlinetime")
  492. ->join("user u","a.user_id = u.id")
  493. ->where($where)
  494. ->autopage()
  495. ->order("a.role","desc")
  496. ->select();
  497. $role = [0=>'成员',1=>'副会长',2=>'会长'];
  498. if($guildMemberList) {
  499. foreach($guildMemberList as $k => &$v) {
  500. $v["role_text"] = $role[$v["role"]];
  501. if ($v['is_online'] == 1) {
  502. $onlineText = '在线';
  503. } else {
  504. $onlineTime = get_last_time($v['onlinetime']);
  505. $onlineText = $onlineTime.'在线';
  506. $limitTime = time() - (3600 * 6);
  507. if ($v['onlinetime'] < $limitTime) {
  508. $onlineText = '离线';
  509. }
  510. }
  511. $v['online_text'] = $onlineText;
  512. }
  513. }
  514. $res = [];
  515. $res["memberCount"] = $guildMemberCount;
  516. $res["memberList"] = $guildMemberList;
  517. $this->success("获取成功!",$res);
  518. }
  519. /**
  520. * 获取公会成员信息
  521. */
  522. public function getGuildMemberInfo() {
  523. $member_id = input("member_id",0,"intval"); //公会成员ID
  524. if($member_id <= 0) $this->error("参数错误!");
  525. //成员信息
  526. $memberData = model('GuildMember')->field('id,user_id')->find($member_id);
  527. //更新成员的 财富值 ,魅力值,收到的礼物数量
  528. $userService = new UserService();
  529. $userService->updateGuildMember(['user_id'=>$memberData['user_id']]);
  530. //从user表拿一些用户信息
  531. $memberInfo = model('GuildMember')->alias('m')
  532. ->join('user','m.user_id = user.id','LEFT')
  533. ->field('m.id,m.user_id,m.wealth,m.charm,m.gift_num,m.role,m.guild_id,m.createtime,
  534. user.avatar,user.nickname,user.gender,user.logintime,user.is_online,user.onlinetime')
  535. ->where('m.id',$member_id)
  536. ->find();
  537. if(!$memberInfo) $this->error("用户信息获取失败!");
  538. $memberInfo = info_domain_image($memberInfo,['avatar']);
  539. //角色
  540. $role = [0=>'成员',1=>'副会长',2=>'公会长'];
  541. $memberInfo["role_text"] = $role[$memberInfo["role"]];
  542. //在线
  543. if ($memberInfo['is_online'] == 1) {
  544. $onlineText = '在线';
  545. } else {
  546. $onlineTime = get_last_time($memberInfo['onlinetime']);
  547. $onlineText = $onlineTime.'在线';
  548. $limitTime = time() - (3600 * 6);
  549. if ($memberInfo['onlinetime'] < $limitTime) {
  550. $onlineText = '离线';
  551. }
  552. }
  553. $memberInfo['online_text'] = $onlineText;
  554. //我在这个公会的角色
  555. /*$myGuildMember = model('GuildMember')->field('role')->where(["user_id"=>$this->auth->id,"guild_id"=>$memberInfo->guild_id])->find();
  556. $memberInfo["my_role"] = !empty($myGuildMember) ? $myGuildMember['role'] : -1;*/
  557. //几个比较大的值,改w单位
  558. $fieldArr = ['wealth','charm','gift_num'];
  559. foreach ($fieldArr as $fkey => $fval) {
  560. if ($memberInfo[$fval] > 10000) {
  561. $memberInfo[$fval] = bcdiv($memberInfo[$fval],10000,2).'w';
  562. }
  563. }
  564. $this->success("获取成功!",$memberInfo);
  565. }
  566. /**
  567. * 公会管理-设为管理员
  568. */
  569. public function guildSetManage() {
  570. $guild_id = input("guild_id",0,"intval"); //公会ID
  571. $member_id = input("member_id",0,"intval"); //成员ID
  572. $type = input("type",1); // 设置类型:1=设为管理员,2=取消管理员
  573. if(!$guild_id || !$member_id || !in_array($type,[1,2])) $this->error("参数缺失!");
  574. $user_id = $this->auth->id;
  575. // 获取公会信息
  576. $guildInfo = \app\common\model\Guild::where(["id"=>$guild_id])->find();
  577. // 验证更新条件
  578. if($user_id !== $guildInfo->user_id) $this->error("身份验证失败!您不是公会长,无权限更改!");
  579. $memberInfo = \app\common\model\GuildMember::where(["id"=>$member_id,'guild_id'=>$guild_id])->find();
  580. if(!$memberInfo) $this->error("会员信息查询失败!");
  581. //取消管理员
  582. if($type == 2) {
  583. $memberInfo->role = 0;
  584. $memberInfo->updatetime = time();
  585. $res = $memberInfo->save();
  586. if($res) {
  587. //[环信]取消聊天室管理员
  588. $easemob = new Easemob();
  589. $easemob->room_demoteRoomAdmin($guildInfo['easemob_room_id'],$memberInfo['user_id']);
  590. $this->success("取消成功!");
  591. } else {
  592. $this->error("网络错误,请稍后重试!");
  593. }
  594. }
  595. //设置管理员
  596. $memberInfo->role = 1;
  597. $memberInfo->updatetime = time();
  598. $res = $memberInfo->save();
  599. if($res) {
  600. // 获取房间id
  601. $party_id = $guildInfo->party_id;
  602. // 添加房间管理员
  603. $redis = new Redis();
  604. $redisconfig = config("redis");
  605. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  606. if ($redisconfig['redis_pwd']) {
  607. $redis->auth($redisconfig['redis_pwd']);
  608. }
  609. if($redisconfig['redis_selectdb'] > 0){
  610. $redis->select($redisconfig['redis_selectdb']);
  611. }
  612. // 获取成员用户信息
  613. $memberuserInfo = \app\common\model\User::field("id,avatar,nickname,gender,level")->where('id',$memberInfo['user_id'])->find();
  614. $data = [];
  615. $data["user_id"] = $memberuserInfo->id;
  616. $data["avatar"] = $memberuserInfo->avatar;
  617. $data["nickname"] = $memberuserInfo->nickname;
  618. $data["gender"] = $memberuserInfo->gender;
  619. $data["item"] = 1;
  620. $data["time"] = 0;
  621. $data["createtime"] = time();
  622. $redis->hSet("party_manage_".$party_id,$memberuserInfo->id."-1",serialize($data));
  623. //[环信]设置群组管理员
  624. $easemob = new Easemob();
  625. $easemob->room_promoteRoomAdmin($guildInfo['easemob_room_id'],$memberInfo['user_id']);
  626. $this->success("设置成功!");
  627. } else {
  628. $this->error("网络错误,请稍后重试!");
  629. }
  630. }
  631. /**
  632. * 公会管理-踢出公会
  633. */
  634. public function guildDelMember() {
  635. $guild_id = input("guild_id",0,"intval"); //公会ID
  636. $member_id = input("member_id",0,"intval"); //成员ID
  637. if(!$guild_id || !$member_id) $this->error("参数缺失!");
  638. // 先判断是否为公会会长
  639. $user_id = $this->auth->id;
  640. // 获取公会信息
  641. $guildInfo = \app\common\model\Guild::where(["id"=>$guild_id])->find();
  642. // 验证更新条件
  643. if($user_id !== $guildInfo->user_id) $this->error("身份验证失败!您不是公会长,无权限更改!");
  644. $memberInfo = \app\common\model\GuildMember::where(["id"=>$member_id,'guild_id'=>$guild_id])->find();
  645. if(!$memberInfo) $this->error("会员信息查询失败!");
  646. if($memberInfo['role'] == 2 || $memberInfo['user_id'] == $guildInfo['user_id']){
  647. $this->error('会长不能被踢出公会');
  648. }
  649. Db::startTrans();
  650. try{
  651. $res1 = $memberInfo->delete();
  652. $res2 = \app\common\model\Guild::where(["id"=>$guild_id])->setDec("member");
  653. if($res1 && $res2) {
  654. Db::commit();
  655. $this->success("踢出成功!");
  656. } else {
  657. $this->error("网络错误,请稍后重试!");
  658. }
  659. }catch (ValidateException $e) {
  660. Db::rollback();
  661. $this->error($e->getMessage());
  662. } catch (PDOException $e) {
  663. Db::rollback();
  664. $this->error($e->getMessage());
  665. } catch (Exception $e) {
  666. Db::rollback();
  667. $this->error($e->getMessage());
  668. }
  669. }
  670. /**
  671. * 公会贡献榜排行
  672. * @return void
  673. */
  674. public function getRankList()
  675. {
  676. try {
  677. $id = input('guild_id',0);
  678. $guildMemberWhere['guild_id'] = $id;
  679. // $guildMemberWhere['status'] = 1;
  680. $guildMember = model('GuildMember')->field('id,user_id,role')->where($guildMemberWhere)->select();
  681. $list = [];
  682. if (!empty($guildMember)) {
  683. $userIds = array_column($guildMember,'user_id');
  684. $where = [];
  685. $where['a.user_to_id'] = ['in', $userIds];
  686. $list = model('GiftUserParty')->alias("a")
  687. ->field('sum(a.value) as total_price,a.user_to_id,u.avatar,u.nickname,u.is_online,u.onlinetime')
  688. ->where($where)
  689. ->join('user u','u.id = a.user_to_id')
  690. ->group('a.user_to_id')
  691. ->order('total_price',"desc")
  692. ->autopage()
  693. ->select();
  694. if (!empty($list)) {
  695. $role = [0=>'成员',1=>'副会长',2=>'会长'];
  696. $guildMemberData = [];
  697. foreach ($guildMember as $mKey => $mVal) {
  698. $guildMemberData[$mVal['user_id']] = [
  699. 'id' => $mVal['id'],
  700. 'role' => $mVal['role'],
  701. 'role_text' => isset($role[$mVal['role']]) ? $role[$mVal['role']] : '',
  702. ];
  703. }
  704. foreach ($list as $key => &$value) {
  705. $member = isset($guildMemberData[$value['user_to_id']]) ? $guildMemberData[$value['user_to_id']] : [];
  706. $value['member_id'] = isset($member['id']) ? $member['id'] : 0;
  707. $value['role'] = isset($member['role']) ? $member['role'] : 0;
  708. $value['role_text'] = isset($member['role_text']) ? $member['role_text'] : '';
  709. if ($value['is_online'] == 1) {
  710. $onlineText = '在线';
  711. } else {
  712. $onlineTime = get_last_time($value['onlinetime']);
  713. $onlineText = $onlineTime.'在线';
  714. $limitTime = time() - (3600 * 6);
  715. if ($value['onlinetime'] < $limitTime) {
  716. $onlineText = '离线';
  717. }
  718. }
  719. $value['online_text'] = $onlineText;
  720. $value['total_price_text'] = $value['total_price'];
  721. if ($value['total_price'] >= 10000) {
  722. $value['total_price_text'] = bcdiv($value['total_price'],10000,1).'w';
  723. }
  724. unset($value['total_price']);
  725. }
  726. }
  727. }
  728. $this->success(__('Operation completed'),$list);
  729. } catch (Exception $e) {
  730. $this->error($e->getMessage());
  731. }
  732. }
  733. /**
  734. * 获取公会状态
  735. * @return void
  736. */
  737. public function getStatus()
  738. {
  739. $id = input('guild_id',0);
  740. if (!empty($id)) {
  741. $where['id'] = $id;
  742. } else {
  743. $where['user_id'] = $this->auth->id;
  744. }
  745. $field = 'id,status';
  746. $guild = model('Guild')->field($field)->where($where)->find();
  747. $this->success('获取成功', $guild);
  748. }
  749. ///////////////没用到的///////////
  750. /**
  751. * 开厅申请
  752. */
  753. public function guildApply() {
  754. $user_id = $this->auth->id;
  755. // 获取用户实名认证信息
  756. $authUserRealname = \app\common\model\UserAuth::where(["user_id"=>$user_id])->value("realname");
  757. if(!$authUserRealname) $this->error("未查询到实名认证信息,请实名认证先!");
  758. // 获取用户信息
  759. $userInfo = \app\common\model\User::field("id,mobile,nickname")->where(["id"=>$user_id])->find();
  760. $data = [];
  761. $data["user_id"] = $user_id;
  762. // 查看是否申请过
  763. $applyInfo = \app\common\model\GuildApply::where($data)->find();
  764. if($applyInfo && $applyInfo->status>=0) $this->error("您已经发送过开厅申请了,请勿重复发送!");
  765. // 查看是否有加入公会的申请
  766. $joinInfo = \app\common\model\GuildJoinin::where(["user_id"=>$user_id,"status"=>0])->find();
  767. if($joinInfo) $this->error("您存在申请加入公会信息,请联系公会长处理完毕后方可申请开厅!");
  768. // 查看是已经是公会成员
  769. $memberInfo = \app\common\model\GuildMember::where(["user_id"=>$user_id])->find();
  770. if($memberInfo) $this->error("您已存在签约中的公会,无法开新厅!");
  771. Db::startTrans();
  772. try{
  773. $data["user_name"] = $authUserRealname;
  774. $data["user_nickname"] = $userInfo->nickname;
  775. $data["user_mobile"] = $userInfo->mobile;
  776. $data["createtime"] = time();
  777. $res1 = \app\common\model\GuildApply::insert($data);
  778. // 更新用户状态
  779. $userInfo->is_guild = 1;
  780. $res2 = $userInfo->save();
  781. if($res1 && $res2) {
  782. Db::commit();
  783. $this->success("申请发送成功!");
  784. } else {
  785. $this->error("网络错误,请稍后重试!");
  786. }
  787. }catch (ValidateException $e) {
  788. Db::rollback();
  789. $this->error($e->getMessage());
  790. } catch (PDOException $e) {
  791. Db::rollback();
  792. $this->error($e->getMessage());
  793. } catch (Exception $e) {
  794. Db::rollback();
  795. $this->error($e->getMessage());
  796. }
  797. }
  798. /**
  799. * 公会申请页面信息
  800. */
  801. public function guildApplyInfo() {
  802. $guild_id = input("guild_id"); //公会ID
  803. if($guild_id <= 0) $this->error("参数缺失!");
  804. $guildInfo = \app\common\model\Guild::alias("a")
  805. ->join("user u","u.id = a.user_id","inner")
  806. ->field("a.id,a.image,a.name,a.g_id,a.user_id,u.avatar,u.nickname,u.u_id")
  807. ->where(["a.id"=>$guild_id])
  808. ->find();
  809. $guildInfo["status"] = 0;
  810. // 获取申请用户是否已经有正在申请的记录
  811. $user_id = $this->auth->id;
  812. $joinStatus = \app\common\model\GuildJoinin::where(["user_id"=>$user_id])->value("status");
  813. if($joinStatus === 0) $guildInfo["status"] = 1;
  814. $Member = \app\common\model\GuildMember::where(["user_id"=>$user_id])->value("id");
  815. if($Member > 0) $guildInfo["status"] = 2;
  816. $this->success("获取成功!",$guildInfo);
  817. }
  818. }