AccountManager.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. <?php
  2. namespace Biz\Account;
  3. use App\Commons\Model\Money_recharge;
  4. use Biz\Account\Repository\AccountRepository;
  5. use Biz\TokenManager;
  6. use App\Api\Model\Dc_user_grade;
  7. use App\Commons\Model\WagentCheckToken as WagentCheckTokenModel;
  8. /**
  9. * Created by PhpStorm.
  10. * User: wang
  11. * Date: 2017/6/12
  12. * Time: 10:05
  13. */
  14. // ini_set('display_errors', 1);
  15. // error_reporting(E_ALL);
  16. class AccountManager
  17. {
  18. public function __construct()
  19. {
  20. $this->model = lm('account', 'Commons');
  21. $this->repository = new AccountRepository();
  22. }
  23. /**
  24. * 用户登录
  25. *
  26. * @param $account
  27. * @param $password
  28. * @return string
  29. */
  30. public function login($account, $password, $agentuseToken = '')
  31. {
  32. $password = trim($password);
  33. $account = strtolower(trim($account));
  34. define("OVERTIME", 1800);
  35. if (!$user = $this->getAccount($account)) {
  36. $result = ['status' => -4025, 'msg' => lang()->get('user does not exist')];
  37. return $result;
  38. } else {
  39. if (!empty($agentuseToken)) {
  40. $ret = (new WagentCheckTokenModel())->getByToken($agentuseToken, $account);
  41. if ($ret) {
  42. goto CHECKOKLAB;
  43. }
  44. }
  45. if ($this->checkPassword($user, $password)) {
  46. CHECKOKLAB:
  47. try {
  48. //绑定用户
  49. // lm('user_logs')->bindUser($user->identity,$user->account);
  50. $token = new TokenManager();
  51. $accountToken = lm('account_token', 'Commons')->where('account_identity', $user->identity)->first();
  52. $effective_time = $accountToken->effective_time;
  53. if (time() - $effective_time <= OVERTIME) {
  54. $tokeninfo = lm('account_detailed', 'commons')
  55. ->select('token')
  56. ->where('account_identity', $user->identity)->first();
  57. if (!$tokeninfo) {
  58. return -4025;
  59. }
  60. if (empty($tokeninfo->token)) {
  61. return -4025;
  62. }
  63. $str_token = $tokeninfo->token;
  64. } else {
  65. $str_token = $token->publish($user->identity);
  66. }
  67. $str_token = $token->publish($user->identity);
  68. // $this->repository->publishAgentToken($user->identity, $str_token); //更新代理表token
  69. $userInfo = $token->doLogin($str_token);
  70. if (!$userInfo) {
  71. $result = ['status' => -4025, 'msg' => lang()->get('user does not exist')];
  72. return $result;
  73. }
  74. $this->insertLogLife($userInfo);
  75. // $agentDetailed = $this->repository->getAgentInfo($user->identity);
  76. // if (!empty($agentDetailed)) {
  77. // $_SESSION['agent'] = $agentDetailed->toArray();
  78. // }
  79. $userInfo[0]['ip'] = GETIP();
  80. $userInfo[0]['newdevice']['uuid'] = UUID();
  81. $userInfo[0]['newdevice']['GetBrowser'] = $this->GetBrowser();
  82. $userInfo[0]['newdevice']['device'] = $this->device();
  83. $_SESSION['newdevice'] = $userInfo[0]['newdevice'];
  84. if (empty($userInfo[0]['open_invitation'])) {
  85. $userInfo[0]['open_invitation'] = $userInfo[0]['id'];
  86. lm('account_detailed', 'commons')->where('account_identity', $userInfo[0]['account_identity'])->update(['open_invitation' => $userInfo[0]['id']]);
  87. } else {
  88. if (!is_numeric($userInfo[0]['open_invitation'])) {
  89. lm('account_detailed', 'commons')->where('account_identity', $userInfo[0]['account_identity'])->update(['open_invitation' => $userInfo[0]['id']]);
  90. }
  91. }
  92. //老账号添加到新代理表
  93. // $nagent = lm('nagent_detailed', 'commons')->where('agent_identity', $userInfo[0]['account_identity'])->first();
  94. //
  95. // if (!$nagent) {
  96. // $nagentData = [
  97. // 'agent_identity' => $userInfo[0]['account_identity'],
  98. // 'agent_user' => $userInfo[0]['account'],
  99. // 'invite' => $userInfo[0]['open_invitation']
  100. // ];
  101. // lm('nagent_detailed', 'commons')->where('agent_identity', $userInfo[0]['account_identity'])->insert($nagentData);
  102. // } else {
  103. // lm('nagent_detailed', 'commons')->where('agent_identity', $userInfo[0]['account_identity'])->update(['invite' => $userInfo[0]['open_invitation']]);
  104. // }
  105. $userInfo[0]['remark'] = str_replace("%", "", $userInfo[0]['remark']);
  106. //获取当前用户分组名称
  107. $substr = substr($userInfo[0]['group_name'], strpos($userInfo[0]['group_name'], '|') + 1);
  108. $role_name = substr($substr, 0, -1);
  109. //获取用户分组的投注限额
  110. $bet_money_limit = Dc_user_grade::get_bet_money_limit($role_name);
  111. $userInfo[0]['bet_money_limit'] = ['lower_limit' => $bet_money_limit->lower_limit, 'upper_limit' => $bet_money_limit->upper_limit];
  112. $result = ['data' => $userInfo, 'status' => 1, 'msg' => lang()->get('login successful')];
  113. return $result;
  114. } catch (Exception $e) {
  115. $result = ['status' => -4003, 'msg' => lang()->get('login error, login again')];
  116. return $result;
  117. }
  118. } else {
  119. $result = ['status' => -4002, 'msg' => lang()->get('password error')];
  120. return $result;
  121. };
  122. }
  123. }
  124. /*
  125. * 生成用户邀请码
  126. */
  127. public function setrandomcodes($account_identity)
  128. {
  129. if (empty($account_identity)) {
  130. return -4080;
  131. }
  132. $open_invitation = $this->getRandomString(8);
  133. $num = lm('account_detailed', 'commons')->where('open_invitation', $open_invitation)->count();
  134. if ($num == 0) {
  135. lm('account_detailed', 'commons')->where('account_identity', $account_identity)->update(['open_invitation' => $open_invitation]);
  136. return 1;
  137. } else {
  138. $this->setrandomcodes($account_identity);
  139. }
  140. }
  141. /**
  142. * 获取用户信息
  143. *
  144. * @param $user
  145. * @return \Sting
  146. */
  147. public function getAccount($account)
  148. {
  149. $result = $this->model->where(['account' => $account, 'status' => 1])->first();
  150. if (!$result) {
  151. $result = $this->model->where(['account' => $account, 'status' => 4])->first();
  152. }
  153. return $result;
  154. }
  155. /**
  156. * 检查登录密码
  157. *
  158. * @param $user
  159. * @param $password
  160. * @return bool
  161. */
  162. public function checkPassword($user, $password)
  163. {
  164. $accountPassword = lm('account_password', 'Commons')->where(['account_identity' => $user->identity, 'status' => 1])->first();
  165. $dataPassword = md5(md5($accountPassword->encryption . $password));
  166. return $dataPassword == $accountPassword->account_password ? true : false;
  167. }
  168. /**
  169. * 添加登录日志
  170. *
  171. * @param $userInfo
  172. * @throws \Exception
  173. */
  174. public function insertLogLife($userInfo)
  175. {
  176. $type = $this->CheckisMobile();
  177. if ($type) {
  178. $intype = "手机端";
  179. $device_number = $userInfo[0]['device_number'];
  180. } else {
  181. $intype = "电脑端";
  182. $device_number = $_SESSION['newdevice']['uuid'];
  183. }
  184. if ($userInfo) {
  185. $device = $this->device();
  186. $res = [
  187. 'account_identity' => $userInfo[0]['account_identity'],
  188. 'account' => $userInfo[0]['account'],
  189. 'type' => $intype,
  190. 'add_time' => date('Y-m-d H:i:s', time()),
  191. 'url' => $_SERVER['HTTP_HOST'],
  192. 'ip' => GETIP(),
  193. 'browser' => $this->GetBrowser(),
  194. 'device' => $device['name'],
  195. 'device_model' => $device['sxid'],
  196. 'device_number' => $device_number,
  197. ];
  198. lm('Logfile', 'Commons')->insert($res);
  199. }
  200. }
  201. /*移动端判断*/
  202. public function CheckisMobile()
  203. {
  204. return is_mobile();
  205. }
  206. /*获取浏览器*/
  207. public function GetBrowser()
  208. {
  209. if (!empty($_SERVER['HTTP_USER_AGENT'])) {
  210. $br = $_SERVER['HTTP_USER_AGENT'];
  211. if (preg_match('/MSIE/i', $br)) {
  212. $br = 'MSIE';
  213. } elseif (preg_match('/Firefox/i', $br)) {
  214. $br = 'Firefox';
  215. } elseif (preg_match('/Chrome/i', $br)) {
  216. $br = 'Chrome';
  217. } elseif (preg_match('/Safari/i', $br)) {
  218. $br = 'Safari';
  219. } elseif (preg_match('/Opera/i', $br)) {
  220. $br = 'Opera';
  221. } else {
  222. $br = 'Other';
  223. }
  224. return $br;
  225. } else {
  226. return "获取浏览器信息失败!";
  227. }
  228. }
  229. /*获取设备*/
  230. public function device()
  231. {
  232. foreach ($_SERVER as $name => $value) {
  233. if (substr($name, 0, 5) == 'HTTP_') {
  234. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  235. }
  236. }
  237. //截取()之内的数据
  238. $sname = substr($headers['User-Agent'], strlen('(') + strpos($headers['User-Agent'], '('), (strlen($headers['User-Agent']) - strpos($headers['User-Agent'], ')')) * (-1));
  239. //遍历数据
  240. $snameInfo = explode(';', $sname);
  241. //判断设备型号类型
  242. if ($snameInfo[0] == 'iPhone') {
  243. //设备系统:0为Windows系统
  244. $update['stype'] = 2;
  245. $update['name'] = 'ios';
  246. //设备序列号
  247. $update['sxid'] = $snameInfo[1];
  248. } elseif ($snameInfo[0] == 'ios') {
  249. //设备系统:2为IOS系统
  250. $update['stype'] = 2;
  251. $update['name'] = 'ios';
  252. //设备序列号
  253. $update['sxid'] = $snameInfo[1];
  254. } elseif ($snameInfo[0] == 'iPad') {
  255. //设备系统:2为IOS系统
  256. $update['stype'] = 2;
  257. $update['name'] = 'ios';
  258. //设备序列号
  259. $update['sxid'] = $snameInfo[1];
  260. } elseif ($snameInfo[0] == 'Linux') {
  261. //设备系统:1为安卓系统
  262. $update['stype'] = 1;
  263. $update['name'] = 'Android';
  264. //设备序列号
  265. $update['sxid'] = $snameInfo[2];
  266. } else {
  267. //设备系统:1为安卓系统
  268. $update['stype'] = 0;
  269. $update['name'] = 'pc';
  270. //设备序列号
  271. $update['sxid'] = $snameInfo[2];
  272. }
  273. return $update;
  274. }
  275. /**
  276. * 刷新token
  277. *
  278. * @return int
  279. */
  280. public function refreshToken($token = '')
  281. {
  282. if (!empty($token)) {
  283. $str_token = $token ? trim($token) : '';
  284. } else {
  285. $str_token = $_POST['token'] ? trim($_POST['token']) : '';
  286. }
  287. $token = new TokenManager();
  288. $userInfo = $token->getAccountInfo($str_token);
  289. if (count($userInfo) > 0) {
  290. $result = ['data' => $userInfo->toArray(), 'status' => 1, 'msg' => lang()->get('success')];
  291. return $result;
  292. } else {
  293. $result = ['status' => -4008, 'msg' => lang()->get('user does not exist')];
  294. return $result;
  295. }
  296. }
  297. /**
  298. * 刷新状态
  299. * @param [type] $token [description]
  300. * @return [type] [description]
  301. */
  302. public function refreshStatus($str_token)
  303. {
  304. $token = new TokenManager();
  305. if (empty($str_token)) {
  306. return -4001;
  307. }
  308. $userInfo = $token->getAccountInfo($str_token);
  309. $agentInfo = $token->getAgentInfo($str_token);
  310. if (count($userInfo) > 0) {
  311. $user = $userInfo->toArray();
  312. $this->repository->updateEffectiveTime($user['account_identity'], $str_token);
  313. $_SESSION['uinfo'] = $user;
  314. if (count($agentInfo) > 0) {
  315. $_SESSION['agent'] = $agentInfo->toArray();
  316. // $str_token = $token->getToken();
  317. // $this->repository->publishAgentToken($_SESSION['agent']['agent_identity'], $str_token);
  318. }
  319. return $user;
  320. }
  321. return -4001;
  322. }
  323. /**
  324. * 修改用户密码
  325. *
  326. * @access public
  327. * @param mixed oldPassword旧密码
  328. * @param mixed newPassword新密码
  329. * @return array JsonString
  330. * @throws \Exception
  331. */
  332. public function savePassword($post)
  333. {
  334. $userInfo = $this->getCurrentUser();
  335. // 判断用户登录
  336. if (empty($userInfo)) {
  337. $result = ['status' => -4001, 'msg' => lang()->get('user does not exist')];
  338. return $result;
  339. }
  340. // 验证用户状态
  341. if ($row = $this->repository->checkAccountStatus($userInfo)) {
  342. return $row;
  343. };
  344. $oldPassword = strip_tags($post['oldPassword']);
  345. $newPassword = strip_tags($post['newPassword']);
  346. // 验证两次密码是否一致
  347. /*if ($r = $this->repository->checkPassword($newPassword, $againPassword)) {
  348. return $r;
  349. }*/
  350. // 验证密码
  351. if ($this->checkPassword($userInfo, $oldPassword)) {
  352. // 密码加密
  353. $pwdData = GenPassword($newPassword);
  354. lm('account_password', 'Commons')->updatePassword($userInfo->identity, $pwdData);
  355. $result = ['status' => 1, 'msg' => lang()->get('update successful')];
  356. return $result;
  357. } else {
  358. $result = ['status' => -4009, 'msg' => lang()->get('old password error')];
  359. return $result;
  360. };
  361. }
  362. /**
  363. * 修改用户支付密码
  364. *
  365. * @access public
  366. * @param mixed oldPassword旧密码
  367. * @param mixed newPassword新密码
  368. * @return array JsonString
  369. * @throws \Exception
  370. */
  371. public function savePayPassword()
  372. {
  373. $userInfo = $this->getCurrentUser();
  374. // 判断用户登录
  375. if (empty($userInfo)) {
  376. $result = ['status' => -4001, 'msg' => lang()->get('user does not exist')];
  377. return $result;
  378. }
  379. // 验证用户状态
  380. if ($row = $this->repository->checkAccountStatus($userInfo)) {
  381. return $row;
  382. };
  383. $oldPayPassword = strip_tags($_POST['oldPayPassword']);
  384. $newPayPassword = strip_tags($_POST['newPayPassword']);
  385. // 验证两次密码是否一致
  386. /*if ($r = $this->repository->checkPassword($newPassword, $againPassword)) {
  387. return $r;
  388. }*/
  389. // 验证交易密码
  390. $this->verifyPayPwd($userInfo['identity'], $oldPayPassword);
  391. // 获取新支付密码加密
  392. $pwdData = GenPassword($newPayPassword);
  393. // 修改支付密码
  394. $upPayPwdWhere['account_identity'] = $userInfo['identity'];
  395. $upPayPwdData['pay_password'] = $pwdData['password'];
  396. $upPayPwdData['encryption'] = $pwdData['encryption'];
  397. lm('pay_password', 'Commons')->updatePayPassword($upPayPwdWhere, $upPayPwdData);
  398. $result = ['status' => 1, 'msg' => lang()->get('update successful')];
  399. return $result;
  400. }
  401. /**
  402. * 验证交易密码
  403. *
  404. * @access public
  405. * @param mixed $identity 用户id
  406. * @param mixed $payPassword 支付密码
  407. * @return String
  408. * @throws \Exception
  409. */
  410. public function verifyPayPwd($identity, $payPassword)
  411. {
  412. $payPasswordWhere['account_identity'] = $identity;
  413. $payPasswordSelect = ['encryption'];
  414. $payPasswordMd = lm('pay_password', "commons");
  415. // 获取加密盐值
  416. $accountPwdInfo = $payPasswordMd->payPassword($payPasswordSelect, $payPasswordWhere);
  417. $encryptionPwd = md5(md5($accountPwdInfo["encryption"] . $payPassword));
  418. $verifyPayPwdWhere['account_identity'] = $identity;
  419. $verifyPayPwdWhere['pay_password'] = $encryptionPwd;
  420. $verifyPayPwdSelect = ['id'];
  421. // 验证支付密码
  422. $verifyPayPwd = $payPasswordMd->payPassword($verifyPayPwdSelect, $verifyPayPwdWhere);
  423. if (!$verifyPayPwd['id']) {
  424. Render([], '2020', lang('Common', 'Api')->get('payment password error'));
  425. }
  426. return 1;
  427. }
  428. /**
  429. * 修改用户交易密码
  430. *
  431. * @return array
  432. * @throws \Exception
  433. */
  434. public function updatePayPassword($post)
  435. {
  436. $checkPassword = $this->repository->checkOldPayPassword($post['token'], strip_tags($post['oldPassword']));
  437. if ($checkPassword['status'] != 1) {
  438. return $checkPassword;
  439. };
  440. $payPassword = GenPassword(strip_tags($post['pay_password']));
  441. lm('pay_password', 'Commons')->where('account_identity', $checkPassword['account_identity'])->update(['pay_password' => $payPassword['password'], 'encryption' => $payPassword['encryption']]);
  442. $result = ['status' => 1, 'msg' => lang()->get('update successful')];
  443. return $result;
  444. }
  445. public function chHeadImg($imgUrl, $imgId)
  446. {
  447. $userInfo = $this->getCurrentUser();
  448. $uuid = $userInfo->account_identity;
  449. if (!$uuid) return -4001;
  450. $lm = lm('AccountDetail', 'Api');
  451. $ret = $lm->where('account_identity', $uuid)->update(['img_url' => $imgUrl, 'img_id' => $imgId]);
  452. return $ret;
  453. }
  454. /**
  455. * 获取当前用户信息
  456. *
  457. * @return mixed
  458. * @throws \Exception
  459. */
  460. public function getCurrentUser($token = '')
  461. {
  462. if (empty($token)) $token = $_POST['token'];
  463. $token = trim($token, "\"");
  464. $token = trim($token, "'");
  465. $userInfo = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $token)->first();
  466. if (empty($userInfo)) {
  467. return false;
  468. }
  469. // $userInfo->identity = $userInfo->account_identity;
  470. return $userInfo;
  471. }
  472. /**
  473. * 用户注册
  474. *
  475. * @return array
  476. */
  477. public function register($post)
  478. {
  479. if ($check = $this->repository->checkRegisterData($post)) {
  480. return $check;
  481. };
  482. foreach ($post as $k => $v) {
  483. $post[$k] = strip_tags($v);
  484. }
  485. $post['account'] = strtolower($post['account']);
  486. //$post['open_invitation'] = $this->getRandomString (8);
  487. $account = $this->model->where("account", $post['account'])->first();
  488. if (!empty($account)) {
  489. $result = ['status' => -4004, 'msg' => lang()->get('the user already exists. Please change it')];
  490. return $result;
  491. }
  492. if ($row = $this->repository->checkPassword($post['password'], $post['again_password'])) {
  493. return $row;
  494. }
  495. //添加新代理关系,$post['invita'] //前台提交的邀请码
  496. // if (isset($post['invita']) && !empty($post['invita'])) {
  497. // $data = $this->repository->checkInvitation($post['invita']);
  498. // if ($data < 0) {
  499. // $result = ['status' => -4004, 'msg' => lang()->get('invitation code error')];
  500. // return $result;
  501. // }
  502. // $post['parent_id'] = $data['account_identity']; //父级ID
  503. // $post['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  504. // $post['level'] = intval($data['level']) + 1;
  505. // //父级代理
  506. // $dataAgent['agent_identity'] = $data['account_identity'];
  507. // $dataAgent['agent_name'] = $data['account'];
  508. // //新代理父级
  509. // $newAgent['parent_id'] = $data['account_identity']; //父级ID
  510. // $newAgent['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  511. //
  512. // } else {
  513. //为空默认父级代理为root
  514. // $root = $this->repository->getRoot();
  515. // if ($root < 0) {
  516. // $result = ['status' => -4004, 'msg' => lang()->get('root does not exist')];
  517. // return $result;
  518. // }
  519. // $post['parent_id'] = $root['account_identity']; //父级ID
  520. // $post['parent_path'] = $root['account_identity']; //父级路径
  521. // $post['level'] = intval($root['level']) + 1; //等级
  522. // //父级代理
  523. // $dataAgent['agent_identity'] = $root['account_identity'];
  524. // $dataAgent['agent_name'] = $root['account'];
  525. // //新代理父级
  526. // $newAgent['parent_id'] = $root['account_identity']; //父级ID
  527. // $newAgent['parent_path'] = $root['account_identity']; //父级路径
  528. // }
  529. try {
  530. _beginTransaction();
  531. $accountData = [
  532. 'identity' => UUID(),
  533. 'account' => $post['account'],
  534. ];
  535. $res = $this->model->insert($accountData);
  536. if (!$res) {
  537. _rollBack();
  538. $result = ['status' => -4015, 'msg' => lang()->get('add fail')];
  539. return $result;
  540. }
  541. $res = $this->repository->addPassword($post['password'], $accountData['identity']);
  542. if ($res < 0) {
  543. _rollBack();
  544. $result = ['status' => -4113, 'msg' => lang()->get('passsword fail')];
  545. return $result;
  546. }
  547. $res = $this->repository->addAccountDetailed($post, $accountData['identity']);
  548. if ($res < 0) {
  549. _rollBack();
  550. $result = ['status' => -30101, 'msg' => lang()->get('add user fail')];
  551. return $result;
  552. }
  553. //新代理上下级关系信息
  554. // $dataAgent['account_identity'] = $accountData['identity'];
  555. // $dataAgent['account_name'] = $post['account'];
  556. //
  557. // $res = $this->repository->addNewAgent($dataAgent);
  558. // if ($res < 0) {
  559. // _rollBack();
  560. // $result = ['status' => -30101, 'msg' => lang()->get('add agent parent error')];
  561. // return $result;
  562. // }
  563. // $account = lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->first();
  564. //新代理数据封装
  565. // $newAgent['agent_identity'] = $accountData['identity'];
  566. // $newAgent['agent_user'] = $post['account'];
  567. // $newAgent['invite'] = $account->id;
  568. // $newAgent['register_time'] = date('Y-m-d H:i:s', time());
  569. // //添加新代理到代理详情表
  570. // $res = $this->repository->addNewAgentDetail($newAgent);
  571. // if ($res < 0) {
  572. // _rollBack();
  573. // $result = ['status' => -30101, 'msg' => lang()->get('add new agent error')];
  574. // return $result;
  575. // }
  576. $result = $this->login($post['account'], $post['password']);
  577. if ($post['introduce_user']) {
  578. $res = $this->repository->addParent($post['introduce_user'], $accountData);
  579. if ($res < 0) {
  580. _rollBack();
  581. $result = ['status' => $res, 'msg' => lang()->get('empty introduce_user')];
  582. lm("message", 'commons')->updateMessage('admin', $post['account'], '欢迎', '欢迎欢迎欢迎', 2);
  583. return $result;
  584. }
  585. }
  586. _commit();
  587. return $result;
  588. } catch (\Exception $e) {
  589. $result = ['status' => -4005, 'msg' => lang()->get('registration failed. please re-registration'), 'data' => $e->getMessage()];
  590. return $result;
  591. }
  592. }
  593. /*
  594. * 随机生成
  595. */
  596. public function getRandomString($len, $chars = null)
  597. {
  598. if (is_null($chars)) {
  599. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  600. }
  601. mt_srand(10000000 * (double)microtime());
  602. for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
  603. $str .= $chars[mt_rand(0, $lc)];
  604. }
  605. return $str;
  606. }
  607. /*
  608. * 用户开新户
  609. */
  610. public function newhouseholds($post)
  611. {
  612. if ($check = $this->repository->checkRegisterData($post)) {
  613. return $check;
  614. };
  615. $post['account'] = strtolower($post['account']);
  616. //$post['open_invitation'] = $this->getRandomString (8);
  617. $account = $this->model->where("account", $post['account'])->first();
  618. if (!empty($account)) {
  619. $result = ['status' => -4004, 'msg' => lang()->get('the user already exists. Please change it')];
  620. return $result;
  621. }
  622. if ($row = $this->repository->checkPassword($post['password'], $post['again_password'])) {
  623. return $row;
  624. }
  625. $parentinfo = lm('account_detailed', 'commons')->where('account_identity', $post['parent_identity'])->first();
  626. if (empty($parentinfo)) {
  627. return -500500;
  628. }
  629. $parentinfo = $parentinfo->toArray();
  630. $parentinfo['invitation'] = explode(',', $parentinfo['invitation']);
  631. $post['parent_id'] = $post['parent_identity'];
  632. if (empty($parentinfo['parent_path'][0])) {
  633. $post['parent_path'] = '2,' . $parentinfo['id'];
  634. } else {
  635. $post['parent_path'] = ($parentinfo['invitation'][0] + 1) . ',' . $parentinfo['id'];;
  636. }
  637. //unset($post['parent_identity']);
  638. try {
  639. _beginTransaction();
  640. $accountData = [
  641. 'identity' => UUID(),
  642. 'account' => $post['account'],
  643. ];
  644. $res = $this->model->insert($accountData);
  645. if (!$res) {
  646. _rollBack();
  647. $result = ['status' => -4015, 'msg' => lang()->get('add fail')];
  648. return $result;
  649. }
  650. $res = $this->repository->addPassword($post['password'], $accountData['identity']);
  651. if ($res < 0) {
  652. _rollBack();
  653. $result = ['status' => -4113, 'msg' => lang()->get('passsword fail')];
  654. return $result;
  655. }
  656. $res = $this->repository->addAccountDetailed($post, $accountData['identity']);
  657. $newaccountdetailed = lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->first();
  658. if (!empty($newaccountdetailed)) {
  659. $newaccountdetailed = $newaccountdetailed->toArray();
  660. lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->update(['open_invitation' => $newaccountdetailed['id']]);
  661. }
  662. if ($res < 0) {
  663. _rollBack();
  664. $result = ['status' => -30101, 'msg' => lang()->get('add user fail')];
  665. return $result;
  666. }
  667. $result = ['status' => $res, 'msg' => lang()->get('empty introduce_user')];
  668. _commit();
  669. return $result;
  670. } catch (\Exception $e) {
  671. $result = ['status' => -4005, 'msg' => lang()->get('registration failed. please re-registration'), 'data' => $e->getMessage()];
  672. return $result;
  673. }
  674. }
  675. /*
  676. * 手机端直接开户 Anton Liu
  677. */
  678. public function directAccountOpening($post)
  679. {
  680. if ($check = $this->repository->checkRegisterData($post)) {
  681. return $check;
  682. };
  683. $post['account'] = strtolower($post['account']);
  684. //$post['open_invitation'] = $this->getRandomString (8);
  685. $account = $this->model->where("account", $post['account'])->first();
  686. if (!empty($account)) {
  687. $result = ['status' => -4004, 'msg' => lang()->get('the user already exists. Please change it')];
  688. return $result;
  689. }
  690. if ($row = $this->repository->checkPassword($post['password'], $post['again_password'])) {
  691. return $row;
  692. }
  693. //添加新代理关系,$post['invita'] //前台提交的邀请码
  694. if (isset($post['invita']) && !empty($post['invita'])) {
  695. $data = $this->repository->checkInvitation($post['invita']);
  696. if ($data < 0) {
  697. $result = ['status' => -4004, 'msg' => lang()->get('invitation code error')];
  698. return $result;
  699. }
  700. $post['parent_id'] = $data['account_identity']; //父级ID
  701. $post['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  702. $post['level'] = intval($data['level']) + 1;
  703. //父级代理
  704. $dataAgent['agent_identity'] = $data['account_identity'];
  705. $dataAgent['agent_name'] = $data['account'];
  706. //新代理父级
  707. $newAgent['parent_id'] = $data['account_identity']; //父级ID
  708. $newAgent['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  709. } else {
  710. //为空默认父级代理为root
  711. $root = $this->repository->getRoot();
  712. if ($root < 0) {
  713. $result = ['status' => -4004, 'msg' => lang()->get('root does not exist')];
  714. return $result;
  715. }
  716. $post['parent_id'] = $root['account_identity']; //父级ID
  717. $post['parent_path'] = $root['account_identity']; //父级路径
  718. $post['level'] = intval($root['level']) + 1; //等级
  719. //父级代理
  720. $dataAgent['agent_identity'] = $root['account_identity'];
  721. $dataAgent['agent_name'] = $root['account'];
  722. //新代理父级
  723. $newAgent['parent_id'] = $root['account_identity']; //父级ID
  724. $newAgent['parent_path'] = $root['account_identity']; //父级路径
  725. }
  726. try {
  727. _beginTransaction();
  728. $accountData = [
  729. 'identity' => UUID(),
  730. 'account' => $post['account'],
  731. ];
  732. $res = $this->model->insert($accountData);
  733. if (!$res) {
  734. _rollBack();
  735. $result = ['status' => -4015, 'msg' => lang()->get('add fail')];
  736. return $result;
  737. }
  738. $res = $this->repository->addPassword($post['password'], $accountData['identity']);
  739. if ($res < 0) {
  740. _rollBack();
  741. $result = ['status' => -4113, 'msg' => lang()->get('passsword fail')];
  742. return $result;
  743. }
  744. $res = $this->repository->addAccountDetailed($post, $accountData['identity']);
  745. if ($res < 0) {
  746. _rollBack();
  747. $result = ['status' => -30101, 'msg' => lang()->get('add user fail')];
  748. return $result;
  749. }
  750. //新代理上下级关系信息
  751. $dataAgent['account_identity'] = $accountData['identity'];
  752. $dataAgent['account_name'] = $post['account'];
  753. $res = $this->repository->addNewAgent($dataAgent);
  754. if ($res < 0) {
  755. _rollBack();
  756. $result = ['status' => -30101, 'msg' => lang()->get('add agent parent error')];
  757. return $result;
  758. }
  759. $account = lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->first();
  760. //新代理数据封装
  761. $newAgent['agent_identity'] = $accountData['identity'];
  762. $newAgent['agent_user'] = $post['account'];
  763. $newAgent['invite'] = $account->id;
  764. $newAgent['register_time'] = date('Y-m-d H:i:s', time());
  765. //添加新代理到代理详情表
  766. $res = $this->repository->addNewAgentDetail($newAgent);
  767. if ($res < 0) {
  768. _rollBack();
  769. $result = ['status' => -30101, 'msg' => lang()->get('add new agent error')];
  770. return $result;
  771. }
  772. _commit();
  773. if ($res > 0) {
  774. $result = ['status' => 1, 'msg' => lang()->get('register success')];
  775. return $result;
  776. }
  777. } catch (\Exception $e) {
  778. $result = ['status' => -4005, 'msg' => lang()->get('registration failed. please re-registration'), 'data' => $e->getMessage()];
  779. return $result;
  780. }
  781. }
  782. /**
  783. * 检查登录是否过期
  784. *
  785. * @return array
  786. */
  787. public function checkEffectiveTime($token = '')
  788. {
  789. //重写此方法 by xmj
  790. define("OVERTIME", 1800);
  791. if (isCheckToken()) {
  792. $account = $this->getCurrentUser($token);
  793. // $identity = $account->account_identity;
  794. if ($account) {
  795. // $accountToken = lm('account_token', 'Commons')->where('account_identity', $identity)->first();
  796. // $effective_time = $accountToken->effective_time;
  797. // if ($effective_time > time() - OVERTIME) {
  798. // lm('account_token', 'Commons')->where('account_identity', $identity)->update(['effective_time' => strtotime('now')]);
  799. // } else {
  800. // $result = ['status' => -4008, 'msg' => lang()->get('user does not exist')];
  801. // }
  802. $result = $this->repository->checkEffectiveTime($account, OVERTIME);
  803. } else {
  804. $result = ['status' => -4001, 'msg' => lang()->get('user does not exist')];
  805. }
  806. return $result;
  807. }
  808. return ['status' => 1];
  809. }
  810. /**
  811. * 用户退出
  812. *
  813. * @param $accountIdentity
  814. * @return array
  815. */
  816. public function logout($accountIdentity)
  817. {
  818. lm('account_detailed', 'Commons')->where('account_identity', $accountIdentity)->update(['statuss' => '0']);
  819. $result = $this->repository->updateToken($accountIdentity);
  820. return $result;
  821. }
  822. /**
  823. * 销毁超过3天免费试玩玩家
  824. *
  825. * @return mixed
  826. */
  827. public function getAccountId()
  828. {
  829. $threebefore = date('Y-m-d H:i:s', time() - 3 * 24 * 3600);
  830. //$account = $this->model->join('account_detailed', 'account.identity', '=', 'account_detailed.account_identity')->where('account.status', 4)->get();
  831. $account = $this->model->select('logfile.account_identity')->join('logfile', 'account.identity', '=', 'logfile.account_identity')->where('account.status', 4)->where('logfile.add_time', '<', $threebefore)->groupBy('logfile.account_identity')->get();
  832. //echo '<pre>';print_r($account);echo '</pre>';
  833. $this->repository->destroyAccount($account->toArray());
  834. $num = $this->model->select('id')->count();
  835. return $num + 1;
  836. }
  837. /**
  838. * 注册免费试玩用户
  839. *
  840. * @return mixed
  841. */
  842. public function demoAccount()
  843. {
  844. $uname = isset($_POST['account']) ? $_POST['account'] : 'gust' . ($this->getAccountId() + 1); //由ikeke修改于2019-01-08主要增加默认密码便于登录
  845. $uname = strtolower($uname);
  846. $num = $this->getAccountId();
  847. $name = 'gust' . ($num + 1);
  848. $account = $this->model->where("account", $uname)->first();
  849. if (!empty($account) || ($uname != $name)) {
  850. $result = ['status' => -4022, 'msg' => '试玩账号有误'];
  851. return $result;
  852. }
  853. $pwd = isset($_POST['password']) ? $_POST['password'] : '123456'; //由ikeke修改于2019-01-08主要增加默认密码便于登录
  854. if (!$pwd) {
  855. return ['status' => 400, 'msg' => '没有密码'];
  856. }
  857. $result = $this->repository->demoAccount($uname, $pwd);
  858. $userInfo = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $result['data'])->first();
  859. $account = $this->login($uname, $pwd);
  860. isset($result['data']) ? $result['data'] = $account['data'][0]['token'] : '';
  861. $_SESSION['uinfo'] = $userInfo->toArray();
  862. $result['data'] = $_SESSION; //由ikeke修改于2019-01-08主要增加添加登录后的用户信息便于APP端调用
  863. return $result;
  864. }
  865. /**
  866. *
  867. * 判断是否已绑定银行卡
  868. * @return array
  869. * @throws \Exception
  870. */
  871. public function Iscard()
  872. {
  873. //获取用户ID并验证
  874. $uidentity = isset($_SESSION['uinfo']['identity']) ? $_SESSION['uinfo']['account_identity'] : '';
  875. if ($uidentity == '') {
  876. return ['data' => '', 'status' => -4020, 'msg' => lang()->get('Failed to obtain user information. Please refresh retry')];
  877. }
  878. $accountBank = lm('account_bank', 'Commons')->where('account_identity', $uidentity)->first();
  879. if ($accountBank) {
  880. $result = ['data' => '', 'status' => 200, 'msg' => ''];
  881. } else {
  882. $result = ['data' => '', 'status' => 1, 'msg' => lang()->get('Please bind the bank card')];
  883. }
  884. return $result;
  885. }
  886. /**
  887. * 获取用户信息
  888. *
  889. * @param $identity
  890. * @return array
  891. */
  892. public function getAccountDetailed($token)
  893. {
  894. $result = ['data' => '', '-4001', 'msg' => lang()->get('user does not exist')];
  895. $account = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $token)->first();
  896. if ($account) {
  897. $result = ['data' => $account, 'status' => 1, 'msg' => lang()->get('success')];
  898. }
  899. return $result;
  900. }
  901. /**
  902. * 申请代理用户
  903. *
  904. * @param $data
  905. * @return int
  906. */
  907. public function applyAgent($data)
  908. {
  909. // try {
  910. $row = $this->repository->checkAgent($data['agent_user']);
  911. if ($row == 1) {
  912. return -4014;
  913. }
  914. $res = $this->repository->addAgentDetailed($data);
  915. return $res;
  916. // } catch (\Exception $e) {
  917. // return -4015;
  918. // }
  919. }
  920. /**
  921. * 代理人与用户关联
  922. *
  923. * @param $data
  924. * @return int
  925. */
  926. public function agentAccount($data)
  927. {
  928. try {
  929. lm('agent_child', 'Commons')->insert($data);
  930. return 1;
  931. } catch (\Exception $e) {
  932. return -4016;
  933. }
  934. }
  935. /**
  936. * 代理用户登录
  937. *
  938. * @param $data
  939. * @return array|int
  940. */
  941. public function agentLogin($data)
  942. {
  943. try {
  944. return $this->repository->agentLogin($data);
  945. } catch (\Exception $e) {
  946. return -4018;
  947. }
  948. }
  949. /**
  950. * 代理退出
  951. *
  952. * @param $accountIdentity
  953. * @return array
  954. */
  955. public function agentlogout($agentIdentity)
  956. {
  957. $result = $this->repository->updateAgentToken($agentIdentity);
  958. return $result;
  959. }
  960. /**
  961. * 检查代理用户token是否过期
  962. *
  963. * @param $token
  964. * @return array|int
  965. */
  966. public function agentTokenOverdue($token)
  967. {
  968. if (!$token) {
  969. return -4017;
  970. }
  971. if (isCheckToken()) {
  972. $agentDetailed = $this->repository->getAgentDetailed($token);
  973. if ($agentDetailed) {
  974. return $this->repository->agentTokenOverdue($agentDetailed);
  975. } else {
  976. return -4017;
  977. }
  978. }
  979. return 1;
  980. }
  981. /**
  982. * 记录登录区域
  983. */
  984. public function areaLog($ip, $accountIdentity)
  985. {
  986. try {
  987. $ipget = C()->get('IP');
  988. $location = $ipget->getlocation($ip);
  989. $address = $ipget->trunUtf8($location['country'] . $location['area']);
  990. // $url = "http://ip.taobao.com/service/getIpInfo.php?ip=" . $ip;
  991. // $result = file_get_contents($url);
  992. // $row = json_decode($result, true);
  993. $logFile = lm('logfile', 'Commons')->where('account_identity', $accountIdentity)->orderBy('id', 'desc')->first();
  994. lm('logfile', 'Commons')->where('id', $logFile->id)->update(['area' => $address]);
  995. return 1;
  996. } catch (\Exception $e) {
  997. return -4022;
  998. }
  999. }
  1000. /**
  1001. * 设置游戏分类
  1002. *
  1003. * @param $data
  1004. * @return array
  1005. */
  1006. public function getSetOdds($data)
  1007. {
  1008. try {
  1009. $result = [];
  1010. foreach ($data as $key => $val) {
  1011. $result = $this->repository->getResult($key, $val, $result);
  1012. }
  1013. return ['data' => $result, 'status' => 1];
  1014. } catch (\Exception $e) {
  1015. return 0;
  1016. }
  1017. }
  1018. /**
  1019. * 设置倍率获取需要数据格式
  1020. *
  1021. * @param $data
  1022. * @return array
  1023. */
  1024. public function getNeedData($data)
  1025. {
  1026. $lang = $data['lang'];
  1027. $item = $data['data'];
  1028. $result = [];
  1029. foreach ($item as $k => $v) {
  1030. if ($v['type'] == 0) {
  1031. $name = $this->getGameName($k);
  1032. $v['lang'] = $lang[$k];
  1033. $result[$name][] = $v;
  1034. }
  1035. }
  1036. return $result;
  1037. }
  1038. /**
  1039. * 获取设置倍率的游戏类
  1040. *
  1041. * @param $k
  1042. * @return string
  1043. */
  1044. public function getGameName($k)
  1045. {
  1046. substr($k, 0, 5) == 'chain' ? $parent = 'chain' : '';
  1047. substr($k, 0, 6) == 'dragon' ? $parent = 'dragon' : '';
  1048. substr($k, 0, 8) == 'head_one' ? $parent = 'head_one' : '';
  1049. substr($k, 0, 8) == 'head_two' ? $parent = 'head_two' : '';
  1050. substr($k, 0, 10) == 'head_three' ? $parent = 'head_three' : '';
  1051. substr($k, 0, 7) == 'poscode' ? $parent = 'poscode' : '';
  1052. substr($k, 0, 5) == 'color' ? $parent = 'color' : '';
  1053. substr($k, 0, 3) == 'mix' ? $parent = 'mix' : '';
  1054. substr($k, 0, 4) == 'size' ? $parent = 'size' : '';
  1055. substr($k, 0, 11) == 'specialCode' ? $parent = 'specialCode' : '';
  1056. return $parent;
  1057. }
  1058. }