MemGameModel.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /**
  3. * MemGameModel.php UTF-8
  4. * 玩家游戏
  5. *
  6. * @date : 2018/1/19 18:40
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\member;
  13. use huo\controller\game\GameCache;
  14. use huo\model\common\CommonModel;
  15. use huo\model\game\GameModel;
  16. use huo\model\user\UserModel;
  17. use huolib\constant\AccountConst;
  18. use huolib\constant\CacheConst;
  19. use huolib\constant\CommonConst;
  20. use huolib\constant\GameConst;
  21. use huolib\constant\OrderConst;
  22. use huolib\tool\Time;
  23. use think\Cache;
  24. class MemGameModel extends CommonModel {
  25. const IS_DEFAULT_FALSE = 1;
  26. const IS_DEFAULT_TRUE = 2;
  27. const STATUS_NORMAL = 2;
  28. const STATUS_LOCKED = 1;
  29. protected $name = 'mem_game';
  30. //设置只读字段
  31. protected $readonly = ['app_id'];
  32. // 开启自动写入时间戳字段
  33. protected $autoWriteTimestamp = true;
  34. protected $cache_prefix = CacheConst::CACHE_MEM_GAME_CNT_PREFIX;
  35. public function game() {
  36. return $this->hasOne('huo\model\game\GameModel', 'id', 'app_id')->setEagerlyType(0);
  37. }
  38. public function game2() {
  39. return $this->hasOne('huo\model\game\GameModel', 'id', 'app_id')->field('id,name,icon,publicity oneword');
  40. }
  41. public function gamemini() {
  42. return $this->hasOne('huomp\model\game\GameMiniModel', 'app_id', 'app_id')->field(
  43. 'app_id,mini_app_id,need_popup,entrance_image'
  44. );
  45. }
  46. public function gameext() {
  47. return $this->hasOne('huo\model\game\GameextModel', 'app_id', 'app_id')->field('app_id,down_cnt');
  48. }
  49. public function mem() {
  50. return $this->hasOne('huo\model\member\MemberModel', 'id', 'mem_id')->field('id,username');
  51. }
  52. public function leftmem() {
  53. return $this->belongsTo('huo\model\member\MemberModel', 'mem_id', 'id')
  54. ->setEagerlyType(0);
  55. }
  56. public function pay() {
  57. return $this->hasMany('huo\model\order\OrderModel', 'mg_mem_id')
  58. ->where(['status' => OrderConst::PAY_STATUS_SUC])
  59. ->field('mg_mem_id,sum(real_amount) sum')->group('mg_mem_id');
  60. }
  61. public function paystat() {
  62. return $this->hasOne('huo\model\order\OrderModel', 'mg_mem_id')
  63. ->where(['status' => OrderConst::PAY_STATUS_SUC])
  64. ->field('mg_mem_id,sum(amount) sum_amount')->group('mg_mem_id');
  65. }
  66. /**
  67. * 关联game_version表
  68. *
  69. * @return \think\model\relation\HasMany
  70. */
  71. public function gv() {
  72. return $this->hasMany('huo\model\game\GameversionModel', 'app_id')->field('app_id,package_url');
  73. }
  74. public function guidedagent() {
  75. return $this->belongsTo(UserModel::className(), 'guided_agent_id', 'id')
  76. ->field('id,user_login,user_nicename,role_id');
  77. }
  78. /**
  79. * mg_game登陆
  80. *
  81. * @param $mem_data
  82. * @param $mem_id
  83. * @param $app_id
  84. *
  85. * @return array|bool|false
  86. */
  87. public function login($mem_data, $mem_id, $app_id) {
  88. if (empty($app_id)) {
  89. return true;
  90. }
  91. $_map['mem_id'] = $mem_id;
  92. $_map['app_id'] = $app_id;
  93. $_map['is_default'] = 2;
  94. /* 获取关联的游戏共用mg_mem_id */
  95. $_link_app_id = (new GameModel())->getLinkAppId($app_id);
  96. $_app_ids = [$app_id];
  97. if (!empty($_link_app_id)) {
  98. $_app_ids[] = $_link_app_id;
  99. $_game_data = GameCache::ins()->getInfoByAppId($app_id);
  100. if ($_game_data['classify'] == GameConst::GAME_H5) {
  101. // 管理后台,H5游戏关联主包小游戏
  102. $_same_series_app_ids = (array)(new GameModel())->getIdsByWhere(['apple_id' => $_link_app_id]);
  103. $_app_ids = array_merge($_app_ids, $_same_series_app_ids);
  104. }
  105. }
  106. $_map['app_id'] = ['in', $_app_ids];
  107. $_mg_data = $this->where($_map)->order('id ASC')->find();
  108. if (is_object($_mg_data)) {
  109. $_mg_data = $_mg_data->toArray();
  110. }
  111. if (empty($_mg_data)) {
  112. $_guided_agent_id = $mem_data['guided_agent_id'] ?? 0;
  113. if (empty($_guided_agent_id)) {
  114. $_guided_agent_id = $mem_data['agent_id'] ?? 0;
  115. }
  116. $_data['mem_id'] = $mem_id;
  117. $_data['app_id'] = $app_id;
  118. $_data['guided_agent_id'] = $_guided_agent_id;
  119. $_data['nickname'] = get_val($mem_data, 'nickname', '');
  120. $_data['create_time'] = time();// 取用当前时间
  121. $_data['update_time'] = $_data['create_time'];
  122. $_data['is_default'] = 2;
  123. if ($_obj = self::create($_data, true)) {
  124. $_data['id'] = $_obj->id;
  125. } else {
  126. return false;
  127. }
  128. unset($_map['is_default']);
  129. $_key_map = $_map;
  130. $_cache_key = $this->cache_prefix.md5(json_encode($_key_map));
  131. Cache::set($_cache_key, 1);
  132. $_key_map['update_time'] = $_data['update_time'];
  133. $_time_cache_key = $this->cache_prefix.md5(json_encode($_key_map));
  134. list($_start_time, $_end_time) = Time::today();
  135. $_diff_time = $_end_time - time();
  136. Cache::set($_time_cache_key, 1, $_diff_time);
  137. $_mg_data = $_data;
  138. } else {
  139. $_mg_data['mem_id'] = $mem_id;
  140. $_mg_data['app_id'] = $app_id;
  141. $_mg_data['nickname'] = get_val($mem_data, 'nickname', '');
  142. $_mg_data['update_time'] = time();
  143. $_mg_data['is_default'] = 2;
  144. unset($_map['is_default']);
  145. $_key_map = $_map;
  146. $_key_map['update_time'] = $_mg_data['update_time'];
  147. $_time_cache_key = $this->cache_prefix.md5(json_encode($_key_map));
  148. list($_start_time, $_end_time) = Time::today();
  149. $_diff_time = $_end_time - time();
  150. Cache::set($_time_cache_key, 1, $_diff_time);
  151. $_rs = self::update($_mg_data);
  152. if (false === $_rs) {
  153. return false;
  154. }
  155. }
  156. return $_mg_data;
  157. }
  158. /**
  159. * 新增数据
  160. *
  161. * @param $data
  162. *
  163. * @return bool|mixed
  164. */
  165. public function addData($data) {
  166. $_data = $data;
  167. if ($_obj = self::create($_data, true)) {
  168. return $_obj->id;
  169. } else {
  170. return false;
  171. }
  172. }
  173. public function updateData($data, $id) {
  174. $_map['id'] = $id;
  175. $_data = $data;
  176. $_rs = self::update($_data, $_map);
  177. if (false === $_rs) {
  178. return false;
  179. } else {
  180. return true;
  181. }
  182. }
  183. /**
  184. * 获取玩家玩过的游戏id列表
  185. *
  186. * @param $mem_id
  187. *
  188. * @return array
  189. */
  190. public function getAppIds($mem_id) {
  191. return $this->where(['mem_id' => $mem_id])->column('app_id');
  192. }
  193. /**
  194. * 判断游戏是否在有打开过
  195. *
  196. * @param int $mem_id
  197. * @param int $app_id
  198. * @param int $time //大于0 则表示判断该时间以后
  199. *
  200. * @return bool true 有登陆 false 没登陆
  201. */
  202. public function hasOpen($mem_id, $app_id, $time = 0) {
  203. $_map['mem_id'] = $mem_id;
  204. $_map['app_id'] = $app_id;
  205. $_cache_time = CommonConst::CONST_DAY_SECONDS;
  206. if (!empty($time)) {
  207. $_map['update_time'] = ['gt', $time];
  208. list($_start_time, $_end_time) = Time::today();
  209. $_cache_time = $_end_time - time();
  210. }
  211. $_cache_key = $this->cache_prefix.md5(json_encode($_map));
  212. $_cnt = Cache::get($_cache_key);
  213. if (is_numeric($_cnt)) {
  214. if (empty($_cnt)) {
  215. return false;
  216. }
  217. return true;
  218. }
  219. $_cnt = $this->where($_map)->count();
  220. if (empty($_cnt)) {
  221. Cache::set($_cache_key, 0, $_cache_time);
  222. return false;
  223. }
  224. Cache::set($_cache_key, 1, $_cache_time);
  225. return true;
  226. }
  227. /**
  228. * 删除打开状态缓存
  229. *
  230. * @param $mem_id
  231. * @param $app_id
  232. * @param int $time
  233. */
  234. public function rmHasOpen($mem_id, $app_id, $time = 0) {
  235. $_map['mem_id'] = $mem_id;
  236. $_map['app_id'] = $app_id;
  237. if (!empty($time)) {
  238. $_map['update_time'] = ['gt', $time];
  239. }
  240. $_cache_key = $this->cache_prefix.md5(json_encode($_map));
  241. Cache::rm($_cache_key);
  242. }
  243. /**
  244. * 获取mg_mem_id
  245. *
  246. * @param $app_id
  247. * @param $mem_id
  248. *
  249. * @return int|mixed
  250. */
  251. public function getMgMemId($app_id, $mem_id) {
  252. $_map = ['app_id' => $app_id, 'mem_id' => $mem_id];
  253. $_mg_mem_id = $this->where($_map)->value('id');
  254. if (empty($_mg_mem_id)) {
  255. return 0;
  256. }
  257. return $_mg_mem_id;
  258. }
  259. public function getInfoByAppMemId($app_id, $mem_id) {
  260. $_map = ['app_id' => $app_id, 'mem_id' => $mem_id];
  261. $_data = $this->where($_map)->find();
  262. if (is_object($_data)) {
  263. $_data = $_data->toArray();
  264. }
  265. return $_data;
  266. }
  267. public function getMemGameIds($app_ids, $mem_id) {
  268. $_map = ['app_id' => ['in', $app_ids], 'mem_id' => $mem_id];
  269. $_mg_mem_id = $this->where($_map)->order('id ASC')->column('id');
  270. if (empty($_mg_mem_id)) {
  271. return [];
  272. }
  273. return $_mg_mem_id;
  274. }
  275. /**
  276. * 通过昵称获取玩家id
  277. *
  278. * @param $nickname
  279. *
  280. * @return array
  281. */
  282. public function getIdsByNickname($nickname) {
  283. $_map = ['nickname' => ['like', $nickname.'%']];
  284. $_ids = $this->where($_map)->column('id');
  285. return $_ids;
  286. }
  287. public function getNicknameById($id) {
  288. $_data = $this->getInfoById($id);
  289. return get_val($_data, 'nickname', '');
  290. }
  291. /**
  292. * 获取默认小号ID
  293. *
  294. * @param int $mem_id 玩家ID
  295. * @param int $app_id 应用ID
  296. *
  297. * @return int
  298. */
  299. public function getDefaultMgIdByMemAppId($mem_id, $app_id) {
  300. if (empty($mem_id) || empty($app_id)) {
  301. return 0;
  302. }
  303. /* 取默认 */
  304. $_map = [
  305. 'mem_id' => $mem_id,
  306. 'app_id' => $app_id,
  307. 'is_default' => CommonConst::CONST_DEFAULT,
  308. 'status' => AccountConst::ACCOUNT_STATUS_UNLOCKED
  309. ];
  310. /* 获取关联的游戏共用mg_mem_id */
  311. $_link_app_id = (new GameModel())->getLinkAppId($app_id);
  312. if (!empty($_link_app_id)) {
  313. $_map['app_id'] = ['in', [$app_id, $_link_app_id]];
  314. }
  315. $_id = $this->where($_map)->order('update_time desc')->value('id');
  316. if (!empty($_id)) {
  317. return $_id;
  318. }
  319. /* 无默认 取最近登录游戏 */
  320. unset($_map['is_default']);
  321. $_id = $this->where($_map)->order('update_time desc')->value('id');
  322. if (empty($_id)) {
  323. /* 无正常值 */
  324. return 0;
  325. }
  326. return $_id;
  327. }
  328. }