387 lines
14 KiB
PHP
387 lines
14 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller\user4s;
|
||
|
||
use app\common\controller\Backend;
|
||
use app\common\model\user4s\Category;
|
||
use app\common\model\user4s\Level;
|
||
use app\common\model\user4s\Log;
|
||
use app\admin\model\Admin;
|
||
use Exception;
|
||
use think\Db;
|
||
use think\exception\PDOException;
|
||
use think\exception\ValidateException;
|
||
|
||
/**
|
||
* 用户档案
|
||
*
|
||
* @icon fa fa-circle-o
|
||
*/
|
||
class User extends Backend
|
||
{
|
||
|
||
/**
|
||
* User模型对象
|
||
* @var \app\common\model\user4s\User
|
||
*/
|
||
protected $model = null;
|
||
protected $searchFields = 'id,name,tel,carno';
|
||
/**
|
||
* 是否开启数据限制
|
||
* 支持auth/personal
|
||
* 表示按权限判断/仅限个人
|
||
* 默认为禁用,若启用请务必保证表中存在admin_id字段
|
||
*/
|
||
protected $dataLimit = 'auth';
|
||
|
||
/**
|
||
* 数据限制字段
|
||
*/
|
||
protected $dataLimitField = 'admin_id';
|
||
|
||
public function _initialize()
|
||
{
|
||
parent::_initialize();
|
||
$this->model = new \app\common\model\user4s\User;
|
||
$this->level_model = new \app\common\model\user4s\Level();
|
||
$this->levellog_model = new \app\common\model\user4s\Levellog();
|
||
$this->log_model = new \app\common\model\user4s\Log();
|
||
$this->view->assign("genderdataList", $this->model->getGenderdataList());
|
||
$this->view->assign("statusList", $this->model->getStatusList());
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||
*/
|
||
|
||
|
||
/**
|
||
* 查看
|
||
*/
|
||
public function index()
|
||
{
|
||
//当前是否为关联查询
|
||
$this->relationSearch = true;
|
||
//设置过滤方法
|
||
$this->request->filter(['strip_tags', 'trim']);
|
||
if ($this->request->isAjax()) {
|
||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||
if ($this->request->request('keyField')) {
|
||
return $this->selectpage();
|
||
}
|
||
|
||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||
//var_dump($where);
|
||
$list = $this->model
|
||
->with(['category', 'level'])
|
||
->where($where)
|
||
->order($sort, $order)
|
||
->paginate($limit);
|
||
|
||
foreach ($list as $row) {
|
||
|
||
$row->getRelation('category')->visible(['name']);
|
||
$row->getRelation('level')->visible(['name']);
|
||
}
|
||
|
||
$result = array("total" => $list->total(), "rows" => $list->items());
|
||
|
||
return json($result);
|
||
}
|
||
return $this->view->fetch();
|
||
}
|
||
public function add2()
|
||
{
|
||
if ($this->request->isPost()) {
|
||
//$this->token();
|
||
}
|
||
return parent::add();
|
||
}
|
||
public function add()
|
||
{
|
||
if ($this->request->isPost()) {
|
||
//$this->token();
|
||
}
|
||
if ($this->request->isPost()) {
|
||
$params = $this->request->post("row/a");
|
||
if ($params) {
|
||
$params = $this->preExcludeFields($params);
|
||
|
||
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
||
$params[$this->dataLimitField] = $this->auth->id;
|
||
}
|
||
$result = false;
|
||
$this->model->startTrans();
|
||
try {
|
||
//是否采用模型验证
|
||
if ($this->modelValidate) {
|
||
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
|
||
$this->model->validateFailException(true)->validate($validate);
|
||
}
|
||
$result = $this->model->allowField(true)->save($params);
|
||
//新加用户期初等级
|
||
if($result && $this->model->id){
|
||
$log_res = $this->model->afterlog($this->auth->id,$this->model,$this->model->id);
|
||
if(!$log_res){
|
||
$this->model->rollback();
|
||
$this->error('新增失败');
|
||
}
|
||
}
|
||
//var_dump($result,$this->model->id);
|
||
$this->model->commit();
|
||
} catch (ValidateException $e) {
|
||
$this->model->rollback();
|
||
$this->error($e->getMessage());
|
||
} catch (PDOException $e) {
|
||
$this->model->rollback();
|
||
$this->error($e->getMessage());
|
||
} catch (Exception $e) {
|
||
$this->model->rollback();
|
||
$this->error($e->getMessage());
|
||
}
|
||
if ($result !== false) {
|
||
$this->success();
|
||
} else {
|
||
$this->error(__('No rows were inserted'));
|
||
}
|
||
}
|
||
$this->error(__('Parameter %s can not be empty', ''));
|
||
}
|
||
return $this->view->fetch();
|
||
}
|
||
/**
|
||
* 编辑
|
||
*/
|
||
public function edit($ids = null)
|
||
{
|
||
if ($this->request->isPost()) {
|
||
$this->token();
|
||
}
|
||
$row = $this->model->get($ids);
|
||
if (!$row) {
|
||
$this->error(__('No Results were found'));
|
||
}
|
||
|
||
$adminIds = $this->getDataLimitAdminIds();
|
||
if (is_array($adminIds)) {
|
||
if (!in_array($row[$this->dataLimitField], $adminIds)) {
|
||
$this->error(__('You have no permission'));
|
||
}
|
||
}
|
||
|
||
if ($this->request->isPost()) {
|
||
$params = $this->request->post("row/a");
|
||
}
|
||
return parent::edit($ids);
|
||
}
|
||
/**
|
||
* 充值
|
||
*/
|
||
public function recharge($ids = null)
|
||
{
|
||
if ($this->request->isPost()) {
|
||
$this->token();
|
||
}
|
||
$row = $this->model->get($ids);
|
||
if (!$row) {
|
||
$this->error(__('No Results were found'));
|
||
}
|
||
|
||
$adminIds = $this->getDataLimitAdminIds();
|
||
if (is_array($adminIds)) {
|
||
if (!in_array($row[$this->dataLimitField], $adminIds)) {
|
||
$this->error(__('You have no permission'));
|
||
}
|
||
}
|
||
$all_level = $this->level_model->where('status', 1)->column('id,name');
|
||
|
||
if ($this->request->isPost()) {
|
||
$params = $this->request->post("row/a");
|
||
//var_dump($params);
|
||
$user_data = array(
|
||
'id' => $row->id
|
||
);
|
||
//记录用户充值、积分、等级记录
|
||
$log_data = array(
|
||
'user4s_id' => $row->id,
|
||
'admin_id'=>$this->auth->id
|
||
);
|
||
//余额充值
|
||
if ($params['balance'] > 0) {
|
||
$now = round($params['balance'], 2);
|
||
$end = round($row->balance + $now, 2);
|
||
|
||
$log_data['start'] = $row->balance;
|
||
$log_data['end'] = $end;
|
||
$log_data['state'] = 0;
|
||
$log_data['description'] = $params['description'];
|
||
$this->log_model->isUpdate(false)->save($log_data);
|
||
$user_data['balance'] = $end;
|
||
}
|
||
//积分记录
|
||
if ($params['integral'] > 0) {
|
||
$now = intval($params['integral']);
|
||
$end = intval($row->integral + $now);
|
||
|
||
$log_data['start'] = $row->integral;
|
||
$log_data['end'] = $end;
|
||
$log_data['state'] = 1;
|
||
if($now > 0){
|
||
$log_data['description'] = $params['description'].' 赠送积分:'.$now;
|
||
}else{
|
||
$log_data['description'] = $params['description'].' 系统后台扣除积分'.$now;
|
||
}
|
||
$this->log_model = new \app\common\model\user4s\Log();
|
||
$this->log_model->isUpdate(false)->save($log_data);
|
||
$user_data['integral'] = $end;
|
||
//var_dump($log_data, $user_data);
|
||
}
|
||
//等级记录
|
||
if ($params['level_id'] != $row->level_id && $params['level_id'] > 0) {
|
||
$end = intval($params['level_id']);
|
||
$log_data['start'] = $row->level_id;
|
||
$log_data['end'] = $end;
|
||
|
||
$log_data['description'] = '会员等级变动,由 '.$all_level[$row->level_id].' 更改为:'.$all_level[$end];
|
||
//var_dump($log_data);
|
||
$this->levellog_model->isUpdate(false)->save($log_data);
|
||
$user_data['level_id'] = $end;
|
||
}
|
||
if(isset($user_data['balance']) || isset($user_data['integral']) || isset($user_data['level_id'])){
|
||
//var_dump($user_data);
|
||
//$this->model->get($row->id);
|
||
$this->model->save($user_data, ['id' => $row->id]);
|
||
}
|
||
$this->success();
|
||
}
|
||
//var_dump($col);
|
||
$this->view->assign("all_level", $all_level);
|
||
$this->view->assign('row', $row);
|
||
return $this->view->fetch();
|
||
}
|
||
/**
|
||
* 保险临期用户列表
|
||
*/
|
||
public function expireins()
|
||
{
|
||
//当前是否为关联查询
|
||
$this->relationSearch = true;
|
||
//设置过滤方法
|
||
$this->request->filter(['strip_tags', 'trim']);
|
||
$start_date = strtotime("-1 year");
|
||
$end_date = strtotime("+2 month",$start_date);
|
||
$ins_where = array(
|
||
'insdate'=>['between',[date('Y-m-d',$start_date),date('Y-m-d',$end_date)]]
|
||
);
|
||
if ($this->request->isAjax()) {
|
||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||
$list = $this->model
|
||
->with(['category', 'level'])
|
||
->where($where)
|
||
->where($ins_where)
|
||
->order($sort, $order)
|
||
->paginate($limit);
|
||
//var_dump($this->model->getLastSql());
|
||
foreach ($list as $row) {
|
||
$row->getRelation('category')->visible(['name']);
|
||
$row->getRelation('level')->visible(['name']);
|
||
}
|
||
|
||
$result = array("total" => $list->total(), "rows" => $list->items(),"extend" => ['start_date' => date('Y-m-d',$start_date), 'end_date' => date('Y-m-d',$end_date)]);
|
||
|
||
return json($result);
|
||
}
|
||
return $this->view->fetch();
|
||
}
|
||
/**
|
||
* 续保操作
|
||
*/
|
||
public function renewal($ids = null)
|
||
{
|
||
if ($this->request->isPost()) {
|
||
$this->token();
|
||
}
|
||
$row = $this->model->get($ids);
|
||
if (!$row) {
|
||
$this->error(__('No Results were found'));
|
||
}
|
||
|
||
$adminIds = $this->getDataLimitAdminIds();
|
||
if (is_array($adminIds)) {
|
||
if (!in_array($row[$this->dataLimitField], $adminIds)) {
|
||
$this->error(__('You have no permission'));
|
||
}
|
||
}
|
||
$all_level = $this->level_model->where('status', 1)->column('id,name');
|
||
|
||
if ($this->request->isPost()) {
|
||
$params = $this->request->post("row/a");
|
||
//var_dump($params);
|
||
$user_data = array(
|
||
'id' => $row->id
|
||
);
|
||
//记录用户充值、积分、等级记录
|
||
$log_data = array(
|
||
'user4s_id' => $row->id,
|
||
'admin_id'=>$this->auth->id
|
||
);
|
||
//余额充值
|
||
if ($params['balance'] > 0) {
|
||
$now = round($params['balance'], 2);
|
||
$end = round($row->balance + $now, 2);
|
||
|
||
$log_data['start'] = $row->balance;
|
||
$log_data['end'] = $end;
|
||
$log_data['state'] = 0;
|
||
$log_data['description'] = $params['description'];
|
||
$this->log_model->isUpdate(false)->save($log_data);
|
||
$user_data['balance'] = $end;
|
||
}
|
||
//积分记录
|
||
if ($params['integral'] > 0) {
|
||
$now = intval($params['integral']);
|
||
$end = intval($row->integral + $now);
|
||
|
||
$log_data['start'] = $row->integral;
|
||
$log_data['end'] = $end;
|
||
$log_data['state'] = 1;
|
||
if($now > 0){
|
||
$log_data['description'] = $params['description'].' 赠送积分:'.$now;
|
||
}else{
|
||
$log_data['description'] = $params['description'].' 系统后台扣除积分'.$now;
|
||
}
|
||
$this->log_model = new \app\common\model\user4s\Log();
|
||
$this->log_model->isUpdate(false)->save($log_data);
|
||
$user_data['integral'] = $end;
|
||
//var_dump($log_data, $user_data);
|
||
}
|
||
//等级记录
|
||
if ($params['level_id'] != $row->level_id && $params['level_id'] > 0) {
|
||
$end = intval($params['level_id']);
|
||
$log_data['start'] = $row->level_id;
|
||
$log_data['end'] = $end;
|
||
|
||
$log_data['description'] = '会员等级变动,由 '.$all_level[$row->level_id].' 更改为:'.$all_level[$end];
|
||
//var_dump($log_data);
|
||
$this->levellog_model->isUpdate(false)->save($log_data);
|
||
$user_data['level_id'] = $end;
|
||
}
|
||
if(isset($user_data['balance']) || isset($user_data['integral']) || isset($user_data['level_id'])){
|
||
//var_dump($user_data);
|
||
//$this->model->get($row->id);
|
||
$this->model->save($user_data, ['id' => $row->id]);
|
||
}
|
||
$this->success();
|
||
}
|
||
//var_dump($col);
|
||
$this->view->assign("all_level", $all_level);
|
||
$this->view->assign('row', $row);
|
||
return $this->view->fetch();
|
||
}
|
||
}
|