Guild.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. if($is_pass == 1){
  393. $memberfind = \app\common\model\GuildMember::where(["user_id"=>$joininInfo['user_id']])->find();
  394. if($memberfind) $this->error("该用户已有公会!");//包括成员,副会,会长角色
  395. }
  396. //清理其他公会未审核的数据
  397. if ($is_pass == 1) {
  398. $joinWhere['user_id'] = $joininInfo['user_id'];
  399. $joinWhere['guild_id'] = ['neq',$joininInfo['guild_id']];
  400. $joinWhere['status'] = 0;
  401. $joinData = model('GuildJoinin')->where($joinWhere)->delete();
  402. }
  403. // 计算签约时间段
  404. Db::startTrans();
  405. try{
  406. // 更新申请状态
  407. $joininInfo->status = $is_pass;
  408. $joininInfo->updatetime = time();
  409. $res2 = $joininInfo->save();
  410. if($is_pass == 1) {
  411. // 同意后 加入公会成员
  412. $data = [];
  413. $data["guild_id"] = $guildInfo->id;
  414. $data["user_id"] = $joininInfo->user_id;
  415. $data["role"] = 0;
  416. $data["createtime"] = time();
  417. $res3 = \app\common\model\GuildMember::insert($data);
  418. $res4 = \app\common\model\Guild::where(["id"=>$guildInfo->id])->setInc("member");
  419. if($res2 && $res3 && $res4) {
  420. // +message
  421. \app\common\model\Message::addMessage($joininInfo->user_id,"公会审核通知","恭喜,您的加入公会审核通过啦!");
  422. Db::commit();
  423. $this->success("已审核通过!");
  424. }
  425. } else {
  426. if($res2) {
  427. // +message
  428. \app\common\model\Message::addMessage($joininInfo->user_id,"公会审核通知","您的公会加入申请,会长审核拒绝,没关系,调整姿势再来一遍!");
  429. Db::commit();
  430. $this->success("已审核拒绝!");
  431. }
  432. }
  433. $this->error("网络错误,请稍后重试!");
  434. }catch (ValidateException $e) {
  435. Db::rollback();
  436. $this->error($e->getMessage());
  437. } catch (PDOException $e) {
  438. Db::rollback();
  439. $this->error($e->getMessage());
  440. } catch (Exception $e) {
  441. Db::rollback();
  442. $this->error($e->getMessage());
  443. }
  444. }
  445. /**
  446. * 公会申请加入列表
  447. */
  448. public function guildJoinList() {
  449. $user_id = $this->auth->id;
  450. // 获取公会信息
  451. $guildInfo = model('Guild')->where(["user_id"=>$user_id])->find();
  452. if(!$guildInfo) {
  453. $this->success("获取成功",[]);
  454. }
  455. // 先获取申请列表
  456. $where = [];
  457. $where["m.guild_id"] = $guildInfo->id;
  458. $where["m.status"] = 0;
  459. $result = Db::name('guild_joinin')->alias('m')
  460. ->field("m.id,m.user_id,m.createtime,user.is_online,user.onlinetime,user.nickname,user.avatar")
  461. ->join('user','m.user_id = user.id','LEFT')
  462. ->where($where)->order("createtime","desc")->select();
  463. if($result) {
  464. foreach($result as $k => &$v) {
  465. if ($v['is_online'] == 1) {
  466. $onlineText = '在线';
  467. } else {
  468. $onlineTime = get_last_time($v['onlinetime']);
  469. $onlineText = $onlineTime.'在线';
  470. $limitTime = time() - (3600 * 6);
  471. if ($v['onlinetime'] < $limitTime) {
  472. $onlineText = '离线';
  473. }
  474. }
  475. $v['online_text'] = $onlineText;
  476. unset($v['user']);
  477. }
  478. }
  479. $this->success("获取成功",$result);
  480. }
  481. /**
  482. * 公会成员列表
  483. */
  484. public function guildMember() {
  485. $guild_id = input("guild_id",0,"intval"); //公会ID
  486. if($guild_id <= 0) $this->error("参数错误!");
  487. $where = [];
  488. $where["a.guild_id"] = $guild_id;
  489. // 获取公会成员总数
  490. $guildMemberCount = \app\common\model\GuildMember::alias("a")->where($where)->count("id");
  491. // 获取公会成员列表
  492. $guildMemberList = \app\common\model\GuildMember::alias("a")
  493. ->field("a.id,a.user_id,u.avatar,u.nickname,u.gender,a.role,u.is_online,u.onlinetime")
  494. ->join("user u","a.user_id = u.id")
  495. ->where($where)
  496. ->autopage()
  497. ->order("a.role","desc")
  498. ->select();
  499. $role = [0=>'成员',1=>'副会长',2=>'会长'];
  500. if($guildMemberList) {
  501. foreach($guildMemberList as $k => &$v) {
  502. $v["role_text"] = $role[$v["role"]];
  503. if ($v['is_online'] == 1) {
  504. $onlineText = '在线';
  505. } else {
  506. $onlineTime = get_last_time($v['onlinetime']);
  507. $onlineText = $onlineTime.'在线';
  508. $limitTime = time() - (3600 * 6);
  509. if ($v['onlinetime'] < $limitTime) {
  510. $onlineText = '离线';
  511. }
  512. }
  513. $v['online_text'] = $onlineText;
  514. }
  515. }
  516. $res = [];
  517. $res["memberCount"] = $guildMemberCount;
  518. $res["memberList"] = $guildMemberList;
  519. $this->success("获取成功!",$res);
  520. }
  521. /**
  522. * 获取公会成员信息
  523. */
  524. public function getGuildMemberInfo() {
  525. $member_id = input("member_id",0,"intval"); //公会成员ID
  526. if($member_id <= 0) $this->error("参数错误!");
  527. //成员信息
  528. $memberData = model('GuildMember')->field('id,user_id')->find($member_id);
  529. //更新成员的 财富值 ,魅力值,收到的礼物数量
  530. $userService = new UserService();
  531. $userService->updateGuildMember(['user_id'=>$memberData['user_id']]);
  532. //从user表拿一些用户信息
  533. $memberInfo = model('GuildMember')->alias('m')
  534. ->join('user','m.user_id = user.id','LEFT')
  535. ->field('m.id,m.user_id,m.wealth,m.charm,m.gift_num,m.role,m.guild_id,m.createtime,
  536. user.avatar,user.nickname,user.gender,user.logintime,user.is_online,user.onlinetime')
  537. ->where('m.id',$member_id)
  538. ->find();
  539. if(!$memberInfo) $this->error("用户信息获取失败!");
  540. $memberInfo = info_domain_image($memberInfo,['avatar']);
  541. //角色
  542. $role = [0=>'成员',1=>'副会长',2=>'公会长'];
  543. $memberInfo["role_text"] = $role[$memberInfo["role"]];
  544. //在线
  545. if ($memberInfo['is_online'] == 1) {
  546. $onlineText = '在线';
  547. } else {
  548. $onlineTime = get_last_time($memberInfo['onlinetime']);
  549. $onlineText = $onlineTime.'在线';
  550. $limitTime = time() - (3600 * 6);
  551. if ($memberInfo['onlinetime'] < $limitTime) {
  552. $onlineText = '离线';
  553. }
  554. }
  555. $memberInfo['online_text'] = $onlineText;
  556. //我在这个公会的角色
  557. $myGuildMember = model('GuildMember')->field('role')->where(["user_id"=>$this->auth->id,"guild_id"=>$memberInfo->guild_id])->find();
  558. $memberInfo["my_role"] = !empty($myGuildMember) ? $myGuildMember['role'] : 3;
  559. //几个比较大的值,改w单位
  560. $fieldArr = ['wealth','charm','gift_num'];
  561. foreach ($fieldArr as $fkey => $fval) {
  562. if ($memberInfo[$fval] > 10000) {
  563. $memberInfo[$fval] = bcdiv($memberInfo[$fval],10000,2).'w';
  564. }
  565. }
  566. $this->success("获取成功!",$memberInfo);
  567. }
  568. /**
  569. * 公会管理-设为管理员
  570. */
  571. public function guildSetManage() {
  572. $guild_id = input("guild_id",0,"intval"); //公会ID
  573. $member_id = input("member_id",0,"intval"); //成员ID
  574. $type = input("type",1); // 设置类型:1=设为管理员,2=取消管理员
  575. if(!$guild_id || !$member_id || !in_array($type,[1,2])) $this->error("参数缺失!");
  576. $user_id = $this->auth->id;
  577. // 获取公会信息
  578. $guildInfo = \app\common\model\Guild::where(["id"=>$guild_id])->find();
  579. // 验证更新条件
  580. if($user_id !== $guildInfo->user_id) $this->error("身份验证失败!您不是公会长,无权限更改!");
  581. $memberInfo = \app\common\model\GuildMember::where(["id"=>$member_id,'guild_id'=>$guild_id])->find();
  582. if(!$memberInfo) $this->error("会员信息查询失败!");
  583. //取消管理员
  584. if($type == 2) {
  585. $memberInfo->role = 0;
  586. $memberInfo->updatetime = time();
  587. $res = $memberInfo->save();
  588. if($res) {
  589. //[环信]取消聊天室管理员
  590. $easemob = new Easemob();
  591. $easemob->room_demoteRoomAdmin($guildInfo['easemob_room_id'],$memberInfo['user_id']);
  592. $this->success("取消成功!");
  593. } else {
  594. $this->error("网络错误,请稍后重试!");
  595. }
  596. }
  597. //设置管理员
  598. $memberInfo->role = 1;
  599. $memberInfo->updatetime = time();
  600. $res = $memberInfo->save();
  601. if($res) {
  602. // 获取房间id
  603. $party_id = $guildInfo->party_id;
  604. // 添加房间管理员
  605. $redis = new Redis();
  606. $redisconfig = config("redis");
  607. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  608. if ($redisconfig['redis_pwd']) {
  609. $redis->auth($redisconfig['redis_pwd']);
  610. }
  611. if($redisconfig['redis_selectdb'] > 0){
  612. $redis->select($redisconfig['redis_selectdb']);
  613. }
  614. // 获取成员用户信息
  615. $memberuserInfo = \app\common\model\User::field("id,avatar,nickname,gender,level")->where('id',$memberInfo['user_id'])->find();
  616. $data = [];
  617. $data["user_id"] = $memberuserInfo->id;
  618. $data["avatar"] = $memberuserInfo->avatar;
  619. $data["nickname"] = $memberuserInfo->nickname;
  620. $data["gender"] = $memberuserInfo->gender;
  621. $data["item"] = 1;
  622. $data["time"] = 0;
  623. $data["createtime"] = time();
  624. $redis->hSet("party_manage_".$party_id,$memberuserInfo->id."-1",serialize($data));
  625. //[环信]设置群组管理员
  626. $easemob = new Easemob();
  627. $easemob->room_promoteRoomAdmin($guildInfo['easemob_room_id'],$memberInfo['user_id']);
  628. $this->success("设置成功!");
  629. } else {
  630. $this->error("网络错误,请稍后重试!");
  631. }
  632. }
  633. /**
  634. * 公会管理-踢出公会
  635. */
  636. public function guildDelMember() {
  637. $guild_id = input("guild_id",0,"intval"); //公会ID
  638. $member_id = input("member_id",0,"intval"); //成员ID
  639. if(!$guild_id || !$member_id) $this->error("参数缺失!");
  640. // 获取公会信息
  641. $guildInfo = \app\common\model\Guild::where(["id"=>$guild_id])->find();
  642. $memberInfo = \app\common\model\GuildMember::where(["id"=>$member_id,'guild_id'=>$guild_id])->find();
  643. if(!$memberInfo) $this->error("会员信息查询失败!");
  644. if($memberInfo['role'] == 2 || $memberInfo['user_id'] == $guildInfo['user_id']){
  645. $this->error('会长不能被踢出公会');
  646. }
  647. if($memberInfo['role'] == 1 && $this->auth->id !== $guildInfo->user_id) {
  648. $this->error("族长才能踢出管理员");
  649. }
  650. Db::startTrans();
  651. try{
  652. $res1 = $memberInfo->delete();
  653. $res2 = \app\common\model\Guild::where(["id"=>$guild_id])->setDec("member");
  654. if($res1 && $res2) {
  655. Db::commit();
  656. $this->success("踢出成功!");
  657. } else {
  658. $this->error("网络错误,请稍后重试!");
  659. }
  660. }catch (ValidateException $e) {
  661. Db::rollback();
  662. $this->error($e->getMessage());
  663. } catch (PDOException $e) {
  664. Db::rollback();
  665. $this->error($e->getMessage());
  666. } catch (Exception $e) {
  667. Db::rollback();
  668. $this->error($e->getMessage());
  669. }
  670. }
  671. /**
  672. * 公会管理-自己退出公会
  673. */
  674. public function guildMemberQuit() {
  675. $guild_id = input("guild_id",0,"intval"); //公会ID
  676. if(!$guild_id) $this->error("参数缺失!");
  677. // 获取公会信息
  678. $guildInfo = \app\common\model\Guild::where(["id"=>$guild_id])->find();
  679. $memberInfo = \app\common\model\GuildMember::where(["user_id"=>$this->auth->id,'guild_id'=>$guild_id])->find();
  680. if(!$memberInfo) {
  681. $this->error("会员信息查询失败!");
  682. }
  683. if($memberInfo['role'] == 2){
  684. $this->error('公会长不能退出公会');
  685. }
  686. Db::startTrans();
  687. try{
  688. $res1 = $memberInfo->delete();
  689. $res2 = \app\common\model\Guild::where(["id"=>$guild_id])->setDec("member");
  690. if($res1 && $res2) {
  691. Db::commit();
  692. $this->success("退出成功!");
  693. } else {
  694. $this->error("网络错误,请稍后重试!");
  695. }
  696. }catch (ValidateException $e) {
  697. Db::rollback();
  698. $this->error($e->getMessage());
  699. } catch (PDOException $e) {
  700. Db::rollback();
  701. $this->error($e->getMessage());
  702. } catch (Exception $e) {
  703. Db::rollback();
  704. $this->error($e->getMessage());
  705. }
  706. }
  707. /**
  708. * 公会贡献榜排行
  709. * @return void
  710. */
  711. public function getRankList()
  712. {
  713. try {
  714. $id = input('guild_id',0);
  715. $guildMemberWhere['guild_id'] = $id;
  716. // $guildMemberWhere['status'] = 1;
  717. $guildMember = model('GuildMember')->field('id,user_id,role')->where($guildMemberWhere)->select();
  718. $list = [];
  719. if (!empty($guildMember)) {
  720. $userIds = array_column($guildMember,'user_id');
  721. $where = [];
  722. $where['a.user_to_id'] = ['in', $userIds];
  723. $list = model('GiftUserParty')->alias("a")
  724. ->field('sum(a.value) as total_price,a.user_to_id,u.avatar,u.nickname,u.is_online,u.onlinetime')
  725. ->where($where)
  726. ->join('user u','u.id = a.user_to_id')
  727. ->group('a.user_to_id')
  728. ->order('total_price',"desc")
  729. ->autopage()
  730. ->select();
  731. if (!empty($list)) {
  732. $role = [0=>'成员',1=>'副会长',2=>'会长'];
  733. $guildMemberData = [];
  734. foreach ($guildMember as $mKey => $mVal) {
  735. $guildMemberData[$mVal['user_id']] = [
  736. 'id' => $mVal['id'],
  737. 'role' => $mVal['role'],
  738. 'role_text' => isset($role[$mVal['role']]) ? $role[$mVal['role']] : '',
  739. ];
  740. }
  741. foreach ($list as $key => &$value) {
  742. $member = isset($guildMemberData[$value['user_to_id']]) ? $guildMemberData[$value['user_to_id']] : [];
  743. $value['member_id'] = isset($member['id']) ? $member['id'] : 0;
  744. $value['role'] = isset($member['role']) ? $member['role'] : 0;
  745. $value['role_text'] = isset($member['role_text']) ? $member['role_text'] : '';
  746. if ($value['is_online'] == 1) {
  747. $onlineText = '在线';
  748. } else {
  749. $onlineTime = get_last_time($value['onlinetime']);
  750. $onlineText = $onlineTime.'在线';
  751. $limitTime = time() - (3600 * 6);
  752. if ($value['onlinetime'] < $limitTime) {
  753. $onlineText = '离线';
  754. }
  755. }
  756. $value['online_text'] = $onlineText;
  757. $value['total_price_text'] = $value['total_price'];
  758. if ($value['total_price'] >= 10000) {
  759. $value['total_price_text'] = bcdiv($value['total_price'],10000,1).'w';
  760. }
  761. unset($value['total_price']);
  762. }
  763. }
  764. }
  765. $this->success(__('Operation completed'),$list);
  766. } catch (Exception $e) {
  767. $this->error($e->getMessage());
  768. }
  769. }
  770. /**
  771. * 获取公会状态
  772. * @return void
  773. */
  774. public function getStatus()
  775. {
  776. $id = input('guild_id',0);
  777. if (!empty($id)) {
  778. $where['id'] = $id;
  779. } else {
  780. $where['user_id'] = $this->auth->id;
  781. }
  782. $field = 'id,status';
  783. $guild = model('Guild')->field($field)->where($where)->find();
  784. $this->success('获取成功', $guild);
  785. }
  786. ///////////////没用到的///////////
  787. /**
  788. * 开厅申请
  789. */
  790. public function guildApply() {
  791. $user_id = $this->auth->id;
  792. // 获取用户实名认证信息
  793. $authUserRealname = \app\common\model\UserAuth::where(["user_id"=>$user_id])->value("realname");
  794. if(!$authUserRealname) $this->error("未查询到实名认证信息,请实名认证先!");
  795. // 获取用户信息
  796. $userInfo = \app\common\model\User::field("id,mobile,nickname")->where(["id"=>$user_id])->find();
  797. $data = [];
  798. $data["user_id"] = $user_id;
  799. // 查看是否申请过
  800. $applyInfo = \app\common\model\GuildApply::where($data)->find();
  801. if($applyInfo && $applyInfo->status>=0) $this->error("您已经发送过开厅申请了,请勿重复发送!");
  802. // 查看是否有加入公会的申请
  803. $joinInfo = \app\common\model\GuildJoinin::where(["user_id"=>$user_id,"status"=>0])->find();
  804. if($joinInfo) $this->error("您存在申请加入公会信息,请联系公会长处理完毕后方可申请开厅!");
  805. // 查看是已经是公会成员
  806. $memberInfo = \app\common\model\GuildMember::where(["user_id"=>$user_id])->find();
  807. if($memberInfo) $this->error("您已存在签约中的公会,无法开新厅!");
  808. Db::startTrans();
  809. try{
  810. $data["user_name"] = $authUserRealname;
  811. $data["user_nickname"] = $userInfo->nickname;
  812. $data["user_mobile"] = $userInfo->mobile;
  813. $data["createtime"] = time();
  814. $res1 = \app\common\model\GuildApply::insert($data);
  815. // 更新用户状态
  816. $userInfo->is_guild = 1;
  817. $res2 = $userInfo->save();
  818. if($res1 && $res2) {
  819. Db::commit();
  820. $this->success("申请发送成功!");
  821. } else {
  822. $this->error("网络错误,请稍后重试!");
  823. }
  824. }catch (ValidateException $e) {
  825. Db::rollback();
  826. $this->error($e->getMessage());
  827. } catch (PDOException $e) {
  828. Db::rollback();
  829. $this->error($e->getMessage());
  830. } catch (Exception $e) {
  831. Db::rollback();
  832. $this->error($e->getMessage());
  833. }
  834. }
  835. /**
  836. * 公会申请页面信息
  837. */
  838. public function guildApplyInfo() {
  839. $guild_id = input("guild_id"); //公会ID
  840. if($guild_id <= 0) $this->error("参数缺失!");
  841. $guildInfo = \app\common\model\Guild::alias("a")
  842. ->join("user u","u.id = a.user_id","inner")
  843. ->field("a.id,a.image,a.name,a.g_id,a.user_id,u.avatar,u.nickname,u.u_id")
  844. ->where(["a.id"=>$guild_id])
  845. ->find();
  846. $guildInfo["status"] = 0;
  847. // 获取申请用户是否已经有正在申请的记录
  848. $user_id = $this->auth->id;
  849. $joinStatus = \app\common\model\GuildJoinin::where(["user_id"=>$user_id])->value("status");
  850. if($joinStatus === 0) $guildInfo["status"] = 1;
  851. $Member = \app\common\model\GuildMember::where(["user_id"=>$user_id])->value("id");
  852. if($Member > 0) $guildInfo["status"] = 2;
  853. $this->success("获取成功!",$guildInfo);
  854. }
  855. }