123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use app\model\Account;
- use app\model\AccountCompany;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- //【公司账号管理】
- class UserCompanyBasic extends BaseController
- {
- //列表
- public function getList()
- {
- $post = $this->request->only(['nickname' => '', 'username' => '', 'status' => '', 'page' => 1, 'size' => 10, 'islevel' => 1,'level'=>'2,3','companyNo'=>''], 'post');
- $condition = [['a.is_del', '=', 0]];
- if ($post['nickname'] != '') $condition[] = ['nickname', 'like', "%{$post['nickname']}%"];
- if ($post['username'] != '') $condition[] = ['username', 'like', "%{$post['username']}%"];
- if ($post['status'] != '') $condition[] = ['a.status', '=', $post['status']];
- if ($post['level'] != '') $condition[] = ['a.level', 'in', $post['level']];
- // if ($post['islevel'] == 1) $condition[] = ['a.level', '=', 1];
- // else $condition[] = ['a.level', '<>', 1];
- if ($post['companyNo'] !== '') {
- //根据公司筛选账号
- $rs = Db::name('account_company')
- ->where(['is_del' => 0, 'status' => 1])
- ->whereLike('companyCode', '%' . $post['companyNo'] . '%')
- ->column('account_id');
- $condition[] = ['a.id', 'in', $rs];
- }
- $count = Db::name('account')
- ->alias('a')
- ->leftJoin('user b', 'a.id=b.account_id and b.status=1')
- ->where($condition)
- ->count('a.id');
- $list = Db::name('account')
- ->alias('a')
- ->field('a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime,a.level')
- ->leftJoin('user b', 'a.id=b.account_id and b.status=1')
- ->where($condition)
- ->page($post['page'], $post['size'])
- ->append(['plat', 'company_relaton'])
- // ->withAttr('plat', function ($val, $da) {
- // return Db::name('account_plat')
- // ->alias('a')
- // ->leftJoin('platform b', 'a.plat_code=b.plat_code and b.is_del=0 and b.status=1')
- // ->where(['a.status' => 1, 'a.is_del' => 0, 'a.account_id' => $da['id']])
- // ->field('a.plat_code,plat_name')
- // ->select()
- // ->toArray();
- // })
- ->withAttr('company_relaton', function ($val, $da) {
- return Db::name('account_company')
- ->where(['account_id' => $da['id'], 'is_del' => 0])
- ->field('companyCode,companyName,company_type,is_main,status')
- ->select()
- ->toArray();
- })
- ->order('a.addtime desc')
- ->select()
- ->toArray();
- return json_show(0, '获取成功', ['list' => $list, 'count' => $count]);
- }
- //注册
- public function add()
- {
- $post = $this->request->only(['nickname' => '', 'mobile' => '', 'email' => '', 'level' => 2, 'companyArr' => []], 'post', 'trim');
- $validate = Validate::rule([
- 'nickname|真实姓名' => 'require|min:2|max:200',
- 'mobile|手机号' => 'require|number|length:11|mobile',
- 'email|邮箱' => 'email',
- 'level|账号等级' => 'number|in:1,2',
- 'companyArr|关联业务公司' => 'require|array|max:100',
- ]);
- if ($validate->check($post) == false) return json_show(1004, $validate->getError());
- Db::startTrans();
- try {
- $uiq = Db::table('sys_account')
- ->field('id')
- ->where(['mobile' => $post['mobile']])
- ->findOrEmpty();
- if ($uiq) throw new Exception('手机号已注册!');
- $date = date('Y-m-d H:i:s');
- $salt = makeSalt();
- $password = sha1('dingding123' . $salt);
- $data = [
- 'username' => $post['mobile'],
- 'password' => $password,
- 'salt' => $salt,
- 'status' => 1,
- 'mobile' => $post['mobile'],
- 'level' => $post['level'],
- 'source' => 'paltadd',
- 'addtime' => $date,
- 'updatetime' => $date
- ];
- $reuslt = Db::table('sys_account')->insertGetId($data);
- if ($reuslt) {
- $data = [
- 'nickname' => $post['nickname'],
- 'mobile' => $post['mobile'],
- 'email' => $post['email'],
- 'portrait' => '',
- 'sex' => 1,
- 'post' => '',
- 'department' => '',
- 'account_id' => $reuslt,
- 'status' => 1,
- 'addtime' => $date,
- 'updatetime' => $date
- ];
- $user = Db::table('sys_user')->insert($data);
- if ($user != false) {
- if (!empty($post['companyArr'])) {
- $company_insert = [];
- $acount = new AccountCompany();
- foreach ($post['companyArr'] as $company) {
- $company_insert[] = [
- 'account_id' => $reuslt,
- 'companyCode' => $company['companyCode'],
- 'companyName' => $company['companyName'],
- 'company_type' => $company['company_type'],
- 'is_main' => $company['is_main'],
- 'status' => 1,
- 'is_del' => 0,
- 'addtime' => $date,
- 'updatetime' => $date,
- ];
- }
- $acount->saveAll($company_insert);
- }
- Db::commit();
- return json_show(0, '账户注册成功', ['uid' => $reuslt]);
- }
- } else throw new Exception();
- } catch (Exception $e) {
- Db::rollback();
- return json_show(1002, '账户注册失败,' . $e->getMessage());
- }
- }
- //删除
- public function delete()
- {
- $param = $this->request->only(['ids'], 'post', 'trim');
- $val = Validate::rule([
- 'ids' => 'require|array|max:100',
- ]);
- if (!$val->check($param)) return json_show(1004, $val->getError());
- Db::startTrans();
- try {
- Db::name('account')
- ->where(['is_del' => 0])
- ->whereIn('id', $param['ids'])
- ->update([
- 'is_del' => 1,
- 'updatetime' => date('Y-m-d H:i:s')
- ]);
- Db::name('account_company')
- ->where(['is_del' => 0])
- ->whereIn('account_id', $param['ids'])
- ->update([
- 'is_del' => 1,
- 'updatetime' => date('Y-m-d H:i:s')
- ]);
- Db::commit();
- return json_show(0, '删除成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1005, '删除失败,' . $exception->getMessage());
- }
- }
- //编辑
- public function update()
- {
- $post = $this->request->only(['id', 'itemid'], 'post');
- $validate = Validate::rule([
- 'id|主键ID' => 'require|number|gt:0',
- 'itemid|部门ID' => 'require|number|gt:0',
- ]);
- if ($validate->check($post) == false) return json_show(1004, $validate->getError());
- $account = Db::name('account')
- ->field('id')
- ->where([['id', '=', $post['id']], ['is_del', '=', 0]])
- ->findOrEmpty();
- if (empty($account)) return json_show(1003, '账户不存在');
- $tmp = Db::name('account_item')
- ->field('id')
- ->where(['account_id' => $post['id']])
- ->findOrEmpty();
- $date = date('Y-m-d H:i:s');
- if (empty($tmp)) $rs = Db::name('account_item')
- ->insert([
- 'account_id' => $post['id'],
- 'itemid' => $post['itemid'],
- 'addtime' => $date,
- 'updatetime' => $date,
- ]);
- else $rs = Db::name('account_item')
- ->where(['id' => $tmp['id']])
- ->update([
- 'itemid' => $post['itemid'],
- 'updatetime' => $date
- ]);
- return $rs ? json_show(0, '信息修改成功') : json_show(0, '信息修改失败');;
- // $accountinfo = Db::name('user')
- // ->field('id')
- // ->where([['account_id', '=', $post['id']]])
- // ->findOrEmpty();
- // if (empty($accountinfo)) return json_show(1003, '账户信息不存在');
- // Db::startTrans();
- // try {
- // Db::name('user')
- // ->where($accountinfo)
- // ->update([
- // 'nickname' => $post['nickname'],
- // 'mobile' => $post['mobile'],
- // 'email' => $post['email'],
- // 'portrait' => $post['portrait'],
- // 'sex' => $post['sex'],
- // 'updatetime' => date('Y-m-d H:i:s')
- // ]);
- //
- // Db::name('account')
- // ->where('id', $post['id'])
- // ->update([
- // 'id' => $post['id'],
- // 'mobile' => $post['mobile'],
- // 'username' => $post['mobile'],
- // 'updatetime' => date('Y-m-d H:i:s'),
- // ]);
- // Db::commit();
- // return json_show(0, '信息修改成功');
- // } catch (Exception $exception) {
- // Db::rollback();
- // return json_show(1005, '信息修改失败,' . $exception->getMessage());
- // }
- }
- //启禁用
- public function status()
- {
- $post = $this->request->only(['id', 'status'], 'post', 'trim');
- $validate = Validate::rule(['id|主键ID' => 'require|number|gt:0', 'status|状态' => 'require|number|in:0,1']);
- if ($validate->check($post) == false) return json_show(1004, $validate->getError());
- Db::startTrans();
- try {
- $account = Account::where(['id' => $post['id'], 'is_del' => 0])
- ->field('id,status')
- ->findOrEmpty();
- if (empty($account)) throw new Exception('账户不存在');
- if ($account['status'] == $post['status']) throw new Exception('重复操作');
- $message = $post['status'] == 1 ? '启用' : '禁用';
- Db::name('account')
- ->where('status', '<>', $post['status'])
- ->where(['id' => $post['id'], 'is_del' => 0])
- ->save(['status' => $post['status'], 'updatetime' => date('Y-m-d H:i:s')]);
- Db::name('account_company')
- ->where('status', '<>', $post['status'])
- ->where(['account_id' => $post['id'], 'is_del' => 0])
- ->save(['status' => $post['status'], 'updatetime' => date('Y-m-d H:i:s')]);
- Db::commit();
- return json_show(0, "账户{$message}成功");
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1004, '操作失败,' . $exception->getMessage());
- }
- }
- //详情
- public function info()
- {
- $id = $this->request->post('id/d', 0, 'trim');
- if ($id == 0) return json_show(1003, "参数 id 不能为空");
- $list = Db::name('account')
- ->alias('a')
- ->field('a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime,c.itemid,d.name item_name,d.companyNo item_companyNo')
- ->leftJoin('user b', 'a.id=b.account_id')
- ->leftJoin('account_item c', 'c.account_id=a.id')
- ->leftJoin('company_item d', 'd.id=c.itemid')
- ->append(['plat', 'company_relaton','item_list'])
- // ->withAttr('plat', function ($val, $da) {
- // return Db::name('account_plat')
- // ->alias('a')
- // ->leftJoin('platform b', 'a.plat_code=b.plat_code and b.status=1')
- // ->where(['a.status' => 1, 'a.is_del' => 0, 'a.account_id' => $da['id']])
- // ->column('a.plat_code,plat_name');
- // })
- ->withAttr('company_relaton', function ($val, $da) {
- return Db::name('account_company')
- ->where(['account_id' => $da['id'], 'is_del' => 0, 'status' => 1])
- ->column('companyCode,companyName,company_type,is_main,status');
- })
- ->withAttr('item_list',function($val,$da){
- return get_part($da['itemid'] ?? 0);
- })
- ->where(['a.id' => $id, 'a.is_del' => 0])
- ->findOrEmpty();
- if (empty($list)) return json_show(1004, '未找到用户信息');
- // $list['plat'] = Db::name('account_plat')->alias('a')
- // ->leftJoin('platform b', 'a.plat_code=b.plat_code and b.status=1')
- // ->where(['a.status' => 1, 'a.is_del' => 0, 'a.account_id' => $list['id']])->column('a.plat_code,plat_name');
- // $list['company_relaton'] = Db::name('account_company')->where(['account_id' => $list['id'], 'is_del' => 0, 'status' => 1])
- // ->column('companyCode,companyName,company_type,is_main,status');
- return json_show(0, '获取成功', $list);
- }
- }
|