123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944 |
- <?php
- declare (strict_types=1);
- namespace app\admin\controller;
- use app\admin\common\User as CommonUser;
- use think\facade\Db;
- use app\admin\model\ActionLog;
- use think\facade\Validate;
- //运营账户
- class User extends Base
- {
- //登录
- public function login()
- {
- $param = $this->request->only(['username', 'password'], 'post', 'trim');
- $val = Validate::rule([
- 'username|用户名' => 'require',
- 'password|密码' => 'require',
- ]);
- if (!$val->check($param)) return json_show(1004, $val->getError());
- $user = CommonUser::getIns();
- $rs = $user->login(array_merge($param, ['plat_code' => 'cx']));
- $rs = json_decode($rs, true);
- if (!isset($rs['code']) || $rs['code'] != 0) return json_show(1004, $rs['message']);
- switch ($rs['data']['level']){
- case 1:
- $role = [
- 'role_name' => '',
- 'role_level' => $rs['data']['level'],
- 'role_id' => env('default_admin_addcount_role_id',1),//指定一个默认角色id,1超管
- ];
- break;
- case 2:
- case 3:
- //获取默认公司
- $default = array_column($rs['data']['company_relaton'], 'companyNo', 'is_main');
- if (!isset($default) || !isset($default[1])) return json_show(1004, '没有默认公司,无法登录');
- $role = Db::name('user_role')
- ->alias('a')
- ->field('a.roleid role_id,b.role_name,b.role_level')
- ->leftJoin('role b', 'b.id=a.roleid')
- ->where(['a.uid' => $rs['data']['id'], 'a.companyNo' => $default[1], 'a.is_del' => 0])
- ->findOrEmpty();
- if (empty($role)) return json_show(1004, '没有默认角色,无法登录,请联系管理员');
- break;
- default:
- return json_show(1004, '没有默认角色,无法登录,请联系管理员');
- }
- $data = array_merge($rs['data'], [
- 'role_name' => $role['role_name'],
- 'role_level' => $role['role_level'],
- 'role_id' => $role['role_id'],
- ]);
- return json_show(0, '登录成功', $data);
- }
- public function list()
- {
- $param = $this->request->only(['nickname' => '', 'username' => '', 'status' => '', 'page' => 1, 'size' => 10, 'uid' => '', 'nuid' => '', 'companyNo' => ''], 'post', 'trim');
- $param['level'] = 1;//只筛选超管用户
- $userCommon = CommonUser::getIns();
- $rs = $userCommon->GetUserlist($param);
- $rs = json_decode($rs, true);
- return json_show($rs['code'], $rs['message'], $rs['data']);
- }
- //删除
- 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());
- if (in_array($this->uid, $param['ids'])) return json_show(1004, '自己不能删除自己');
- $userCommon = CommonUser::getIns();
- $rs = $userCommon->userDelete($param);
- $rs = json_decode($rs, true);
- return json_show($rs['code'], $rs['message'], $rs['data']);
- }
- //详情
- public function userInfo()
- {
- $userCommon = CommonUser::getIns();
- $rs = json_decode($userCommon->GetUserInfo(['id' => $this->uid]), true);
- $rs['data']['level'] = $this->level;
- return json_show($rs['code'], $rs['message'], $rs['data']);
- }
- /**
- * @param token
- * @param passwd
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function resetPwd()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return error_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return error_show($effetc['code'], $effetc['message']);
- }
- $newPwd = isset($post['newpwd']) ? trim($post['newpwd']) : "";
- if ($newPwd == "") {
- return error_show(1001, '新密码不能为空');
- }
- $oldpwd = isset($post['oldpwd']) ? trim($post['oldpwd']) : "";
- if ($oldpwd == "") {
- return error_show(1002, "原密码不能为空");
- }
- $data = resetpasswd($token, ['newpwd' => $newPwd, 'oldpwd' => $oldpwd]);
- if (empty($userinfo) || $userinfo['code'] != 0) {
- return error_show($userinfo['code'], $userinfo['message']);
- } else {
- return app_show(0, "密码修改成功");
- }
- }
- //获取所有账号
- public function userAll()
- {
- $post = $this->request->post();
- // $token = isset($post['token']) ? trim($post['token']) : "";
- // if ($token == "") {
- // return app_show(101, 'token不能为空');
- // }
- // $effetc = VerifyTokens($token);
- // if (!empty($effetc) && $effetc['code'] != 0) {
- // return app_show($effetc['code'], $effetc['message']);
- // }
- // if (isset($post['status']) && $post['status'] !== '') {
- // $uid = Db::name("user_role")->where(["status" => 1, "is_del" => 0])->column("uid");
- // if ($post['status'] == 1) {
- // $post['uid'] = $uid;
- // } else {
- // $post['nouid'] = $uid;
- // }
- // unset($post['status']);
- // }
- $post['level'] = 2;
- $userCommon = \app\admin\common\User::getIns();
- $userlist = $userCommon->handle('userall', $post);
- // if (!isset($userlist['code']) || $userlist['code'] != 0)
- return json_show($userlist['code'], $userlist['message'], $userlist['data']);
- // $userinfo = GetAccountall($token, $post);
- // if (empty($userinfo) || $userinfo['code'] != 0) {
- // return app_show(1002, "员工信息不存在");
- // }
- // $all_uid = array_column($userlist['data'],'id');
- //所有角色
- // $role = Db::name("user_role")
- // ->alias('a')
- // ->leftJoin('role b', "a.roleid = b.id")
- // ->where("a.uid", "in", $all_uid)
- // ->column('a.roleid,b.role_name','a.uid');
- // $list = [];
- // foreach ($userlist['data'] as &$value) {
- // $role = Db::name("user_role")
- // ->alias('a')
- // ->leftJoin('role b', "a.roleid = b.id")
- // ->where("a.uid", "=", $value['id'])
- // ->field("roleid,role_name,a.status")
- // ->find();
- // $value['roleid'] = $role[$value['id']]['roleid'] ?? 0;
- // $value['role_name'] = $role[$value['id']]['role_name'] ?? '';
- // $depart = Db::name("depart_user")->where([["uid", "=", $value['id']], ["is_del", "=", 0]])->column("itemid");
- // $value['depart'] = "";
- // $value['status'] = isset($role['status']) ? $role['status'] : '0';
- // if (!empty($depart)) {
- // $deparname = Db::name("company_item")->where(["id" => $depart, "is_del" => 0])->field("id,name,depart_link")
- // ->select()->toArray();
- //
- // array_walk($deparname, function (&$v) {
- // $v = array_filter(explode("-", $v['depart_link']));
- // });
- // $value['depart'] = $deparname;
- // }
- // $list[] = $value;
- // }
- // return app_show(0, "获取成功", $userlist['data']);
- }
- /**
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function userList()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return app_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return app_show($effetc['code'], $effetc['message']);
- }
- $itemid = isset($post['itemid']) && $post['itemid'] !== "" ? intval($post['itemid']) : "";
- if ($itemid !== "") {
- $uids = Db::name("depart_user")->where([["itemid", "=", $itemid], ["is_del", "=", 0]])->column("uid");
- $post['uid'] = $uids;
- }
- if (isset($post['status']) && $post['status'] !== '') {
- $uid = Db::name("user_role")->where(["status" => 1, "is_del" => 0])->column("uid");
- if ($post['status'] == 1) {
- $post['uid'] = isset($uids) ? array_unique(array_merge($uids, $uid)) : $uid;
- } else {
- $post['nouid'] = $uid;
- }
- unset($post['status']);
- }
- $userinfo = GetList($token, $post);
- if (empty($userinfo) || $userinfo['code'] != 0) {
- return app_show($userinfo['code'], $userinfo['msg']);
- }
- $data = $userinfo['data']['list'];
- $list = [];
- foreach ($data as $value) {
- $role = Db::name("user_role")->alias('a')->leftJoin('role b', "a.roleid = b.id")->where("a.uid", "=", $value['id'])
- ->field("roleid,role_name,a.status")->find();
- $value['roleid'] = isset($role['roleid']) ? $role['roleid'] : "";
- $value['role_name'] = isset($role['role_name']) ? $role['role_name'] : "";
- $depart = Db::name("depart_user")->where([["uid", "=", $data['uid']], ["is_del", "=", 0]])->column("itemid");
- $value['depart'] = "";
- $value['status'] = isset($role['status']) ? $role['status'] : '0';
- if (!empty($depart)) {
- $deparname = Db::name("company_item")->where(["id" => $depart, "is_del" => 0])->field("id,name,depart_link")
- ->select()->toArray();
- $value['depart'] = $deparname;
- $value['depart_code'] = [];
- if (!empty($deparname)) {
- foreach ($deparname as $item) {
- $value['depart_code'][] = array_filter(explode("-", $item['depart_link']));
- }
- }
- }
- $list[] = $value;
- }
- return app_show(0, "获取成功", ["list" => $list, "count" => $userinfo['data']["count"]]);
- }
- //账户角色列表
- public function userRoleList()
- {
- $post = $this->request->only(["token" => "", "nickname" => "", "relaComNo" => "", "level" => "", "username" => "", "companyNo" => "", "status" => "", "page" => 1, "size" => 10], "post");
- if ($this->level == 2) {
- // $companyNo = $post['relaComNo'] ?? "";
- $post['companyNo'] = $post['relaComNo'] ?? "";
- }
- $where = [["a.is_del", "=", 0]];
- unset($post['relaComNo']);
- if ($post['companyNo'] != "") {
- $where[] = ["a.companyNo", "=", $post['companyNo']];
- }
- if ($post['status'] !== '') {
- $uid = Db::name("user_role")
- ->alias("a")
- ->where(["status" => 1, "is_del" => 0])
- ->where($where)
- ->column("uid");
- if ($post['status'] == 1) {
- $where[] = ["a.status", "=", 1];
- $post['uid'] = $uid;
- } else $post['nuid'] = $uid;
- unset($post['status']);
- }
- $userCommon = CommonUser::getIns();
- $userinfo = $userCommon->GetList($post);
- $userinfo = json_decode($userinfo, true);
- if (empty($userinfo) || $userinfo['code'] != 0) {
- return app_show($userinfo['code'], $userinfo['msg']);
- }
- $data = $userinfo['data']['list'];
- $list = [];
- foreach ($data as $value) {
- $tmp_where=["uid" => $value['id'],"is_del" => 0];
- if ($value['level'] != 1) $tmp_where['a.companyNo'] = $value['companyCode'];
- $roleArr = Db::name("user_role")
- ->alias("a")
- ->leftJoin("role b", "a.roleid=b.id")
- ->where($tmp_where)
- ->field("role_name,roleid,a.status")
- ->order(['a.companyNo'=>'desc'])
- ->findOrEmpty();
- $value['roleid'] = $roleArr['roleid'] ?? "";
- $value['status'] = $value['status'] == 1 ? ($roleArr['status'] ?? 0) : 0;
- $value["role_name"] = $roleArr['role_name'] ?? "";
- $value["role_level"] = $value['level'] ?? "";
- $list[] = $value;
- }
- return app_show(0, "获取成功", ["list" => $list, "count" => $userinfo['data']["count"]]);
- }
- //账号角色修改
- public function setRole()
- {
- //if ($this->level == 1) return json_show(1004, '超管账号不允许设置角色');
- $post = $this->request->only(["id", "roleid", 'companyNo'], "post", "trim");
- //待修改的账户详情
- $userCommon = \app\admin\common\User::getIns();
- $userinfo = $userCommon->handle('userinfo', ['id' => $post['id']]);
- if (!isset($userinfo['code']) || $userinfo['code'] != 0) return json_show($userinfo['code'], $userinfo['message'], $userinfo['data']);
- $post['level'] = $userinfo['data']['level'];
- if ($post['level'] == 1) return json_show(1004, '超管账号不允许设置角色');
- $valide = Validate::rule([
- "id|账户ID" => "require|number|gt:0",
- // "nickname|名称" => "require|max:255",
- // "mobile|手机号" => "require|mobile",
- // "relaComNo|业务公司" => "max:255",
- "roleid|角色ID" => "require|number|gt:0",
- "companyNo|业务公司" => "requireIf:level,2|max:255",
- ]);
- if ($valide->check($post) == false) return json_show(1004, $valide->getError());
- if ($post['roleid'] != 0) {
- $roleinfo = Db::name("role")
- ->field('id,status')
- ->where("id", $post['roleid'])
- ->find();
- if (empty($roleinfo)) return json_show(1002, "角色不存在");
- if ($roleinfo['status'] == 0) return json_show(1002, "角色已禁用");
- // if ($roleinfo['level'] == 1) return json_show(1002, "超管级别角色不允许设置");
- }
- // if ($this->level == 1) $companyNo = isset($post['companyNo']) && $post['companyNo'] !== '' ? trim($post['companyNo']) : "";
- // else $companyNo = isset($post['relaComNo']) && $post['relaComNo'] !== '' ? trim($post['relaComNo']) : "";
- // if ($companyNo == "") return json_show(1004, "业务公司编号不能为空");
- // $company = Db::name("supplier_info")->where(["code" => $companyNo])->find();
- // if ($company == false) return json_show(1004, "业务公司不存在");
- // $isSuper = Db::name("user_role")->alias("a")
- // ->leftJoin("role b", "a.roleid=b.id")
- // ->where(["a.uid" => $post['id'], "a.is_del" => 0, "b.level" => 1])
- // ->findOrEmpty();
- // if (!empty($isSuper)) return json_show(1002, "超管账户不允许设置其他角色");
- $where = [
- ['uid', '=', $post['id']],
- ['is_del', '=', 0],
- ];
- $is_main = 1;
- $company_type = 0;
- if ($post['companyNo'] != '') {
- $tmp = $userCommon->handle('userCompanyList', ['account_id' => $post['id'], 'companyCode' => $post['companyNo']]);
- if (!isset($tmp['code']) || $tmp['code'] != 0) return json_show($tmp['code'], $tmp['message'], $tmp['data']);
- if ($tmp['data']['count'] != 1) return json_show(1005, '尚未绑定该公司');
- $is_main = $tmp['data']['list'][0]['is_main'];
- $company_type = $tmp['data']['list'][0]['company_type'];
- $where[] = ['companyNo', '=', $post['companyNo']];
- }
- $userrole = Db::name("user_role")
- ->field('id')
- ->where($where)
- ->find();
- // $ismain = Db::name("user_role")->where(["uid" => $post['id'], "is_main" => 1, "is_del" => 0])->find();
- Db::startTrans();
- try {
- if (empty($userrole)) {
- $roledata = [
- "uid" => $post['id'],
- "roleid" => $post['roleid'],
- "nickname" => $userinfo['data']['nickname']??"",
- "companyNo" => $post['companyNo'] != '' ? $post['companyNo'] : '',
- "company_type" => $company_type,
- "is_main" => $is_main,
- "status" => 1,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- // $companyarr = [
- // [
- // "companyCode" => $company["code"],
- // "companyName" => $company["name"],
- // "company_type" => $company["type"] == 3 ? 2 : 1,
- // "is_main" => $ismain ? 0 : 1
- // ]
- // ];
- // $companyinfo = ["id" => $post['id'], "company" => $companyarr, "token" => $post['token']];
- // $userCommon = \app\admin\common\User::getIns();
- // $companyset = $userCommon->handle('setcompany', $companyinfo);
- // if (!isset($companyset['code']) || $companyset['code'] != 0) return json_show(1004, $companyset['message']);
- $userrole['id'] = Db::name("user_role")->insertGetId($roledata);
- } else {
- // $userrole['roleid'] = $post['roleid'];
- // $userrole['nickname'] = $post['nickname'];
- // $userrole['companyNo'] = $companyNo;
- // $userrole['company_type'] = $company["type"] == 3 ? 2 : 1;
- // $userrole['updatetime'] = date("Y-m-d H:i:s");
- $up = Db::name("user_role")
- ->where('id', $userrole['id'])
- ->update([
- "company_type" => $company_type,
- "nickname" => $userinfo['data']['nickname']??"",
- "is_main" => $is_main,
- 'roleid' => $post['roleid'],
- 'updatetime' => date("Y-m-d H:i:s"),
- ]);
- }
- //设置默认角色
- if ($is_main == 1) {
- Db::name('user_role')
- ->where(["uid" => $post['id'], "is_del" => 0])
- ->where('id', '<>', $userrole['id'])
- ->update(['is_main' => 0]);
- }
- // $saveinfo = [
- // "id" => $post['id'],
- // "nickname" => $post['nickname'],
- // "mobile" => $post['mobile'],
- // "token" => $post['token']
- // ];
- // $saev = resetinfo($saveinfo);
- // $userCommon = \app\admin\common\User::getIns();
- // $saev = $userCommon->handle('usersave', $saveinfo);
- // if (!isset($saev['code']) || $saev['code'] != 0) {
- // return json_show(1004, $saev['message']);
- // }
- // if ($up == false) {
- // Db::rollback();
- // return json_show(1004, '用户信息更新失败');
- // }
- Db::commit();
- return json_show(0, '操作成功');
- } catch (\Exception $e) {
- Db::rollback();
- return json_show(1004, '操作失败,' . $e->getMessage());
- }
- }
- //修改
- public function UserSave()
- {
- /**
- * $post = $this->request->post();
- * $toke = isset($post['token']) ? trim($post['token']) : "";
- * if ($toke == "") {
- * return app_show(100, "token不能为空");
- * }
- * $verify = VerifyTokens($toke);
- * if ($verify['code'] != 0) {
- * return app_show($verify['code'], $verify['message']);
- * }
- * $userid = isset($post['id']) && $post['id'] !== "" ? intval($post['id']) : "";
- * if ($userid == "") {
- * return error_show(1004, "参数id 不能为空");
- * }
- *
- * $nickname = isset($post['nickname']) ? trim($post['nickname']) : "";
- * if ($nickname == "") {
- * return app_show(1001, "昵称不能为空");
- * }
- * $mobile = isset($post['mobile']) ? trim($post['mobile']) : "";
- * if ($mobile == "") {
- * return app_show(1002, "手机号不能为空");
- * }
- * if (checkMobile($mobile) == false) {
- * return app_show(1002, "手机号格式不正确");
- * }
- * $email = isset($post['email']) ? trim($post['email']) : "";
- * $role = isset($post['role']) ? intval($post['role']) : 0;
- * if ($role != 0) {
- * $roleinfo = Db::name("role")->where("id", "=", $role)->find();
- * if (empty($roleinfo)) {
- * return error_show(1002, "角色不存在");
- * }
- * if ($roleinfo['status'] == 0) {
- * return error_show(1002, "角色已禁用");
- * }
- * }
- * $itemid = isset($post['itemid']) && $post['itemid'] !== "" ? $post['itemid'] : "";
- * $item_arr = [];
- * if ($itemid !== "") {
- * foreach ($itemid as $value) {
- * $item_arr[] = end($value);
- * }
- * $iteminfo = Db::name("company_item")->where(["id" => $item_arr, 'is_del' => 0])->select()->toArray();
- * if (empty($iteminfo)) {
- * return error_show(1002, "未找到部门信息");
- * }
- * }
- * $companyArr = isset($post['companyArr']) && !empty($post['companyArr']) ? $post['companyArr'] : "";
- * if ($companyArr == "") {
- * return error_show(1004, "参数companyArr不能为空");
- * }
- * $compArr = [];
- * foreach ($companyArr as $value) {
- * $tmp = [];
- * if (!isset($value['businessid']) || $value['businessid'] == '') {
- * return error_show(1004, "参数业务公司businessid不能为空");
- * }
- * $tmp['userid'] = $userid;
- * $tmp['businessid'] = $value['businessid'];
- * $tmp['is_main'] = isset($value['is_main']) ? $value['is_main'] : 0;
- * $tmp['is_del'] = 0;
- * $tmp['addtime'] = date("Y-m-d H:i:s");
- * $tmp['updatetime'] = date("Y-m-d H:i:s");
- * $compArr[] = $tmp;
- * }
- * $platform = isset($post['platform']) && !empty($post['platform']) ? $post['platform'] : "";
- * if ($platform == "") {
- * return error_show(1004, "参数platform不能为空");
- * }
- * $plta = Db::name("platform")->where(["id" => $platform, "is_del" => 0])->select()->toArray();
- * if (empty($plta)) {
- * return error_show(1004, "未找到可用平台");
- * }
- * $userplat = Db::name("user_platform")->where(["uid" => $userid, "is_del" => 0])->find();
- *
- * $isall = isset($post['is_all']) && $post['is_all'] !== "" ? intval($post['is_all']) : "0";
- *
- * Db::startTrans();
- * try {
- * $sc = Db::name("user_business")->where(["userid" => $userid, "is_del" => 0])->select()->toArray();
- * if (!empty($sc)) {
- * $del = Db::name("user_business")->where(["userid" => $userid, "is_del" => 0])->save(["is_del" => 1,
- * "updatetime" => date("Y-m-d H:i:s")]);
- * if ($del == false) {
- * Db::rollback();
- * return error_show(1005, '业务公司添加失败');
- * }
- * }
- * if (!empty($compArr)) {
- * $in = Db::name("user_business")->insertAll($compArr);
- * if ($in == 0) {
- * Db::rollback();
- * return error_show(1005, '业务公司添加失败');
- * }
- * }
- *
- * if ($userplat == false) {
- * $userplat = [
- * "uid" => $userid,
- * "platform" => implode(",", $platform),
- * "is_del" => 0,
- * "addtime" => date("Y-m-d H:i:s"),
- * "updatetime" => date("Y-m-d H:i:s")
- * ];
- * } else {
- * $userplat['platform'] = implode(",", $platform);
- * $userplat['updatetime'] = date("Y-m-d H:i:s");
- * }
- * $patup = Db::name("user_platform")->save($userplat);
- * if ($patup == false) {
- * Db::rollback();
- * return error_show(1005, '业务平台添加失败');
- * }
- * $userrole = Db::name("user_role")->where(["uid" => $userid, "is_del" => 0])->find();
- * if (empty($userrole)) {
- * $roledata = [
- * "uid" => $userid,
- * "roleid" => $role,
- * "status" => 1,
- * "business_all" => $isall,
- * "addtime" => date("Y-m-d H:i:s"),
- * "updatetime" => date("Y-m-d H:i:s")
- * ];
- * $up = Db::name("user_role")->insert($roledata);
- * } else {
- * $userrole['roleid'] = $role;
- * $userrole['business_all'] = $isall;
- * $userrole['updatetime'] = date("Y-m-d H:i:s");
- * $up = Db::name("user_role")->save($userrole);
- * }
- * if ($up) {
- * Db::name("depart_user")->where(["uid" => $userid])->save(['is_del' => 1]);
- * if (isset($item_arr) && !empty($item_arr)) {
- * foreach ($item_arr as $v) {
- * $depin = [
- * "uid" => $userid,
- * "nickname" => $nickname,
- * "itemid" => $v,
- * "is_del" => 0,
- * "updatetime" => date("Y-m-d H:i:s")
- * ];
- * $ist = Db::name("depart_user")->where(["uid" => $userid, "itemid" => $v])->find();
- * if ($ist) {
- * $up = Db::name("depart_user")->where(["uid" => $userid, "itemid" => $v])->save($depin);
- * } else {
- * $depin['addtime'] = date("Y-m-d H:i:s");
- * $up = Db::name("depart_user")->insert($depin);
- * }
- * if ($up == false) {
- * Db::rollback();
- * return error_show(1005, '用户部门更新失败');
- * }
- * }
- * }
- * } else {
- * Db::rollback();
- * return error_show(1005, '角色更新失败');
- * }
- * $data = [
- * "id" => $userid,
- * "nickname" => $nickname,
- * "mobile" => $mobile,
- * "email" => $email
- * ];
- * $save = resetinfo($toke, $data);
- * if (!empty($save) && $save['code'] != 0) {
- * Db::rollback();
- * return error_show($save['code'], $save['message']);
- * }
- * Db::commit();
- * return app_show(0, "更新成功");
- * } catch (\Exception $e) {
- * Db::rollback();
- * return app_show(1002, $e->getMessage());
- * }
- *
- * **/
- $post = $this->request->only(["id", "nickname", "mobile", "email" => "", "portrait" => "", "sex" => ""], "post", 'trim');
- $validate = Validate::rule([
- 'id|主键ID' => 'require|number|gt:0',
- 'nickname|名称' => 'require|max:255',
- 'mobile|手机号' => 'require|mobile',
- 'email|名称' => 'email',
- 'sex|性别' => 'number|in:0,1,2',
- ]);
- if ($validate->check($post) == false) return json_show(1004, $validate->getError());
- $userCommon = CommonUser::getIns();
- $rs = json_decode($userCommon->userSave($post), true);
- return json_show($rs['code'], $rs['message'], $rs['data']);
- }
- //修改密码
- public function setPwd()
- {
- /**
- * $post = $this->request->post();
- * $token = isset($post['token']) ? trim($post['token']) : "";
- * if ($token == "") {
- * return error_show(101, 'token不能为空');
- * }
- * $effetc = VerifyTokens($token);
- * if (!empty($effetc) && $effetc['code'] != 0) {
- * return error_show($effetc['code'], $effetc['message']);
- * }
- * $userid = isset($post['id']) && $post['id'] !== "" ? intval($post['id']) : "";
- * if ($userid == "") {
- * return error_show(1004, "参数id 不能为空");
- * }
- * $newPwd = isset($post['password']) ? trim($post['password']) : "";
- * if ($newPwd == "") {
- * return error_show(1001, '密码不能为空');
- * }
- *
- * $data = resetpwd($token, ["id" => $userid, 'password' => $newPwd]);
- * if (empty($data) || $data['code'] != 0) {
- * return error_show($data['code'], $data['message']);
- * } else {
- * return app_show(0, "密码修改成功");
- * }
- * **/
- $post = $this->request->only(["id", "password"], "post", "trim");
- $validate = Validate::rule([
- 'id|账户ID' => 'require|number',
- 'password|密码' => 'require|min:6|max:200',
- ]);
- if (!$validate->check($post)) return json_show(1004, $validate->getError());
- $userCommon = CommonUser::getIns();
- $rs = json_decode($userCommon->resetPasswd($post), true);
- return json_show($rs['code'], $rs['message'], $rs['data']);
- }
- //启禁用
- public function userStatus()
- {
- /**
- * $post = $this->request->post();
- * $toke = isset($post['token']) ? trim($post['token']) : "";
- * if ($toke == "") {
- * return app_show(100, "token不能为空");
- * }
- * $verify = VerifyTokens($toke);
- * if ($verify['code'] != 0) {
- * return app_show($verify['code'], $verify['message']);
- * }
- * $userid = isset($post['id']) && $post['id'] !== "" ? intval($post['id']) : "";
- * if ($userid == "") {
- * return error_show(1004, "参数userid 不能为空");
- * }
- * $status = isset($post['status']) && $post['status'] != "" ? intval($post['status']) : "";
- *
- * if ($status === "") {
- * return error_show(1004, "参数status 不能为空");
- * }
- * if (!in_array($status, [0, 1])) {
- * return error_show(1004, "参数status无效");
- * }
- * $message = $status == 1 ? "启用" : "禁用";
- * $userinfo = Db::name("user_role")->where(["uid" => $userid, "is_del" => 0])->find();
- * if ($userinfo == false) {
- * $data = [
- * "uid" => $userid,
- * "roleid" => 0,
- * "status" => $status,
- * "is_del" => 0,
- * "addtime" => date("Y-m-d H:i:s"),
- * "updatetime" => date("Y-m-d H:i:s"),
- * ];
- * $sa = Db::name("user_role")->save($data);
- * } else {
- * $userinfo['status'] = $status;
- * $userinfo['updatetime'] = date("Y-m-d H:i:s");
- * $sa = Db::name("user_role")->save($userinfo);
- * }
- * if ($sa == false) {
- * return error_show(1005, "{$message}失败");
- * } else {
- *
- * return app_show(0, "{$message}成功");
- * }
- *
- * // $set = resetstatus($toke,["id"=>$userid,"status"=>$status]);
- * //
- * // if(empty($set)||$set['code']!=0){
- * // return error_show(1005,"{$message}失败");
- * // }else{
- * // D
- * // return app_show(0,"{$message}成功");
- * // }
- * **/
- $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)) return json_show(1004, $validate->getError());
- $userCommon = CommonUser::getIns();
- $rs = json_decode($userCommon->resetState($post), true);
- return json_show($rs['code'], $rs['message'], $rs['data']);
- }
- public function getinfo()
- {
- $post = $this->request->post();
- $token = isset($post['token']) ? trim($post['token']) : "";
- if ($token == "") {
- return app_show(101, 'token不能为空');
- }
- $effetc = VerifyTokens($token);
- if (!empty($effetc) && $effetc['code'] != 0) {
- return app_show($effetc['code'], $effetc['message']);
- }
- $userinfo = GetUserInfo($token);
- if (empty($userinfo) || $userinfo['code'] != 0) {
- return app_show(102, "员工信息不存在");
- }
- $data = $userinfo['data'];
- $role = Db::name("user_role")->alias('a')->leftJoin('role b', "a.roleid = b.id")->where("a.uid", "=", $data['id'])
- ->field("roleid,role_name,business_all,uid")->find();
- $data['role_name'] = isset($role['role_name']) ? $role['role_name'] : "";
- $data['roleid'] = isset($role['roleid']) ? $role['roleid'] : "";
- $st = Db::name('user_business')->where(['userid' => $role['uid'], "is_del" => 0])->field('businessid,is_main')->select()->toArray();
- $val = [];
- if (!empty($st)) {
- foreach ($st as $itom) {
- $tep = [];
- // $business = Db::name('business')->where(['id' => $itom['businessid'], "is_del" => 0])->field("company,companyNo")->find();
- //这个地方是根据业务企业id来匹配的,无法去sys_user查找
- $tep['company'] = '';//isset($business['company']) ? $business['company'] : "";
- $tep['companyNo'] = '';//isset($business['companyNo']) ? $business['companyNo'] : "";
- $tep['businessid'] = $itom['businessid'];
- $tep['is_main'] = $itom['is_main'];
- $val[] = $tep;
- }
- }
- $data['private_field'] = [];
- if ($data['roleid'] != "") {
- $action = Db::name("role_action")->where(["role_id" => $data['roleid'], "status" => 1])->find();
- if ($action != false) {
- $data['private_field'] = isset($action['private_field']) && $action['private_field'] != "" ? explode(",", $action['private_field']) : [];
- }
- }
- $data['business'] = $val;
- $info = [];
- $stn = Db::name('user_platform')->where(['uid' => $role['uid']])->find();
- if ($stn != false) {
- $plat = isset($stn['platform']) ? explode(",", $stn['platform']) : "";
- if ($plat != "") {
- $platform = Db::name('platform')->where(['id' => $plat, 'is_del' => 0])->column("id,platform_name,platform_code");
- $info = $platform;
- }
- }
- $depart = Db::name("depart_user")->where([["uid", "=", $data['id']], ["is_del", "=", 0]])->column("itemid");
- $data['depart'] = "";
- if (!empty($depart)) {
- $deparname = Db::name("company_item")->where(["id" => $depart, "is_del" => 0])->field("id,name,depart_link")->select()->toArray();
- if (!empty($deparname)) {
- foreach ($deparname as $ky => $item) {
- $deparname[$ky]['depart_code'] = array_filter(explode("-", $item['depart_link']));
- }
- }
- $data['depart'] = $deparname;
- }
- $data['platform'] = $info;
- return app_show(0, "获取成功", $data);
- }
- //超管账号列表
- public function userSingleList()
- {
- $post = $this->request->only(["token" => "", "nickname" => "", "relaComNo" => "", "username" => "", "companyNo" => "", "status" => "", "page" => 1, "size" => 10], "post");
- if ($this->level == 1) {
- $companyNo = $post['companyNo'] ?? "";
- } else {
- $companyNo = $post['relaComNo'] ?? "";
- $post['companyNo'] = $post['relaComNo'] ?? "";
- }
- $where = [["a.is_del", "=", 0]];
- unset($post['relaComNo']);
- if ($post['companyNo'] != "") {
- $where[] = ["a.companyNo", "=", $companyNo];
- }
- // if ($post["islevel"] != "") {
- // $uid = Db::name("user_role")->alias("a")
- // ->leftJoin("role b", "a.roleid=b.id")->where([["b.level", "=", 1], ["a.is_del", "=", 0]])->where($where)->column
- // ("uid");
- // if ($post['islevel'] == 1) $post['uid'] = $uid;
- // else $post['nuid'] = $uid;
- //
- // unset($post["islevel"]);
- // }
- $userCommon = \app\admin\common\User::getIns();
- $userinfo = $userCommon->GetAccountall($post);
- $userinfo = json_decode($userinfo, true);
- if (empty($userinfo) || $userinfo['code'] != 0) return app_show($userinfo['code'], $userinfo['msg']);
- $data = $userinfo['data']['list'];
- $list = [];
- foreach ($data as $value) {
- if (isset($value['company_relaton']) && !empty($value['company_relaton'])) {
- foreach ($value['company_relaton'] as &$item) {
- $roleArr = Db::name("user_role")
- ->alias("a")
- ->leftJoin("role b", "a.roleid=b.id")
- ->where(["uid" => $value['id'], "a.companyNo" => $item['companyCode'], "is_del" => 0])
- ->field("role_name,roleid,a.status")
- ->findOrEmpty();
- $item['roleid'] = $roleArr['roleid'] ?? "";
- $item['status'] = $roleArr['status'] ?? 0;
- $item["role_name"] = $roleArr['role_name'] ?? "";
- $item["role_level"] = $value['level'];
- }
- }
- $value['roleid'] = $roleArr['roleid'] ?? "";
- $value['status'] = $value['status'] ?? 0;
- $value["role_name"] = $roleArr['role_name'] ?? "";
- $value["role_level"] = $value['level'] ?? "";
- $list[] = $value;
- }
- return app_show(0, "获取成功", ["list" => $list, "count" => $userinfo['data']["count"]]);
- }
- //修改密码
- public function passSetByPassword()
- {
- $param = $this->request->only(['old_pass', 'new_pass'], 'post', 'trim');
- $param['uid'] = $this->uid;
- $userCommon = \app\admin\common\User::getIns();
- $rs = $userCommon->handle('passSetByPassword', $param);
- return json_show($rs['code'], $rs['message'], $rs['data']);
- }
- }
|