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); } }