Reg.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. $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['option_' . $account] = $this->$method($code);
  187. if ($param['option_' . $account]) {
  188. $param['add_' . $account] = $param['option_' . $account];
  189. $param['add_username'] = Dever::input('username');
  190. $param['add_password'] = Dever::input('password');
  191. $sex = Dever::input('sex');
  192. $wechat = Dever::input('wechat');
  193. $profession = Dever::input('profession');
  194. $truename = Dever::input('truename');
  195. $area_id = Dever::input('area_id');
  196. $area = Dever::input('area');
  197. if ($sex) {
  198. $param['add_sex'] = Dever::input('sex');
  199. }
  200. if ($wechat) {
  201. $param['add_wechat'] = Dever::input('wechat');
  202. }
  203. if ($profession) {
  204. $param['add_profession'] = Dever::input('profession');
  205. }
  206. if ($truename) {
  207. $param['add_truename'] = Dever::input('truename');
  208. }
  209. if ($area) {
  210. $param['add_area'] = Dever::input('area');
  211. }
  212. if ($area_id) {
  213. $param['add_area_id'] = Dever::input('area_id');
  214. }
  215. $cpassword = Dever::input('cpassword');
  216. if (!$param['add_username']) {
  217. Dever::alert('昵称不能为空');
  218. }
  219. if (!$param['add_password']) {
  220. Dever::alert('密码不能为空');
  221. }
  222. if ($cpassword != $param['add_password']) {
  223. Dever::alert('确认密码不正确');
  224. }
  225. if ($param['add_sex']) {
  226. $config_sex = Dever::config('base', 'project')->sex;
  227. if (isset($config_sex[$param['add_sex']])) {
  228. $param['add_sex'] = $config_sex[$param['add_sex']];
  229. } else {
  230. if ($sex == '男') {
  231. $param['add_sex'] = 1;
  232. } elseif ($sex == '女') {
  233. $param['add_sex'] = 2;
  234. } elseif ($sex == '未知') {
  235. $param['add_sex'] = 3;
  236. }
  237. }
  238. }
  239. if ($param['add_username'] == $param['add_' . $account]) {
  240. $param['add_username'] = substr_replace($param['add_username'],'****',3,4);
  241. }
  242. $id = Dever::load('passport/user-insert', $param);
  243. if ($id > 0) {
  244. $this->save($id);
  245. $this->refer();
  246. } else {
  247. Dever::alert('注册失败');
  248. }
  249. }
  250. }
  251. public function forget_email()
  252. {
  253. #发送邮件
  254. $email = Dever::input('email');
  255. if ($email) {
  256. if (!preg_match(Dever::rule('email'), $email)) {
  257. Dever::alert('请输入正确的邮箱');
  258. }
  259. $method = 'checkEmailExists';
  260. $this->$method(1, 1);
  261. $code = $this->code(false, false);
  262. $email = base64_encode($email);
  263. //return Dever::load('passport/lib/email')->forget($email, $code);
  264. Dever::daemon('lib/email.forget?email=' . $email . '&code=' . $code, 'passport');
  265. $refer = $this->refer(true);
  266. return $refer . '||邮件已经发送成功!请到您的邮箱里查看。';
  267. } else {
  268. Dever::alert('请输入邮箱');
  269. }
  270. }
  271. public function forget()
  272. {
  273. $signature = Dever::input('signature');
  274. if (!$signature) {
  275. Dever::alert('您的验证信息已失效,请重新发送验证邮件');
  276. }
  277. $signature = Dever::decode($signature);
  278. $temp = explode('|||', $signature);
  279. $code = $temp[1];
  280. $state = $this->code($code);
  281. if (!$state) {
  282. Dever::alert('您的验证信息已失效,请重新发送验证邮件');
  283. }
  284. $account = Dever::config('base', 'project')->account;
  285. $code = Dever::config('base', 'project')->nocode ? Dever::config('base', 'project')->nocode : state;
  286. $baccount = ucfirst($account);
  287. $method = 'check' . $baccount . 'Exists';
  288. $param['option_' . $account] = $this->$method($code, true);
  289. $user = Dever::load('passport/user-one', $param);
  290. if (!$user) {
  291. Dever::alert('该账号还未注册,请先注册');
  292. } else {
  293. $param['set_password'] = Dever::input('password');
  294. $cpassword = Dever::input('cpassword');
  295. if (sha1($param['set_password']) == $user['password']) {
  296. Dever::alert('您的新密码和旧密码相同');
  297. }
  298. if (!$param['set_password']) {
  299. Dever::alert('新密码不能为空');
  300. }
  301. if ($param['set_password'] != $cpassword) {
  302. Dever::alert('确认密码不正确');
  303. }
  304. $id = $param['where_id'] = $user['id'];
  305. Dever::load('passport/user-update', $param);
  306. if ($id > 0) {
  307. $refer = $this->refer(true);
  308. return $refer . '||您已经成功修改密码';
  309. } else {
  310. Dever::alert('修改失败');
  311. }
  312. }
  313. }
  314. public function url()
  315. {
  316. return Dever::url('reg?' . $this->createRefer(), 'main');
  317. }
  318. }