AccountManager.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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() {
  256. $str_token = $_GET['token'] ? trim($_GET['token']) : '';
  257. $token = new TokenManager();
  258. $userInfo = $token->getAccountInfo($str_token);
  259. if (count($userInfo) > 0) {
  260. $result = ['data' => $userInfo->toArray(), 'status' => 1, 'msg' => lang()->get('success')];
  261. return $result;
  262. } else {
  263. $result = ['status' => -4008, 'msg' => lang()->get('user does not exist')];
  264. return $result;
  265. }
  266. }
  267. /**
  268. * 刷新状态
  269. * @param [type] $token [description]
  270. * @return [type] [description]
  271. */
  272. public function refreshStatus($str_token) {
  273. $token = new TokenManager();
  274. if (empty($str_token)) {
  275. return -4001;
  276. }
  277. $userInfo = $token->getAccountInfo($str_token);
  278. $agentInfo = $token->getAgentInfo($str_token);
  279. if (count($userInfo) > 0) {
  280. $user = $userInfo->toArray();
  281. $this->repository->updateEffectiveTime($user['account_identity'], $str_token);
  282. $_SESSION['uinfo'] = $user;
  283. if (count($agentInfo) > 0) {
  284. $_SESSION['agent'] = $agentInfo->toArray();
  285. // $str_token = $token->getToken();
  286. // $this->repository->publishAgentToken($_SESSION['agent']['agent_identity'], $str_token);
  287. }
  288. return $user;
  289. }
  290. return -4001;
  291. }
  292. /**
  293. * 修改用户密码
  294. *
  295. * @access public
  296. * @param mixed oldPassword旧密码
  297. * @param mixed newPassword新密码
  298. * @return array JsonString
  299. * @throws \Exception
  300. */
  301. public function savePassword($post) {
  302. $userInfo = $this -> getCurrentUser();
  303. // 判断用户登录
  304. if (empty($userInfo)) {
  305. $result = ['status' => -4001, 'msg' => lang() -> get('user does not exist')];
  306. return $result;
  307. }
  308. // 验证用户状态
  309. if ($row = $this -> repository -> checkAccountStatus($userInfo)) {
  310. return $row;
  311. };
  312. $oldPassword = strip_tags($post['oldPassword']);
  313. $newPassword = strip_tags($post['newPassword']);
  314. // 验证两次密码是否一致
  315. /*if ($r = $this->repository->checkPassword($newPassword, $againPassword)) {
  316. return $r;
  317. }*/
  318. if ($this -> checkPassword($userInfo, $oldPassword)) {
  319. // 密码加密
  320. $pwdData = GenPassword($newPassword);
  321. lm('account_password', 'Commons') -> updatePassword($userInfo -> identity, $pwdData);
  322. $result = ['status' => 1, 'msg' => lang()->get('update successful')];
  323. return $result;
  324. } else {
  325. $result = ['status' => -4009, 'msg' => lang()->get('old password error')];
  326. return $result;
  327. };
  328. }
  329. public function chHeadImg($imgUrl, $imgId) {
  330. $userInfo = $this->getCurrentUser();
  331. $uuid = $userInfo->account_identity;
  332. if (!$uuid) return -4001;
  333. $lm = lm('AccountDetail', 'Api');
  334. $ret = $lm->where('account_identity', $uuid)->update(['img_url' => $imgUrl, 'img_id' => $imgId]);
  335. return $ret;
  336. }
  337. /**
  338. * 获取当前用户信息
  339. *
  340. * @return mixed
  341. * @throws \Exception
  342. */
  343. public function getCurrentUser() {
  344. $token = $_GET['token'];
  345. $token = trim($token, "\"");
  346. $token = trim($token, "'");
  347. $userInfo = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $token)->first();
  348. if (empty($userInfo)) {
  349. return false;
  350. }
  351. $userInfo->identity = $userInfo->account_identity;
  352. return $userInfo;
  353. }
  354. /**
  355. * 用户注册
  356. *
  357. * @return array
  358. */
  359. public function register($post) {
  360. if ($check = $this->repository->checkRegisterData($post)) {
  361. return $check;
  362. };
  363. foreach ($post as $k => $v) {
  364. $post[$k] = strip_tags($v);
  365. }
  366. $post['account'] = strtolower($post['account']);
  367. //$post['open_invitation'] = $this->getRandomString (8);
  368. $account = $this->model->where("account", $post['account'])->first();
  369. if (!empty($account)) {
  370. $result = ['status' => -4004, 'msg' => lang()->get('the user already exists. Please change it')];
  371. return $result;
  372. }
  373. if ($row = $this->repository->checkPassword($post['password'], $post['again_password'])) {
  374. return $row;
  375. }
  376. //添加新代理关系,$post['invita'] //前台提交的邀请码
  377. // if (isset($post['invita']) && !empty($post['invita'])) {
  378. // $data = $this->repository->checkInvitation($post['invita']);
  379. // if ($data < 0) {
  380. // $result = ['status' => -4004, 'msg' => lang()->get('invitation code error')];
  381. // return $result;
  382. // }
  383. // $post['parent_id'] = $data['account_identity']; //父级ID
  384. // $post['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  385. // $post['level'] = intval($data['level']) + 1;
  386. // //父级代理
  387. // $dataAgent['agent_identity'] = $data['account_identity'];
  388. // $dataAgent['agent_name'] = $data['account'];
  389. // //新代理父级
  390. // $newAgent['parent_id'] = $data['account_identity']; //父级ID
  391. // $newAgent['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  392. //
  393. // } else {
  394. //为空默认父级代理为root
  395. // $root = $this->repository->getRoot();
  396. // if ($root < 0) {
  397. // $result = ['status' => -4004, 'msg' => lang()->get('root does not exist')];
  398. // return $result;
  399. // }
  400. // $post['parent_id'] = $root['account_identity']; //父级ID
  401. // $post['parent_path'] = $root['account_identity']; //父级路径
  402. // $post['level'] = intval($root['level']) + 1; //等级
  403. // //父级代理
  404. // $dataAgent['agent_identity'] = $root['account_identity'];
  405. // $dataAgent['agent_name'] = $root['account'];
  406. // //新代理父级
  407. // $newAgent['parent_id'] = $root['account_identity']; //父级ID
  408. // $newAgent['parent_path'] = $root['account_identity']; //父级路径
  409. // }
  410. try {
  411. _beginTransaction();
  412. $accountData = [
  413. 'identity' => UUID(),
  414. 'account' => $post['account'],
  415. ];
  416. $res = $this->model->insert($accountData);
  417. if (!$res) {
  418. _rollBack();
  419. $result = ['status' => -4015, 'msg' => lang()->get('add fail')];
  420. return $result;
  421. }
  422. $res = $this->repository->addPassword($post['password'], $accountData['identity']);
  423. if ($res < 0) {
  424. _rollBack();
  425. $result = ['status' => -4113, 'msg' => lang()->get('passsword fail')];
  426. return $result;
  427. }
  428. $res = $this->repository->addAccountDetailed($post, $accountData['identity']);
  429. if ($res < 0) {
  430. _rollBack();
  431. $result = ['status' => -30101, 'msg' => lang()->get('add user fail')];
  432. return $result;
  433. }
  434. //新代理上下级关系信息
  435. // $dataAgent['account_identity'] = $accountData['identity'];
  436. // $dataAgent['account_name'] = $post['account'];
  437. //
  438. // $res = $this->repository->addNewAgent($dataAgent);
  439. // if ($res < 0) {
  440. // _rollBack();
  441. // $result = ['status' => -30101, 'msg' => lang()->get('add agent parent error')];
  442. // return $result;
  443. // }
  444. // $account = lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->first();
  445. //新代理数据封装
  446. // $newAgent['agent_identity'] = $accountData['identity'];
  447. // $newAgent['agent_user'] = $post['account'];
  448. // $newAgent['invite'] = $account->id;
  449. // $newAgent['register_time'] = date('Y-m-d H:i:s', time());
  450. // //添加新代理到代理详情表
  451. // $res = $this->repository->addNewAgentDetail($newAgent);
  452. // if ($res < 0) {
  453. // _rollBack();
  454. // $result = ['status' => -30101, 'msg' => lang()->get('add new agent error')];
  455. // return $result;
  456. // }
  457. $result = $this->login($post['account'], $post['password']);
  458. if ($post['introduce_user']) {
  459. $res = $this->repository->addParent($post['introduce_user'], $accountData);
  460. if ($res < 0) {
  461. _rollBack();
  462. $result = ['status' => $res, 'msg' => lang()->get('empty introduce_user')];
  463. lm("message", 'commons')->updateMessage('admin', $post['account'], '欢迎', '欢迎欢迎欢迎', 2);
  464. return $result;
  465. }
  466. }
  467. _commit();
  468. return $result;
  469. } catch (\Exception $e) {
  470. $result = ['status' => -4005, 'msg' => lang()->get('registration failed. please re-registration'),'data'=>$e->getMessage()];
  471. return $result;
  472. }
  473. }
  474. /*
  475. * 随机生成
  476. */
  477. public function getRandomString($len, $chars = null) {
  478. if (is_null($chars)) {
  479. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  480. }
  481. mt_srand(10000000 * (double)microtime());
  482. for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
  483. $str .= $chars[mt_rand(0, $lc)];
  484. }
  485. return $str;
  486. }
  487. /*
  488. * 用户开新户
  489. */
  490. public function newhouseholds($post) {
  491. if ($check = $this->repository->checkRegisterData($post)) {
  492. return $check;
  493. };
  494. $post['account'] = strtolower($post['account']);
  495. //$post['open_invitation'] = $this->getRandomString (8);
  496. $account = $this->model->where("account", $post['account'])->first();
  497. if (!empty($account)) {
  498. $result = ['status' => -4004, 'msg' => lang()->get('the user already exists. Please change it')];
  499. return $result;
  500. }
  501. if ($row = $this->repository->checkPassword($post['password'], $post['again_password'])) {
  502. return $row;
  503. }
  504. $parentinfo = lm('account_detailed', 'commons')->where('account_identity', $post['parent_identity'])->first();
  505. if (empty($parentinfo)) {
  506. return -500500;
  507. }
  508. $parentinfo = $parentinfo->toArray();
  509. $parentinfo['invitation'] = explode(',', $parentinfo['invitation']);
  510. $post['parent_id'] = $post['parent_identity'];
  511. if (empty($parentinfo['parent_path'][0])) {
  512. $post['parent_path'] = '2,' . $parentinfo['id'];
  513. } else {
  514. $post['parent_path'] = ($parentinfo['invitation'][0] + 1) . ',' . $parentinfo['id'];;
  515. }
  516. //unset($post['parent_identity']);
  517. try {
  518. _beginTransaction();
  519. $accountData = [
  520. 'identity' => UUID(),
  521. 'account' => $post['account'],
  522. ];
  523. $res = $this->model->insert($accountData);
  524. if (!$res) {
  525. _rollBack();
  526. $result = ['status' => -4015, 'msg' => lang()->get('add fail')];
  527. return $result;
  528. }
  529. $res = $this->repository->addPassword($post['password'], $accountData['identity']);
  530. if ($res < 0) {
  531. _rollBack();
  532. $result = ['status' => -4113, 'msg' => lang()->get('passsword fail')];
  533. return $result;
  534. }
  535. $res = $this->repository->addAccountDetailed($post, $accountData['identity']);
  536. $newaccountdetailed = lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->first();
  537. if (!empty($newaccountdetailed)) {
  538. $newaccountdetailed = $newaccountdetailed->toArray();
  539. lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->update(['open_invitation' => $newaccountdetailed['id']]);
  540. }
  541. if ($res < 0) {
  542. _rollBack();
  543. $result = ['status' => -30101, 'msg' => lang()->get('add user fail')];
  544. return $result;
  545. }
  546. $result = ['status' => $res, 'msg' => lang()->get('empty introduce_user')];
  547. _commit();
  548. return $result;
  549. } catch (\Exception $e) {
  550. $result = ['status' => -4005, 'msg' => lang()->get('registration failed. please re-registration'),'data'=>$e->getMessage()];
  551. return $result;
  552. }
  553. }
  554. /*
  555. * 手机端直接开户 Anton Liu
  556. */
  557. public function directAccountOpening($post) {
  558. if ($check = $this->repository->checkRegisterData($post)) {
  559. return $check;
  560. };
  561. $post['account'] = strtolower($post['account']);
  562. //$post['open_invitation'] = $this->getRandomString (8);
  563. $account = $this->model->where("account", $post['account'])->first();
  564. if (!empty($account)) {
  565. $result = ['status' => -4004, 'msg' => lang()->get('the user already exists. Please change it')];
  566. return $result;
  567. }
  568. if ($row = $this->repository->checkPassword($post['password'], $post['again_password'])) {
  569. return $row;
  570. }
  571. //添加新代理关系,$post['invita'] //前台提交的邀请码
  572. if (isset($post['invita']) && !empty($post['invita'])) {
  573. $data = $this->repository->checkInvitation($post['invita']);
  574. if ($data < 0) {
  575. $result = ['status' => -4004, 'msg' => lang()->get('invitation code error')];
  576. return $result;
  577. }
  578. $post['parent_id'] = $data['account_identity']; //父级ID
  579. $post['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  580. $post['level'] = intval($data['level']) + 1;
  581. //父级代理
  582. $dataAgent['agent_identity'] = $data['account_identity'];
  583. $dataAgent['agent_name'] = $data['account'];
  584. //新代理父级
  585. $newAgent['parent_id'] = $data['account_identity']; //父级ID
  586. $newAgent['parent_path'] = !empty($data['parent_path']) ? $data['parent_path'] . ',' . $data['account_identity'] : $data['account_identity'];
  587. } else {
  588. //为空默认父级代理为root
  589. $root = $this->repository->getRoot();
  590. if ($root < 0) {
  591. $result = ['status' => -4004, 'msg' => lang()->get('root does not exist')];
  592. return $result;
  593. }
  594. $post['parent_id'] = $root['account_identity']; //父级ID
  595. $post['parent_path'] = $root['account_identity']; //父级路径
  596. $post['level'] = intval($root['level']) + 1; //等级
  597. //父级代理
  598. $dataAgent['agent_identity'] = $root['account_identity'];
  599. $dataAgent['agent_name'] = $root['account'];
  600. //新代理父级
  601. $newAgent['parent_id'] = $root['account_identity']; //父级ID
  602. $newAgent['parent_path'] = $root['account_identity']; //父级路径
  603. }
  604. try {
  605. _beginTransaction();
  606. $accountData = [
  607. 'identity' => UUID(),
  608. 'account' => $post['account'],
  609. ];
  610. $res = $this->model->insert($accountData);
  611. if (!$res) {
  612. _rollBack();
  613. $result = ['status' => -4015, 'msg' => lang()->get('add fail')];
  614. return $result;
  615. }
  616. $res = $this->repository->addPassword($post['password'], $accountData['identity']);
  617. if ($res < 0) {
  618. _rollBack();
  619. $result = ['status' => -4113, 'msg' => lang()->get('passsword fail')];
  620. return $result;
  621. }
  622. $res = $this->repository->addAccountDetailed($post, $accountData['identity']);
  623. if ($res < 0) {
  624. _rollBack();
  625. $result = ['status' => -30101, 'msg' => lang()->get('add user fail')];
  626. return $result;
  627. }
  628. //新代理上下级关系信息
  629. $dataAgent['account_identity'] = $accountData['identity'];
  630. $dataAgent['account_name'] = $post['account'];
  631. $res = $this->repository->addNewAgent($dataAgent);
  632. if ($res < 0) {
  633. _rollBack();
  634. $result = ['status' => -30101, 'msg' => lang()->get('add agent parent error')];
  635. return $result;
  636. }
  637. $account = lm('account_detailed', 'Commons')->where('account_identity', $accountData['identity'])->first();
  638. //新代理数据封装
  639. $newAgent['agent_identity'] = $accountData['identity'];
  640. $newAgent['agent_user'] = $post['account'];
  641. $newAgent['invite'] = $account->id;
  642. $newAgent['register_time'] = date('Y-m-d H:i:s', time());
  643. //添加新代理到代理详情表
  644. $res = $this->repository->addNewAgentDetail($newAgent);
  645. if ($res < 0) {
  646. _rollBack();
  647. $result = ['status' => -30101, 'msg' => lang()->get('add new agent error')];
  648. return $result;
  649. }
  650. _commit();
  651. if ($res > 0) {
  652. $result = ['status' => 1, 'msg' => lang()->get('register success')];
  653. return $result;
  654. }
  655. } catch (\Exception $e) {
  656. $result = ['status' => -4005, 'msg' => lang()->get('registration failed. please re-registration'),'data'=>$e->getMessage()];
  657. return $result;
  658. }
  659. }
  660. /**
  661. * 检查登录是否过期
  662. *
  663. * @return array
  664. */
  665. public function checkEffectiveTime() {
  666. //重写此方法 by xmj
  667. define("OVERTIME", 1800);
  668. if (isCheckToken()) {
  669. $account = $this->getCurrentUser();
  670. // $identity = $account->account_identity;
  671. if ($account) {
  672. // $accountToken = lm('account_token', 'Commons')->where('account_identity', $identity)->first();
  673. // $effective_time = $accountToken->effective_time;
  674. // if ($effective_time > time() - OVERTIME) {
  675. // lm('account_token', 'Commons')->where('account_identity', $identity)->update(['effective_time' => strtotime('now')]);
  676. // } else {
  677. // $result = ['status' => -4008, 'msg' => lang()->get('user does not exist')];
  678. // }
  679. $result = $this->repository->checkEffectiveTime($account, OVERTIME);
  680. } else {
  681. $result = ['status' => -4001, 'msg' => lang()->get('user does not exist')];
  682. }
  683. return $result;
  684. }
  685. return ['status' => 1];
  686. }
  687. /**
  688. * 修改用户交易密码
  689. *
  690. * @return array
  691. * @throws \Exception
  692. */
  693. public function updatePayPassword($post) {
  694. $checkPassword = $this->repository->checkOldPayPassword($post['token'], strip_tags($post['oldPassword']));
  695. if ($checkPassword['status'] != 1) {
  696. return $checkPassword;
  697. };
  698. $payPassword = GenPassword(strip_tags($post['pay_password']));
  699. lm('pay_password', 'Commons')->where('account_identity', $checkPassword['account_identity'])->update(['pay_password' => $payPassword['password'], 'encryption' => $payPassword['encryption']]);
  700. $result = ['status' => 1, 'msg' => lang()->get('update successful')];
  701. return $result;
  702. }
  703. /**
  704. * 用户退出
  705. *
  706. * @param $accountIdentity
  707. * @return array
  708. */
  709. public function logout($accountIdentity) {
  710. $result = $this->repository->updateToken($accountIdentity);
  711. return $result;
  712. }
  713. /**
  714. * 销毁超过3天免费试玩玩家
  715. *
  716. * @return mixed
  717. */
  718. public function getAccountId() {
  719. $threebefore = date('Y-m-d H:i:s', time() - 3 * 24 * 3600);
  720. //$account = $this->model->join('account_detailed', 'account.identity', '=', 'account_detailed.account_identity')->where('account.status', 4)->get();
  721. $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();
  722. //echo '<pre>';print_r($account);echo '</pre>';
  723. $this->repository->destroyAccount($account->toArray());
  724. $num = $this->model->select('id')->count();
  725. return $num + 1;
  726. }
  727. /**
  728. * 注册免费试玩用户
  729. *
  730. * @return mixed
  731. */
  732. public function demoAccount()
  733. {
  734. $uname = isset($_POST['account']) ? $_POST['account'] : 'gust'.($this->getAccountId() + 1); //由ikeke修改于2019-01-08主要增加默认密码便于登录
  735. $uname = strtolower($uname);
  736. $num = $this->getAccountId();
  737. $name = 'gust' . ($num + 1);
  738. $account = $this->model->where("account", $uname)->first();
  739. if (!empty($account) || ($uname != $name))
  740. {
  741. $result = ['status' => -4022, 'msg' => '试玩账号有误'];
  742. return $result;
  743. }
  744. $pwd = isset($_POST['password']) ? $_POST['password'] : '123456'; //由ikeke修改于2019-01-08主要增加默认密码便于登录
  745. if (!$pwd)
  746. {
  747. return ['status' => 400, 'msg' => '没有密码'];
  748. }
  749. $result = $this->repository->demoAccount($uname, $pwd);
  750. $userInfo = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $result['data'])->first();
  751. $account = $this->login($uname, $pwd);
  752. isset($result['data']) ? $result['data'] = $account['data'][0]['token'] : '';
  753. $_SESSION['uinfo'] = $userInfo->toArray();
  754. $result['data'] = $_SESSION; //由ikeke修改于2019-01-08主要增加添加登录后的用户信息便于APP端调用
  755. return $result;
  756. }
  757. /**
  758. *
  759. * 判断是否已绑定银行卡
  760. * @return array
  761. * @throws \Exception
  762. */
  763. public function Iscard() {
  764. //获取用户ID并验证
  765. $uidentity = isset($_SESSION['uinfo']['identity']) ? $_SESSION['uinfo']['account_identity'] : '';
  766. if ($uidentity == '') {
  767. return ['data' => '', 'status' => -4020, 'msg' => lang()->get('Failed to obtain user information. Please refresh retry')];
  768. }
  769. $accountBank = lm('account_bank', 'Commons')->where('account_identity', $uidentity)->first();
  770. if ($accountBank) {
  771. $result = ['data' => '', 'status' => 200, 'msg' => ''];
  772. } else {
  773. $result = ['data' => '', 'status' => 1, 'msg' => lang()->get('Please bind the bank card')];
  774. }
  775. return $result;
  776. }
  777. /**
  778. * 获取用户信息
  779. *
  780. * @param $identity
  781. * @return array
  782. */
  783. public function getAccountDetailed($token) {
  784. $result = ['data' => '', '-4001', 'msg' => lang()->get('user does not exist')];
  785. $account = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $token)->first();
  786. if ($account) {
  787. $result = ['data' => $account, 'status' => 1, 'msg' => lang()->get('success')];
  788. }
  789. return $result;
  790. }
  791. /**
  792. * 申请代理用户
  793. *
  794. * @param $data
  795. * @return int
  796. */
  797. public function applyAgent($data) {
  798. // try {
  799. $row = $this->repository->checkAgent($data['agent_user']);
  800. if ($row == 1) {
  801. return -4014;
  802. }
  803. $res = $this->repository->addAgentDetailed($data);
  804. return $res;
  805. // } catch (\Exception $e) {
  806. // return -4015;
  807. // }
  808. }
  809. /**
  810. * 代理人与用户关联
  811. *
  812. * @param $data
  813. * @return int
  814. */
  815. public function agentAccount($data) {
  816. try {
  817. lm('agent_child', 'Commons')->insert($data);
  818. return 1;
  819. } catch (\Exception $e) {
  820. return -4016;
  821. }
  822. }
  823. /**
  824. * 代理用户登录
  825. *
  826. * @param $data
  827. * @return array|int
  828. */
  829. public function agentLogin($data) {
  830. try {
  831. return $this->repository->agentLogin($data);
  832. } catch (\Exception $e) {
  833. return -4018;
  834. }
  835. }
  836. /**
  837. * 代理退出
  838. *
  839. * @param $accountIdentity
  840. * @return array
  841. */
  842. public function agentlogout($agentIdentity) {
  843. $result = $this->repository->updateAgentToken($agentIdentity);
  844. return $result;
  845. }
  846. /**
  847. * 检查代理用户token是否过期
  848. *
  849. * @param $token
  850. * @return array|int
  851. */
  852. public function agentTokenOverdue($token) {
  853. if (!$token) {
  854. return -4017;
  855. }
  856. if (isCheckToken()) {
  857. $agentDetailed = $this->repository->getAgentDetailed($token);
  858. if ($agentDetailed) {
  859. return $this->repository->agentTokenOverdue($agentDetailed);
  860. } else {
  861. return -4017;
  862. }
  863. }
  864. return 1;
  865. }
  866. /**
  867. * 记录登录区域
  868. */
  869. public function areaLog($ip, $accountIdentity) {
  870. try {
  871. $ipget = C()->get('IP');
  872. $location = $ipget->getlocation($ip);
  873. $address = $ipget->trunUtf8($location['country'] . $location['area']);
  874. // $url = "http://ip.taobao.com/service/getIpInfo.php?ip=" . $ip;
  875. // $result = file_get_contents($url);
  876. // $row = json_decode($result, true);
  877. $logFile = lm('logfile', 'Commons')->where('account_identity', $accountIdentity)->orderBy('id', 'desc')->first();
  878. lm('logfile', 'Commons')->where('id', $logFile->id)->update(['area' => $address]);
  879. return 1;
  880. } catch (\Exception $e) {
  881. return -4022;
  882. }
  883. }
  884. /**
  885. * 设置游戏分类
  886. *
  887. * @param $data
  888. * @return array
  889. */
  890. public function getSetOdds($data) {
  891. try {
  892. $result = [];
  893. foreach ($data as $key => $val) {
  894. $result = $this->repository->getResult($key, $val, $result);
  895. }
  896. return ['data' => $result, 'status' => 1];
  897. } catch (\Exception $e) {
  898. return 0;
  899. }
  900. }
  901. /**
  902. * 设置倍率获取需要数据格式
  903. *
  904. * @param $data
  905. * @return array
  906. */
  907. public function getNeedData($data) {
  908. $lang = $data['lang'];
  909. $item = $data['data'];
  910. $result = [];
  911. foreach ($item as $k => $v) {
  912. if ($v['type'] == 0) {
  913. $name = $this->getGameName($k);
  914. $v['lang'] = $lang[$k];
  915. $result[$name][] = $v;
  916. }
  917. }
  918. return $result;
  919. }
  920. /**
  921. * 获取设置倍率的游戏类
  922. *
  923. * @param $k
  924. * @return string
  925. */
  926. public function getGameName($k) {
  927. substr($k, 0, 5) == 'chain' ? $parent = 'chain' : '';
  928. substr($k, 0, 6) == 'dragon' ? $parent = 'dragon' : '';
  929. substr($k, 0, 8) == 'head_one' ? $parent = 'head_one' : '';
  930. substr($k, 0, 8) == 'head_two' ? $parent = 'head_two' : '';
  931. substr($k, 0, 10) == 'head_three' ? $parent = 'head_three' : '';
  932. substr($k, 0, 7) == 'poscode' ? $parent = 'poscode' : '';
  933. substr($k, 0, 5) == 'color' ? $parent = 'color' : '';
  934. substr($k, 0, 3) == 'mix' ? $parent = 'mix' : '';
  935. substr($k, 0, 4) == 'size' ? $parent = 'size' : '';
  936. substr($k, 0, 11) == 'specialCode' ? $parent = 'specialCode' : '';
  937. return $parent;
  938. }
  939. }