AccountManager.php 41 KB

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