Reg.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace Passport\Src;
  3. use Dever;
  4. use Passport\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, -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 getMCodeReg()
  41. {
  42. $mobile = $this->checkMobileExists(1);
  43. $msg = $this->getMcode_action($mobile);
  44. return $msg;
  45. }
  46. /**
  47. * 获取手机随机验证码:已经登录状态
  48. * @return mixed
  49. */
  50. public function getMCodeLogin()
  51. {
  52. $mobile = $this->checkMobileExists(1, true);
  53. $type = Dever::input('type', 'code');
  54. if ($type == 'code') {
  55. $msg = $this->getMcode_action($mobile);
  56. } else {
  57. $content = Dever::input('content');
  58. $msg = $this->send($mobile, $content);
  59. }
  60. return $msg;
  61. }
  62. /**
  63. * 检测手机号是否注册
  64. * @return mixed
  65. */
  66. public function checkMobileExist()
  67. {
  68. $param['option_mobile'] = $this->checkMobile();
  69. $user = Dever::db('passport/user')->one($param);
  70. if (!$user) {
  71. Dever::alert('该手机号未注册');
  72. }
  73. return 'ok';
  74. }
  75. /**
  76. * 检测图形验证码
  77. * @return mixed
  78. */
  79. private function checkCode()
  80. {
  81. $code = Dever::input('code');
  82. $code = $this->code($code);
  83. if (!$code) {
  84. Dever::alert('验证码输入错误');
  85. }
  86. }
  87. /**
  88. * 检测手机验证码
  89. * @return mixed
  90. */
  91. private function checkMCode($mobile)
  92. {
  93. $code = Dever::input('mcode');
  94. if (!$code) {
  95. Dever::alert('请输入验证码');
  96. }
  97. $code = $this->mcode($mobile, $code);
  98. if (!$code) {
  99. Dever::alert('验证码输入错误');
  100. }
  101. }
  102. /**
  103. * 验证手机号
  104. * @return mixed
  105. */
  106. private function checkMobile()
  107. {
  108. $rule = Dever::rule('mobile');
  109. $mobile = Dever::input('mobile');
  110. if (!$mobile) {
  111. Dever::alert('手机号不能为空');
  112. }
  113. if (!preg_match($rule, $mobile)) {
  114. Dever::alert('请填写正确的手机号');
  115. }
  116. return $mobile;
  117. }
  118. /**
  119. * 验证手机号是否注册
  120. * @return mixed
  121. */
  122. public function checkMobileExists($state = false, $login = false, $table = 'passport/user')
  123. {
  124. $param['option_mobile'] = $this->checkMobile();
  125. if ($state && $state == 1) {
  126. } else {
  127. $this->checkMCode($param['option_mobile']);
  128. }
  129. if ($login != -1) {
  130. $user = Dever::db($table)->one($param);
  131. if ($login == false && $user) {
  132. Dever::alert('该手机号已经注册');
  133. } elseif ($login == true && !$user) {
  134. Dever::alert('该手机号未注册');
  135. }
  136. }
  137. return $param['option_mobile'];
  138. }
  139. /**
  140. * 验证邮箱
  141. * @return mixed
  142. */
  143. private function checkEmail()
  144. {
  145. $rule = Dever::rule('email');
  146. $email = Dever::input('email');
  147. if (!$email) {
  148. Dever::alert('邮箱不能为空');
  149. }
  150. if (!preg_match($rule, $email)) {
  151. Dever::alert('请填写正确的邮箱');
  152. }
  153. return $email;
  154. }
  155. /**
  156. * 验证邮箱是否注册
  157. * @return mixed
  158. */
  159. public function checkEmailExists($state = false, $login = false, $table = 'passport/user')
  160. {
  161. $param['option_email'] = $this->checkEmail();
  162. if ($state && $state == 1) {
  163. } else {
  164. $this->checkCode($param['option_email']);
  165. }
  166. if ($login != -1) {
  167. $user = Dever::db($table)->one($param);
  168. if ($login == false && $user) {
  169. Dever::alert('该邮箱已经注册');
  170. } elseif ($login == true && !$user) {
  171. Dever::alert('该邮箱未注册');
  172. }
  173. }
  174. return $param['option_email'];
  175. }
  176. public function action()
  177. {
  178. $account = Dever::config('base', 'project')->account;
  179. $baccount = ucfirst($account);
  180. $method = 'check' . $baccount . 'Exists';
  181. $param['option_' . $account] = $this->$method();
  182. if ($param['option_' . $account]) {
  183. $param['add_' . $account] = $param['option_' . $account];
  184. $param['add_username'] = Dever::input('username');
  185. $param['add_password'] = Dever::input('password');
  186. $param['add_sex'] = Dever::input('sex');
  187. $cpassword = Dever::input('cpassword');
  188. if (!$param['add_username']) {
  189. Dever::alert('昵称不能为空');
  190. }
  191. if (!$param['add_password']) {
  192. Dever::alert('密码不能为空');
  193. }
  194. if ($cpassword != $param['add_password']) {
  195. Dever::alert('确认密码不正确');
  196. }
  197. if ($param['add_sex']) {
  198. $config_sex = Dever::config('base', 'project')->sex;
  199. if (isset($config_sex[$param['add_sex']])) {
  200. $param['add_sex'] = $config_sex[$param['add_sex']];
  201. } else {
  202. if ($sex == '男') {
  203. $param['add_sex'] = 1;
  204. } elseif ($sex == '女') {
  205. $param['add_sex'] = 2;
  206. } elseif ($sex == '未知') {
  207. $param['add_sex'] = 3;
  208. }
  209. }
  210. }
  211. if ($param['add_username'] == $param['add_' . $account]) {
  212. $param['add_username'] = substr_replace($param['add_username'],'****',3,4);
  213. }
  214. $id = Dever::load('passport/user-insert', $param);
  215. if ($id > 0) {
  216. $this->save($id);
  217. $this->refer();
  218. } else {
  219. Dever::alert('注册失败');
  220. }
  221. }
  222. }
  223. public function forget()
  224. {
  225. $account = Dever::config('base', 'project')->account;
  226. $baccount = ucfirst($account);
  227. $method = 'check' . $baccount . 'Exists';
  228. $param['option_' . $account] = $this->$method(0, true);
  229. $user = Dever::load('passport/user-one', $param);
  230. if (!$user) {
  231. Dever::alert('该账号还未注册,请先注册');
  232. } else {
  233. $param['set_password'] = Dever::input('password');
  234. $cpassword = Dever::input('cpassword');
  235. if (md5($param['set_password']) == $user['password']) {
  236. Dever::alert('您的新密码和旧密码相同');
  237. }
  238. if (!$param['set_password']) {
  239. Dever::alert('新密码不能为空');
  240. }
  241. if ($param['set_password'] != $cpassword) {
  242. Dever::alert('确认密码不正确');
  243. }
  244. $id = $param['where_id'] = $user['id'];
  245. Dever::load('passport/user-update', $param);
  246. if ($id > 0) {
  247. $this->refer();
  248. } else {
  249. Dever::alert('修改失败');
  250. }
  251. }
  252. }
  253. public function url()
  254. {
  255. return Dever::url('reg?' . $this->createRefer(), 'main');
  256. }
  257. }