| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- /**
- * User: nickbai
- * Date: 2017/10/24 10:46
- * Email: 1902822973@qq.com
- */
- namespace app\index\controller;
- use think\Controller;
- use Xmail\PHPMailer;
- class Register extends Controller
- {
- // 注册首页
- public function index()
- {
- $this->assign([
- 'version' => config('version')
- ]);
- return $this->fetch();
- }
- // 处理注册
- public function doRegister()
- {
- if(request()->isPost()){
- $userName = input("param.user_name/s");
- $userEmail = input("param.user_email/s");
- $password = input("param.password/s");
- $emailCode = input("param.code/s");
- if(empty($userName)){
- return json(['code' => -1, 'data' => '', 'msg' => '用户名不能为空']);
- }
- if(empty($userEmail)){
- return json(['code' => -2, 'data' => '', 'msg' => '邮箱不能为空']);
- }
- if(empty($password)){
- return json(['code' => -3, 'data' => '', 'msg' => '密码不能为空']);
- }
- if(empty($emailCode)){
- return json(['code' => -4, 'data' => '', 'msg' => '验证码不能为空']);
- }
- if($emailCode != cookie('code')){
- return json(['code' => -5, 'data' => '', 'msg' => '验证码不正确']);
- }
- $name = db('accounts')->where('user_name', $userName)->find();
- if(!empty($name)){
- return json(['code' => -6, 'data' => '', 'msg' => '用户名已存在']);
- }
- $email = db('accounts')->where('user_email', $userEmail)->find();
- if(!empty($email)){
- return json(['code' => -7, 'data' => '', 'msg' => '邮箱已存在']);
- }
- // 添加用户信息
- $userInfo = [
- 'user_name' => $userName,
- 'user_email' => $userEmail,
- 'password' => md5($password . config('salt')),
- 'status' => 1,
- 'add_time' => time(),
- 'last_login_time' => time()
- ];
- $user_id = db('accounts')->insertGetId($userInfo);
- // 记录用户状态
- cookie('user_name', $userName, config('save_time'));
- cookie('user_id', $user_id, config('save_time'));
- return json(['code' => 1, 'data' => url('user/index'), 'msg' => '注册成功']);
- }
- }
- public function email(){
- $email=input("post.email");//获取收件人邮箱
- $mail = new PHPMailer();
- $mail->IsSMTP();
- $mail->isHTML(true);
- $mail->AltBody = "更好地查看这封邮件,请打开HTML兼容视图"; // optional, comment out and test
- $mail->CharSet = 'utf-8'; // 编码格式为utf8,不设置编码的话,中文会出现乱码
- $mail->SMTPDebug = 0; // enables SMTP debug information
- $mail->SMTPAuth = true; // enable SMTP authentication
- $mail->Host = "smtp.163.com"; // 发送方的SMTP服务器地址
- $mail->Port = 25; // 端口
- $mail->Password = "jonlin2468"; //客户端授权密码,而不是邮箱的登录密码!
- $mail->Username = "jonlinhuang@163.com"; // 发件人邮箱
- $mail->SetFrom('jonlinhuang@163.com', '安全猫-系统邮件'); // 安全猫科技有限公司co.ltd
- $code=rand(100000,999999);
- cookie('code', $code, config('save_time'));
- $mail->Subject = '邮箱验证';
- $mail->AddAddress($email);
- $mail->MsgHTML('邮件内容是 <b>您的验证码是:'.$code.'</b>,如果非本人操作无需理会!');
- $res = $mail->send();
- if($res == true){
- return "发送成功";
- }else{
- echo "Message could not be sent.";
- echo "Mailer Error: " . $mail->ErrorInfo;// 输出错误信息
- }
- }
- }
|