AccountManager.php 41 KB

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