AccountManager.php 41 KB

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