customer_id) { return $this->redirect(url('id/user/index')); } return $this->fetch(); } /** * 前台用户登录 * @param string $username 前台用户名 * @param string $password 密码 * @param string $verify 验证码 */ public function login() { if ($this->customer_id) { return $this->redirect(url('id/user/index')); } $this->request->isPost() || $this->error(Lang::get('illegal request')); //判断是否ajax登录 $data = $this->request->post(); if (empty($data) || !is_array($data)) { return $this->error('未知错误'); } $this->verify_check($data['authcode'], 'yanzhengma') || $this->error('验证码 ' . Lang::get('error'), url('/login')); $validaterule = [ //会员登陆字段验证 'firstname|' . Lang::get('user name') => 'require|min:2', 'password|' . Lang::get('user password') => 'require|min:6', ]; // 数据验证 $valid_result = $this->validate($data, $validaterule); if (true !== $valid_result) { // 验证失败 输出错误信息 return $this->error($valid_result); } $result = Loader::model('Customer')->login($data['firstname'], $data['password']); $result['status'] !== true && $this->error($result['msg'], url('us/login')); //登录失败 if ($this->request->isAjax()) { $result['id'] ? $this->success('登录成功', url('us/user/index')) : $this->error(Lang::get('unknown error'), url('us/login')); } return $result['id'] ? $this->redirect(url('us/user/index')) : $this->error(Lang::get('unknown error'), url('us/login')); } /** * 退出登录 */ public function logout() { if (!$this->customer_id) { return $this->redirect(url('us/login')); } Session::delete('customer_auth', null); Session::delete('customer_auth_sign', null); return $this->redirect(url('us/login')); } public function register() { if ($this->customer_id) { return $this->redirect(url('us/user/index')); } if ($this->request->isPost()) { $data = $this->request->post(); if (empty($data) || !is_array($data)) { return $this->error(Lang::get('incorrect operation')); } $this->verify_check($data['authcode'], 'yanzhengma') || $this->error('验证码 ' . Lang::get('error'), url('/login')); //验证规则 $validaterule = [ 'firstname' => 'require|length:2,32|unique:customer,firstname', 'email' => 'email|unique:customer,email', 'telephone' => ['regex' => '^1[345789]\d{9}$', 'unique' => 'customer,telephone',], 'password' => 'require|min:6|max:32', 'repassword' => 'require|confirm:password', //'group_id' => 'require|between:0,2147483647', 'item' => 'accepted', ]; //验证提示信息 $validatemsg = [ 'firstname.require' => '用户名不能为空', 'firstname.unique' => '用户名已经被使用', 'firstname.length' => '用户名在2-32个字符之间', 'email.email' => '邮箱格式错误', 'email.unique' => '邮箱已经被使用', 'telephone.regex' => '电话格式错误', 'telephone.unique' => '电话已经被使用', 'password.require' => '密码不能为空', 'password.min' => '密码不少于6个字符', 'password.max' => '密码不多于32个字符', 'repassword.require' => '确认密码不能为空', 'repassword.confirm' => '两次密码不相符', 'group_id.require' => '用户组不能为空', 'item' => '请确认阅读服务条款', ]; $valid_result = $this->validate($data, $validaterule, $validatemsg); if (true !== $valid_result) { // 验证失败 输出错误信息 return $this->error($valid_result); } $code = $this->cacheGet('regtel' . $data['telephone']); if ($code != $data['code']) { return $this->error('短信验证码不正确,请输入正确验证码'); } $addtime = time(); $set = [ 'group_id' => 1, 'email' => isset($data['email']) ? $data['email'] : '', 'telephone' => isset($data['telephone']) ? $data['telephone'] : '', 'firstname' => $data['firstname'], 'lastname' => isset($data['lastname']) ? $data['lastname'] : '', 'newsletter' => isset($data['newsletter']) ? $data['newsletter'] : 0, 'salt' => $data['password'], 'password' => md5($data['password']), 'stat' => 0, 'safe' => 1, 'code' => '', 'item' => isset($data['item']) ? $data['item'] : 0, 'token' => isset($data['token']) ? $data['token'] : '', 'wishlist' => isset($data['wishlist']) ? $data['wishlist'] : '', 'ip' => isset($data['ip']) ? $data['ip'] : '', 'fenxiang' => isset($data['fenxiang']) ? $data['fenxiang'] : 0, 'guanzhu' => isset($data['guanzhu']) ? $data['guanzhu'] : 0, 'hangye' => isset($data['hangye']) ? $data['hangye'] : '', 'zhiye' => isset($data['zhiye']) ? $data['zhiye'] : '', 'sex' => isset($data['sex']) ? $data['sex'] : '', 'birthday' => isset($data['birthday']) ? $data['birthday'] : '', 'qq' => isset($data['qq']) ? $data['qq'] : '', 'addtime' => $addtime, 'custom_field' => json_encode([]), ]; $model = Loader::model('Customer')->insertRow($set); if ($model && $customer_id = $model->getData('id')) { return $this->success('注册成功', url('/us/customer/information', ['key' => 'regsuccess'])); } return $this->error(Lang::get('operation failed')); } return $this->fetch(); } public function information($key) { $key = (string) $key; $this->engine->layout(false); $result = [ 'regsuccess' => ['msg' => '注册成功', 'url' => ''], 'getpwdsuccess' => ['msg' => '找回密码完成', 'url' => ''], ]; if ($result[$key]) { $value = $result[$key]; } else { $value = ['msg' => '信息提示', 'url' => '']; } $this->assign($value); return $this->fetch(); } public function forgetpwd() { if ($this->customer_id) { return $this->redirect(url('us/user/index')); } if ($this->request->isPost()) { $data = $this->request->post(); if (empty($data) || !is_array($data)) { return $this->error(Lang::get('incorrect operation')); } //验证规则 $validaterule = [ 'email' => 'email', 'password' => 'require|min:6|max:32', 'repassword' => 'require|confirm:password', ]; //验证提示信息 $validatemsg = [ 'email.email' => '邮箱格式错误', 'password.require' => '密码不能为空', 'password.min' => '密码不少于6个字符', 'password.max' => '密码不多于32个字符', 'repassword.require' => '确认密码不能为空', 'repassword.confirm' => '两次密码不相符', ]; $valid_result = $this->validate($data, $validaterule, $validatemsg); if (true !== $valid_result) { // 验证失败 输出错误信息 return $this->error($valid_result); } $row = Loader::model('Customer')->getRow(['email' => $data['email']]); if (empty($row)) { return $this->error('该邮箱尚未注册!'); } $code = $this->cacheGet('regemail' . $data['email']); if ($code != $data['code']) { return $this->error('邮箱验证码不正确,请输入正确验证码'); } // if ($row['password'] != md5($data['password'])) { // return $this->error('原密码不正确'); // } $data['id'] = $row['id']; $model = Loader::model('Customer')->updatePassword($data); if ($model && $model->getData('id')) { return $this->success('找回密码完成', url('/us/customer/information', ['key' => 'getpwdsuccess'])); } } return $this->fetch(); } public function sendsms() { $data = $this->request->param(); if (empty($data) || !is_array($data)) { return $this->error(Lang::get('incorrect operation')); } //验证规则 $validaterule = [ 'telephone' => ['regex' => '^1[345789]\d{9}$', 'unique' => 'customer,telephone',], ]; //验证提示信息 $validatemsg = [ 'telephone.regex' => '电话格式错误', 'telephone.unique' => '电话已经被使用', ]; $valid_result = $this->validate($data, $validaterule, $validatemsg); if (true !== $valid_result) { // 验证失败 输出错误信息 return $this->error($valid_result); } //$mobile = $data['telephone']; //$code = mt_rand(10000, 99999); //$this->cacheSet('regtel' . $mobile, $code, 300); //return $this->success($code); //获取对象,如果上面没有引入命名空间,可以这样实例化:$sms = new \alisms\SendSms() $sms = new \alisms\SendSms(); //设置关键的四个配置参数,其实配置参数应该写在公共或者模块下的config配置文件中,然后在获取使用,这里我就直接使用了。 $sms->accessKeyId = (string) Config::get('sms_accesskeyid'); $sms->accessKeySecret = (string) Config::get('sms_accesskeysecret'); $sms->signName = (string) Config::get('sms_signname'); $sms->templateCode = (string) Config::get('sms_templatecode'); //$mobile为手机号 $mobile = $data['telephone']; //模板参数,自定义了随机数,你可以在这里保存在缓存或者cookie等设置有效期以便逻辑发送后用户使用后的逻辑处理 $code = mt_rand(10000, 99999); $this->cacheSet('regtel' . $mobile, $code, 300); $templateParam = array('code' => $code); $m = $sms->send($mobile, $templateParam); //类中有说明,默认返回的数组格式,如果需要json,在自行修改类,或者在这里将$m转换后在输出 if ($m['Code'] == 'OK') { return $this->success($m['Message']); } else { return $this->error($m['Message']); } } public function sendresetemail() { $data = $this->request->param(); if (empty($data) || !is_array($data)) { return $this->error(Lang::get('incorrect operation')); } //验证规则 $validaterule = ['email' => 'email',]; //验证提示信息 $validatemsg = ['email.email' => '邮箱格式错误',]; $valid_result = $this->validate($data, $validaterule, $validatemsg); if (true !== $valid_result) { // 验证失败 输出错误信息 return $this->error($valid_result); } $row = Loader::model('Customer')->getRow(['email' => $data['email']]); if (empty($row)) { return $this->error('该邮箱尚未注册!'); } //$email = $data['email']; //$code = mt_rand(10000, 99999); //$this->cacheSet('regemail' . $email, $code, 1800); //return $this->success($code); //$email为邮箱 $email = $data['email']; //模板参数,自定义了随机数,你可以在这里保存在缓存或者cookie等设置有效期以便逻辑发送后用户使用后的逻辑处理 $code = mt_rand(10000, 99999); $this->cacheSet('regemail' . $email, $code, 1800); //邮件标题 $subject = $this->request->host() . '-找回密码'; //邮件内容 $body = "

亲爱的" . $row['firstname'] . "

您在" . date('Y-m-d H:i') . "提交了找回密码请求.

本次验证码:" . $code . "

有效期为30分钟,请及时做出处理."; $res = $this->sendemail($data['email'], $row['firstname'], $subject, $body); if ($res['code'] == 'Success') { return $this->success("系统已向您的邮箱发送了一封邮件\n请登录到您的邮箱及时获取您的验证码!"); } else { return $this->error($res['msg']); } } public function sendemail($to, $to_name, $subject, $body, $from_email = '', $from_name = 'From') { $email_host = (string) Config::get('email_host'); $email_tls = (string) Config::get('email_tls'); $email_port = (string) Config::get('email_port'); $email_user = (string) Config::get('email_user'); $email_pass = (string) Config::get('email_pass'); $email_code = (string) Config::get('email_code'); $email_replyaddr = (string) Config::get('email_replyaddr'); $website_email = (string) Config::get('website_email'); // Passing `true` enables exceptions $mail = new \mail\PHPMailer\PHPMailer(false); try { //Tell PHPMailer to use SMTP $mail->isSMTP(); //$mail->setLanguage('en'); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 0; $mail->Host = $email_host; // if your network does not support SMTP over IPv6 //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission $mail->Port = $email_port; $mail->CharSet = strtolower($email_code); $mail->Encoding = 'base64'; $mail->SMTPKeepAlive = true; //Set the encryption system to use - ssl (deprecated) or tls $mail->SMTPSecure = strtolower($email_tls); //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication - use full email address for gmail $mail->Username = $email_user; //Password to use for SMTP authentication $mail->Password = $email_pass; //Set who the message is to be sent from if ($from_email) { $mail->setFrom($from_email, $from_name); } else { $mail->setFrom($email_replyaddr, 'Sender'); } //Set an alternative reply-to address if ($website_email) { $mail->addReplyTo($website_email, 'Reply'); } //Set who the message is to be sent to $mail->addAddress($to, $to_name); //$mail->addAddress($website_email, 'Recipient'); //Set the subject line $mail->Subject = $subject; //Read an HTML message body from an external file, convert referenced images to embedded, //convert HTML into a basic plain-text alternative body $mail->msgHTML($body); //$mail->Body = 'This is the HTML message body in bold!'; //Replace the plain text body with one created manually $mail->AltBody = 'This is a plain-text message body'; $mail->WordWrap = 60; //send the message, check for errors if (!$mail->send()) { $result = ['code' => 'Failure', 'msg' => "Mailer Error: " . $mail->ErrorInfo]; } else { $result = ['code' => 'Success', 'msg' => 'Message has been sent']; } } catch (\mail\PHPMailer\Exception $e) { $result = ['code' => 'Failure', 'msg' => 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo]; } return $result; } }