319 lines
14 KiB
PHP
Executable File
319 lines
14 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use think\Lang;
|
|
use think\Loader;
|
|
use think\Config;
|
|
use think\Session;
|
|
use storage\Storage;
|
|
|
|
class Customer extends BaseController {
|
|
|
|
/**
|
|
* 后台用户首页
|
|
*/
|
|
public function index() {
|
|
$this->redirect('/admin/customer/lists');
|
|
}
|
|
|
|
public function lists() {
|
|
$skeyword = $this->request->get('skeyword', '', 'urldecode');
|
|
if (!empty($skeyword)) {
|
|
$skeyword = trim($skeyword);
|
|
$arg_where['c.firstname|c.email|c.telephone'] = ['like', '%' . $skeyword . '%'];
|
|
$search['skeyword'] = $skeyword;
|
|
Config::set('paginate.query', ['skeyword' => $skeyword]); //分页参数
|
|
} else {
|
|
$search['skeyword'] = '';
|
|
}
|
|
$arg_where['c.stat'] = ['in', '0,1'];
|
|
$dataObject = Loader::model('Customer')->getCustomerLists($arg_where, null, ['c.*', 'cg.name' => 'group_name'], 24);
|
|
//$groupOption = Loader::model('CustomerGroup')->getOption($customer['group_id'], ['stat' => 0, 'id' => ['neq', $this->user_id == 1 ? 0 : 1]], ['id' => 'desc'], ['id', 'name',], 50);
|
|
$value = [
|
|
//'groupOption' => $groupOption,
|
|
'list' => $dataObject->isEmpty() ? null : $dataObject->items(), //$dataObject->getCollection()->toArray()
|
|
'page' => $dataObject->render(),
|
|
//'page_previous' => $dataObject->getUrl($dataObject->currentPage() - 1),
|
|
//'page_next' => $dataObject->getUrl($dataObject->currentPage() + 1),
|
|
'search' => $search,
|
|
];
|
|
$this->assign($value);
|
|
return $this->fetch();
|
|
}
|
|
|
|
public function edit($id = 0) {
|
|
$id = intval($id);
|
|
if ($id > 0) {
|
|
$customer = Loader::model('Customer')->getRow(['id' => $id, 'stat' => ['gt', '-1']]);
|
|
if (empty($customer)) {
|
|
return $this->error('数据有误,请检查后再操作');
|
|
}
|
|
$value['customer'] = $customer;
|
|
$groupOption = Loader::model('CustomerGroup')->getOption($customer['group_id'], ['stat' => 0], ['id' => 'desc'], ['id', 'name'], 50);
|
|
$value['groupOption'] = $groupOption;
|
|
$value['hangye'] = (array) Config::get('website_hangye');
|
|
$value['zhiye'] = (array) Config::get('website_zhiye');
|
|
$this->assign($value);
|
|
return $this->fetch();
|
|
} else {
|
|
return $this->fetch('add');
|
|
}
|
|
}
|
|
|
|
public function getajaxcustomer() {
|
|
$pid = $this->request->get('pid', 0);
|
|
$id = $this->request->get('id', 0);
|
|
$id = intval($id);
|
|
if ($this->request->isAjax() && $id > 0) {
|
|
$arg_where = array('stat' => 0);
|
|
$arg_order = array('id' => 'desc');
|
|
$arg_field = array('id', 'customername');
|
|
$customerOption = Loader::model('Customer')->getOption($id, $arg_where, $arg_order, $arg_field, 100);
|
|
return $this->result($customerOption, true, Lang::get('operation successed'));
|
|
}
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
|
|
public function update() {
|
|
if ($this->request->isPost()) {
|
|
$data = $this->request->post();
|
|
if (empty($data) || !is_array($data)) {
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
//验证规则
|
|
$validaterule = [
|
|
'firstname' => 'require|length:2,32|unique:customer,firstname',
|
|
'email' => 'email|unique:customer,email',
|
|
'telephone' => ['regex' => '^1[345789]\d{9}$|^([0-9]{3,4}-?)?[0-9]{7,8}$', 'unique' => 'customer,telephone',],
|
|
'group_id' => 'require|between:0,2147483647',
|
|
];
|
|
//验证提示信息
|
|
$validatemsg = [
|
|
'firstname.require' => '用户名不能为空',
|
|
'firstname.unique' => '用户名已经被使用',
|
|
'firstname.length' => '用户名在2-32个字符之间',
|
|
'email.email' => '邮箱格式错误',
|
|
'email.unique' => '邮箱已经被使用',
|
|
'telephone.regex' => '电话格式错误',
|
|
'telephone.unique' => '电话已经被使用',
|
|
'group_id.require' => '用户组不能为空',
|
|
];
|
|
$valid_result = $this->validate($data, $validaterule, $validatemsg);
|
|
if (true !== $valid_result) {
|
|
// 验证失败 输出错误信息
|
|
return $this->error($valid_result);
|
|
}
|
|
$set = [
|
|
'id' => $data['id'],
|
|
'email' => isset($data['email']) ? $data['email'] : '',
|
|
'telephone' => isset($data['telephone']) ? $data['telephone'] : '',
|
|
'firstname' => $data['firstname'],
|
|
'lastname' => isset($data['lastname']) ? $data['lastname'] : '',
|
|
'stat' => $data['stat'],
|
|
'safe' => $data['safe'],
|
|
'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'] : '',
|
|
];
|
|
$model = Loader::model('Customer')->updateRow($set);
|
|
if ($model && $customer_id = $model->getData('id')) {
|
|
return $this->success(Lang::get('operation successed'), url('/admin/customer/lists'));
|
|
}
|
|
}
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
|
|
public function add() {
|
|
$groupOption = Loader::model('CustomerGroup')->getOption(0, ['stat' => 0, 'id' => ['neq', $this->user_id == 1 ? 0 : 1]], ['id' => 'desc'], ['id', 'name',], 50);
|
|
$value = ['groupOption' => $groupOption,];
|
|
$value['hangye'] = (array) Config::get('website_hangye');
|
|
$value['zhiye'] = (array) Config::get('website_zhiye');
|
|
$this->assign($value);
|
|
return $this->fetch();
|
|
}
|
|
|
|
public function create() {
|
|
if ($this->request->isPost()) {
|
|
$data = $this->request->post();
|
|
if (empty($data) || !is_array($data)) {
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
//验证规则
|
|
$validaterule = [
|
|
'firstname' => 'require|length:2,32|unique:customer,firstname',
|
|
'email' => 'email|unique:customer,email',
|
|
'telephone' => ['regex' => '^1[345789]\d{9}$|^([0-9]{3,4}-?)?[0-9]{7,8}$', '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);
|
|
}
|
|
$set = [
|
|
'group_id' => $data['group_id'],
|
|
'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' => time(),
|
|
'custom_field' => json_encode([]),
|
|
];
|
|
$model = Loader::model('Customer')->insertRow($set);
|
|
if ($model && $customer_id = $model->getData('id')) {
|
|
return $this->success(Lang::get('operation successed'), url('/admin/customer/lists'));
|
|
} else {
|
|
return $this->error(Lang::get('operation failed'));
|
|
}
|
|
}
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
|
|
public function delete($id = 0) {
|
|
$id = intval($id);
|
|
if ($id > 0) {
|
|
$result = Loader::model('Customer')->deleteRow($id);
|
|
if ($result) {
|
|
if ($id == Session::get('customer_auth.id')) {
|
|
Session::delete('customer_auth', null);
|
|
Session::delete('customer_auth_sign', null);
|
|
}
|
|
return $this->success(Lang::get('operation successed'), url('/admin/customer/lists'));
|
|
} else {
|
|
return $this->error(Lang::get('operation failed'));
|
|
}
|
|
}
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
|
|
public function deletes() {
|
|
if ($this->request->isPost()) {
|
|
$data = $this->request->post();
|
|
$result = Loader::model('Customer')->deleteRows($data['ids']);
|
|
if ($result) {
|
|
return $this->success(Lang::get('operation successed'), url('/admin/customer/lists'));
|
|
} else {
|
|
return $this->error(Lang::get('operation failed'));
|
|
}
|
|
}
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
|
|
public function destroy($id = 0) {
|
|
$id = intval($id);
|
|
if ($id > 0) {
|
|
$result = Loader::model('Customer')->destroyCustomer($id);
|
|
if ($result) {
|
|
return $this->success(Lang::get('operation successed'), url('/admin/customer/lists'));
|
|
} else {
|
|
return $this->error(Lang::get('operation failed'));
|
|
}
|
|
}
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
|
|
public function destroys() {
|
|
if ($this->request->isPost()) {
|
|
$data = $this->request->post();
|
|
$result = Loader::model('Customer')->destroyRows($data['ids']);
|
|
if ($result) {
|
|
return $this->success(Lang::get('operation successed'), url('/admin/customer/lists'));
|
|
} else {
|
|
return $this->error(Lang::get('operation failed'));
|
|
}
|
|
}
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
|
|
public function updatepassword() {
|
|
if ($this->request->isPost()) {
|
|
$data = $this->request->post();
|
|
if (empty($data) || !is_array($data)) {
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
//验证规则
|
|
$validaterule = [
|
|
'id' => "require",
|
|
'newpassword' => 'require|min:6',
|
|
'repassword' => 'require|confirm:newpassword',
|
|
];
|
|
if (!$this->administrator) {
|
|
$validaterule['oldpassword'] = 'require|min:6';
|
|
}
|
|
//验证提示信息
|
|
$validatemsg = [
|
|
'id.require' => 'ID参数错误',
|
|
'oldpassword.require' => '密码不能为空',
|
|
'oldpassword.min' => '密码最低6个字符',
|
|
'newpassword.require' => '密码不能为空',
|
|
'newpassword.min' => '密码最低6个字符',
|
|
'repassword.require' => '确认密码不能为空',
|
|
'repassword.confirm' => '两次密码不相符',
|
|
];
|
|
$valid_result = $this->validate($data, $validaterule, $validatemsg);
|
|
if (true !== $valid_result) {
|
|
// 验证失败 输出错误信息
|
|
return $this->error($valid_result);
|
|
}
|
|
$customersModel = Loader::model('Customer');
|
|
if (!$this->administrator) {
|
|
$customer = $customersModel->getCustomer($data['id'], ['password', 'salt', 'id']);
|
|
if (empty($customer)) {
|
|
return $this->error('数据有误,请检查后再操作');
|
|
}
|
|
if ($customer['password'] != md5($data['oldpassword'])) {
|
|
return $this->error('旧密码输入错误');
|
|
}
|
|
}
|
|
$model = $customersModel->updatePassword($data);
|
|
if ($model && $model->getData('id')) {
|
|
return $this->success(Lang::get('operation successed'), url('/admin/customer/lists'));
|
|
} else {
|
|
return $this->error(Lang::get('operation failed'));
|
|
}
|
|
}
|
|
return $this->error(Lang::get('incorrect operation'));
|
|
}
|
|
|
|
}
|