AccountRepository.php 21 KB

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