Files
2024-10-29 14:04:59 +08:00

506 lines
17 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\us\controller;
use think\Cookie;
use think\Lang;
use think\Loader;
use think\Config;
use think\Session;
use think\Cache;
class Customer extends BaseController {
public function index() {
if ($this->customer_id > 0)
{
$this->redirect(url('us/customer/personal'));
}
$url = $this->request->get('url');
$url = $url != '' ? $url : '';
$this->assign('url', $url);
return $this->fetch();
}
# 用旧密码改新密码
public function update_pwd()
{
$data = $this->request->post();
// tiaoshi($data);die;
if (empty($data) || $this->customer_id <= 0)
{
return $this->json(-1, 'Data error');
}
if ($this->customer_info['have_pwd'])
{
$customer_info = model('customer')->where(['id' => $this->customer_id])->find();
if (md5($data['old_password']) != $customer_info['password'])
{
return $this->json(-2, 'Old password incorrect');
}
}
$update_data = [
'password' => md5($data['password']),
'salt' => $data['password']
];
$result = model('customer')->where(['id' => $this->customer_id])->update($update_data);
if (!$result)
{
return $this->json(-4, 'New passwords do not match.');
}
$customer_info = model('customer')->getBasicInfo($this->customer_id);
$this->set_login_token($customer_info);
return $this->json(200, 'Your password has been updated.');
}
# 用邮箱改密码
public function update_forget_pwd()
{
$data = $this->request->post();
// tiaoshi($data);die;
if (empty($data))
{
return $this->json(-1, 'Data error');
}
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $data['email']))
{
return $this->json(-2, 'Error Mail Form');
}
$customer_info = model('customer')->getBasicInfoByEmail($data['email']);
if (empty($customer_info))
{
return $this->json(-3, 'The email is not registered');
}
$token = md5($data['email'] . 'forgetpwd');
$this->cacheSet($token, $data['email'], 3600);
$result = $this->send_forgetpwd_email($data['email'], $token);
if ($result['code'] < 0)
{
return $this->json(-4, $result['msg']);
}
$this->_logout();
return $this->json(200, 'The email sending successful');
}
public function retrieve_password()
{
return view();
}
public function change_password()
{
$token = $this->request->post('token');
$password = $this->request->post('password');
$email = $this->cacheGet($token, '');
if ($email == '')
{
return $this->json(-1, 'Link Invalid');
}
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $email))
{
return $this->json(-2, 'Error Mail Form');
}
if (!preg_match("/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*-.]+$)[a-zA-Z\d!@#$%^&*-.]{8,20}$/", $password))
{
return $this->json(-3, 'The password must contain 8-20 characters and at least two types of characters.');
}
model('customer')->where(['stat' => 0, 'email' => $email])->update(['password' => md5($password)]);
$this->cacheDelete($token);
return $this->json(200, 'Your password has been updated.');
}
public function check_forgetpwd_email()
{
$token = $this->request->param('token');
$email = $this->cacheGet($token, '');
if ($email == '' || !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $email))
{
return $this->json(-1, 'Error Mail Form');
}
$password = $this->request->post('password');
if (!preg_match("/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*-.]+$)[a-zA-Z\d!@#$%^&*-.]{8,20}$/", $data['password']))
{
return $this->json(-2, 'The password must contain 8-20 characters and at least two types of characters.');
}
$customer = model('customer')->where(['stat' => 0, 'email' => $email])->find();
if (empty($customer))
{
return $this->json(-3, 'The email is not registered');
}
model('customer')->where(['stat' => 0, 'email' => $email])->update(['password' => md5($password)]);
$this->cacheDelete($token);
return $this->json(200, 'Your password has been updated.');
}
private function send_forgetpwd_email($email, $token)
{
//邮件标题
$subject = $this->request->host() . '-retrieve_password';
//邮件内容
$body = "<p>Dear $email,</p>
<p>We recently received a request to reset your password.</p>
<p>You may change your password to something secure and memorable here:</p>
<p>http://www.orico.cc/us/forgetpwd.html?token=$token</p>
<p>If you did not request to reset your password, please ignore this email and log in with your existing password.</p>
<p>Feel free to get in touch if you have any questions.</p>
<p>The Orico Team</p>
<p>supports@orico.com.cn</p>";
$res = $this->sendemail($email, $email, $subject, $body, 'oricogroup@orico.com.cn');
if ($res['code'] == 200) {
return ['code' => 200, 'msg' => "Well send you a link so you can please confirm."];
} else {
return ['code' => -3, 'msg' => $res['msg']];
}
}
public function activation()
{
$email = $this->request->param('email');
$email = isset($email) ? $email : '';
$this->assign('email', $email);
return $this->view->fetch();
}
public function forgetpwd_email()
{
return $this->view->fetch();
}
public function new_register()
{
$data = $this->request->post();
// tiaoshi($data);die;
if (empty($data) || $this->customer_id > 0)
{
return $this->json(-1, 'data error');
}
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $data['email']))
{
return $this->json(-2, 'Error Mail Form');
}
if (!isset($data['re_send']) && !preg_match("/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*-.]+$)[a-zA-Z\d!@#$%^&*-.]{8,20}$/", $data['password']))
{
return $this->json(-3, 'The password must contain 8-20 characters and at least two types of characters.');
}
if (!isset($data['re_send']) && !$this->verify_check($data['captcha'], 'authcode'))
{
return $this->json(-4, 'Verification code error');
}
$customer = model('customer')->where(['email' => $data['email'], 'stat' => 0])->find();
if (!empty($customer))
{
return $this->json(-5, 'This email has previously been used.');
}
// if (!isset($data['re_send']))
// {
// }
// else
// {
// $token = md5($data['email'] . 'register');
// if ($this->cacheHas($token))
// {
// $this->cacheSet($token, $this->cacheGet($token), 3600);
// }
// else
// {
// return $this->json(-100, '该链接已失效,请重新获取');
// }
// }
$token = md5($data['email'] . 'register');
$result = $this->send_register_email($data['email'], $token);
if ($result['code'] < 0)
{
return $this->json(-6, $result['msg']);
}
if (!isset($data['re_send']))
{
$delimiter = '$*$%&';
$this->cacheSet($token, $data['email'] . $delimiter . md5($data['password']), 3600);
}
else
{
if ($this->cacheHas($token))
{
$this->cacheSet($token, $this->cacheGet($token), 3600);
}
else
{
return $this->json(-100, '该链接已失效');
}
}
return $this->json(200, 'Send Success');
}
public function check_register_email()
{
$token = $this->request->param('token');
$data = $this->cacheGet($token, '');
if ($data == '')
{
echo '<script>alert("Captcha Invalid")</script>';
exit;
}
$delimiter = '$*$%&';
$arr = explode($delimiter, $data);
if (!is_array($arr) || !isset($arr[0]) || !isset($arr[1]))
{
echo '<script>alert("Data Invalid")</script>';
exit;
}
$email = $arr[0];
$password = $arr[1];
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $email))
{
echo '<script>alert("Error Mail Form")</script>';
exit;
}
$customer = model('customer')->where(['stat' => 0, 'email' => $email])->find();
if (!empty($customer))
{
echo '<script>alert("This email has previously been used.")</script>';
exit;
}
$firstname = 'Orico' . rand(10000000, 99999999);
$insert_data = [
'firstname' => $firstname,
'email' => $email,
'password' => $password,
'country_code' => $this->country_code
];
$customer_id = model('customer')->insertGetId($insert_data);
if (!$customer_id)
{
echo '<script>alert("Registry Faild")</script>';
}
$customer_info = model('customer')->getBasicInfo($customer_id);
$this->set_login_token($customer_info);
$this->cacheDelete($token);
echo '<script>
alert("Registry Success");
location.href="http://www.orico.cc/us";
</script>';
exit;
}
private function send_register_email($email, $token)
{
//邮件标题
$subject = $this->request->host() . '-registry';
//邮件内容
$body = "<p>Dear $email</p><p>Thank you for registering at orico, were excited to have you with us!</p><p>Click the link below to activate your account:</p><p>http://www.orico.cc/us/customer/check_register_email.html?token=$token</p><p>The Orico Team</p><p>support@orico.com.cn</p>";
$res = $this->sendemail($email, $email, $subject, $body, 'oricogroup@orico.com.cn');
if ($res['code'] == 200) {
return ['code' => 200, 'msg' => "Well send you a link so you can please confirm."];
} else {
return ['code' => -3, 'msg' => $res['msg']];
}
}
public function new_login()
{
$data = $this->request->post();
if (empty($data) || $this->customer_id > 0)
{
return $this->json(-1, 'Data error');
}
$where = [
'stat' => 0,
'email' => $data['email']
];
$customer_info = model('customer')->where($where)->field('id, firstname, picture, sex, email, telephone, qq, birthday, password')->find();
if (empty($customer_info))
{
return $this->json(-2, 'The email is not registered');
}
if ($customer_info['password'] != md5($data['password']) || empty($data['password']))
{
return $this->json(-3, 'Email address or password incorrect');
}
$this->set_login_token($customer_info);
return $this->json(200, 'Login Successful');
}
public function register() {
if ($this->customer_id > 0) {
return $this->redirect(url('us/customer/index'));
}
return $this->fetch();
}
public function personal()
{
if ($this->customer_id <= 0)
{
$this->redirect(url('us/customer/index'));
}
return $this->fetch();
}
public function my_collection()
{
if ($this->customer_id <= 0)
{
$this->redirect(url('us/customer/index'));
}
$param = $this->request->param();
// tiaoshi($param);die;
$where = ['a.stat' => 0, 'b.stat' => 0, 'a.customer_id' => $this->customer_id, 'b.country_code' => $this->country_code];
if (isset($param['cid']))
{
$cid_arr = model('product_category')->getChildIDArray($param['cid']);
$where['b.cid'] = ['in', $cid_arr];
$cid = $param['cid'];
}
else
{
$cid = 0;
}
$field = ['b.id', 'b.cid', 'b.name', 'b.shortname', 'b.isnew', 'b.ishot', 'b.recommend', 'b.viewcount', 'b.brand_id'];
$order = ['a.id' => 'desc'];
$list = model('collection')->getList($where, $order, $field, 10);
foreach ($list as $key => $value) {
$product_two_img = model('product_two_img')->where(['product_id' => $value['id']])->find();
$list[$key]['product_two_img'] = $product_two_img['image_url'];
}
$data = [
'list' => $list->isEmpty() ? null : $list->items(),
'page' => $list->render(),
'cid' => $cid
];
$this->assign($data);
return $this->fetch();
}
public function new_logout()
{
$this->_logout();
return $this->redirect('us/customer/index');
}
public function forgetpwd() {
$token = $this->request->param('token') ? $this->request->param('token') : '';
$this->assign('token', $token);
return $this->fetch();
}
public function sendemail($to, $to_name, $subject, $body, $from_email = '', $from_name = 'ORICO') {
$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(true);
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 <b>in bold!</b>';
//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' => -1, 'msg' => 'The email sending failed, try again later. '];
} else {
$result = ['code' => 200, 'msg' => 'The email sending successful'];
}
} catch (\mail\PHPMailer\Exception $e) {
$result = ['code' => -2, 'msg' => 'The email sending failed, try again later. '];
}
return $result;
}
}