AccountManager.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. // Render([], '2001', lang('Common','Api') -> get('user does login'));
  468. return false;
  469. }
  470. // $userInfo->identity = $userInfo->account_identity;
  471. return $userInfo;
  472. }
  473. /**
  474. * 用户注册
  475. *
  476. * @return array
  477. */
  478. public function register($post)
  479. {
  480. if ($check = $this->repository->checkRegisterData($post)) {
  481. return $check;
  482. };
  483. foreach ($post as $k => $v) {
  484. $post[$k] = strip_tags($v);
  485. }
  486. $post['account'] = strtolower($post['account']);
  487. //$post['open_invitation'] = $this->getRandomString (8);
  488. $account = $this->model->where("account", $post['account'])->first();
  489. if (!empty($account)) {
  490. $result = ['status' => -4004, 'msg' => lang()->get('the user already exists. Please change it')];
  491. return $result;
  492. }
  493. if ($row = $this->repository->checkPassword($post['password'], $post['again_password'])) {
  494. return $row;
  495. }
  496. //添加新代理关系,$post['invita'] //前台提交的邀请码
  497. // if (isset($post['invita']) && !empty($post['invita'])) {
  498. // $data = $this->repository->checkInvitation($post['invita']);
  499. // if ($data < 0) {
  500. // $result = ['status' => -4004, 'msg' => lang()->get('invitation code error')];
  501. // return $result;
  502. // }
  503. // $post['parent_id'] = $data['account_identity']; //父级ID
  504. // $post['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  505. // $post['level'] = intval($data['level']) + 1;
  506. // //父级代理
  507. // $dataAgent['agent_identity'] = $data['account_identity'];
  508. // $dataAgent['agent_name'] = $data['account'];
  509. // //新代理父级
  510. // $newAgent['parent_id'] = $data['account_identity']; //父级ID
  511. // $newAgent['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  512. //
  513. // } else {
  514. //为空默认父级代理为root
  515. // $root = $this->repository->getRoot();
  516. // if ($root < 0) {
  517. // $result = ['status' => -4004, 'msg' => lang()->get('root does not exist')];
  518. // return $result;
  519. // }
  520. // $post['parent_id'] = $root['account_identity']; //父级ID
  521. // $post['parent_path'] = $root['account_identity']; //父级路径
  522. // $post['level'] = intval($root['level']) + 1; //等级
  523. // //父级代理
  524. // $dataAgent['agent_identity'] = $root['account_identity'];
  525. // $dataAgent['agent_name'] = $root['account'];
  526. // //新代理父级
  527. // $newAgent['parent_id'] = $root['account_identity']; //父级ID
  528. // $newAgent['parent_path'] = $root['account_identity']; //父级路径
  529. // }
  530. try {
  531. _beginTransaction();
  532. $accountData = [
  533. 'identity' => UUID(),
  534. 'account' => $post['account'],
  535. ];
  536. $res = $this->model->insert($accountData);
  537. if (!$res) {
  538. _rollBack();
  539. $result = ['status' => -4015, 'msg' => lang()->get('add fail')];
  540. return $result;
  541. }
  542. $res = $this->repository->addPassword($post['password'], $accountData['identity']);
  543. if ($res < 0) {
  544. _rollBack();
  545. $result = ['status' => -4113, 'msg' => lang()->get('passsword fail')];
  546. return $result;
  547. }
  548. $res = $this->repository->addAccountDetailed($post, $accountData['identity']);
  549. if ($res < 0) {
  550. _rollBack();
  551. $result = ['status' => -30101, 'msg' => lang()->get('add user fail')];
  552. return $result;
  553. }
  554. //新代理上下级关系信息
  555. // $dataAgent['account_identity'] = $accountData['identity'];
  556. // $dataAgent['account_name'] = $post['account'];
  557. //
  558. // $res = $this->repository->addNewAgent($dataAgent);
  559. // if ($res < 0) {
  560. // _rollBack();
  561. // $result = ['status' => -30101, 'msg' => lang()->get('add agent parent error')];
  562. // return $result;
  563. // }
  564. // $account = lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->first();
  565. //新代理数据封装
  566. // $newAgent['agent_identity'] = $accountData['identity'];
  567. // $newAgent['agent_user'] = $post['account'];
  568. // $newAgent['invite'] = $account->id;
  569. // $newAgent['register_time'] = date('Y-m-d H:i:s', time());
  570. // //添加新代理到代理详情表
  571. // $res = $this->repository->addNewAgentDetail($newAgent);
  572. // if ($res < 0) {
  573. // _rollBack();
  574. // $result = ['status' => -30101, 'msg' => lang()->get('add new agent error')];
  575. // return $result;
  576. // }
  577. $result = $this->login($post['account'], $post['password']);
  578. if ($post['introduce_user']) {
  579. $res = $this->repository->addParent($post['introduce_user'], $accountData);
  580. if ($res < 0) {
  581. _rollBack();
  582. $result = ['status' => $res, 'msg' => lang()->get('empty introduce_user')];
  583. lm("message", 'commons')->updateMessage('admin', $post['account'], '欢迎', '欢迎欢迎欢迎', 2);
  584. return $result;
  585. }
  586. }
  587. _commit();
  588. return $result;
  589. } catch (\Exception $e) {
  590. $result = ['status' => -4005, 'msg' => lang()->get('registration failed. please re-registration'), 'data' => $e->getMessage()];
  591. return $result;
  592. }
  593. }
  594. /*
  595. * 随机生成
  596. */
  597. public function getRandomString($len, $chars = null)
  598. {
  599. if (is_null($chars)) {
  600. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  601. }
  602. mt_srand(10000000 * (double)microtime());
  603. for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
  604. $str .= $chars[mt_rand(0, $lc)];
  605. }
  606. return $str;
  607. }
  608. /*
  609. * 用户开新户
  610. */
  611. public function newhouseholds($post)
  612. {
  613. if ($check = $this->repository->checkRegisterData($post)) {
  614. return $check;
  615. };
  616. $post['account'] = strtolower($post['account']);
  617. //$post['open_invitation'] = $this->getRandomString (8);
  618. $account = $this->model->where("account", $post['account'])->first();
  619. if (!empty($account)) {
  620. $result = ['status' => -4004, 'msg' => lang()->get('the user already exists. Please change it')];
  621. return $result;
  622. }
  623. if ($row = $this->repository->checkPassword($post['password'], $post['again_password'])) {
  624. return $row;
  625. }
  626. $parentinfo = lm('account_detailed', 'commons')->where('account_identity', $post['parent_identity'])->first();
  627. if (empty($parentinfo)) {
  628. return -500500;
  629. }
  630. $parentinfo = $parentinfo->toArray();
  631. $parentinfo['invitation'] = explode(',', $parentinfo['invitation']);
  632. $post['parent_id'] = $post['parent_identity'];
  633. if (empty($parentinfo['parent_path'][0])) {
  634. $post['parent_path'] = '2,' . $parentinfo['id'];
  635. } else {
  636. $post['parent_path'] = ($parentinfo['invitation'][0] + 1) . ',' . $parentinfo['id'];;
  637. }
  638. //unset($post['parent_identity']);
  639. try {
  640. _beginTransaction();
  641. $accountData = [
  642. 'identity' => UUID(),
  643. 'account' => $post['account'],
  644. ];
  645. $res = $this->model->insert($accountData);
  646. if (!$res) {
  647. _rollBack();
  648. $result = ['status' => -4015, 'msg' => lang()->get('add fail')];
  649. return $result;
  650. }
  651. $res = $this->repository->addPassword($post['password'], $accountData['identity']);
  652. if ($res < 0) {
  653. _rollBack();
  654. $result = ['status' => -4113, 'msg' => lang()->get('passsword fail')];
  655. return $result;
  656. }
  657. $res = $this->repository->addAccountDetailed($post, $accountData['identity']);
  658. $newaccountdetailed = lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->first();
  659. if (!empty($newaccountdetailed)) {
  660. $newaccountdetailed = $newaccountdetailed->toArray();
  661. lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->update(['open_invitation' => $newaccountdetailed['id']]);
  662. }
  663. if ($res < 0) {
  664. _rollBack();
  665. $result = ['status' => -30101, 'msg' => lang()->get('add user fail')];
  666. return $result;
  667. }
  668. $result = ['status' => $res, 'msg' => lang()->get('empty introduce_user')];
  669. _commit();
  670. return $result;
  671. } catch (\Exception $e) {
  672. $result = ['status' => -4005, 'msg' => lang()->get('registration failed. please re-registration'), 'data' => $e->getMessage()];
  673. return $result;
  674. }
  675. }
  676. /*
  677. * 手机端直接开户 Anton Liu
  678. */
  679. public function directAccountOpening($post)
  680. {
  681. if ($check = $this->repository->checkRegisterData($post)) {
  682. return $check;
  683. };
  684. $post['account'] = strtolower($post['account']);
  685. //$post['open_invitation'] = $this->getRandomString (8);
  686. $account = $this->model->where("account", $post['account'])->first();
  687. if (!empty($account)) {
  688. $result = ['status' => -4004, 'msg' => lang()->get('the user already exists. Please change it')];
  689. return $result;
  690. }
  691. if ($row = $this->repository->checkPassword($post['password'], $post['again_password'])) {
  692. return $row;
  693. }
  694. //添加新代理关系,$post['invita'] //前台提交的邀请码
  695. if (isset($post['invita']) && !empty($post['invita'])) {
  696. $data = $this->repository->checkInvitation($post['invita']);
  697. if ($data < 0) {
  698. $result = ['status' => -4004, 'msg' => lang()->get('invitation code error')];
  699. return $result;
  700. }
  701. $post['parent_id'] = $data['account_identity']; //父级ID
  702. $post['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  703. $post['level'] = intval($data['level']) + 1;
  704. //父级代理
  705. $dataAgent['agent_identity'] = $data['account_identity'];
  706. $dataAgent['agent_name'] = $data['account'];
  707. //新代理父级
  708. $newAgent['parent_id'] = $data['account_identity']; //父级ID
  709. $newAgent['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  710. } else {
  711. //为空默认父级代理为root
  712. $root = $this->repository->getRoot();
  713. if ($root < 0) {
  714. $result = ['status' => -4004, 'msg' => lang()->get('root does not exist')];
  715. return $result;
  716. }
  717. $post['parent_id'] = $root['account_identity']; //父级ID
  718. $post['parent_path'] = $root['account_identity']; //父级路径
  719. $post['level'] = intval($root['level']) + 1; //等级
  720. //父级代理
  721. $dataAgent['agent_identity'] = $root['account_identity'];
  722. $dataAgent['agent_name'] = $root['account'];
  723. //新代理父级
  724. $newAgent['parent_id'] = $root['account_identity']; //父级ID
  725. $newAgent['parent_path'] = $root['account_identity']; //父级路径
  726. }
  727. try {
  728. _beginTransaction();
  729. $accountData = [
  730. 'identity' => UUID(),
  731. 'account' => $post['account'],
  732. ];
  733. $res = $this->model->insert($accountData);
  734. if (!$res) {
  735. _rollBack();
  736. $result = ['status' => -4015, 'msg' => lang()->get('add fail')];
  737. return $result;
  738. }
  739. $res = $this->repository->addPassword($post['password'], $accountData['identity']);
  740. if ($res < 0) {
  741. _rollBack();
  742. $result = ['status' => -4113, 'msg' => lang()->get('passsword fail')];
  743. return $result;
  744. }
  745. $res = $this->repository->addAccountDetailed($post, $accountData['identity']);
  746. if ($res < 0) {
  747. _rollBack();
  748. $result = ['status' => -30101, 'msg' => lang()->get('add user fail')];
  749. return $result;
  750. }
  751. //新代理上下级关系信息
  752. $dataAgent['account_identity'] = $accountData['identity'];
  753. $dataAgent['account_name'] = $post['account'];
  754. $res = $this->repository->addNewAgent($dataAgent);
  755. if ($res < 0) {
  756. _rollBack();
  757. $result = ['status' => -30101, 'msg' => lang()->get('add agent parent error')];
  758. return $result;
  759. }
  760. $account = lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->first();
  761. //新代理数据封装
  762. $newAgent['agent_identity'] = $accountData['identity'];
  763. $newAgent['agent_user'] = $post['account'];
  764. $newAgent['invite'] = $account->id;
  765. $newAgent['register_time'] = date('Y-m-d H:i:s', time());
  766. //添加新代理到代理详情表
  767. $res = $this->repository->addNewAgentDetail($newAgent);
  768. if ($res < 0) {
  769. _rollBack();
  770. $result = ['status' => -30101, 'msg' => lang()->get('add new agent error')];
  771. return $result;
  772. }
  773. _commit();
  774. if ($res > 0) {
  775. $result = ['status' => 1, 'msg' => lang()->get('register success')];
  776. return $result;
  777. }
  778. } catch (\Exception $e) {
  779. $result = ['status' => -4005, 'msg' => lang()->get('registration failed. please re-registration'), 'data' => $e->getMessage()];
  780. return $result;
  781. }
  782. }
  783. /**
  784. * 检查登录是否过期
  785. *
  786. * @return array
  787. */
  788. public function checkEffectiveTime($token = '')
  789. {
  790. //重写此方法 by xmj
  791. define("OVERTIME", 1800);
  792. if (isCheckToken()) {
  793. $account = $this->getCurrentUser($token);
  794. // $identity = $account->account_identity;
  795. if ($account) {
  796. // $accountToken = lm('account_token', 'Commons')->where('account_identity', $identity)->first();
  797. // $effective_time = $accountToken->effective_time;
  798. // if ($effective_time > time() - OVERTIME) {
  799. // lm('account_token', 'Commons')->where('account_identity', $identity)->update(['effective_time' => strtotime('now')]);
  800. // } else {
  801. // $result = ['status' => -4008, 'msg' => lang()->get('user does not exist')];
  802. // }
  803. $result = $this->repository->checkEffectiveTime($account, OVERTIME);
  804. } else {
  805. $result = ['status' => -4001, 'msg' => lang()->get('user does not exist')];
  806. }
  807. return $result;
  808. }
  809. return ['status' => 1];
  810. }
  811. /**
  812. * 用户退出
  813. *
  814. * @param $accountIdentity
  815. * @return array
  816. */
  817. public function logout($accountIdentity)
  818. {
  819. lm('account_detailed', 'Commons')->where('account_identity', $accountIdentity)->update(['statuss' => '0']);
  820. $result = $this->repository->updateToken($accountIdentity);
  821. return $result;
  822. }
  823. /**
  824. * 销毁超过3天免费试玩玩家
  825. *
  826. * @return mixed
  827. */
  828. public function getAccountId()
  829. {
  830. $threebefore = date('Y-m-d H:i:s', time() - 3 * 24 * 3600);
  831. //$account = $this->model->join('account_detailed', 'account.identity', '=', 'account_detailed.account_identity')->where('account.status', 4)->get();
  832. $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();
  833. //echo '<pre>';print_r($account);echo '</pre>';
  834. $this->repository->destroyAccount($account->toArray());
  835. $num = $this->model->select('id')->count();
  836. return $num + 1;
  837. }
  838. /**
  839. * 注册免费试玩用户
  840. *
  841. * @return mixed
  842. */
  843. public function demoAccount()
  844. {
  845. $uname = isset($_POST['account']) ? $_POST['account'] : 'gust' . ($this->getAccountId() + 1); //由ikeke修改于2019-01-08主要增加默认密码便于登录
  846. $uname = strtolower($uname);
  847. $num = $this->getAccountId();
  848. $name = 'gust' . ($num + 1);
  849. $account = $this->model->where("account", $uname)->first();
  850. if (!empty($account) || ($uname != $name)) {
  851. $result = ['status' => -4022, 'msg' => '试玩账号有误'];
  852. return $result;
  853. }
  854. $pwd = isset($_POST['password']) ? $_POST['password'] : '123456'; //由ikeke修改于2019-01-08主要增加默认密码便于登录
  855. if (!$pwd) {
  856. return ['status' => 400, 'msg' => '没有密码'];
  857. }
  858. $result = $this->repository->demoAccount($uname, $pwd);
  859. $userInfo = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $result['data'])->first();
  860. $account = $this->login($uname, $pwd);
  861. isset($result['data']) ? $result['data'] = $account['data'][0]['token'] : '';
  862. $_SESSION['uinfo'] = $userInfo->toArray();
  863. $result['data'] = $_SESSION; //由ikeke修改于2019-01-08主要增加添加登录后的用户信息便于APP端调用
  864. return $result;
  865. }
  866. /**
  867. *
  868. * 判断是否已绑定银行卡
  869. * @return array
  870. * @throws \Exception
  871. */
  872. public function Iscard()
  873. {
  874. //获取用户ID并验证
  875. $uidentity = isset($_SESSION['uinfo']['identity']) ? $_SESSION['uinfo']['account_identity'] : '';
  876. if ($uidentity == '') {
  877. return ['data' => '', 'status' => -4020, 'msg' => lang()->get('Failed to obtain user information. Please refresh retry')];
  878. }
  879. $accountBank = lm('account_bank', 'Commons')->where('account_identity', $uidentity)->first();
  880. if ($accountBank) {
  881. $result = ['data' => '', 'status' => 200, 'msg' => ''];
  882. } else {
  883. $result = ['data' => '', 'status' => 1, 'msg' => lang()->get('Please bind the bank card')];
  884. }
  885. return $result;
  886. }
  887. /**
  888. * 获取用户信息
  889. *
  890. * @param $identity
  891. * @return array
  892. */
  893. public function getAccountDetailed($token)
  894. {
  895. $result = ['data' => '', '-4001', 'msg' => lang()->get('user does not exist')];
  896. $account = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $token)->first();
  897. if ($account) {
  898. $result = ['data' => $account, 'status' => 1, 'msg' => lang()->get('success')];
  899. }
  900. return $result;
  901. }
  902. /**
  903. * 申请代理用户
  904. *
  905. * @param $data
  906. * @return int
  907. */
  908. public function applyAgent($data)
  909. {
  910. // try {
  911. $row = $this->repository->checkAgent($data['agent_user']);
  912. if ($row == 1) {
  913. return -4014;
  914. }
  915. $res = $this->repository->addAgentDetailed($data);
  916. return $res;
  917. // } catch (\Exception $e) {
  918. // return -4015;
  919. // }
  920. }
  921. /**
  922. * 代理人与用户关联
  923. *
  924. * @param $data
  925. * @return int
  926. */
  927. public function agentAccount($data)
  928. {
  929. try {
  930. lm('agent_child', 'Commons')->insert($data);
  931. return 1;
  932. } catch (\Exception $e) {
  933. return -4016;
  934. }
  935. }
  936. /**
  937. * 代理用户登录
  938. *
  939. * @param $data
  940. * @return array|int
  941. */
  942. public function agentLogin($data)
  943. {
  944. try {
  945. return $this->repository->agentLogin($data);
  946. } catch (\Exception $e) {
  947. return -4018;
  948. }
  949. }
  950. /**
  951. * 代理退出
  952. *
  953. * @param $accountIdentity
  954. * @return array
  955. */
  956. public function agentlogout($agentIdentity)
  957. {
  958. $result = $this->repository->updateAgentToken($agentIdentity);
  959. return $result;
  960. }
  961. /**
  962. * 检查代理用户token是否过期
  963. *
  964. * @param $token
  965. * @return array|int
  966. */
  967. public function agentTokenOverdue($token)
  968. {
  969. if (!$token) {
  970. return -4017;
  971. }
  972. if (isCheckToken()) {
  973. $agentDetailed = $this->repository->getAgentDetailed($token);
  974. if ($agentDetailed) {
  975. return $this->repository->agentTokenOverdue($agentDetailed);
  976. } else {
  977. return -4017;
  978. }
  979. }
  980. return 1;
  981. }
  982. /**
  983. * 记录登录区域
  984. */
  985. public function areaLog($ip, $accountIdentity)
  986. {
  987. try {
  988. $ipget = C()->get('IP');
  989. $location = $ipget->getlocation($ip);
  990. $address = $ipget->trunUtf8($location['country'] . $location['area']);
  991. // $url = "http://ip.taobao.com/service/getIpInfo.php?ip=" . $ip;
  992. // $result = file_get_contents($url);
  993. // $row = json_decode($result, true);
  994. $logFile = lm('logfile', 'Commons')->where('account_identity', $accountIdentity)->orderBy('id', 'desc')->first();
  995. lm('logfile', 'Commons')->where('id', $logFile->id)->update(['area' => $address]);
  996. return 1;
  997. } catch (\Exception $e) {
  998. return -4022;
  999. }
  1000. }
  1001. /**
  1002. * 设置游戏分类
  1003. *
  1004. * @param $data
  1005. * @return array
  1006. */
  1007. public function getSetOdds($data)
  1008. {
  1009. try {
  1010. $result = [];
  1011. foreach ($data as $key => $val) {
  1012. $result = $this->repository->getResult($key, $val, $result);
  1013. }
  1014. return ['data' => $result, 'status' => 1];
  1015. } catch (\Exception $e) {
  1016. return 0;
  1017. }
  1018. }
  1019. /**
  1020. * 设置倍率获取需要数据格式
  1021. *
  1022. * @param $data
  1023. * @return array
  1024. */
  1025. public function getNeedData($data)
  1026. {
  1027. $lang = $data['lang'];
  1028. $item = $data['data'];
  1029. $result = [];
  1030. foreach ($item as $k => $v) {
  1031. if ($v['type'] == 0) {
  1032. $name = $this->getGameName($k);
  1033. $v['lang'] = $lang[$k];
  1034. $result[$name][] = $v;
  1035. }
  1036. }
  1037. return $result;
  1038. }
  1039. /**
  1040. * 获取设置倍率的游戏类
  1041. *
  1042. * @param $k
  1043. * @return string
  1044. */
  1045. public function getGameName($k)
  1046. {
  1047. substr($k, 0, 5) == 'chain' ? $parent = 'chain' : '';
  1048. substr($k, 0, 6) == 'dragon' ? $parent = 'dragon' : '';
  1049. substr($k, 0, 8) == 'head_one' ? $parent = 'head_one' : '';
  1050. substr($k, 0, 8) == 'head_two' ? $parent = 'head_two' : '';
  1051. substr($k, 0, 10) == 'head_three' ? $parent = 'head_three' : '';
  1052. substr($k, 0, 7) == 'poscode' ? $parent = 'poscode' : '';
  1053. substr($k, 0, 5) == 'color' ? $parent = 'color' : '';
  1054. substr($k, 0, 3) == 'mix' ? $parent = 'mix' : '';
  1055. substr($k, 0, 4) == 'size' ? $parent = 'size' : '';
  1056. substr($k, 0, 11) == 'specialCode' ? $parent = 'specialCode' : '';
  1057. return $parent;
  1058. }
  1059. }