AccountRepository.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. <?php
  2. namespace Biz\Account\Repository;
  3. use Biz\TokenManager;
  4. use App\Commons\Model\Account_detailed;
  5. /**
  6. * Created by PhpStorm.
  7. * User: wang
  8. * Date: 2017/6/13
  9. * Time: 9:23
  10. */
  11. class AccountRepository {
  12. private $tokenManager;
  13. public function __construct() {
  14. $this->tokenManager = new TokenManager();
  15. }
  16. /**
  17. * 验证用户状态
  18. *
  19. * @param $userInfo
  20. * @return array
  21. */
  22. public function checkAccountStatus($userInfo) {
  23. $result = [];
  24. if ($userInfo->status == 0) {
  25. $result['status'] = -4001;
  26. $result['msg'] = lang()->get('sorry, this user does not exist');
  27. } elseif ($userInfo->status == -1) {
  28. $result['status'] = -4007;
  29. $result['msg'] = lang()->get('sorry, the account has been disabled');
  30. } elseif ($userInfo->status == -2) {
  31. $result['status'] = -4007;
  32. $result['msg'] = lang()->get('Sorry, the account is unusual');
  33. } elseif ($userInfo->status == 1) {
  34. $result = false;
  35. }
  36. return $result;
  37. }
  38. /**
  39. * 添加密码
  40. *
  41. * @param $password
  42. * @param $identity
  43. * @throws \Exception
  44. */
  45. public function addPassword($password, $identity) {
  46. $pwd = GenPassword($password);
  47. $accountPassword = [
  48. 'account_identity' => $identity,
  49. 'identity' => UUID(),
  50. 'encryption' => $pwd['encryption'],
  51. 'account_password' => $pwd['password'],
  52. ];
  53. $res = lm('account_password', 'Commons')->insert($accountPassword);
  54. if (!$res) {
  55. return -4113;
  56. }
  57. return 1;
  58. }
  59. /**
  60. * 验证两次密码是否一致
  61. *
  62. * @param $password
  63. * @param $againPassword
  64. * @return bool
  65. */
  66. public function checkPassword($password, $againPassword) {
  67. if ($password != $againPassword) {
  68. $result['status'] = -4006;
  69. $result['msg'] = lang()->get('the two password is different');
  70. return $result;
  71. } else {
  72. return false;
  73. }
  74. }
  75. /**
  76. * 添加用户详情
  77. *
  78. * @param $data
  79. * @param $identity
  80. * @throws \Exception
  81. */
  82. public function addAccountDetailed($data, $identity) {
  83. $invitation = isset($data['introduce_user']) ? $data['introduce_user'] : '';
  84. $email = isset($data['email']) ? $data['email'] : '';
  85. $qq = isset($data['qq']) ? $data['qq'] : '';
  86. $phone = isset($data['phone']) ? $data['phone'] : '';
  87. $wechat = isset($data['wechat']) ? $data['wechat'] : '';
  88. $name = isset($data['name']) ? $data['name'] : '';
  89. $open_invitation = isset($data['open_invitation']) ? $data['open_invitation'] : '';
  90. $parent_id = isset($data['parent_id']) ? $data['parent_id'] : '';
  91. $parent_path = isset($data['parent_path']) ? $data['parent_path'] : '';
  92. $level = isset($data['level']) ? $data['level'] : '';
  93. $wagent_name = isset($data['wagent_name']) ? $data['wagent_name'] : '';
  94. $accountDetailed = [
  95. 'phone' => $phone,
  96. 'invitation' => $invitation,
  97. 'register_time' => date('Y-m-d H:i:s'),
  98. 'register_ip' => GETIP(),
  99. 'last_ip' => GETIP(),
  100. 'account_identity' => $identity,
  101. 'identity' => UUID(),
  102. 'email' => $email,
  103. 'wechat' => $wechat,
  104. 'name' => $name,
  105. 'qq' => $qq,
  106. 'open_invitation' => $open_invitation,
  107. ];
  108. if (!empty($parent_id)) {
  109. $accountDetailed['parent_id'] = $parent_id;
  110. }
  111. if (!empty($parent_path)) {
  112. $accountDetailed['parent_path'] = $parent_path;
  113. }
  114. if (!empty($level)) {
  115. $accountDetailed['level'] = $level;
  116. }
  117. if (!empty($wagent_name)){
  118. $accountDetailed['wagent_name'] = $wagent_name;
  119. }
  120. $res = lm('account_detailed', 'Commons')->insert($accountDetailed);
  121. if (!$res) {
  122. return -30101;
  123. }
  124. return 1;
  125. }
  126. /**
  127. * 用户注册数据验证
  128. *
  129. * @param $data
  130. * @return array|bool
  131. */
  132. public function checkRegisterData($data) {
  133. if (!$data['account']) {
  134. $result = ['status' => -4010, 'msg' => lang()->get('account cannot be empty')];
  135. return $result;
  136. }
  137. if (!$data['password']) {
  138. $result = ['status' => -4011, 'msg' => lang()->get('password cannot be empty')];
  139. return $result;
  140. }
  141. if (!$data['again_password']) {
  142. $result = ['status' => -4012, 'msg' => lang()->get('verify that the password is not empty')];
  143. return $result;
  144. }
  145. if (empty($data['name'])) {
  146. $result = ['status' => -4013, 'msg' => lang()->get('name is empty')];
  147. return $result;
  148. }
  149. if(empty($data['phone'])){
  150. $result = ['status' => -4300, 'msg' => lang()->get('phone is empty')];
  151. return $result;
  152. }else{
  153. $account_detailed = new Account_detailed();
  154. $where['phone'] = $data['phone'];
  155. $num = $account_detailed->getUsernum($where);
  156. if($num != 0){
  157. $result = ['status' => -4301, 'msg' => lang()->get('Existing mobile phone number')];
  158. return $result;
  159. }
  160. }
  161. return false;
  162. }
  163. /**
  164. * 登录是否过期验证
  165. *
  166. * @param $account
  167. * @return array
  168. * @throws \Exception
  169. */
  170. public function checkEffectiveTime($account, $effective = 1800) {
  171. $accountToken = lm('account_token', 'Commons')->where('account_identity', $account->account_identity)->first();
  172. if (strtotime('now') > ($accountToken->effective_time + $effective)) {
  173. return $this->updateToken($account->account_identity);
  174. } else {
  175. return $this->updateEffectiveTime($account->account_identity, $account->token);
  176. }
  177. }
  178. /**
  179. * 登录过期刷新token
  180. *
  181. * @param $accountIdentity
  182. * @return array
  183. * @throws \Exception
  184. */
  185. public function updateToken($accountIdentity) {
  186. $token = $this->tokenManager->getToken();
  187. lm('account_detailed', 'Commons')->where('account_identity', $accountIdentity)->update(['token' => $token]);
  188. //lm('agent_detailed', 'Commons')->where('agent_identity', $accountIdentity)->update(['agent_token' => $token]);
  189. unset($_SESSION['uinfo']);
  190. unset($_SESSION['agent']);
  191. $result = ['status' => 1, 'msg' => lang()->get('success')];
  192. return $result;
  193. }
  194. /**
  195. * 更新登录过期时间
  196. *
  197. * @param $accountIdentity
  198. * @return array
  199. * @throws \Exception
  200. */
  201. public function updateEffectiveTime($accountIdentity, $token) {
  202. lm('account_token', 'Commons')->where('account_identity', $accountIdentity)->update(['effective_time' => strtotime('now'), 'last_time' => date('Y-m-d H:i:s')]);
  203. lm('account_detailed', 'Commons')->where('account_identity', $accountIdentity)->update(['last_time' => date('Y-m-d H:i:s')]);
  204. $userInfo = $this->tokenManager->getAccountInfo($token);
  205. if (!$userInfo)
  206. return ['data' => '', 'status' => '-4001', 'msg' => lang()->get('-4001')];
  207. $data = $_SESSION['uinfo'] = $userInfo->toArray();
  208. $result = ['data' => $data, 'status' => 1, 'msg' => lang()->get('success')];
  209. return $result;
  210. }
  211. /**
  212. * 修改支付密码,验证原密码
  213. *
  214. * @param $token
  215. * @param $oldPassword
  216. * @return array|bool
  217. * @throws \Exception
  218. */
  219. public function checkOldPayPassword($token, $oldPassword) {
  220. $accountDetailed = lm('account_detailed', 'Commons')->where('token', $token)->first();
  221. if (empty($accountDetailed)) {
  222. $result = ['status' => -4011, 'msg' => lang()->get('user error')];
  223. return $result;
  224. }
  225. $payPassword = lm('pay_password', 'Commons')->where('account_identity', $accountDetailed->account_identity)->first();
  226. if (empty($payPassword)) {
  227. $result = ['status' => -4012, 'msg' => lang()->get('no payment password, please set the payment password first')];
  228. return $result;
  229. }
  230. if ($payPassword->pay_password != md5(md5($payPassword->encryption . $oldPassword))) {
  231. $result = ['status' => -4013, 'msg' => lang()->get('old password error')];
  232. return $result;
  233. }
  234. return ['status' => 1, 'account_identity' => $accountDetailed->account_identity];
  235. }
  236. /**
  237. * 销毁超过3天的免费试玩账号
  238. *
  239. * @param $account
  240. * @throws \Exception
  241. */
  242. public function destroyAccount($account) {
  243. $gameInfo = lm('game_type', 'Commons')->where('table_name', '<>', '-1')->get();
  244. $accountToken = lm('account_token', 'Commons');
  245. $accountDetailedModel = lm('account_detailed', 'Commons');
  246. $accountModel = lm('account', 'Commons');
  247. $accountPassword = lm('account_password', 'Commons');
  248. $moneyBuy = lm('money_buy', 'Commons');
  249. $moneyDetails = lm('money_details', 'Commons');
  250. $moneyPrize = lm('money_prize', 'Commons');
  251. $moneyReturn = lm('money_return', 'Commons');
  252. $moneyReward = lm('money_reward', 'Commons');
  253. $sixmoneyBuy = lm('sixMoney_buy', 'Commons');
  254. $sixmoneyPrize = lm('sixMoney_prize', 'Commons');
  255. $identitys = array();
  256. if (count($account) > 0) {
  257. foreach ($account as $key => $val) {
  258. $identitys[] = $val['account_identity'];
  259. }
  260. //$time = strtotime($val['register_time']) + 3 * 24 * 3600;
  261. //if (time() > $time) {
  262. $accountToken->whereIn('account_identity', $identitys)->delete();
  263. $accountDetailedModel->whereIn('account_identity', $identitys)->delete();
  264. $accountPassword->whereIn('account_identity', $identitys)->delete();
  265. $accountModel->whereIn('identity', $identitys)->delete();
  266. $moneyBuy->whereIn('account_identity', $identitys)->delete();
  267. $moneyDetails->whereIn('account_identity', $identitys)->delete();
  268. $moneyPrize->whereIn('account_identity', $identitys)->delete();
  269. $moneyReturn->whereIn('account_identity', $identitys)->delete();
  270. $moneyReward->whereIn('account_identity', $identitys)->delete();
  271. $sixmoneyBuy->whereIn('account_identity', $identitys)->delete();
  272. $sixmoneyPrize->whereIn('account_identity', $identitys)->delete();
  273. if (count($gameInfo) > 0) {
  274. foreach ($gameInfo as $k => $v) {
  275. $cls = '\\App\\Commons\\Model\\' . ucfirst($v['table_name']) . '_buy';
  276. if (class_exists($cls)) {
  277. lm($v['table_name'] . '_buy', 'Commons')->whereIn('account_identity', $identitys)->delete();
  278. }
  279. }
  280. }
  281. //}
  282. }
  283. }
  284. /**
  285. * 注册试玩用户
  286. *
  287. * @param $uname
  288. * @return array
  289. * @throws \Exception
  290. */
  291. public function demoAccount($uname, $pwd) {
  292. $id = UUID();
  293. $account = [
  294. 'account' => $uname,
  295. 'identity' => $id,
  296. 'status' => 4,
  297. ];
  298. lm('account', 'Commons')->insert($account);
  299. $this->addPassword($pwd, $id);
  300. $tokenManager = new TokenManager();
  301. $token = $tokenManager->getToken();
  302. $accountDetailed = [
  303. 'identity' => UUID(),
  304. 'account_identity' => $id,
  305. 'available_cash' => 2000,
  306. 'token' => $token,
  307. 'register_time' => date('Y-m-d H:i:s'),
  308. 'register_ip' => GETIP(),
  309. 'last_ip' => GETIP(),
  310. 'grade' => 0,
  311. 'cash' => 2000,
  312. 'frozen_cash' => 0,
  313. ];
  314. lm('account_detailed', 'Commons')->insert($accountDetailed);
  315. return ['data' => $token, 'status' => 1, 'msg' => lang()->get('success')];
  316. }
  317. /**
  318. * 代理用户是否存在
  319. *
  320. * @param $agentUser
  321. * @return int
  322. * @throws \Exception
  323. */
  324. public function checkAgent($agentUser) {
  325. $agent = lm('agent_detailed', 'Commons')->where('invite', $agentUser)->first();
  326. if ($agent) {
  327. return 1;
  328. }
  329. return -4017;
  330. }
  331. /**
  332. * 代理用户登录
  333. *
  334. * @param $data
  335. * @return array|int
  336. * @throws \Exception
  337. */
  338. public function agentLogin($data) {
  339. if (!$agent = lm('agent_detailed', 'Commons')->where('agent_user', $data['agent_user'])->first()) {
  340. return -4017;
  341. }
  342. $row = $this->checkAgentPassword($data['agent_user'], $data['password']);
  343. if ($row != 1) {
  344. return $row;
  345. }
  346. $str_token = $this->tokenManager->getToken();
  347. $this->publishAgentToken($agent->agent_identity, $str_token);
  348. $agentDetailed = $this->getAgentDetailed($str_token);
  349. if (!$agentDetailed) {
  350. return -4017;
  351. }
  352. $_SESSION['agent'] = $agentDetailed->toArray();
  353. return $agentDetailed->toArray();
  354. }
  355. /**
  356. * 验证代理用户登录密码
  357. *
  358. * @param $user
  359. * @param $password
  360. * @return int
  361. * @throws \Exception
  362. */
  363. public function checkAgentPassword($user, $password) {
  364. if ($agentDetailed = lm('agent_detailed', 'Commons')->where(['agent_user' => $user, 'status' => 1])->first()) {
  365. $dataPassword = md5(md5($agentDetailed->encryption . $password));
  366. return $dataPassword == $agentDetailed->password ? 1 : -4021;
  367. } else {
  368. return -4017;
  369. };
  370. }
  371. /**
  372. * 更新代理用户token有效时间
  373. *
  374. * @param $agentIdentity
  375. * @param $token
  376. * @throws \Exception
  377. */
  378. public function publishAgentToken($agentIdentity, $token) {
  379. lm('agent_detailed', 'Commons')->where('agent_identity', $agentIdentity)->update(array('agent_token' => $token));
  380. }
  381. /**
  382. * @param $accountIdentity 检测是否是代理用户
  383. */
  384. public function IsAgent($accountIdentity) {
  385. $data = lm('agent_detailed', 'Commons')->where('agent_identity', $accountIdentity)->first();
  386. if (empty($data)) {
  387. return -321;
  388. }
  389. return 1;
  390. }
  391. /**
  392. * token获取代理用户信息
  393. *
  394. * @param $token
  395. * @return mixed
  396. * @throws \Exception
  397. */
  398. public function getAgentDetailed($token) {
  399. return lm('agent_detailed', 'Commons')->where('agent_token', $token)->first();
  400. }
  401. public function getAgentInfo($agent_identity) {
  402. return lm('agent_detailed', 'Commons')->where('agent_identity', $agent_identity)->first();
  403. }
  404. /**
  405. * 代理用户token是否过期
  406. *
  407. * @param $agentTokenOverdue
  408. * @return array|int
  409. * @throws \Exception
  410. */
  411. public function agentTokenOverdue($agentTokenOverdue) {
  412. $accountToken = lm('account_token', 'Commons')->where('account_identity', $agentTokenOverdue->agent_identity)->first();
  413. if (!$accountToken) {
  414. return -4019;
  415. }
  416. if (strtotime('now') > $accountToken->effective_time) {
  417. return $this->updateAgentToken($agentTokenOverdue->agent_identity);
  418. } else {
  419. return $this->updateAgentEffectiveTime($agentTokenOverdue->agent_identity, $agentTokenOverdue->agent_token);
  420. }
  421. }
  422. /**
  423. * 更新代理用户登录token
  424. *
  425. * @param $agentIdentity
  426. * @return int
  427. * @throws \Exception
  428. */
  429. public function updateAgentToken($agentIdentity) {
  430. $token = $this->tokenManager->getToken();
  431. lm('agent_detailed', 'Commons')->where('agent_identity', $agentIdentity)->update(['agent_token' => $token]);
  432. unset($_SESSION['agent']);
  433. return -4019;
  434. }
  435. /**
  436. * 更新代理用户登录过期时间
  437. *
  438. * @param $accountIdentity
  439. * @return array
  440. * @throws \Exception
  441. */
  442. public function updateAgentEffectiveTime($accountIdentity, $token) {
  443. lm('account_token', 'Commons')->where('account_identity', $accountIdentity)->update(['effective_time' => strtotime('now') + 1200]);
  444. //lm('account_token', 'Commons')->where('account_identity', $accountIdentity)->update(['effective_time' => strtotime('now')]);
  445. $agentDetailed = $this->getAgentDetailed($token);
  446. $_SESSION['agent'] = $agentDetailed->toArray();
  447. return $agentDetailed->toArray();
  448. }
  449. /**
  450. * 注册添加代理用户
  451. *
  452. * @param $data
  453. * @throws \Exception
  454. */
  455. public function addAgentDetailed($data) {
  456. $pwd = GenPassword($data['password']);
  457. $invite = substr(uniqid(md5($data['agent_user'])), 0, 32);
  458. $insertData = [
  459. 'agent_identity' => $data['agent_identity'],
  460. 'agent_user' => $data['agent_user'],
  461. 'phone' => $data['phone'],
  462. 'start_time' => date('Y-m-d H:i:s'),
  463. 'invite' => $invite,
  464. 'email' => $data['email'],
  465. 'bank_number' => $data['bank_number'],
  466. 'type' => $data['type'],
  467. ];
  468. $res = lm('agent_detailed', 'Commons')->insert($insertData);
  469. if (!$res) {
  470. return -141313;
  471. }
  472. return 1;
  473. }
  474. /**
  475. * 获取游戏分类
  476. *
  477. * @param $key
  478. * @param $val
  479. * @param $result
  480. * @return mixed
  481. */
  482. public function getResult($key, $val, $result) {
  483. substr($key, 0, 5) == 'chain' ? $parent = 'chain' : '';
  484. substr($key, 0, 6) == 'dragon' ? $parent = 'dragon' : '';
  485. substr($key, 0, 8) == 'head_one' ? $parent = 'head_one' : '';
  486. substr($key, 0, 8) == 'head_two' ? $parent = 'head_two' : '';
  487. substr($key, 0, 10) == 'head_three' ? $parent = 'head_three' : '';
  488. substr($key, 0, 7) == 'poscode' ? $parent = 'poscode' : '';
  489. substr($key, 0, 5) == 'color' ? $parent = 'color' : '';
  490. substr($key, 0, 3) == 'mix' ? $parent = 'mix' : '';
  491. substr($key, 0, 4) == 'size' ? $parent = 'size' : '';
  492. substr($key, 0, 11) == 'specialCode' ? $parent = 'specialCode' : '';
  493. $str = explode(':', $key);
  494. $result[$str[0]] = $this->getArray($key, $val, $parent, $result[$str[0]]);
  495. return $result;
  496. }
  497. /**
  498. * 单个游戏分类组装数据
  499. *
  500. * @param $name
  501. * @param $val
  502. * @param $parent
  503. * @param $result
  504. * @return mixed
  505. */
  506. public function getArray($name, $val, $parent, $result) {
  507. $str = explode(':', $name);
  508. $result['parent'] = $parent;
  509. $result['name'] = $str[0];
  510. $result['type'] = 0;
  511. if ($str[1] == 'max' || $str[1] == 'min') {
  512. $result['settings']['multiple'][$str[1]] = $val;
  513. } elseif ($str[1] == 'back') {
  514. $result['settings'][$str[1]] = $val;
  515. } elseif ($str[1] = 'multiple') {
  516. $result['settings'][$str[1]] = $val;
  517. }
  518. return $result;
  519. }
  520. /**
  521. * 增加用户上下级关系
  522. * @param [type] $invitation 邀请码
  523. * @param [type] $account_name 用户名
  524. * @param [type] $user_id 用户id
  525. */
  526. public function addParent($invitation, $arr) {
  527. $agent = $this->getAgent($invitation);
  528. if ($agent < 0) {
  529. return $agent;
  530. }
  531. $data = array(
  532. 'agent_identity' => $agent['agent_identity'],
  533. 'agent_name' => $agent['agent_user'],
  534. 'account_name' => $arr['account'],
  535. 'account_identity' => $arr['identity'],
  536. );
  537. $res = lm('agent_child', 'Commons')->insert($data);
  538. }
  539. /**
  540. * 根据邀请码获取代理信息
  541. * @param [type] $invite 邀请码
  542. * @return [type] [description]
  543. */
  544. public function getAgent($invite) {
  545. $res = lm('agent_detailed', 'Commons')->where('id', $invite)->first();
  546. if (!$res) {
  547. return -4017;
  548. }
  549. return $res->toArray();
  550. }
  551. //检测邀请码是否存在
  552. public function checkInvitation($invitation) {
  553. $res = lm('account_detailed', 'Commons')
  554. ->leftJoin('account', 'account.identity', '=', 'account_detailed.account_identity')
  555. ->where('account_detailed.open_invitation', $invitation)
  556. ->first();
  557. if (!$res) {
  558. return -4017;
  559. }
  560. return $res->toArray();
  561. }
  562. //添加代理人,代理下级
  563. public function addNewAgent($data) {
  564. $res = lm('nagent_child', 'Commons')->insert($data);
  565. if (!$res) {
  566. return -141313;
  567. }
  568. return 1;
  569. }
  570. //添加代理人
  571. public function addNewAgentDetail($data) {
  572. $res = lm('nagent_detailed', 'Commons')->insert($data);
  573. if (!$res) {
  574. return -141313;
  575. }
  576. return 1;
  577. }
  578. //获取root用户
  579. public function getRoot() {
  580. $root = lm('account', 'Commons')
  581. ->leftJoin('account_detailed', 'account.identity', '=', 'account_detailed.account_identity')
  582. ->where('account.id', 83632)
  583. ->first();
  584. if (!$root) {
  585. return -4107;
  586. }
  587. return $root->toArray();
  588. }
  589. }