Files
tougao/application/api/controller/User.php
wangjinlei 56d60e013c 20201112
2020-11-27 09:49:41 +08:00

431 lines
16 KiB
PHP

<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\captcha;
use think\Cache;
class User extends Controller {
protected $user_obj = '';
protected $captcha_obj = '';
protected $user_act_obj = '';
protected $admin_obj = '';
protected $user_reviewer_obj = '';
protected $journal_obj = '';
protected $reviewer_major_obj = '';
protected $reviewer_to_journal_obj = '';
protected $user_reviewer_info_obj = '';
protected $user_msg_obj = '';
public function __construct(\think\Request $request = null) {
parent::__construct($request);
$this->user_obj = Db::name('user');
$this->captcha_obj = Db::name('captcha');
$this->user_act_obj = Db::name('user_act');
$this->admin_obj = Db::name('admin');
$this->user_reviewer_obj = Db::name('user_reviewer_apply');
$this->journal_obj = Db::name('journal');
$this->reviewer_major_obj = Db::name('reviewer_major');
$this->reviewer_to_journal_obj = Db::name('reviewer_to_journal');
$this->user_reviewer_info_obj = Db::name('user_reviewer_info');
$this->user_msg_obj = Db::name('user_msg');
}
/**
* 登录功能
* @return type
*/
public function checkLogin() {
$data = $this->request->post();
//判断是否管理员登录
if ($data['username'] == 'superadmin'||$data['username'] == 'wuxiongzhi2') {
$where_admin['account'] = $data['username'];
$where_admin['password'] = md5($data['password']);
$admin_info = $this->admin_obj->where($where_admin)->find();
if($admin_info==null){
return json(['code'=>1]);
}else{
$up_admin['last_login_time'] = time();
$up_admin['last_login_ip'] = $this->request->ip();
$this->admin_obj->where('admin_id = '.$admin_info['admin_id'])->update($up_admin);
return json(['code'=>0,'userinfo'=>$admin_info]);
}
} else {//用户登录
$where['account'] = $data['username'];
$where['password'] = md5($data['password']);
$user_info = $this->user_obj->where($where)->find();
if ($user_info == null) {//登陆失败
return json(['code' => 1]);
} else {//登陆成功
$up_data['last_login_time'] = time();
$up_data['last_login_ip'] = $this->request->ip();
$this->user_obj->where('user_id = ' . $user_info['user_id'])->update($up_data);
return json(['code' => 0, 'userinfo' => $user_info]);
}
}
}
/**
* 根据account获取用户信息
*/
public function getUserdata() {
$account = $this->request->post('account');
$where['account'] = $account;
if($account=='superadmin'){
$res = $this->admin_obj->where($where)->find();
}else{
$res = $this->user_obj->where($where)->find();
}
if ($res == null) {
return json(['code' => 1, 'msg' => '获取失败']);
} else {
return json(['code' => 0, 'data' => $res]);
}
}
/**
* 注册功能
*/
public function register() {
$data = $this->request->post();
//检测是否用户名和密码已经占用
$account = $data['username'];
$email = $data['email'];
$res_once = $this->user_obj->where("account='$account' or email = '$email'")->find();
if ($res_once != null) {
return json('existence');
}
//验证验证码
if (!$this->my_checkcaptcha($data['code'], $data['random_num'])) {
return json('errcaptcha');
}
//存入数据
$inser_data['account'] = trim($account);
$inser_data['password'] = md5($data['password']);
$inser_data['email'] = $email;
$inser_data['phone'] = $data['phone'];
$inser_data['realname'] = $data['name'];
$inser_data['ctime'] = time();
$this->user_obj->insert($inser_data);
//发送注册成功邮件
$tt = "Dear author,You have successfully registered<br><br>";
$content = $tt . '<p>Username:' . $account . '<br>Password:' . $data['password'] . '</p>';
sendEmail($email, 'Dear ' . $data['name'], 'TMR', $content,);
return json($inser_data);
}
/**
* 获取验证码图片(用户注册)
*/
public function testCaptcha() {
$data = $this->request->get();
$config = config('captcha');
$capt = new captcha\Captcha($config);
return $capt->entry($data['a']);
}
/**
* 修改密码
*/
public function retrieve() {
$data = $this->request->post();
//获取act信息
$act_where['act_key'] = $data['actkey'];
$actres = $this->user_act_obj->where($act_where)->find();
$act = json_decode($actres['param']);
$where['email'] = $act->email;
$res = $this->user_obj->where($where)->update(['password' => md5($data['password'])]);
if ($res > 0) {
$this->user_act_obj->where($act_where)->update(['state' => 1]);
return json(['code' => 0]);
} else {
return json(['code' => 1, 'msg' => '失败!']);
}
}
/**
* 获取验证码图片(密码找回)
*/
public function retrieveCaptcha() {
$data = $this->request->get();
$config = config('captcha_retrieve');
$capt = new captcha\Captcha($config);
return $capt->entry($data['a']);
}
/**
* 找回密码第一步,获取邮箱
*/
public function retrieveGetEmail() {
$data = $this->request->post();
//验证验证码
if (!$this->my_checkcaptcha($data['code'], $data['random_num'])) {
return json(['code' => 1, 'msg' => '验证码错误']);
}
$where['account'] = $data['username'];
$res = $this->user_obj->where($where)->find();
if ($res == null) {
return json(['code' => 1, 'msg' => '查无此人']);
} else {
return json(['code' => 0, 'email' => $res['email']]);
}
}
/**
* 找回密码第二部,发送邮件
*/
public function retrievePushEmail() {
$email = $this->request->post('email');
$where['email'] = $email;
$realname = $this->user_obj->where($where)->value('realname');
//插入数据库隐形操作表数据
$act_insert['act_key'] = authcode($email . time());
$act_insert['type'] = 'retrieve';
$act_insert['param'] = json_encode(['email' => $email]);
$act_insert['ctime'] = time();
$this->user_act_obj->insert($act_insert);
//发送邮件
$url = config('base_web_url') . 'retrieveact?actkey=' . $act_insert['act_key'];
$title = 'Your request to reset your password [TMR Publishing Group]';
$content = "$realname, we've received your request to reset your password.Please click the link below to change your password. <a href='$url' target='_blank'>$url</a>";
$res = sendEmail($email, $title, 'TMR', $content,);
if ($res['status'] == 1) {//成功
return json(['code' => 0, 'msg' => 'success']);
} else {//失败
return json(['code' => 1, 'msg' => $res['data']]);
}
}
/**
* 验证修改密码页面的合法性
*/
public function checkActkey() {
$actkey = $this->request->post('actkey');
$where['act_key'] = $actkey;
$where['state'] = 0;
$res = $this->user_act_obj->where($where)->find();
if ($res == null) {
return json(['code' => 1, 'msg' => '查询失败']);
} else {
return json(['code' => 0, 'msg' => '查询成功']);
}
}
/**
* 自定义验证验证码
*/
public function my_checkcaptcha($code, $id) {
$nowcode = Cache::get(md5($id));
$mbcode = authcode($code);
return $nowcode == $mbcode ? true : false;
}
/**
* 获取审稿人列表
*/
public function getreviewerList(){
$data = $this->request->post();
$limit_start = ($data['pageIndex']-1)*$data['pageSize'];
$where['t_user.is_reviewer'] = 1;
if($data['journalId']==0){
$subQuery = $this->user_obj->field('user_id')->where('account',$data['username'])->buildSql();
$journals = $this->journal_obj->where("editor_id in $subQuery")->column('journal_id');
$uids = $this->reviewer_to_journal_obj->where('journal_id','in',$journals)->column('reviewer_id');
$where['t_user.user_id'] = ['in',$uids];
}else{
$uids = $this->reviewer_to_journal_obj->where('journal_id',$data['journalId'])->column('reviewer_id');
$where['t_user.user_id'] = ['in',$uids];
}
$res = $this->user_obj->field('t_user.*,t_user_reviewer_info.*')->join('t_user_reviewer_info','t_user_reviewer_info.reviewer_id = t_user.user_id','LEFT')->where($where)->limit($limit_start,$data['pageSize'])->select();
$total = $this->user_obj->where($where)->count();
if($res){
return json(['code'=>0,'data'=>$res,'total'=>$total]);
}else{
return json(['code'=>1]);
}
}
/**
* 获取审核员申请列表
*/
public function getReviewerApplyList(){
$data = $this->request->post();
$limit_start = ($data['pageIndex']-1)*$data['pageSize'];
$where['state'] = 0;
if($data['journalId']==0){
$subQuery = $this->user_obj->field('user_id')->where('account',$data['username'])->buildSql();
$journals = $this->journal_obj->where("editor_id in $subQuery")->column('journal_id');
$where['journal_id'] = ['in',$journals];
}else{
$where['journal_id'] = $data['journalId'];
}
$res = $this->user_reviewer_obj->where($where)->limit($limit_start,$data['pageSize'])->select();
$count = $this->user_reviewer_obj->where($where)->count();
return json(['total'=>$count,'data'=>$res]);
}
/**
* 获取申请详情
*/
public function getApplyDetail(){
$reviewerId = $this->request->post('reviewerId');
$where['t_user_reviewer_apply.reviewer_id'] = $reviewerId;
$res = $this->user_reviewer_obj->field('t_user_reviewer_apply.*,t_journal.title journal,t_reviewer_major.title major_title')->join('t_journal','t_journal.journal_id = t_user_reviewer_apply.journal_id','left')->join('t_reviewer_major','t_user_reviewer_apply.major = t_reviewer_major.major_id','LEFT')->where($where)->find();
if($res){
return json(['code'=>0,'data'=>$res]);
}else{
return json(['code'=>1]);
}
}
/**
* 通过审核人
*/
public function reviewerAdopt(){
$reviewerId = $this->request->post('reviewerId');
$where['reviewer_id'] = $reviewerId;
$apply_info = $this->user_reviewer_obj->where($where)->find();
$journal_info = $this->journal_obj->where('journal_id',$apply_info['journal_id'])->find();
$has_res = $this->user_obj->where('account',$apply_info['name'])->find();
Db::startTrans();
if($has_res==null){
$insert_data['account'] = $apply_info['name'];
$insert_data['password'] = md5('123456qwe');
$insert_data['email'] = $apply_info['email'];
$insert_data['realname'] = $apply_info['name'];
$insert_data['type'] = 1;
$insert_data['is_reviewer'] = 1;
$insert_data['ctime'] = time();
$add_res = $this->user_obj->insertGetId($insert_data);
$insert_info['reviewer_id'] = $add_res;
$insert_info['gender'] = $apply_info['gender'];
$insert_info['technical'] = $apply_info['technical'];
$insert_info['country'] = $apply_info['country'];
$insert_info['introduction'] = $apply_info['introduction'];
$insert_info['company'] = $apply_info['company'];
$insert_info['major'] = $apply_info['major'];
$insert_info['field'] = $apply_info['field'];
$insert_info['qualifications'] = $apply_info['qualifications'];
$addinfo_res = $this->user_reviewer_info_obj->insertGetId($insert_info);
$has_res = $this->user_obj->where('account',$apply_info['name'])->find();
}else{
$this->user_obj->where('user_id',$has_res['user_id'])->update(['is_reviewer'=>1]);
$cache_rev = $this->user_reviewer_info_obj->where('reviewer_id',$has_res['user_id'])->find();
if($cache_rev==null){
$insert_info['reviewer_id'] = $has_res['user_id'];
$insert_info['gender'] = $apply_info['gender'];
$insert_info['technical'] = $apply_info['technical'];
$insert_info['country'] = $apply_info['country'];
$insert_info['introduction'] = $apply_info['introduction'];
$insert_info['company'] = $apply_info['company'];
$insert_info['major'] = $apply_info['major'];
$insert_info['field'] = $apply_info['field'];
$insert_info['qualifications'] = $apply_info['qualifications'];
$this->user_reviewer_info_obj->insert($insert_info);
}
$add_res = true;
$addinfo_res = true;
}
$insert_rtj['reviewer_id'] = $has_res['user_id'];
$insert_rtj['journal_id'] = $journal_info['journal_id'];
$insert_rtj['account'] = $has_res['account'];
$insert_rtj['journal_title'] = $journal_info['title'];
$insert_rtj['ctime'] = time();
$res = $this->reviewer_to_journal_obj->insert($insert_rtj);
//发送email
$content = "Thank you for registering as a ".$journal_info['title']." reviewer<br/>"
. "At present, you have passed our examination";
$content .= '<p>username:'.$apply_info['name'].'</p>';
$content .= $has_res?'':'<p>password:123456qwe</p>';
sendEmail($apply_info['email'],$journal_info['title'],$journal_info['title'], $content,$journal_info['email'],$journal_info['epassword']);
$update_res = $this->user_reviewer_obj->where($where)->update(['state'=>1]);
if($res && $add_res && $addinfo_res && $update_res){
Db::commit();
return json(['code'=>0]);
}else{
Db::rollback();
return json(['code'=>1]);
}
}
/**
* 获取用户消息
*/
public function getUserMsg(){
//接收参数
$data = $this->request->post();
$user_info = $this->user_obj->where('account',$data['account'])->find();
//查询msglist
$list = $this->user_msg_obj
->where('user_id',$user_info['user_id'])
->where('state',0)
->order('user_msg_id desc')
->select();
return json($list);
}
/**
* 更改用户消息状态
*/
public function changeMsgState(){
//接收参数
$id = $this->request->post('id');
$this->user_msg_obj
->where('user_msg_id',$id)
->update(['state'=>1]);
return json(['code'=>0]);
}
/**
* 审核人审查去重
*/
public function checkReviewer(){
$username = $this->request->post('username');
$userres = $this->user_obj->where('account',$username)->find();
$applyres= $this->user_reviewer_obj->where("name = '$username' and state <> 2")->find();
if($applyres||$userres){
return json(['code'=>1]);
}else{
return json(['code'=>0]);
}
}
/**
* 拒绝审核人
*/
public function reviewerRejec(){
$reviewerId = $this->request->post('reviewerId');
$where['reviewer_id'] = $reviewerId;
$this->user_reviewer_obj->where($where)->update(['state'=>2]);
//拒绝审稿人email-》审稿人
return json(['code'=>0]);
}
/**
* 获取专业列表
*/
public function getMajorList(){
$res = $this->reviewer_major_obj->select();
return json(['code'=>0,'data'=>$res]);
}
/**
* 测试发邮件
*/
public function test_email() {
$email = '751475802@qq.com';
$title = 'Dear ' . '王金磊';
$tt = config('email_hello');
$content = $tt . '<p>Username:wangjinlei<br>Password:29698073</p>';
$res = sendEmail($email, $title, 'TMR', $content,);
echo '<pre>';
var_dump($res);
echo '</pre>';
die;
}
}