|
@@ -3,20 +3,22 @@ declare (strict_types = 1);
|
|
|
|
|
|
namespace app\controller;
|
|
|
|
|
|
-use app\BaseController;
|
|
|
use app\model\Account;
|
|
|
use think\facade\Cache;
|
|
|
-use think\Exception;use think\facade\Db;
|
|
|
+use think\Exception;
|
|
|
+use think\facade\Db;
|
|
|
use think\App;
|
|
|
use think\facade\Validate;
|
|
|
|
|
|
-class User extends BaseController
|
|
|
+class User extends Base
|
|
|
{
|
|
|
private $token_time = 0;// token 有效时间
|
|
|
+ private $model =null;// token 有效时间
|
|
|
public function __construct(App $app)
|
|
|
{
|
|
|
parent::__construct($app);
|
|
|
$this->token_time= env("token.expire");
|
|
|
+ $this->model = new Account();
|
|
|
}
|
|
|
/**注册接口
|
|
|
* @param string username 账户名称
|
|
@@ -128,11 +130,12 @@ class User extends BaseController
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param string username 账户
|
|
|
+ * @param string username 账户
|
|
|
* @param string password 密码
|
|
|
* @param string plat_code 来源
|
|
|
* @return \think\response\Json
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException
|
|
|
+ * @throws \think\Exception
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\DbException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
@@ -140,53 +143,46 @@ class User extends BaseController
|
|
|
*/
|
|
|
public function login()
|
|
|
{
|
|
|
- $post = $this->request->only(["username" => "", "password" => "", "plat_code" => ""], "post", "trim");
|
|
|
+ $post = $this->request->only(["username" => "", "password" => "", "plat_code" => "","openId"=>""], "post", "trim");
|
|
|
$validate = Validate::rule([
|
|
|
'username|账户名称' => 'require|max:255',
|
|
|
'password|密码' => 'require|min:6|max:200',
|
|
|
]);
|
|
|
- if ($validate->check($post) == false) return json_show(1004, $validate->getError());
|
|
|
-
|
|
|
- $acc = Db::name("account")
|
|
|
- ->where(['username' => $post['username'], "is_del" => Account::$account_del])
|
|
|
- ->find();
|
|
|
- if ($acc == false) return json_show(1003, '账户名不存在');
|
|
|
+ if ($validate->check($post) == false) $this->error($validate->getError(),1004);
|
|
|
|
|
|
- if ($acc['status'] == Account::$account_end) return json_show(1003, '账户已禁用');
|
|
|
+ $acc = $this->model->withJoin(["userinfo","accountitem"],"left")
|
|
|
+ ->where(['username' => $post['username']])
|
|
|
+ ->findOrEmpty();
|
|
|
+ if ($acc->isEmpty()) return json_show(1003, '账户名不存在');
|
|
|
|
|
|
+ if ($acc['status'] == Account::$account_end)$this->error('账户已禁用',1003);
|
|
|
$sha1 = sha1($post['password'] . $acc['salt']);
|
|
|
- if ($sha1 != $acc['password']) return json_show(1003, '账户或密码错误');
|
|
|
-
|
|
|
- $token = makeToken($acc);
|
|
|
- if ($token == "") return json_show(1003, 'token生成失败');
|
|
|
-
|
|
|
- //account_plat 是空表
|
|
|
-// if ($post['plat_code'] != "") {
|
|
|
-// $platinfo = 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.plat_code" => $post['plat_code'], "a.account_id" => $acc['id']])
|
|
|
-// ->findOrEmpty();
|
|
|
-// if (empty($platinfo)) return json_show(1003, '该系统账号未开通登录');
|
|
|
-//
|
|
|
-// }
|
|
|
- $user = Db::name("account")
|
|
|
- ->alias("a")
|
|
|
- ->leftJoin("user b", "a.id=b.account_id and b.status=1")
|
|
|
- ->leftJoin("account_item c", "c.account_id=a.id")
|
|
|
- ->field("a.id,a.username,a.mobile,a.source,a.level,b.nickname,b.sex,b.email,a.addtime,a.updatetime,c.itemid,c.position")
|
|
|
- ->where(["a.id" => $acc["id"]])
|
|
|
- ->find();
|
|
|
- if ($user == false) return json_show(1003, '用户信息不存在');
|
|
|
-
|
|
|
- $usercompany = Db::name("account_company")
|
|
|
- ->where(["account_id" => $user['id'], "is_del" => 0, "status" => 1])
|
|
|
- ->column("companyCode,companyCode companyNo,companyName,companyName company_name,company_type,is_main,status");
|
|
|
- $user['company_relaton'] = $usercompany;
|
|
|
- $cache = Cache::store("redis")->set("user:info:{$token}", $user, $this->token_time);
|
|
|
- if ($cache == false) return json_show(1003, 'token保存失败');
|
|
|
- $user['token'] = $token;
|
|
|
- return json_show(0, "登录成功", $user);
|
|
|
+ if ($sha1 != $acc['password']) $this->error('账户或密码错误',1003);
|
|
|
+ $userinfo=[
|
|
|
+ "id"=>$acc->id,
|
|
|
+ "username"=>$acc->username,
|
|
|
+ "mobile"=>$acc->mobile,
|
|
|
+ "source"=>$acc->source,
|
|
|
+ "nickname"=>$acc->userinfo->nickname,
|
|
|
+ "sex"=>$acc->userinfo->sex,
|
|
|
+ "email"=>$acc->userinfo->email,
|
|
|
+ "level"=>$acc->level,
|
|
|
+ "itemid"=>$acc->accountitem->itemid??0,
|
|
|
+ "position"=>$acc->accountitem->position??'',
|
|
|
+ "depart_name"=>$acc->depart_name,
|
|
|
+ "company_relaton"=>$acc->company_relaton,
|
|
|
+ ];
|
|
|
+
|
|
|
+ if($post['openId']!=''&& $acc->userinfo->openId!=$post['openId'] ){
|
|
|
+ $acc->userinfo->openId=$post['openId'];
|
|
|
+ $this->model->userinfo()->save($acc->userinfo->toArray());
|
|
|
+ }
|
|
|
+ $token = makeToken($userinfo);
|
|
|
+ if ($token == "") $this->error('token生成失败',1003);
|
|
|
+ $cache = Cache::store("redis")->set("user:info:{$token}", $userinfo, $this->token_time);
|
|
|
+ if ($cache == false)$this->error('token保存失败',1003);
|
|
|
+ $userinfo['token'] = $token;
|
|
|
+ $this->success("登录成功", $userinfo,0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -363,4 +359,63 @@ class User extends BaseController
|
|
|
$result=Db::name("account")->save($account);
|
|
|
return $result?json_show(0,"密码修改成功"):json_show(1003,"密码修改失败");
|
|
|
}
|
|
|
+
|
|
|
+ public function AccountQuery(){
|
|
|
+ $param = $this->request->only(['nickname' => '', 'username' => '',"is_wx"=>"",'level'=>'', 'status' => '',
|
|
|
+ 'page'=> 1,'size'=>30], 'post', 'trim');
|
|
|
+ $where=[];
|
|
|
+ $param['nickname']==''?: $where[]=['userinfo.nickname','like',"%{$param['nickname']}%"];
|
|
|
+ $param['is_wx']===''?: $where[]=['userinfo.openId', $param['is_wx']==0?"=":"<>",""];
|
|
|
+ $param['status']==''?: $where[]=['account.status','=',$param['status']];
|
|
|
+ $param['level']==''?: $where[]=['account.level','=',$param['level']];
|
|
|
+ $param['username']==''?: $where[]=['username','like',"%{$param['username']}%"];
|
|
|
+ $acc = $this->model->withJoin(['userinfo'],"left")->where($where)->order('id desc')
|
|
|
+ ->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
|
|
|
+ $tenmp=[];
|
|
|
+ foreach ($acc->items() as $value){
|
|
|
+ $temp=[];
|
|
|
+ $temp['id'] = $value->id;
|
|
|
+ $temp['nickname'] = $value->nickname;
|
|
|
+ $temp['mobile'] = substr_replace($value->mobile,"****",-4);
|
|
|
+ $temp['is_wx'] = $value->is_wx;
|
|
|
+ $temp['status'] = $value->status;
|
|
|
+ $tenmp[]=$temp;
|
|
|
+ }
|
|
|
+ $this->success('获取成功',['list'=>$tenmp,'count'=>$acc->total()],0);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function BindWx(){
|
|
|
+ $param = $this->request->only(['id' => '', 'openId' => '','lastCode'=>''], 'post', 'trim');
|
|
|
+ $valid =Validate::rule(["id|账户主键"=>"require|number|egt:0","openId|微信openid"=>"require|max:255","lastCode|手机号后四位"=>"require|number|length:4"]);
|
|
|
+ if($valid->check($param)==false) $this->error($valid->getError());
|
|
|
+ $acc = $this->model->withJoin(['userinfo','accountitem'],'left')->findOrEmpty($param['id']);
|
|
|
+ if($acc->isEmpty())$this->error("账户信息不存在",1004);
|
|
|
+ if($acc->userinfo->openId!='')$this->error("账户信息已绑定微信请先解除",1004);
|
|
|
+ if(substr($acc->mobile,-4,4)!=$param['lastCode'])$this->error('手机后四位不正确!',1004);
|
|
|
+ if($param['openId']!==''&& $acc->userinfo->openId!=$param['openId'] ){
|
|
|
+ $acc->userinfo->openId=$param['openId'];
|
|
|
+ $this->model->userinfo()->save($acc->userinfo->toArray());
|
|
|
+ }
|
|
|
+ $userinfo=[
|
|
|
+ 'id'=>$acc->id,
|
|
|
+ 'username'=>$acc->username,
|
|
|
+ 'mobile'=>$acc->mobile,
|
|
|
+ 'source'=>$acc->source,
|
|
|
+ 'nickname'=>$acc->userinfo->nickname,
|
|
|
+ 'sex'=>$acc->userinfo->sex,
|
|
|
+ 'email'=>$acc->userinfo->email,
|
|
|
+ 'level'=>$acc->level,
|
|
|
+ 'itemid'=>$acc->accountitem->itemid??0,
|
|
|
+ 'position'=>$acc->accountitem->position??'',
|
|
|
+ 'depart_name'=>$acc->depart_name,
|
|
|
+ 'company_relaton'=>$acc->company_relaton,
|
|
|
+ ];
|
|
|
+ $token = makeToken($userinfo);
|
|
|
+ if ($token == '') $this->error('token生成失败',1003);
|
|
|
+ $cache = Cache::store('redis')->set("user:info:{$token}", $userinfo, $this->token_time);
|
|
|
+ if ($cache == false)$this->error('token保存失败',1003);
|
|
|
+ $userinfo['token'] = $token;
|
|
|
+ $this->success('获取成功',$userinfo,0);
|
|
|
+ }
|
|
|
}
|