AccountRepository.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. echo 1;
  203. lm('account_token', 'Commons')->where('account_identity', $accountIdentity)->update(['effective_time' => strtotime('now'), 'last_time' => date('Y-m-d H:i:s')]);
  204. lm('account_detailed', 'Commons')->where('account_identity', $accountIdentity)->update(['last_time' => date('Y-m-d H:i:s')]);
  205. echo 3;
  206. $userInfo = $this->tokenManager->getAccountInfo($token);
  207. echo 2;
  208. if (!$userInfo)
  209. return ['data' => '', 'status' => '-4001', 'msg' => lang()->get('-4001')];
  210. $data = $_SESSION['uinfo'] = $userInfo->toArray();
  211. $result = ['data' => $data, 'status' => 1, 'msg' => lang()->get('success')];
  212. return $result;
  213. }
  214. /**
  215. * 修改支付密码,验证原密码
  216. *
  217. * @param $token
  218. * @param $oldPassword
  219. * @return array|bool
  220. * @throws \Exception
  221. */
  222. public function checkOldPayPassword($token, $oldPassword) {
  223. $accountDetailed = lm('account_detailed', 'Commons')->where('token', $token)->first();
  224. if (empty($accountDetailed)) {
  225. $result = ['status' => -4011, 'msg' => lang()->get('user error')];
  226. return $result;
  227. }
  228. $payPassword = lm('pay_password', 'Commons')->where('account_identity', $accountDetailed->account_identity)->first();
  229. if (empty($payPassword)) {
  230. $result = ['status' => -4012, 'msg' => lang()->get('no payment password, please set the payment password first')];
  231. return $result;
  232. }
  233. if ($payPassword->pay_password != md5(md5($payPassword->encryption . $oldPassword))) {
  234. $result = ['status' => -4013, 'msg' => lang()->get('old password error')];
  235. return $result;
  236. }
  237. return ['status' => 1, 'account_identity' => $accountDetailed->account_identity];
  238. }
  239. /**
  240. * 销毁超过3天的免费试玩账号
  241. *
  242. * @param $account
  243. * @throws \Exception
  244. */
  245. public function destroyAccount($account) {
  246. $gameInfo = lm('game_type', 'Commons')->where('table_name', '<>', '-1')->get();
  247. $accountToken = lm('account_token', 'Commons');
  248. $accountDetailedModel = lm('account_detailed', 'Commons');
  249. $accountModel = lm('account', 'Commons');
  250. $accountPassword = lm('account_password', 'Commons');
  251. $moneyBuy = lm('money_buy', 'Commons');
  252. $moneyDetails = lm('money_details', 'Commons');
  253. $moneyPrize = lm('money_prize', 'Commons');
  254. $moneyReturn = lm('money_return', 'Commons');
  255. $moneyReward = lm('money_reward', 'Commons');
  256. $sixmoneyBuy = lm('sixMoney_buy', 'Commons');
  257. $sixmoneyPrize = lm('sixMoney_prize', 'Commons');
  258. $identitys = array();
  259. if (count($account) > 0) {
  260. foreach ($account as $key => $val) {
  261. $identitys[] = $val['account_identity'];
  262. }
  263. //$time = strtotime($val['register_time']) + 3 * 24 * 3600;
  264. //if (time() > $time) {
  265. $accountToken->whereIn('account_identity', $identitys)->delete();
  266. $accountDetailedModel->whereIn('account_identity', $identitys)->delete();
  267. $accountPassword->whereIn('account_identity', $identitys)->delete();
  268. $accountModel->whereIn('identity', $identitys)->delete();
  269. $moneyBuy->whereIn('account_identity', $identitys)->delete();
  270. $moneyDetails->whereIn('account_identity', $identitys)->delete();
  271. $moneyPrize->whereIn('account_identity', $identitys)->delete();
  272. $moneyReturn->whereIn('account_identity', $identitys)->delete();
  273. $moneyReward->whereIn('account_identity', $identitys)->delete();
  274. $sixmoneyBuy->whereIn('account_identity', $identitys)->delete();
  275. $sixmoneyPrize->whereIn('account_identity', $identitys)->delete();
  276. if (count($gameInfo) > 0) {
  277. foreach ($gameInfo as $k => $v) {
  278. $cls = '\\App\\Commons\\Model\\' . ucfirst($v['table_name']) . '_buy';
  279. if (class_exists($cls)) {
  280. lm($v['table_name'] . '_buy', 'Commons')->whereIn('account_identity', $identitys)->delete();
  281. }
  282. }
  283. }
  284. //}
  285. }
  286. }
  287. /**
  288. * 注册试玩用户
  289. *
  290. * @param $uname
  291. * @return array
  292. * @throws \Exception
  293. */
  294. public function demoAccount($uname, $pwd) {
  295. $id = UUID();
  296. $account = [
  297. 'account' => $uname,
  298. 'identity' => $id,
  299. 'status' => 4,
  300. ];
  301. lm('account', 'Commons')->insert($account);
  302. $this->addPassword($pwd, $id);
  303. $tokenManager = new TokenManager();
  304. $token = $tokenManager->getToken();
  305. $accountDetailed = [
  306. 'identity' => UUID(),
  307. 'account_identity' => $id,
  308. 'available_cash' => 2000,
  309. 'token' => $token,
  310. 'register_time' => date('Y-m-d H:i:s'),
  311. 'register_ip' => GETIP(),
  312. 'last_ip' => GETIP(),
  313. 'grade' => 0,
  314. 'cash' => 2000,
  315. 'frozen_cash' => 0,
  316. ];
  317. lm('account_detailed', 'Commons')->insert($accountDetailed);
  318. return ['data' => $token, 'status' => 1, 'msg' => lang()->get('success')];
  319. }
  320. /**
  321. * 代理用户是否存在
  322. *
  323. * @param $agentUser
  324. * @return int
  325. * @throws \Exception
  326. */
  327. public function checkAgent($agentUser) {
  328. $agent = lm('agent_detailed', 'Commons')->where('invite', $agentUser)->first();
  329. if ($agent) {
  330. return 1;
  331. }
  332. return -4017;
  333. }
  334. /**
  335. * 代理用户登录
  336. *
  337. * @param $data
  338. * @return array|int
  339. * @throws \Exception
  340. */
  341. public function agentLogin($data) {
  342. if (!$agent = lm('agent_detailed', 'Commons')->where('agent_user', $data['agent_user'])->first()) {
  343. return -4017;
  344. }
  345. $row = $this->checkAgentPassword($data['agent_user'], $data['password']);
  346. if ($row != 1) {
  347. return $row;
  348. }
  349. $str_token = $this->tokenManager->getToken();
  350. $this->publishAgentToken($agent->agent_identity, $str_token);
  351. $agentDetailed = $this->getAgentDetailed($str_token);
  352. if (!$agentDetailed) {
  353. return -4017;
  354. }
  355. $_SESSION['agent'] = $agentDetailed->toArray();
  356. return $agentDetailed->toArray();
  357. }
  358. /**
  359. * 验证代理用户登录密码
  360. *
  361. * @param $user
  362. * @param $password
  363. * @return int
  364. * @throws \Exception
  365. */
  366. public function checkAgentPassword($user, $password) {
  367. if ($agentDetailed = lm('agent_detailed', 'Commons')->where(['agent_user' => $user, 'status' => 1])->first()) {
  368. $dataPassword = md5(md5($agentDetailed->encryption . $password));
  369. return $dataPassword == $agentDetailed->password ? 1 : -4021;
  370. } else {
  371. return -4017;
  372. };
  373. }
  374. /**
  375. * 更新代理用户token有效时间
  376. *
  377. * @param $agentIdentity
  378. * @param $token
  379. * @throws \Exception
  380. */
  381. public function publishAgentToken($agentIdentity, $token) {
  382. lm('agent_detailed', 'Commons')->where('agent_identity', $agentIdentity)->update(array('agent_token' => $token));
  383. }
  384. /**
  385. * @param $accountIdentity 检测是否是代理用户
  386. */
  387. public function IsAgent($accountIdentity) {
  388. $data = lm('agent_detailed', 'Commons')->where('agent_identity', $accountIdentity)->first();
  389. if (empty($data)) {
  390. return -321;
  391. }
  392. return 1;
  393. }
  394. /**
  395. * token获取代理用户信息
  396. *
  397. * @param $token
  398. * @return mixed
  399. * @throws \Exception
  400. */
  401. public function getAgentDetailed($token) {
  402. return lm('agent_detailed', 'Commons')->where('agent_token', $token)->first();
  403. }
  404. public function getAgentInfo($agent_identity) {
  405. return lm('agent_detailed', 'Commons')->where('agent_identity', $agent_identity)->first();
  406. }
  407. /**
  408. * 代理用户token是否过期
  409. *
  410. * @param $agentTokenOverdue
  411. * @return array|int
  412. * @throws \Exception
  413. */
  414. public function agentTokenOverdue($agentTokenOverdue) {
  415. $accountToken = lm('account_token', 'Commons')->where('account_identity', $agentTokenOverdue->agent_identity)->first();
  416. if (!$accountToken) {
  417. return -4019;
  418. }
  419. if (strtotime('now') > $accountToken->effective_time) {
  420. return $this->updateAgentToken($agentTokenOverdue->agent_identity);
  421. } else {
  422. return $this->updateAgentEffectiveTime($agentTokenOverdue->agent_identity, $agentTokenOverdue->agent_token);
  423. }
  424. }
  425. /**
  426. * 更新代理用户登录token
  427. *
  428. * @param $agentIdentity
  429. * @return int
  430. * @throws \Exception
  431. */
  432. public function updateAgentToken($agentIdentity) {
  433. $token = $this->tokenManager->getToken();
  434. lm('agent_detailed', 'Commons')->where('agent_identity', $agentIdentity)->update(['agent_token' => $token]);
  435. unset($_SESSION['agent']);
  436. return -4019;
  437. }
  438. /**
  439. * 更新代理用户登录过期时间
  440. *
  441. * @param $accountIdentity
  442. * @return array
  443. * @throws \Exception
  444. */
  445. public function updateAgentEffectiveTime($accountIdentity, $token) {
  446. lm('account_token', 'Commons')->where('account_identity', $accountIdentity)->update(['effective_time' => strtotime('now') + 1200]);
  447. //lm('account_token', 'Commons')->where('account_identity', $accountIdentity)->update(['effective_time' => strtotime('now')]);
  448. $agentDetailed = $this->getAgentDetailed($token);
  449. $_SESSION['agent'] = $agentDetailed->toArray();
  450. return $agentDetailed->toArray();
  451. }
  452. /**
  453. * 注册添加代理用户
  454. *
  455. * @param $data
  456. * @throws \Exception
  457. */
  458. public function addAgentDetailed($data) {
  459. $pwd = GenPassword($data['password']);
  460. $invite = substr(uniqid(md5($data['agent_user'])), 0, 32);
  461. $insertData = [
  462. 'agent_identity' => $data['agent_identity'],
  463. 'agent_user' => $data['agent_user'],
  464. 'phone' => $data['phone'],
  465. 'start_time' => date('Y-m-d H:i:s'),
  466. 'invite' => $invite,
  467. 'email' => $data['email'],
  468. 'bank_number' => $data['bank_number'],
  469. 'type' => $data['type'],
  470. ];
  471. $res = lm('agent_detailed', 'Commons')->insert($insertData);
  472. if (!$res) {
  473. return -141313;
  474. }
  475. return 1;
  476. }
  477. /**
  478. * 获取游戏分类
  479. *
  480. * @param $key
  481. * @param $val
  482. * @param $result
  483. * @return mixed
  484. */
  485. public function getResult($key, $val, $result) {
  486. substr($key, 0, 5) == 'chain' ? $parent = 'chain' : '';
  487. substr($key, 0, 6) == 'dragon' ? $parent = 'dragon' : '';
  488. substr($key, 0, 8) == 'head_one' ? $parent = 'head_one' : '';
  489. substr($key, 0, 8) == 'head_two' ? $parent = 'head_two' : '';
  490. substr($key, 0, 10) == 'head_three' ? $parent = 'head_three' : '';
  491. substr($key, 0, 7) == 'poscode' ? $parent = 'poscode' : '';
  492. substr($key, 0, 5) == 'color' ? $parent = 'color' : '';
  493. substr($key, 0, 3) == 'mix' ? $parent = 'mix' : '';
  494. substr($key, 0, 4) == 'size' ? $parent = 'size' : '';
  495. substr($key, 0, 11) == 'specialCode' ? $parent = 'specialCode' : '';
  496. $str = explode(':', $key);
  497. $result[$str[0]] = $this->getArray($key, $val, $parent, $result[$str[0]]);
  498. return $result;
  499. }
  500. /**
  501. * 单个游戏分类组装数据
  502. *
  503. * @param $name
  504. * @param $val
  505. * @param $parent
  506. * @param $result
  507. * @return mixed
  508. */
  509. public function getArray($name, $val, $parent, $result) {
  510. $str = explode(':', $name);
  511. $result['parent'] = $parent;
  512. $result['name'] = $str[0];
  513. $result['type'] = 0;
  514. if ($str[1] == 'max' || $str[1] == 'min') {
  515. $result['settings']['multiple'][$str[1]] = $val;
  516. } elseif ($str[1] == 'back') {
  517. $result['settings'][$str[1]] = $val;
  518. } elseif ($str[1] = 'multiple') {
  519. $result['settings'][$str[1]] = $val;
  520. }
  521. return $result;
  522. }
  523. /**
  524. * 增加用户上下级关系
  525. * @param [type] $invitation 邀请码
  526. * @param [type] $account_name 用户名
  527. * @param [type] $user_id 用户id
  528. */
  529. public function addParent($invitation, $arr) {
  530. $agent = $this->getAgent($invitation);
  531. if ($agent < 0) {
  532. return $agent;
  533. }
  534. $data = array(
  535. 'agent_identity' => $agent['agent_identity'],
  536. 'agent_name' => $agent['agent_user'],
  537. 'account_name' => $arr['account'],
  538. 'account_identity' => $arr['identity'],
  539. );
  540. $res = lm('agent_child', 'Commons')->insert($data);
  541. }
  542. /**
  543. * 根据邀请码获取代理信息
  544. * @param [type] $invite 邀请码
  545. * @return [type] [description]
  546. */
  547. public function getAgent($invite) {
  548. $res = lm('agent_detailed', 'Commons')->where('id', $invite)->first();
  549. if (!$res) {
  550. return -4017;
  551. }
  552. return $res->toArray();
  553. }
  554. //检测邀请码是否存在
  555. public function checkInvitation($invitation) {
  556. $res = lm('account_detailed', 'Commons')
  557. ->leftJoin('account', 'account.identity', '=', 'account_detailed.account_identity')
  558. ->where('account_detailed.open_invitation', $invitation)
  559. ->first();
  560. if (!$res) {
  561. return -4017;
  562. }
  563. return $res->toArray();
  564. }
  565. //添加代理人,代理下级
  566. public function addNewAgent($data) {
  567. $res = lm('nagent_child', 'Commons')->insert($data);
  568. if (!$res) {
  569. return -141313;
  570. }
  571. return 1;
  572. }
  573. //添加代理人
  574. public function addNewAgentDetail($data) {
  575. $res = lm('nagent_detailed', 'Commons')->insert($data);
  576. if (!$res) {
  577. return -141313;
  578. }
  579. return 1;
  580. }
  581. //获取root用户
  582. public function getRoot() {
  583. $root = lm('account', 'Commons')
  584. ->leftJoin('account_detailed', 'account.identity', '=', 'account_detailed.account_identity')
  585. ->where('account.id', 83632)
  586. ->first();
  587. if (!$root) {
  588. return -4107;
  589. }
  590. return $root->toArray();
  591. }
  592. }