You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

290 lines
8.9 KiB

<?php
namespace app\admin\controller\project;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\common\model\CompanyMoneyLog;
use app\common\model\ProjectCompanyUser;
use app\common\model\User;
use app\common\model\UserCompany;
use think\Db;
use think\exception\PDOException;
/**
* 企业用户管理
*
* @icon fa fa-circle-o
*/
class Company extends ProjectBase
{
/**
* Company模型对象
* @var \app\admin\model\project\Company
*/
protected $model = null;
/**
* @var mixed
*/
private $company_id = 0;
public $noNeedCompany = ['index', 'add', 'edit', 'del', 'detail', 'audit'];
public $needCompany = ['info'];
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\project\Company;
$this->view->assign("auditStatusList", $this->model->getAuditStatusList());
if ($this->auth->isCompanyAdmin()) {
$this->company_id = $this->auth->company_id;
}
}
public function del($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $ids ?: $this->request->post("ids");
if (empty($ids)) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->select();
$count = 0;
Db::startTrans();
try {
foreach ($list as $item) {
$count += $item->delete();
}
//删除企业同时删除企业账号和企业绑定用户
ProjectCompanyUser::where('company_id', 'in', $ids)->delete();
//删除企业角色和角色权限
AuthGroup::where('company_id', 'in', $ids)->delete();
$user_id = User::where('company_id', $ids)->value('id');
AuthGroupAccess::where('uid', 'in', $user_id)->delete();
//删除企业账号
User::where('company_id', 'in', $ids)->update(['company_id'=>0]);
Db::commit();
} catch (PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success();
}
$this->error(__('No rows were deleted'));
}
public function add()
{
if (false === $this->request->isPost()) {
return $this->view->fetch();
}
$params = $this->request->post('row/a');
if (empty($params)) {
$this->error(__('Parameter %s can not be empty', ''));
}
//后台添加默认审核通过
$params['audit_status'] = 1;
//验证数据
$result = $this->model->validate('Company.add')->allowField(true)->save($params);
if ($result === false) {
$this->error($this->model->getError());
}
//添加企业管理员账号
$params['company_id'] = $this->model->id;
UserCompany::saveCompanyAdmin($params);
$this->success();
}
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
if (false === $this->request->isPost()) {
$this->view->assign('row', $row);
return $this->view->fetch();
}
$params = $this->request->post('row/a');
if (empty($params)) {
$this->error(__('Parameter %s can not be empty', ''));
}
//这里需要针对username和email做唯一验证
$validate = \think\Loader::validate('Company');
$validate->rule([
'contact_phone' => 'regex:1[3-9]\d{9}|unique:user_company,contact_phone,' . $row->id,
'contact_email' => 'require|email|unique:user_company,contact_email,' . $row->id,
'account' => 'require|regex:\w{3,30}|unique:user_company,account,' . $row->id,
// 'pwd' => 'regex:\S{32}',
]);
$params = $this->preExcludeFields($params);
$result = $row->validate('Company.edit')->save($params);
if (false === $result) {
$this->error($row->getError());
}
//更新管理员账号
$params['company_id'] = $ids;
UserCompany::saveCompanyAdmin($params, 'update');
$this->success();
}
/**
* 企业信息
* @return string
*/
public function info() {
$row = $this->model->index($this->company_id);
if (!$row) {
$this->error(__('No Results were found'),url('index'));
}
$this->view->assign('row', $row);
return $this->view->fetch();
}
/**
* 选择账号
* @return string|null
*/
public function chooseUser(){
if ($this->request->isPost()) {
//当前页
$page = $this->request->request("pageNumber");
//分页大小
$pagesize = $this->request->request("pageSize");
//主键
$primarykey = $this->request->request("keyField");
//主键值
$primaryvalue = $this->request->request("keyValue");
//搜索字段
$search = $this->request->request("nickname");
//如果发送的来源是Selectpage,则转发到Selectpage
if ($primarykey) {
$user_ids = ProjectCompanyUser::getUserIds($this->company_id);
//获取当前企业所有用户账号
$list = User::where('id', 'in', $user_ids);
if ($primaryvalue) {
$list->where([$primarykey => ['in', $primaryvalue]]);
$pagesize = 999999;
}
if ($search) {
$list->where('nickname', "like", "%{$search}%");
}
$data = $list->field(['id', 'nickname'])
->order('id', 'DESC')
->page($page,$pagesize)
->select();
$total = $data->count();
return json(['list' => $data, 'total' => $total]);
}
$user_ids = $this->request->post('user_ids');
$user_ids = explode(',', $user_ids);
//不是企业管理员无法添加账号
// if (!$this->auth->isCompanyAdmin()) {
// $this->error('无权限');
// }
//更新企业支付账号
$this->model->where('id', $this->company_id)->update(['pay_account'=>implode(',', $user_ids)]);
return $this->success('添加成功');
}
//获取当前账号所有企业支付账号
$pay_account = $this->model->get($this->company_id)['pay_account'];
$this->view->assign('pay_account',$pay_account);
return $this->view->fetch();
}
/**
* 资金明细
* @return string
*/
public function moneyLog($ids = null) {
if ($ids) {
$this->company_id = $ids;
}
if (false === $this->request->isAjax()) {
$this->view->assign('company_id', $this->company_id);
return $this->view->fetch();
}
$userIds = User::where('company_id',$this->company_id)->column('id');
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
$list = CompanyMoneyLog::where('user_id','in', $userIds);
$list = $list
->where($where)
->order($sort, $order)
->paginate($limit);
$data = $list->items();
foreach ($data as $row) {
$row['create_time'] = date('Y-m-d H:i:s',$row['createtime']);
$row['user_id'] = User::get($row['user_id'])->nickname;
}
$result = ['total' => $list->total(), 'rows' => $data];
return json($result);
}
/**
* 充值金额
* @param $ids
* @return string
*/
public function rechargeMoney($ids = null) {
if ($this->request->isPost()) {
$money = $this->request->post('money');
$user_id = User::where('company_id', $ids)->value('id');
if (!empty($user_id)) {
if ($money > 0) {
$remark = '充值';
} else {
$remark = '消费';
}
UserCompany::money($money, $user_id, $ids, $remark);
}
$this->success('充值成功');
}
return $this->view->fetch();
}
/** 详情
* @param $ids
* @return
*/
public function detail($ids = null) {
$row = $this->model->index($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$this->view->assign('row', $row);
return $this->view->fetch();
}
}