AccountRepository.php 21 KB

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