UserController.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. namespace bibidd\Controller;
  3. use Bibidd\Controller\CommonBaseController;
  4. use DateTime;
  5. class UserController extends CommonBaseController
  6. {
  7. //封装返回的数据
  8. protected function returnData()
  9. {
  10. header('Access-Control-Allow-Origin: *');
  11. $data['status'] = '0';
  12. $data['code'] = '202';//未携带参数,请求失败
  13. $data['message'] = 'error';
  14. return $data;
  15. }
  16. /**
  17. * 用户注册时输入判断 用户名是否已被占用
  18. * @param $templateid []
  19. * @return $type 消息类型
  20. * //Error! Please enter another username
  21. */
  22. public function create_account_check()
  23. {
  24. header('Access-Control-Allow-Origin: *');
  25. header('Content-Type: text/html; charset=utf-8');
  26. // 指定允许其他域名访问
  27. // 响应类型
  28. header('Access-Control-Allow-Methods:POST');
  29. // 响应头设置
  30. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  31. if (!empty($_POST)) {
  32. $username = $_POST['bibidd_username'];
  33. //查询是否存在 以用来判断是否唯一
  34. $yn_username = M("user_info")->where("name='$username'")->getField("name");
  35. if (!empty($yn_username)) {
  36. $data['status'] = '2';
  37. $data['message'] = 'Error! Please enter another username';//已存在
  38. echo json_encode($data);
  39. } else {
  40. $type = md5($username);
  41. $data['status'] = '1';
  42. $data['message'] = 'true';//通过
  43. echo json_encode($data);
  44. }
  45. } else {
  46. $data=$this->returnData();
  47. echo json_encode($data);
  48. }
  49. }
  50. /**
  51. * 登录认证
  52. *
  53. * 首先判断是否存在该用户
  54. * 再次判断 该用户密码是否正确
  55. * @param bibidd_username [用户名称]
  56. * @param bibidd_password [账号密码]
  57. * @return $type 消息类型
  58. */
  59. public function login_authenticate()
  60. {
  61. header('Access-Control-Allow-Origin: *');
  62. header('Content-Type: text/html; charset=utf-8');
  63. // 指定允许其他域名访问
  64. // 响应类型
  65. header('Access-Control-Allow-Methods:POST');
  66. // 响应头设置
  67. header('Access-Control-Allow-Headers:x-requested-with,content-type');
  68. if (!empty($_POST)) {
  69. $username = $_POST['bibidd_username'];
  70. $password = $_POST['bibidd_password'];
  71. $user_id = M("user_info")->where("name='$username'")->field('id,password,name')->select();
  72. if (!empty($user_id)) {
  73. $user_password = $user_id[0]['password'];
  74. if ($password==$user_password) {
  75. //记录登录
  76. $_POST['time'] = date("Y-m-d D h:i:s A",time());
  77. $_POST['code'] = '登录成功';
  78. M("loser_login")->add($_POST);
  79. //更改上次登录时间
  80. $last_login_list['last_login_time'] = time();
  81. M("user_info")->where("name='$username'")->save($last_login_list);
  82. $data['success'] = $user_id[0]['id'];
  83. $data['nickname'] = $user_id[0]['name'];
  84. echo json_encode($data);
  85. } else {
  86. $_POST['time'] = date("Y-m-d D h:i:s A",time());
  87. $_POST['code'] = '登录失败';
  88. M("loser_login")->add($_POST);
  89. $data['status'] = '0';
  90. $data['message'] = 'Invalid username/password!';
  91. echo json_encode($data);
  92. }
  93. }else{
  94. $data['status'] = '0';
  95. $data['message'] = 'Invalid username/password!';
  96. echo json_encode($data);
  97. }
  98. } else {
  99. echo $this->returnData();
  100. }
  101. }
  102. /**
  103. *获取用户IP地址
  104. * @param int $type
  105. * @return mixed
  106. */
  107. private function get_bb_ip($type = 0)
  108. {
  109. $type = $type ? 1 : 0;
  110. static $ip = NULL;
  111. if ($ip !== NULL) return $ip[$type];
  112. if ($_SERVER['HTTP_X_REAL_IP']) {//nginx 代理模式下,获取客户端真实IP
  113. $ip = $_SERVER['HTTP_X_REAL_IP'];
  114. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {//客户端的ip
  115. $ip = $_SERVER['HTTP_CLIENT_IP'];
  116. } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {//浏览当前页面的用户计算机的网关
  117. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  118. $pos = array_search('unknown', $arr);
  119. if (false !== $pos) unset($arr[$pos]);
  120. $ip = trim($arr[0]);
  121. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  122. $ip = $_SERVER['REMOTE_ADDR'];//浏览当前页面的用户计算机的ip地址
  123. } else {
  124. $ip = $_SERVER['REMOTE_ADDR'];
  125. }
  126. // IP地址合法验证
  127. $long = sprintf("%u", ip2long($ip));
  128. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  129. return $ip[$type];
  130. }
  131. /**
  132. * slgj用户注册
  133. */
  134. public function slUserRegister()
  135. {
  136. header('Content-Type:text/json;charset=utf-8');
  137. header('Access-Control-Allow-Origin:*');
  138. header("Access-Control-Allow-Headers:token,Origin, X-Requested-With, Content-Type, Accept");
  139. header('Access-Control-Allow-Methods:POST');
  140. header('Access-Control-Expose-Headers:*');
  141. if (!empty($_POST)) {
  142. $account = $_POST['account']; //用户账号
  143. $password = $_POST['password'];
  144. $ddapp = $_POST['ddapp'];
  145. $device_type = $_POST['deviceType'];
  146. $invitation_code = $_POST['invitationCode'];
  147. //验证密码,正则表达式:必须包含至少一个字母和一个数字,且长度至少为8位
  148. $pattern = '/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/';
  149. if(!preg_match($pattern,$password))
  150. {
  151. $this->returnDataAndSendMsg("密码必须是数字加字母组合,且长度不能少于8位");
  152. die();
  153. }
  154. $isRegiest = M("user_info")->where("name='$account'")->find();
  155. $userip = $this->get_bb_ip();
  156. if('111.25.20.29'==$userip)
  157. {
  158. die();
  159. }
  160. if (!empty($isRegiest)) {
  161. $data['code'] = '203';
  162. $data['msg'] = 'You have an account. Go log in';
  163. echo json_encode($data);
  164. die;
  165. } else {
  166. $arr['name'] = $account;
  167. $arr['password'] = $this->SHA256Hex($password);
  168. // $arr['nickname'] = '';
  169. $arr['add_time'] = time();
  170. $arr['userip'] = $userip;
  171. $wula_time = date("Y-m-d H:i:s",time());
  172. $arr['add_date'] = $wula_time;
  173. $arr['add_url'] = $ddapp;
  174. $arr['device_type'] = $device_type;
  175. $arr['balance'] = 30;//新增用户添加30余额
  176. $arr['register_invitation_code'] = $invitation_code;//邀请码
  177. $ret = M("user_info")->data($arr)->add();
  178. $this->proxy_reg_count($invitation_code,$ret,$account,$arr['add_time']);
  179. if($ret) {
  180. $find = "9169dd.app";
  181. $position = strpos($ddapp, $find);
  182. if ($position !== false) {
  183. $dd_uid = $ret;
  184. $ddapp_list['uid'] = $dd_uid;
  185. $ddapp_list['date'] = $wula_time ;
  186. M("ddapp_user")->add($ddapp_list);
  187. }
  188. $data['code'] = '200';
  189. $data['msg'] = 'Registered successfully';
  190. echo json_encode($data);
  191. } else {
  192. $data['code'] = '201';
  193. $data['code'] = 'Registration failed. Please try again';
  194. echo json_encode($data);
  195. }
  196. }
  197. } else {
  198. $data['code'] = '202';
  199. $data['msg'] = 'Incorrect parameters'; // 参数不正确
  200. echo json_encode($data);
  201. die;
  202. }
  203. }
  204. /**
  205. * @notes slgj用户登录
  206. */
  207. public function slUserLogin()
  208. {
  209. header('Content-Type:text/json;charset=utf-8');
  210. header('Access-Control-Allow-Origin:*');
  211. header("Access-Control-Allow-Headers:token,Origin, X-Requested-With, Content-Type, Accept");
  212. header('Access-Control-Allow-Methods:POST');
  213. header('Access-Control-Expose-Headers:*');
  214. if (!empty($_POST)) {
  215. if(empty($_POST['deviceType']))
  216. {
  217. $this->returnDataAndSendMsg("非法请求");
  218. die();
  219. }
  220. $account = $_POST['account'];
  221. $password = $_POST['password'];
  222. $isHavePhone = M("user_info")->where("name='$account'")->find();
  223. if (empty($isHavePhone)) {
  224. $data['code'] = '203';
  225. $data['msg'] = 'The account is not detected'; //未检测到该账号
  226. $data['user_id'] = '';
  227. echo json_encode($data);
  228. die;
  229. } else {
  230. $isRegiest = M("user_info")->where("name='$account' and password='$password'")->find();
  231. if (empty($isRegiest))
  232. {
  233. $pwd = $this->SHA256Hex($password);
  234. $isRegiest = M("user_info")->where("name='$account' and password='$pwd'")->find();
  235. }
  236. if (!empty($isRegiest)) {
  237. //防止代理登录,必须保证登录的域名是自己的域名
  238. if (empty($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], '6icb5zio55m76k6w5lqg5oml.app') === false) {
  239. // 满足条件的情况:
  240. $data['code'] = '200';
  241. $data['token']='0e41337ceef549c8ad8d8ca1a9446f1c';
  242. $data['user_id']='2364985';
  243. $data['zhibo']='1';
  244. $data['msg']='Login successful';
  245. $data['tel']='13967099131';
  246. die();
  247. }
  248. $data['code'] = '200';
  249. $data['msg'] = 'Login successful'; //登录成功
  250. $nowtime = time();
  251. M("user_info")->where(array("id" => $isRegiest['id']))->save(array("lastlogintime" => $nowtime));
  252. $data['user_id'] = $isRegiest['id'];
  253. $data['tel'] = $isRegiest['tel_phone'];
  254. $data['zhibo'] = $isRegiest['zhibo_account'];
  255. //下发token
  256. $data['token'] = $this->genToken($isRegiest['id']);
  257. echo json_encode($data);
  258. //更新请求
  259. $uid = $isRegiest['id'];
  260. $jintian_riqi = date("Y-m-d");
  261. M("huoyue_list")->where("uid='$uid' and today='$jintian_riqi'")->delete();
  262. $add_list['today'] = $jintian_riqi;
  263. $add_list['time'] = time();
  264. $add_list['uid'] = $uid;
  265. $add_list['device_type'] = $_POST['deviceType'];
  266. $add_list['url'] = $_SERVER['HTTP_REFERER'];
  267. M("huoyue_list")->add($add_list);
  268. } else {
  269. $data['code'] = '201';
  270. $data['msg'] = 'The account or password is incorrect'; //账号或密码不正确
  271. $data['user_id'] = '';
  272. echo json_encode($data);
  273. die;
  274. }
  275. }
  276. } else {
  277. $data['code'] = '202';
  278. $data['msg'] = 'Incorrect parameters'; // 参数不正确
  279. echo json_encode($data);
  280. die;
  281. }
  282. }
  283. /**
  284. *
  285. * 获取用户金币签到时间
  286. * @return void
  287. */
  288. public function get_user_coins_sign_time()
  289. {
  290. $this->addHeaders(); //添加头部请求
  291. $this->checkUserIsExist($_POST['uid']); //检查用户
  292. $uid=$_POST['uid'];
  293. $last_time = date('Y-m-d 00:00:00');
  294. $user_info = M("user_info")->field("last_sign_time")->where("id='$uid'")->find();
  295. $data['isSignTody'] = 'no';
  296. if(!empty( $user_info["last_sign_time"]) && $user_info["last_sign_time"]==$last_time)
  297. {
  298. $data['isSignTody'] = 'yes';
  299. }
  300. $data['code'] = '200';
  301. $data['message'] = 'ok';
  302. $data['data'] = $user_info["last_sign_time"];
  303. echo json_encode($data);
  304. }
  305. /**
  306. * 用户金币签名
  307. * @return void
  308. */
  309. public function user_coins_sign()
  310. {
  311. // $this->addHeaders(); //添加头部请求
  312. // $this->checkUserIsExist($_POST['uid']); //检查用户
  313. //
  314. // $uid=$_POST['uid'];
  315. // $sign_time = date('Y-m-d 00:00:00');
  316. //
  317. // $user_info = M("user_info")->field("gold_coins_number,last_sign_time")->where("id='$uid'")->find();
  318. // if($user_info['last_sign_time']==$sign_time)
  319. // {
  320. // //重复签到
  321. // $data['code'] = '200';
  322. // $data['message'] = 'ok';
  323. // $data['data'] = 0;
  324. // echo json_encode($data);
  325. // die();
  326. // }
  327. //
  328. //
  329. // $user_info['gold_coins_number']+=1;
  330. // $user_info['last_sign_time']=$sign_time;
  331. // $rs = M("user_info")->where("id='$uid'")->save($user_info);
  332. //
  333. // //添加统计
  334. // $tongji_info = M("user_sign_coins")->where("user_id='$uid'")->find();
  335. // if(empty($tongji_info))
  336. // {
  337. // $tongji_info['user_id']=$uid;
  338. // $tongji_info['lianxu_days']=1;
  339. // $tongji_info['last_sign']=date('Y-m-d H:i:s');
  340. // $rs = M("user_sign_coins")->add($tongji_info);
  341. // }else
  342. // {
  343. // $date = new DateTime($tongji_info['last_sign']);
  344. // $yesterday = new DateTime('-1 day');
  345. //
  346. // $formattedDate = $date->format('Y-m-d');
  347. // $formattedYesterday = $yesterday->format('Y-m-d');
  348. // if ($formattedDate == $formattedYesterday)
  349. // {
  350. // //昨天
  351. // $tongji_info['lianxu_days']+=1;
  352. // }else{
  353. // $tongji_info['lianxu_days']=1;
  354. // }
  355. //
  356. // $tongji_info['last_sign']=date('Y-m-d H:i:s');
  357. // $rs = M("user_sign_coins")->save($tongji_info);
  358. // }
  359. //
  360. // $data['code'] = '200';
  361. // $data['message'] = 'ok';
  362. // $data['data'] = $rs;
  363. // echo json_encode($data);
  364. }
  365. /**
  366. *
  367. * 判断用户注册时间是否在30分钟
  368. * @return void
  369. */
  370. public function is_reg_in_30()
  371. {
  372. $this->addHeaders(); //添加头部请求
  373. $user_info = $this->checkUserIsExist($_POST['uid']); //检查用户
  374. $uid=$user_info['id'];
  375. $reg_time =$user_info['add_date'];
  376. $rs['isIn30'] = false;
  377. //判断用户注册时间是否在30分钟内
  378. if(empty($reg_time))
  379. {
  380. $this->returnDataAndSendMsg("非法用户");
  381. die();
  382. }
  383. $currentTime = time();
  384. $inputTime = strtotime($reg_time);
  385. $difference = abs($currentTime - $inputTime);
  386. if($difference <= 1800)
  387. {
  388. $rs['isIn30'] = true;
  389. $rs['seconds']=1800-$difference;
  390. }
  391. $data['code'] = '200';
  392. $data['message'] = 'ok';
  393. $data['data'] = $rs;
  394. echo json_encode($data);
  395. }
  396. /**
  397. * 获取用户信息
  398. * @return void
  399. */
  400. public function get_user_info()
  401. {
  402. $this->addHeaders(); //添加头部请求
  403. $user_info = $this->checkUserIsExist($_POST['uid']); //检查用户
  404. //$uid=$user_info['id'];
  405. unset($user_info['name']);
  406. unset($user_info['password']);
  407. unset($user_info['ip_address']);
  408. unset($user_info['userip']);
  409. unset($user_info['id']);
  410. unset($user_info['device_type']);
  411. unset($user_info['beizhu']);
  412. unset($user_info['add_url']);
  413. unset($user_info['tel_phone']);
  414. unset($user_info['zhibo_account']);
  415. $data['code'] = '200';
  416. $data['message'] = 'ok';
  417. $data['data'] = $user_info;
  418. echo json_encode($data);
  419. }
  420. /**
  421. * 修改密码
  422. * @return void
  423. */
  424. public function modify_pwd()
  425. {
  426. $this->addHeaders(); //添加头部请求
  427. $user_info = $this->checkUserIsExist($_POST['uid']); //检查用户
  428. $uid = $user_info['id'];
  429. $oldPwd = $_POST['oldPwd'];
  430. $password = $_POST['password'];
  431. if(empty($password) || empty(trim($password))||empty(oldPwd) || empty(trim(oldPwd)))
  432. {
  433. $this->returnDataAndSendMsg("密码或旧密码不能为空");
  434. die();
  435. }
  436. //判断历史密码是否正确
  437. $old_id = M("user_info")->where("id=$uid AND password='$oldPwd'")->getField("id");
  438. if(empty($old_id))
  439. {
  440. $oldPwd=$this->SHA256Hex($oldPwd);
  441. $old_id = M("user_info")->where("id=$uid AND password='$oldPwd'")->getField("id");
  442. if(empty($old_id))
  443. {
  444. $this->returnDataAndSendMsg("旧密码错误");
  445. die();
  446. }
  447. }
  448. $pwd=$this->SHA256Hex($password);
  449. $rs = M("user_info")->where("id=$uid")->setField("password",$pwd);
  450. $data['code'] = '200';
  451. $data['message'] = 'ok';
  452. $data['data'] = $rs;
  453. echo json_encode($data);
  454. }
  455. }