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

108 lines
3.6 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\common\model;
use think\Model;
use think\Request;
use think\Config;
use think\Session;
class Customer extends Model {
use \app\common\traits\IndexModel;
/**
* 更新用户密码
*/
public function updatePassword($data) {
//$salt = getstr_random();
$row = array(
'id' => $data['id'],
'password' => md5($data['password']),
'salt' => $data['password'],
);
$object = $this::update($row);
return $object;
}
/**
* 用户登录认证
* @param string $condition 验证条件如用户名邮箱手机号ID
* @param string $password 用户密码
* @param integer $type 用户名类型 1-用户名2-邮箱3-手机4-UID
* @return integer 登录成功-用户ID登录失败-错误编号
*/
public function login($condition, $password, $type = 1) {
$where = [];
switch ($type) {
case 1:$where['firstname'] = $condition;
break;
case 2:$where['email'] = $condition;
break;
case 3:$where['mobile'] = $condition;
break;
case 4:$where['id'] = $condition;
break;
default:
return ['status' => false, 'msg' => '参数错误', 'id' => 0]; //参数错误
}
/* 获取用户数据 */
$row = $this->where($where)->field('id,firstname,lastname,password,salt,login,picture,telephone,email,safe,stat')->find();
if (empty($row) || (int) $row->stat !== 0) {
return ['status' => false, 'msg' => '用户不存在或被禁用', 'id' => 0];
}
if (!$row->safe) {
return ['status' => false, 'msg' => '请联系管理员激活您的账户', 'id' => 0];
}
/* 验证用户密码 */
if (md5($password) !== $row->password) {
return ['status' => false, 'msg' => '密码错误', 'id' => 0];
}
unset($row->password);
unset($row->salt);
/* 登录用户 */
$this->autoLogin($row->toArray());
return ['status' => true, 'msg' => '登录成功', 'id' => $row->id]; //登录成功返回用户ID
}
public function getBasicInfo($id) {
return $this->where(['stat' => 0])->field('id, firstname, picture, sex, email, telephone, qq, birthday, password')->find($id);
}
public function getBasicInfoByTelephone($telephone) {
return $this->where(['telephone' => $telephone, 'stat' => 0])->field('id, firstname, picture, sex, email, telephone, qq, birthday, password')->find();
}
public function getBasicInfoByEmail($email) {
return $this->where(['email' => $email, 'stat' => 0])->field('id, firstname, picture, email, sex, telephone, qq, birthday, password')->find();
}
private function autoLogin($row) {
/* 更新登录信息 */
$data = [
'id' => $row['id'],
'login' => \think\Db::raw('`login`+1'),
//'last_login_time' => Request::instance()->time(),
'ip' => Request::instance()->ip()
];
$this::update($data);
/* 记录登录SESSION和COOKIES */
Session::set('customer_auth', $row);
Session::set('customer_auth_sign', data_auth_sign($row));
unset($row);
}
public function insertRow($data) {
$object = $this::create($data);
return $object;
}
protected function setIpAttr($value) {
if (empty($value)) {
return Request::instance()->ip();
}
return $value;
}
}