Reg.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. public 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. $check = Dever::config('base', 'project')->passportCheckEmail;
  154. if ($check && !strstr($email, $check[0])) {
  155. Dever::alert($check[1]);
  156. }
  157. return $email;
  158. }
  159. /**
  160. * 验证邮箱是否注册
  161. * @return mixed
  162. */
  163. public function checkEmailExists($state = false, $login = false, $table = 'passport/user')
  164. {
  165. $param['option_email'] = $this->checkEmail();
  166. if ($state && $state == 1) {
  167. } else {
  168. $this->checkCode($param['option_email']);
  169. }
  170. if ($login != -1) {
  171. $user = Dever::db($table)->one($param);
  172. if ($login == false && $user) {
  173. Dever::alert('该邮箱已经注册');
  174. } elseif ($login == true && !$user) {
  175. Dever::alert('该邮箱未注册');
  176. }
  177. }
  178. return $param['option_email'];
  179. }
  180. public function action()
  181. {
  182. $account = Dever::config('base', 'project')->account;
  183. $code = Dever::config('base', 'project')->nocode ? Dever::config('base', 'project')->nocode : state;
  184. $baccount = ucfirst($account);
  185. $method = 'check' . $baccount . 'Exists';
  186. $param[$account] = $this->$method($code);
  187. if ($param[$account]) {
  188. $param['username'] = Dever::input('username');
  189. $param['password'] = Dever::input('password');
  190. $param['sex'] = Dever::input('sex');
  191. $param['wechat'] = Dever::input('wechat');
  192. $param['profession'] = Dever::input('profession');
  193. $param['truename'] = Dever::input('truename');
  194. $param['area_id'] = Dever::input('area_id');
  195. $param['area'] = Dever::input('area');
  196. $param['source_type'] = Dever::input('source_type', 'h5');//即将废弃
  197. $param['system_source'] = Dever::input('system_source', 1);
  198. $cpassword = Dever::input('cpassword');
  199. if (!$param['username']) {
  200. Dever::alert('昵称不能为空');
  201. }
  202. if (!$param['password']) {
  203. Dever::alert('密码不能为空');
  204. }
  205. if ($cpassword != $param['password']) {
  206. Dever::alert('确认密码不正确');
  207. }
  208. if ($param['sex']) {
  209. $config_sex = Dever::config('base', 'project')->sex;
  210. if (isset($config_sex[$param['sex']])) {
  211. $param['sex'] = $config_sex[$param['sex']];
  212. } else {
  213. if ($sex == '男') {
  214. $param['sex'] = 1;
  215. } elseif ($sex == '女') {
  216. $param['sex'] = 2;
  217. } elseif ($sex == '未知') {
  218. $param['sex'] = 3;
  219. }
  220. }
  221. }
  222. if ($account == 'mobile' && $param['username'] == $param[$account]) {
  223. $param['username'] = Dever::hide($param['username']);
  224. }
  225. $id = Dever::db('passport/user')->insert($param);
  226. if ($id > 0) {
  227. $this->save($id);
  228. $this->refer();
  229. } else {
  230. Dever::alert('注册失败');
  231. }
  232. }
  233. }
  234. public function forget_email()
  235. {
  236. #发送邮件
  237. $email = Dever::input('email');
  238. if ($email) {
  239. if (!preg_match(Dever::rule('email'), $email)) {
  240. Dever::alert('请输入正确的邮箱');
  241. }
  242. $method = 'checkEmailExists';
  243. $this->$method(1, 1);
  244. $code = $this->code(false, false);
  245. $email = base64_encode($email);
  246. //return Dever::load('passport/lib/email')->forget($email, $code);
  247. Dever::daemon('lib/email.forget?email=' . $email . '&code=' . $code, 'passport');
  248. $refer = $this->refer(true);
  249. return $refer . '||邮件已经发送成功!请到您的邮箱里查看。';
  250. } else {
  251. Dever::alert('请输入邮箱');
  252. }
  253. }
  254. public function forget()
  255. {
  256. $signature = Dever::input('signature');
  257. if (!$signature) {
  258. Dever::alert('您的验证信息已失效,请重新发送验证邮件');
  259. }
  260. $signature = Dever::decode($signature);
  261. $temp = explode('|||', $signature);
  262. $code = $temp[1];
  263. $state = $this->code($code);
  264. if (!$state) {
  265. Dever::alert('您的验证信息已失效,请重新发送验证邮件');
  266. }
  267. $account = Dever::config('base', 'project')->account;
  268. $code = Dever::config('base', 'project')->nocode ? Dever::config('base', 'project')->nocode : state;
  269. $baccount = ucfirst($account);
  270. $method = 'check' . $baccount . 'Exists';
  271. $param['option_' . $account] = $this->$method($code, true);
  272. $user = Dever::load('passport/user-one', $param);
  273. if (!$user) {
  274. Dever::alert('该账号还未注册,请先注册');
  275. } else {
  276. $param['set_password'] = Dever::input('password');
  277. $cpassword = Dever::input('cpassword');
  278. if (sha1($param['set_password']) == $user['password']) {
  279. Dever::alert('您的新密码和旧密码相同');
  280. }
  281. if (!$param['set_password']) {
  282. Dever::alert('新密码不能为空');
  283. }
  284. if ($param['set_password'] != $cpassword) {
  285. Dever::alert('确认密码不正确');
  286. }
  287. $id = $param['where_id'] = $user['id'];
  288. Dever::load('passport/user-update', $param);
  289. if ($id > 0) {
  290. $refer = $this->refer(true);
  291. return $refer . '||您已经成功修改密码';
  292. } else {
  293. Dever::alert('修改失败');
  294. }
  295. }
  296. }
  297. public function url()
  298. {
  299. return Dever::url('reg?' . $this->createRefer(), 'main');
  300. }
  301. }