AccountManager.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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. $result = $this->repository->updateToken($accountIdentity);
  819. return $result;
  820. }
  821. /**
  822. * 销毁超过3天免费试玩玩家
  823. *
  824. * @return mixed
  825. */
  826. public function getAccountId()
  827. {
  828. $threebefore = date('Y-m-d H:i:s', time() - 3 * 24 * 3600);
  829. //$account = $this->model->join('account_detailed', 'account.identity', '=', 'account_detailed.account_identity')->where('account.status', 4)->get();
  830. $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();
  831. //echo '<pre>';print_r($account);echo '</pre>';
  832. $this->repository->destroyAccount($account->toArray());
  833. $num = $this->model->select('id')->count();
  834. return $num + 1;
  835. }
  836. /**
  837. * 注册免费试玩用户
  838. *
  839. * @return mixed
  840. */
  841. public function demoAccount()
  842. {
  843. $uname = isset($_POST['account']) ? $_POST['account'] : 'gust' . ($this->getAccountId() + 1); //由ikeke修改于2019-01-08主要增加默认密码便于登录
  844. $uname = strtolower($uname);
  845. $num = $this->getAccountId();
  846. $name = 'gust' . ($num + 1);
  847. $account = $this->model->where("account", $uname)->first();
  848. if (!empty($account) || ($uname != $name)) {
  849. $result = ['status' => -4022, 'msg' => '试玩账号有误'];
  850. return $result;
  851. }
  852. $pwd = isset($_POST['password']) ? $_POST['password'] : '123456'; //由ikeke修改于2019-01-08主要增加默认密码便于登录
  853. if (!$pwd) {
  854. return ['status' => 400, 'msg' => '没有密码'];
  855. }
  856. $result = $this->repository->demoAccount($uname, $pwd);
  857. $userInfo = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $result['data'])->first();
  858. $account = $this->login($uname, $pwd);
  859. isset($result['data']) ? $result['data'] = $account['data'][0]['token'] : '';
  860. $_SESSION['uinfo'] = $userInfo->toArray();
  861. $result['data'] = $_SESSION; //由ikeke修改于2019-01-08主要增加添加登录后的用户信息便于APP端调用
  862. return $result;
  863. }
  864. /**
  865. *
  866. * 判断是否已绑定银行卡
  867. * @return array
  868. * @throws \Exception
  869. */
  870. public function Iscard()
  871. {
  872. //获取用户ID并验证
  873. $uidentity = isset($_SESSION['uinfo']['identity']) ? $_SESSION['uinfo']['account_identity'] : '';
  874. if ($uidentity == '') {
  875. return ['data' => '', 'status' => -4020, 'msg' => lang()->get('Failed to obtain user information. Please refresh retry')];
  876. }
  877. $accountBank = lm('account_bank', 'Commons')->where('account_identity', $uidentity)->first();
  878. if ($accountBank) {
  879. $result = ['data' => '', 'status' => 200, 'msg' => ''];
  880. } else {
  881. $result = ['data' => '', 'status' => 1, 'msg' => lang()->get('Please bind the bank card')];
  882. }
  883. return $result;
  884. }
  885. /**
  886. * 获取用户信息
  887. *
  888. * @param $identity
  889. * @return array
  890. */
  891. public function getAccountDetailed($token)
  892. {
  893. $result = ['data' => '', '-4001', 'msg' => lang()->get('user does not exist')];
  894. $account = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $token)->first();
  895. if ($account) {
  896. $result = ['data' => $account, 'status' => 1, 'msg' => lang()->get('success')];
  897. }
  898. return $result;
  899. }
  900. /**
  901. * 申请代理用户
  902. *
  903. * @param $data
  904. * @return int
  905. */
  906. public function applyAgent($data)
  907. {
  908. // try {
  909. $row = $this->repository->checkAgent($data['agent_user']);
  910. if ($row == 1) {
  911. return -4014;
  912. }
  913. $res = $this->repository->addAgentDetailed($data);
  914. return $res;
  915. // } catch (\Exception $e) {
  916. // return -4015;
  917. // }
  918. }
  919. /**
  920. * 代理人与用户关联
  921. *
  922. * @param $data
  923. * @return int
  924. */
  925. public function agentAccount($data)
  926. {
  927. try {
  928. lm('agent_child', 'Commons')->insert($data);
  929. return 1;
  930. } catch (\Exception $e) {
  931. return -4016;
  932. }
  933. }
  934. /**
  935. * 代理用户登录
  936. *
  937. * @param $data
  938. * @return array|int
  939. */
  940. public function agentLogin($data)
  941. {
  942. try {
  943. return $this->repository->agentLogin($data);
  944. } catch (\Exception $e) {
  945. return -4018;
  946. }
  947. }
  948. /**
  949. * 代理退出
  950. *
  951. * @param $accountIdentity
  952. * @return array
  953. */
  954. public function agentlogout($agentIdentity)
  955. {
  956. $result = $this->repository->updateAgentToken($agentIdentity);
  957. return $result;
  958. }
  959. /**
  960. * 检查代理用户token是否过期
  961. *
  962. * @param $token
  963. * @return array|int
  964. */
  965. public function agentTokenOverdue($token)
  966. {
  967. if (!$token) {
  968. return -4017;
  969. }
  970. if (isCheckToken()) {
  971. $agentDetailed = $this->repository->getAgentDetailed($token);
  972. if ($agentDetailed) {
  973. return $this->repository->agentTokenOverdue($agentDetailed);
  974. } else {
  975. return -4017;
  976. }
  977. }
  978. return 1;
  979. }
  980. /**
  981. * 记录登录区域
  982. */
  983. public function areaLog($ip, $accountIdentity)
  984. {
  985. try {
  986. $ipget = C()->get('IP');
  987. $location = $ipget->getlocation($ip);
  988. $address = $ipget->trunUtf8($location['country'] . $location['area']);
  989. // $url = "http://ip.taobao.com/service/getIpInfo.php?ip=" . $ip;
  990. // $result = file_get_contents($url);
  991. // $row = json_decode($result, true);
  992. $logFile = lm('logfile', 'Commons')->where('account_identity', $accountIdentity)->orderBy('id', 'desc')->first();
  993. lm('logfile', 'Commons')->where('id', $logFile->id)->update(['area' => $address]);
  994. return 1;
  995. } catch (\Exception $e) {
  996. return -4022;
  997. }
  998. }
  999. /**
  1000. * 设置游戏分类
  1001. *
  1002. * @param $data
  1003. * @return array
  1004. */
  1005. public function getSetOdds($data)
  1006. {
  1007. try {
  1008. $result = [];
  1009. foreach ($data as $key => $val) {
  1010. $result = $this->repository->getResult($key, $val, $result);
  1011. }
  1012. return ['data' => $result, 'status' => 1];
  1013. } catch (\Exception $e) {
  1014. return 0;
  1015. }
  1016. }
  1017. /**
  1018. * 设置倍率获取需要数据格式
  1019. *
  1020. * @param $data
  1021. * @return array
  1022. */
  1023. public function getNeedData($data)
  1024. {
  1025. $lang = $data['lang'];
  1026. $item = $data['data'];
  1027. $result = [];
  1028. foreach ($item as $k => $v) {
  1029. if ($v['type'] == 0) {
  1030. $name = $this->getGameName($k);
  1031. $v['lang'] = $lang[$k];
  1032. $result[$name][] = $v;
  1033. }
  1034. }
  1035. return $result;
  1036. }
  1037. /**
  1038. * 获取设置倍率的游戏类
  1039. *
  1040. * @param $k
  1041. * @return string
  1042. */
  1043. public function getGameName($k)
  1044. {
  1045. substr($k, 0, 5) == 'chain' ? $parent = 'chain' : '';
  1046. substr($k, 0, 6) == 'dragon' ? $parent = 'dragon' : '';
  1047. substr($k, 0, 8) == 'head_one' ? $parent = 'head_one' : '';
  1048. substr($k, 0, 8) == 'head_two' ? $parent = 'head_two' : '';
  1049. substr($k, 0, 10) == 'head_three' ? $parent = 'head_three' : '';
  1050. substr($k, 0, 7) == 'poscode' ? $parent = 'poscode' : '';
  1051. substr($k, 0, 5) == 'color' ? $parent = 'color' : '';
  1052. substr($k, 0, 3) == 'mix' ? $parent = 'mix' : '';
  1053. substr($k, 0, 4) == 'size' ? $parent = 'size' : '';
  1054. substr($k, 0, 11) == 'specialCode' ? $parent = 'specialCode' : '';
  1055. return $parent;
  1056. }
  1057. }