Reg.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace Passport\Src;
  3. use Dever;
  4. use Passport\Src\Lib\Base;
  5. # 所有手机发送短信操作,后续要独立成一个组件
  6. class Reg extends Base
  7. {
  8. /**
  9. * 获取图形验证码
  10. * @return mixed
  11. */
  12. public function getCode()
  13. {
  14. return $this->code();
  15. }
  16. /**
  17. * 获取手机随机验证码:未登录
  18. * @return mixed
  19. */
  20. public function getMCode()
  21. {
  22. $mobile = $this->checkMobileExists(1);
  23. $msg = $this->getMcode_action($mobile);
  24. return $msg;
  25. }
  26. private function getMcode_action($mobile)
  27. {
  28. $code = $this->mcode($mobile);
  29. $msg = '验证码已发送至您的手机,请注意查收,十分钟之内有效';
  30. $debug = Dever::config('base', 'project')->mobileCode['debug'];
  31. if ($debug) {
  32. $msg .= ':' . $code;
  33. }
  34. return $msg;
  35. }
  36. /**
  37. * 获取手机随机验证码:已经登录状态
  38. * @return mixed
  39. */
  40. public function getMCodeLogin()
  41. {
  42. $mobile = $this->checkMobileExists(1, true);
  43. $type = Dever::input('type', 'code');
  44. if ($type == 'code') {
  45. $msg = $this->getMcode_action($mobile);
  46. } else {
  47. $content = Dever::input('content');
  48. $msg = $this->send($mobile, $content);
  49. }
  50. return $msg;
  51. }
  52. /**
  53. * 检测图形验证码
  54. * @return mixed
  55. */
  56. private function checkCode()
  57. {
  58. $code = Dever::input('code');
  59. $code = $this->code($code);
  60. if (!$code) {
  61. Dever::alert('验证码输入错误');
  62. }
  63. }
  64. /**
  65. * 检测手机验证码
  66. * @return mixed
  67. */
  68. private function checkMCode($mobile)
  69. {
  70. $code = Dever::input('mcode');
  71. $code = $this->mcode($mobile, $code);
  72. if (!$code) {
  73. Dever::alert('验证码输入错误');
  74. }
  75. }
  76. /**
  77. * 验证手机号
  78. * @return mixed
  79. */
  80. private function checkMobile()
  81. {
  82. $rule = Dever::rule('mobile');
  83. $mobile = Dever::input('mobile');
  84. if (!$mobile) {
  85. Dever::alert('手机号不能为空');
  86. }
  87. if (!preg_match($rule, $mobile)) {
  88. Dever::alert('请填写正确的手机号');
  89. }
  90. return $mobile;
  91. }
  92. /**
  93. * 验证手机号是否注册
  94. * @return mixed
  95. */
  96. public function checkMobileExists($state = false, $login = false)
  97. {
  98. $param['option_mobile'] = $this->checkMobile();
  99. if ($state && $state == 1) {
  100. } else {
  101. $this->checkMCode($param['option_mobile']);
  102. }
  103. $user = Dever::load('passport/user-one', $param);
  104. if ($login == false && $user) {
  105. Dever::alert('该手机号已经注册');
  106. } elseif ($login == true && !$user) {
  107. Dever::alert('该手机号未注册');
  108. }
  109. return $param['option_mobile'];
  110. }
  111. /**
  112. * 验证邮箱
  113. * @return mixed
  114. */
  115. private function checkEmail()
  116. {
  117. $rule = Dever::rule('email');
  118. $email = Dever::input('email');
  119. if (!$email) {
  120. Dever::alert('邮箱不能为空');
  121. }
  122. if (!preg_match($rule, $email)) {
  123. Dever::alert('请填写正确的邮箱');
  124. }
  125. return $email;
  126. }
  127. /**
  128. * 验证邮箱是否注册
  129. * @return mixed
  130. */
  131. public function checkEmailExists($state = false, $login = false)
  132. {
  133. $param['option_email'] = $this->checkEmail();
  134. if ($state && $state == 1) {
  135. } else {
  136. $this->checkCode($param['option_email']);
  137. }
  138. $user = Dever::load('passport/user-one', $param);
  139. if ($login == false && $user) {
  140. Dever::alert('该邮箱已经注册');
  141. } elseif ($login == true && !$user) {
  142. Dever::alert('该邮箱未注册');
  143. }
  144. return $param['option_email'];
  145. }
  146. public function action()
  147. {
  148. $account = Dever::config('base', 'project')->account;
  149. $baccount = ucfirst($account);
  150. $method = 'check' . $baccount . 'Exists';
  151. $param['option_' . $account] = $this->$method();
  152. if ($param['option_' . $account]) {
  153. $param['add_' . $account] = $param['option_' . $account];
  154. $param['add_username'] = Dever::input('username');
  155. $param['add_password'] = Dever::input('password');
  156. $param['add_sex'] = Dever::input('sex');
  157. $cpassword = Dever::input('cpassword');
  158. if (!$param['add_username']) {
  159. Dever::alert('昵称不能为空');
  160. }
  161. if (!$param['add_password']) {
  162. Dever::alert('密码不能为空');
  163. }
  164. if ($cpassword != $param['add_password']) {
  165. Dever::alert('确认密码不正确');
  166. }
  167. if ($param['add_sex']) {
  168. $config_sex = Dever::config('base', 'project')->sex;
  169. if (isset($config_sex[$param['add_sex']])) {
  170. $param['add_sex'] = $config_sex[$param['add_sex']];
  171. } else {
  172. if ($sex == '男') {
  173. $param['add_sex'] = 1;
  174. } elseif ($sex == '女') {
  175. $param['add_sex'] = 2;
  176. } elseif ($sex == '未知') {
  177. $param['add_sex'] = 3;
  178. }
  179. }
  180. }
  181. if ($param['add_username'] == $param['add_' . $account]) {
  182. $param['add_username'] = substr_replace($param['add_username'],'****',3,4);
  183. }
  184. $id = Dever::load('passport/user-insert', $param);
  185. if ($id > 0) {
  186. $this->save($id);
  187. $this->refer();
  188. } else {
  189. Dever::alert('注册失败');
  190. }
  191. }
  192. }
  193. public function forget()
  194. {
  195. $account = Dever::config('base', 'project')->account;
  196. $baccount = ucfirst($account);
  197. $method = 'check' . $baccount . 'Exists';
  198. $param['option_' . $account] = $this->$method(0, true);
  199. $user = Dever::load('passport/user-one', $param);
  200. if (!$user) {
  201. Dever::alert('该账号还未注册,请先注册');
  202. } else {
  203. $param['set_password'] = Dever::input('password');
  204. $cpassword = Dever::input('cpassword');
  205. if (md5($param['set_password']) == $user['password']) {
  206. Dever::alert('您的新密码和旧密码相同');
  207. }
  208. if (!$param['set_password']) {
  209. Dever::alert('新密码不能为空');
  210. }
  211. if ($param['set_password'] != $cpassword) {
  212. Dever::alert('确认密码不正确');
  213. }
  214. $id = $param['where_id'] = $user['id'];
  215. Dever::load('passport/user-update', $param);
  216. if ($id > 0) {
  217. $this->refer();
  218. } else {
  219. Dever::alert('修改失败');
  220. }
  221. }
  222. }
  223. public function url()
  224. {
  225. return Dever::url('reg?' . $this->createRefer(), 'main');
  226. }
  227. }