AccountManager.php 41 KB

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