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

225 lines
6.9 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\admin\model;
use think\Model;
use think\Request;
use think\Config;
use think\Session;
class Customer extends Model {
use \app\common\traits\AdminModel;
public function getLists($where = null, $order = null, $field = null, $limit = null) {
$this->alias('u')
->join('customer_dept ud', 'u.id=ud.id', 'LEFT')
->join('dept d', 'ud.dept_id=d.id', 'LEFT');
if (is_array($where)) {
$where = array_merge(['stat' => ['neq', '-1']], $where);
}
if ($where) {
$this->where($where);
}
if ($order) {
$this->order($order);
}
if ($field) {
$this->field($field);
}
if (empty($limit)) {
$limit = Config::get('list_rows');
}
$object = $this->paginate($limit);
return $object;
}
public function getRoleCustomers($where = null, $order = null, $field = null, $limit = null) {
$this->alias('u')->join('auth_role ar', 'u.role_id=ar.id', 'LEFT');
if (is_array($where)) {
$where = array_merge([], $where);
}
if ($where) {
$this->where($where);
}
if ($order) {
$this->order($order);
}
if ($field) {
$this->field($field);
}
if (empty($limit)) {
$limit = Config::get('list_rows');
}
$this->group('u.id');
//$this->having('max(ud.dept_id)');
$object = $this->paginate($limit);
return $object;
}
/**
* 获取数据库中的配置列表
* @return array
*/
public function getCustomerLists($where = null, $order = null, $field = null, $limit = null) {
$this->alias('c')->join('customer_group cg', 'c.group_id=cg.id', 'LEFT');
if (is_array($where)) {
$where = array_merge(['c.stat' => 0], $where);
}
if ($where) {
$this->where($where);
}
if ($order) {
$this->order($order);
}
if ($field) {
$this->field($field);
}
if (empty($limit)) {
$limit = Config::get('list_rows');
}
$object = $this->paginate($limit);
return $object;
}
public function getOption($id = 0, $where = null, $order = null, $field = null, $limit = 20) {
$options = '';
if ($where) {
$this->where($where);
}
if ($order) {
$this->order($order);
}
if ($field) {
$this->field($field);
}
if ($limit) {
$this->limit($limit);
}
$list = $this->select();
if ($list) {
foreach ($list as $k => $row) {
if ($row['id'] == $id) {
$options.='<option value="' . $row['id'] . '" selected>' . $row['customername'] . '</option>' . "\n";
} else {
$options.='<option value="' . $row['id'] . '">' . $row['customername'] . '</option>' . "\n";
}
}
}
return $options;
}
/*
public function insertRow($data, $siteid = 32267) {
if (isset($data['password'])) {
//$salt = getstr_random();
//$row['password'] = md5($data['password'] . $salt);
$row['salt'] = $data['password'];
$row['password'] = md5($data['password']);
}
$row['siteid'] = $siteid;
$object = $this::create($row);
return $object;
}
*/
/**
* 更新用户密码
*/
public function updatePassword($data) {
//$salt = getstr_random();
$row = array(
'id' => $data['id'],
'password' => md5($data['newpassword']),
'salt' => $data['newpassword'],
);
$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['customername'] = $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,customername,password,salt,picture,position,role_id,stat,last_login_time')->find();
if (empty($row) || (int) $row->stat !== 1) {
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' => false, 'msg' => '登录成功', 'id' => $row->id]; //登录成功返回用户ID
}
/**
* 自动登录用户
* @param integer $row 用户信息数组
*/
private function autoLogin($row) {
/* 更新登录信息 */
$data = [
'id' => $row['id'],
//'login' => \think\Db::raw('`login`+1'),
'last_login_time' => Request::instance()->time(),
'last_login_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);
//记录行为
// $param = ['action' => 'customer_login', 'model' => 'member', 'record_id' => $row['id']];
// Hook::listen('customer_behavior', $param);
}
/**
* 注销当前用户
* @return void
*/
public function logout() {
Session::delete('customer_auth', null);
Session::delete('customer_auth_sign', null);
}
protected function setRegisterTimeAttr($value, $data) {
return time();
}
protected function setLastLoginTimeAttr($value, $data) {
return time();
}
protected function setLastUpdateTimeAttr($value, $data) {
return time();
}
protected function setRegisterIpAttr() {
return Request::instance()->ip();
}
}