【opencart3源码分析】顾客类customer.php

<?php
namespace Cart;
/**
 * 顾客类
 * @package Cart
 */
class Customer {
    // 顾客id
	private $customer_id;
	// 名字
	private $firstname;
	// 姓氏
	private $lastname;
	// 分组id
	private $customer_group_id;
	//邮箱
	private $email;
	// 电话
	private $telephone;
	// 通讯
	private $newsletter;
	// 地址id
	private $address_id;

	public function __construct($registry) {
	    // 注册配置类
		$this->config = $registry->get('config');
		// 注册数据库类
		$this->db = $registry->get('db');
		// 注册请求类
		$this->request = $registry->get('request');
		// 注册session类
		$this->session = $registry->get('session');

		if (isset($this->session->data['customer_id'])) {
		    // 查询顾客信息
			$customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$this->session->data['customer_id'] . "' AND status = '1'");

			if ($customer_query->num_rows) {
				$this->customer_id = $customer_query->row['customer_id'];
				$this->firstname = $customer_query->row['firstname'];
				$this->lastname = $customer_query->row['lastname'];
				$this->customer_group_id = $customer_query->row['customer_group_id'];
				$this->email = $customer_query->row['email'];
				$this->telephone = $customer_query->row['telephone'];
				$this->newsletter = $customer_query->row['newsletter'];
				$this->address_id = $customer_query->row['address_id'];

				$this->db->query("UPDATE " . DB_PREFIX . "customer SET language_id = '" . (int)$this->config->get('config_language_id') . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE customer_id = '" . (int)$this->customer_id . "'");
			} else {
				$this->logout();
			}
		}
	}

  // 登陆
  public function login($email, $password, $override = false) {
		$customer_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "customer WHERE LOWER(email) = '" . $this->db->escape(utf8_strtolower($email)) . "' AND status = '1'");

		if ($customer_query->num_rows) {
			if (!$override) {
			    // 验证密码和散列值是否匹配
				if (password_verify($password, $customer_query->row['password'])) {
				    // 重新加密
					if (password_needs_rehash($customer_query->row['password'], PASSWORD_DEFAULT)) {
					    // 对密码加密
						$new_password_hashed = password_hash($password, PASSWORD_DEFAULT);
					}
				} elseif ($customer_query->row['password'] == sha1($customer_query->row['salt'] . sha1($customer_query->row['salt'] . sha1($password))) || $customer_query->row['password'] == md5($password)) {
					$new_password_hashed = password_hash($password, PASSWORD_DEFAULT);
				} else {
					return false;
				}
			}
			
			$this->session->data['customer_id'] = $customer_query->row['customer_id'];

			$this->customer_id = $customer_query->row['customer_id'];
			$this->firstname = $customer_query->row['firstname'];
			$this->lastname = $customer_query->row['lastname'];
			$this->customer_group_id = $customer_query->row['customer_group_id'];
			$this->email = $customer_query->row['email'];
			$this->telephone = $customer_query->row['telephone'];
			$this->newsletter = $customer_query->row['newsletter'];
			$this->address_id = $customer_query->row['address_id'];

			$this->db->query("UPDATE " . DB_PREFIX . "customer SET " . ((isset($new_password_hashed)) ? "salt = '', password = '" . $this->db->escape($new_password_hashed) . "', " : "") . "language_id = '" . (int)$this->config->get('config_language_id') . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "' WHERE customer_id = '" . (int)$this->customer_id . "'");

			return true;
		} else {
			return false;
		}
	}

	// 退出
	public function logout() {
	    // 清除session
		unset($this->session->data['customer_id']);

		$this->customer_id = '';
		$this->firstname = '';
		$this->lastname = '';
		$this->customer_group_id = '';
		$this->email = '';
		$this->telephone = '';
		$this->newsletter = '';
		$this->address_id = '';
	}

	// 是否登录
	public function isLogged() {
		return $this->customer_id;
	}

	// 获取顾客id
	public function getId() {
		return $this->customer_id;
	}

	// 获取名字
	public function getFirstName() {
		return $this->firstname;
	}

	// 获取姓氏
	public function getLastName() {
		return $this->lastname;
	}

	// 获取分组id
	public function getGroupId() {
		return $this->customer_group_id;
	}

	// 获取邮箱
	public function getEmail() {
		return $this->email;
	}

	// 获取电话
	public function getTelephone() {
		return $this->telephone;
	}

	// 获取通讯信息
	public function getNewsletter() {
		return $this->newsletter;
	}

	public function getAddressId() {
		return $this->address_id;
	}

	// 交易总额
	public function getBalance() {
		$query = $this->db->query("SELECT SUM(amount) AS total FROM " . DB_PREFIX . "customer_transaction WHERE customer_id = '" . (int)$this->customer_id . "'");

		return $query->row['total'];
	}

	// 增送积分
	public function getRewardPoints() {
		$query = $this->db->query("SELECT SUM(points) AS total FROM " . DB_PREFIX . "customer_reward WHERE customer_id = '" . (int)$this->customer_id . "'");

		return $query->row['total'];
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值