Register.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * User: nickbai
  4. * Date: 2017/10/24 10:46
  5. * Email: 1902822973@qq.com
  6. */
  7. namespace app\index\controller;
  8. use think\Controller;
  9. class Register extends Controller
  10. {
  11. // 注册首页
  12. public function index()
  13. {
  14. $this->assign([
  15. 'version' => config('version')
  16. ]);
  17. return $this->fetch();
  18. }
  19. // 处理注册
  20. public function doRegister()
  21. {
  22. if (request()->isPost()) {
  23. $userName = input("param.user_name/s");
  24. $userEmail = input("param.user_email/s");
  25. $password = input("param.password/s");
  26. $phone = input("param.phone/s");
  27. $emailCode = input("param.code/s");
  28. if (empty($userName)) {
  29. return json(['code' => -1, 'data' => '', 'msg' => '用户名不能为空']);
  30. }
  31. if (empty($userEmail)) {
  32. return json(['code' => -2, 'data' => '', 'msg' => '邮箱不能为空']);
  33. }
  34. if (empty($password)) {
  35. return json(['code' => -3, 'data' => '', 'msg' => '密码不能为空']);
  36. }
  37. if (empty($phone)) {
  38. return json(['code' => -4, 'data' => '', 'msg' => '密码不能为空']);
  39. }
  40. if (empty($emailCode)) {
  41. return json(['code' => -5, 'data' => '', 'msg' => '验证码不能为空']);
  42. }
  43. if ($emailCode != session('code')) {
  44. return json(['code' => -6, 'data' => '', 'msg' => '验证码不正确']);
  45. }
  46. $name = db('accounts')->where('account_name', $userName)->find();
  47. if (!empty($name)) {
  48. return json(['code' => -7, 'data' => '', 'msg' => '用户名已存在']);
  49. }
  50. $email = db('accounts')->where('account_email', $userEmail)->find();
  51. if (!empty($email)) {
  52. return json(['code' => -8, 'data' => '', 'msg' => '邮箱已存在']);
  53. }
  54. // 添加用户信息
  55. $userInfo = [
  56. 'account_name' => $userName,
  57. 'account_email' => $userEmail,
  58. 'password' => md5($password . config('salt')),
  59. 'account_phone' => $phone,
  60. 'status' => 1,
  61. 'add_time' => time(),
  62. 'last_login_time' => time()
  63. ];
  64. $user_id = db('accounts')->insertGetId($userInfo);
  65. // 注册成功 生成token
  66. $module = mt_rand(100000, 999999);
  67. $token = base64_encode($module . '#$@%!^*/' . time() . '/' . $userInfo['id']);
  68. // 更新用户状态
  69. $param = [
  70. 'token' => $token,
  71. 'expire_time' => time(),
  72. ];
  73. db('accounts')->where('id', $user_id)->update($param);
  74. return json(['code' => 1, 'data' => url('user/index'), 'msg' => '注册成功']);
  75. }
  76. }
  77. //自动注册
  78. public function autoReg()
  79. {
  80. $appid = input("post.appid/s");
  81. $appuid = input("post.appuid/s");
  82. $token = trim(input("post.token/s"));
  83. $nowuid = (empty($appuid) || empty($appuid)) ? uniqid('anon_') : $appid . '_' . $appuid;
  84. $avatar = "http://wx2.sinaimg.cn/mw690/5db11ff4gy1flxmew7edlj203d03wt8n.jpg"; //默认头像
  85. if ($token) {
  86. $old = db('accounts')->where(['token' => $token])->find();
  87. if ($old) {
  88. if ($old['status'] != 1) {
  89. return json(['code' => 0, 'data' => [], 'msg' => '禁止登陆']);
  90. }
  91. return json(['code' => 1, 'data' => ['id' => $old['id'], 'name' => $old['account_name'], 'token' => $old['token'], 'avatar' => $avatar], 'msg' => '注册成功']);
  92. }
  93. }
  94. $now = time();
  95. $token = md5(uniqid() . rand(10000, 50000));
  96. $newdata = [
  97. 'account_name' => $nowuid,
  98. 'password' => md5(time() . rand(1, 5000)),
  99. 'status' => 1,
  100. 'add_time' => $now,
  101. 'last_login_time' => $now,
  102. 'token' => $token,
  103. 'expire_time' => $now,
  104. ];
  105. $retid = Db::name('accounts')->insertGetId($newdata);
  106. if ($retid) {
  107. return json(['code' => 1, 'data' => ['id' => $retid, 'name' => $nowuid, 'token' => $token, 'avatar' => $avatar], 'msg' => '注册成功']);
  108. } else {
  109. return json(['code' => 0, 'data' => [], 'msg' => '注册用户失败']);
  110. }
  111. }
  112. }