AccountManager.php 41 KB

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