Chat_rooms.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blues
  5. * Date: 2018/12/1
  6. * Time: 15:06
  7. */
  8. namespace App\Api\Model;
  9. use phpDocumentor\Reflection\Types\Mixed_;
  10. use \System\Model;
  11. class Chat_rooms extends Model {
  12. public $timestamps = false;
  13. protected $table = 'chat_rooms';
  14. static $room_info;
  15. public function getRoomList(int $vip_level) {
  16. $cond = ['status' => 1];
  17. if ($vip_level === -1) {
  18. $vip_level = 0;
  19. }
  20. if ($vip_level === -2) {
  21. $cond['limit_vip'] = -2;
  22. } else {
  23. $cond['limit_vip'] = ['<=', $vip_level];
  24. }
  25. return self::where($cond)->whereOr('limit', 0)->get();
  26. }
  27. public function getRoomListByGameName(int $vip_level, string $game_name) {
  28. $cond = ['status' => 1];
  29. if ($vip_level === -1) {
  30. $vip_level = 0;
  31. }
  32. $cond['game_name'] = $game_name;
  33. if ($vip_level === -2) {
  34. $cond['limit_vip'] = -2;
  35. } else {
  36. $cond['limit_vip'] = ['<=', $vip_level];
  37. }
  38. return self::where($cond)->get();
  39. }
  40. /**
  41. * 代理身份校验(必要条件)
  42. * @param int $room_id
  43. * @param string $account
  44. * @return bool
  45. */
  46. public static function agentCheck(int $room_id, string $account): bool {
  47. //代理房间验证
  48. if ((new self)->isAdmin(null, $room_id, $_SESSION['uinfo']['account'])) return true;
  49. $root_agent = self::getRootAgent($account);
  50. if (!self::$room_info) {
  51. self::$room_info = self::where('id', $room_id)->first();
  52. }
  53. $room_info = self::$room_info;
  54. if (!$room_info) {
  55. return false;
  56. }
  57. if (intval($room_info->room_type) === 1) {
  58. return true;
  59. }
  60. return $root_agent === $room_info->agentname;
  61. }
  62. /**
  63. * VIP等级校验(必要条件)
  64. * @param int $room_id
  65. * @param int $vip_level
  66. * @return bool
  67. */
  68. public static function vipCheck(int $room_id, int $vip_level): bool {
  69. //vip等级验证
  70. if ((new self)->isAdmin(null, $room_id, $_SESSION['uinfo']['account'])) return true;
  71. if ($vip_level === -1) {
  72. $vip_level = 0;
  73. }
  74. if (!self::$room_info) {
  75. self::$room_info = self::where('id', $room_id)->first();
  76. }
  77. $room_level = self::$room_info->limit_vip;
  78. if ($room_level === -2) {
  79. if ($vip_level === 0 || $vip_level === -1) {
  80. return false;
  81. }
  82. return true;
  83. } elseif ($room_level === 0 || $room_level === -1) return true;
  84. else
  85. return $room_level <= $vip_level;
  86. }
  87. /**
  88. * 红包超领校验,true=pass
  89. * @param int $room_id
  90. * @param string $account_identity
  91. * @param int $vip_level
  92. * @return bool
  93. */
  94. public static function overTakeCheck(int $room_id, string $account_identity, int $vip_level): bool {
  95. //红包个数验证
  96. if ((new self)->isAdmin(null, $room_id, $_SESSION['uinfo']['account'])) return true;
  97. $redpackNum = (new LuckyMoneyTake())->where('account_identity', $account_identity)->select('hash')->count();
  98. if (!self::$room_info) {
  99. self::$room_info = self::where('id', $room_id)->first();
  100. }
  101. $targetRoom = self::$room_info;
  102. $targetLevel = $targetRoom->redbag_grade;//红包限制等级
  103. $targetLimit = $targetRoom->redbag_limit;//限制等级会员的红包总可领取个数
  104. if ($targetLevel === -2) {
  105. if ($vip_level === 0 || $vip_level === -1) {
  106. return $targetLimit > $redpackNum;
  107. } else {
  108. return true;
  109. }
  110. } elseif ($targetLevel === 0 || $targetLevel === -1) {
  111. return true;
  112. } else {
  113. if ($targetLevel <= $vip_level) {
  114. return true;
  115. }
  116. return $targetLimit > $redpackNum;
  117. }
  118. }
  119. public static function isAgentRoom($room_id) {
  120. return self::where('id', $room_id)->first()->room_type === 2 ? true : false;
  121. }
  122. public static function isAgentUser(string $account, $parent_path, int $room_id): bool {
  123. if (!self::$room_info) {
  124. self::$room_info = self::where('id', $room_id)->first();
  125. }
  126. if (self::$room_info->room_type !== 2) return true;
  127. $p_identity = explode(',', $parent_path);
  128. $ar = [];
  129. if (!empty($p_identity)) {
  130. foreach ($p_identity as $key => $val) {
  131. $parents = lm('Account', 'Api')->where('identity', $val)->where('account', '!=', 'root')->first(['identity', 'account']);
  132. $parents = $parents ? $parents->toarray() : [];
  133. if ($parents) array_unshift($ar, $parents['account']);
  134. }
  135. }
  136. $ar[] = $account;
  137. return in_array(self::$room_info->agentname, $ar);
  138. }
  139. protected function getRootAgent(string $account_identity): string {
  140. $agent = lm('nagent_detailed', 'commons')->where('agent_user', $account_identity)->first(['parent_path', 'parent_id']);
  141. $parent_path = $agent->parent_path ?? '';
  142. $rootUUID = explode(',', $parent_path)[0];
  143. if (!$rootUUID) return '';
  144. return lm('Account', 'Api')->where('identity', $rootUUID)->first()->account ?? '';
  145. }
  146. /**
  147. * 红包领取规则验证
  148. * @param $room_id
  149. * @return mixed
  150. */
  151. public function redPackTakeRule($room_id) {
  152. //房间打赏规则
  153. $roomInfo = $this
  154. ->where('id', $room_id)
  155. ->where('status', 1)
  156. ->first(['redbag_grade', 'redbag_limit', 'redbag_max_money', 'redbag_max_no']);
  157. if (!$roomInfo) {
  158. //房间不存在或已关闭
  159. return -7000;
  160. }
  161. if ($this->isAdmin($roomInfo, $room_id, $_SESSION['uinfo']['name'])) return $roomInfo;
  162. $uuid = $_SESSION['uinfo']['identity'];
  163. //取得当前用户当天领取红包的总个数
  164. $luckyMoneyTakeNum = lm('LuckyMoneyTake', 'Api')
  165. ->where('room_id', $room_id)
  166. ->where('account_identity', $uuid)
  167. ->where('time', '>=', strtotime('-1 day'))
  168. ->whereIn('type', [0, 1])
  169. ->count('id');
  170. $roomInfo->lucky_money_take_num = $luckyMoneyTakeNum;//今日领取红包的总个数
  171. $level = $_SESSION['uinfo']['level'];
  172. $minLevel = (int)$roomInfo->redbag_grade;
  173. if ($minLevel === -2) {
  174. $sw = ($level === -2 || $level > 0);
  175. } else if ($minLevel > 0) {
  176. $sw = ($level >= $minLevel);
  177. } else {
  178. $sw = !($level === 0 || $level === -1);
  179. }
  180. // $roomInfo->canTake = $sw;
  181. if (!$sw && $luckyMoneyTakeNum >= $roomInfo->redbag_limit) {
  182. //可领个数超出限制
  183. return -70162;
  184. }
  185. return $roomInfo;
  186. }
  187. /**
  188. * 房间用户昵称列表
  189. * @param int $room_id
  190. * @return array
  191. */
  192. public function getNickNames(int $room_id): array {
  193. $lm = (new Chat_rooms())->where('id', $room_id)->first();
  194. $manArr = [];
  195. if ($lm) {
  196. $mans = trim($lm->nickname_list) . ',';
  197. $tmp = explode(',', $mans);
  198. foreach ($tmp as $k => $v) {
  199. if (!$v) continue;
  200. list ($kk, $vv) = explode('||', $v);
  201. $manArr[$kk] = $vv;
  202. }
  203. $manArr['room_mangers_id'] = $lm['room_mangers_id'];
  204. }
  205. return $manArr;
  206. }
  207. /**
  208. * 验证用户是否是管理员
  209. * @param $roomObj
  210. * @param int $room_id
  211. * @param string $user_name
  212. * @return mixed
  213. */
  214. public function isAdmin( $roomObj, int $room_id, string $user_name){
  215. $roomObj ?: $roomObj = $this->where('id', $room_id)->first();
  216. if (!$roomObj) return false;
  217. $admins = explode(',', $roomObj->room_mangers_id);
  218. return in_array($user_name, $admins);
  219. }
  220. }