Guild.php 36 KB

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