Guild.php 36 KB

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