register.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace WY\app\controller;
  3. use WY\app\libs\Controller;
  4. if (!defined('WY_ROOT')) {
  5. exit;
  6. }
  7. class register extends Controller
  8. {
  9. public function index()
  10. {
  11. $data = array('title' => '用户注册', 'superid' => $this->req->get('id'));
  12. $this->put('register.php', $data);
  13. }
  14. public function save()
  15. {
  16. $email = $this->req->post('email');
  17. $chkcode = $this->req->post('chkcode');
  18. $id = $this->req->post('id');
  19. if ($email == '' || $chkcode == '') {
  20. echo json_encode(array('status' => 1, 'msg' => '选项填写不完整', 'url' => '/register'));
  21. exit;
  22. }
  23. if (strtolower($chkcode) != $this->session->get('chkcode')) {
  24. echo json_encode(array('status' => 1, 'msg' => '验证码错误', 'url' => '/register'));
  25. exit;
  26. }
  27. if (!$this->res->isMail($email)) {
  28. echo json_encode(array('status' => 1, 'msg' => '邮箱格式错误', 'url' => '/register'));
  29. exit;
  30. }
  31. $userinfo = $this->model()->select('email')->from('userinfo')->where(array('fields' => 'email=?', 'values' => array($email)))->fetchRow();
  32. if ($userinfo) {
  33. echo json_encode(array('status' => 1, 'msg' => '邮箱' . $email . '已被注册', 'url' => '/register'));
  34. exit;
  35. }
  36. $superid = 0;
  37. if ($id && preg_match('/\\d/', $id)) {
  38. if ($this->model()->select()->from('users')->where(array('fields' => 'id=? and is_agent=?', 'values' => array($id, 1)))->count()) {
  39. $superid = $id;
  40. }
  41. }
  42. $data = array('email' => $email, 'token' => sha1($this->res->getRandomString(40)), 'superid' => $superid);
  43. if ($this->model()->from('register')->insertData($data)->insert()) {
  44. if ($mailtpl = $this->model()->select()->from('mailtpl')->where(array('fields' => 'is_state=? and cname=?', 'values' => array(0, '注册确认')))->fetchRow()) {
  45. $orginData = array('sitename' => $this->config['sitename'], 'url' => 'http://' . $this->config['siteurl'] . '/register/complete?email=' . $email . '&token=' . $data['token']);
  46. $newData = $this->res->replaceMailTpl($mailtpl, $orginData);
  47. $subject = array('title' => $newData['title'], 'email' => $email, 'content' => $newData['content']);
  48. $result = $this->res->sendMail($subject, $this->config);
  49. echo json_encode(array('status' => 1, 'msg' => '注册邮件已发送,请登录您的邮箱查看','res'=>$result, 'url' => '/login'));
  50. exit;
  51. }
  52. }
  53. echo json_encode(array('status' => 1, 'msg' => '注册失败', 'url' => '/register'));
  54. exit;
  55. }
  56. public function complete()
  57. {
  58. $email = $this->req->get('email');
  59. $token = $this->req->get('token');
  60. if ($email == '' || $token == '') {
  61. $data = array('msg' => '来源错误,请返回重试');
  62. $this->put('woodyapp.php', $data);
  63. exit;
  64. }
  65. $where = array('fields' => 'email=? and token=?', 'values' => array($email, $token));
  66. if ($this->model()->select()->from('register')->where($where)->orderby('id desc')->fetchRow()) {
  67. $userinfo = $this->model()->select('email')->from('userinfo')->where(array('fields' => 'email=?', 'values' => array($email)))->fetchRow();
  68. if ($userinfo) {
  69. $data = array('msg' => '邮箱' . $email . '已被注册');
  70. $this->put('woodyapp.php', $data);
  71. exit;
  72. }
  73. $data = array('email' => $email, 'token' => $token);
  74. $this->put('regcomplete.php', $data);
  75. exit;
  76. }
  77. $data = array('msg' => '来源错误,请返回重试');
  78. $this->put('woodyapp.php', $data);
  79. exit;
  80. }
  81. public function savetwo()
  82. {
  83. $data = isset($_POST) ? $_POST : false;
  84. if (!$data) {
  85. echo json_encode(array('status' => 1, 'msg' => '选项填写不完整'));
  86. exit;
  87. }
  88. foreach ($data as $key => $val) {
  89. ${$key} = $val;
  90. }
  91. if ($email == '' || $token == '' || $chkcode == '' || $username == '' || $userpass == '' || $cirmpwd == '' || $phone == '' || $qq == '' || $sitename == '' || $siteurl == '') {
  92. echo json_encode(array('status' => 1, 'msg' => '选项填写不完整'));
  93. exit;
  94. }
  95. if ($chkcode != $this->session->get('chkcode')) {
  96. echo json_encode(array('status' => 1, 'msg' => '验证码错误'));
  97. exit;
  98. }
  99. if (!preg_match('/[0-9a-z]{5,20}/', $username)) {
  100. echo json_encode(array('status' => 1, 'msg' => '用户名格式错误,请使用5-20位小写字母或数字组合'));
  101. exit;
  102. }
  103. if ($this->model()->select()->from('users')->where(array('fields' => 'username=?', 'values' => array($username)))->count()) {
  104. echo json_encode(array('status' => 1, 'msg' => '用户名已存在'));
  105. exit;
  106. }
  107. if ($userpass != $cirmpwd) {
  108. echo json_encode(array('status' => 1, 'msg' => '两次填写的密码不匹配'));
  109. exit;
  110. }
  111. if (!preg_match('/\\d{11}/', $phone)) {
  112. echo json_encode(array('status' => 1, 'msg' => '手机号码格式错误'));
  113. exit;
  114. }
  115. if (!preg_match('/\\d{5,12}/', $qq)) {
  116. echo json_encode(array('status' => 1, 'msg' => 'QQ号码格式错误'));
  117. exit;
  118. }
  119. $where = array('fields' => 'email=? and token=?', 'values' => array($email, $token));
  120. if (!($regdata = $this->model()->select()->from('register')->where($where)->orderby('id desc')->fetchRow())) {
  121. echo json_encode(array('status' => 1, 'msg' => '来源错误'));
  122. exit;
  123. }
  124. if ($this->model()->select('email')->from('userinfo')->where(array('fields' => 'email=?', 'values' => array($email)))->fetchRow()) {
  125. echo json_encode(array('status' => 1, 'msg' => '邮箱已被注册'));
  126. exit;
  127. }
  128. $data = array('username' => $username, 'userpass' => sha1($userpass), 'addtime' => time(), 'token' => sha1($this->res->getRandomString(40)), 'apikey' => sha1($this->res->getRandomString(40)), 'is_verify_email' => 1, 'superid' => $regdata['superid'], 'ship_type' => 1);
  129. if (!($userid = $this->model()->from('users')->insertData($data)->insert())) {
  130. echo json_encode(array('status' => 1, 'msg' => '注册失败-err01'));
  131. exit;
  132. }
  133. if (preg_match('/http[s]?:\\/\\/(.*)/', $siteurl, $match)) {
  134. $siteurl = $match[1];
  135. }
  136. $userinfo = array('userid' => $userid, 'sitename' => $sitename, 'siteurl' => $siteurl, 'email' => $email, 'phone' => $phone, 'qq' => $qq, 'addtime' => time(), 'lastime' => time());
  137. if (!$this->model()->from('userinfo')->insertData($userinfo)->insert()) {
  138. $this->model()->from('users')->where(array('fields' => 'id=?', 'values' => array($userid)))->delete();
  139. echo json_encode(array('status' => 1, 'msg' => '注册失败-err02'));
  140. exit;
  141. }
  142. $this->model()->from('register')->where(array('fields' => 'email=?', 'values' => array($email)))->delete();
  143. echo json_encode(array('status' => 1, 'msg' => '注册成功,请联系客服开通', 'url' => '/login'));
  144. exit;
  145. }
  146. }
  147. ?>