canyin-project/ybcy/controllers/admin/ApplyuserController.php
2024-11-01 16:07:54 +08:00

248 lines
10 KiB
PHP

<?php
namespace app\controllers\admin;
use Yii;
use yii\web\Controller;
class ApplyuserController extends CommonController{
public $enableCsrfValidation = false;
//通过平台id获取用户信息
public function actionGetuserbyid(){
$request = Yii::$app->request;
if ($request->isPost){
$id=axios_request()['id'];
$data= (new \yii\db\Query())
->select('a.id,a.userName,a.portrait,FROM_UNIXTIME(a.createdAt) createdAt,a.passWord,a.phone,b.apply_name')
->from('{{%ybwm_users}} a')
->Leftjoin('{{%ybwm_apply}} b','a.unionid=b.id')
->where(['a.id'=>$id])
->orderBy(['id'=>'asc'])
->one();
return $this->result(1,'成功',$data);
}
return $this->result(2,'请求异常');
}
//获取用户列表
public function actionGetuserlist(){
$request = Yii::$app->request;
if ($request->isPost){
$result=axios_request();
$where=array(
'a.status'=>$result['status'],
'type'=>1
);
$where=filter_array($where);
$page=$result['page'];
$num=($page-1)*10;
$limit=10;
$query= (new \yii\db\Query())
->select('a.id,b.end_time,a.uniacid,a.phone,a.portrait,FROM_UNIXTIME(a.createdAt) createdAt,a.userName,b.apply_name,b.apply_type')
->from('{{%ybwm_users}} a')
->Leftjoin('{{%ybwm_apply}} b','a.uniacid=b.id')
->where($where)
->orderBy(['id'=>'asc']);
if($result['keyword']){
$query->andWhere(['like','userName',$result['keyword']])
->orWhere(['like','apply_name',$result['keyword']]);
}
$count=count($query->all());
$data=$query->offset($num)->limit($limit)->all();
return $this->result(1,'成功',$data,$count);
}
return $this->result(2,'请求异常');
}
//获取手机号列表
public function actionUserPhoneList(){
$result=axios_request();
$page=$result['page'];
$num=($page-1)*10;
$limit=10;
$query= (new \yii\db\Query())
->from('{{%ybwm_users}}')
->where(['>','phone',''])
->andWhere(['>','uniacid',''])
->andWhere(['type'=>1])
->groupBy('phone')
->orderBy(['id'=>'asc']);
//var_dump(ddSql($query));die;
$count=count($query->all());
$data=$query->offset($num)->limit($limit)->all();
foreach ($data as $k=>$v){
$applyIds=(new \yii\db\Query())
->select('uniacid')->from('{{%ybwm_users}}')
->where(['phone'=>$v['phone']])->andWhere(['>','uniacid',''])
->andWhere(['type'=>1])->all();
$applyIds=array_column($applyIds,'uniacid');
$appData=[];
$appData=(new \yii\db\Query())->select('id uniacid,apply_name')->from('{{%ybwm_apply}}')->where(['in','id',$applyIds])->all();
if(empty($appData)){
unset($data[$k]);
}else{
$data[$k]['apply']=$appData;
}
$data[$k]['ratio']=$data[$k]['ratio']?:0;
}
return $this->result(1,'成功',array_values($data),$count);
}
//修改手机号可创建平台数量
public function actionSetAppNum(){
$result=axios_request();
$data=array(
'appNum'=>trim($result['appNum']),
'ratio'=>trim($result['ratio']),
);
$data=filter_array($data);
if($data){
$res = YII::$app->db->createCommand()->update('{{%ybwm_users}}', $data, 'phone=:phone', ['phone'=>$result['phone']])->execute();
}
echo json_encode(['code' => 1, 'msg' => '成功']);die;
}
//获取平台用户信息
public function actionApplyUserInfo(){
$request = Yii::$app->request;
if ($request->isGet){
$request=axios_request();
$re=(new \yii\db\Query())
->select('userName,passWord,phone,portrait')
->from('{{%ybwm_users}}')
->where(['uniacid'=>$request['id'],'id'=>$request['userId']])
->one();
echo json_encode(['code' => 1, 'msg' => '成功','data'=>$re]);die;
}
}
/**
* @return 平台用户添加
*/
public function actionUsersave(){
$request = Yii::$app->request;
if ($request->isPost){
$result=axios_request();
if($result['user_phone']){
if(strlen(trim($result['user_phone']))<>11){
echo json_encode(['code'=>2,'msg'=>'请输入正确的手机号码']);die;
}
// if(!preg_match("/^1[1-9]\d{9}$/", $result['user_phone'])){
// return $this->result(2,'请输入正确的手机号码');die;
// }
}
$query=(new \yii\db\Query())->from('{{%ybwm_apply}} a')
->select('a.id,apply_type,apply_image,apply_name,time_type,start_time,end_time,b.id userId,b.userName')
->leftJoin('{{%ybwm_users}} b','a.id=b.uniacid')
->where(['phone'=>trim($result['user_phone']),'b.type'=>1]);
//var_dump(ddSql($query));die;
$count=count($query->all());
$auth_key=Yii::$app->params['authkey'];
$re=YII::$app->db->createCommand("select phone,userName,passWord,salt from {{%ybwm_users}} where id=:id",['id'=>$result['userId']])->queryOne();
// $appNumRe=(new \yii\db\Query())->from('{{%ybwm_users}}')->where(['phone'=>trim($result['user_phone']),'type'=>1])->one();
// if($result['userId']){
// if(trim($result['user_phone'])&&trim($result['user_phone'])==$re['phone']){
// if($appNumRe['appNum']<=$count){
// echo json_encode(['code' => 2, 'msg' => '手机号对应可平台数量达到上限']);die;
// }
// }
// }else{
// if($appNumRe){
// //判断手机号可创建平台次数
// if($appNumRe['appNum']<=$count){
// echo json_encode(['code' => 2, 'msg' => '手机号对应可平台数量达到上限']);die;
// }
// }
// }
if($re){
if(!$result['userId']||$result['userId']==0){
echo json_encode(['code' => 3, 'msg' => '无效的参数id']);die;
}
$data=array(
'userName'=>$result['user_name'],
'phone'=>$result['user_phone'],
'portrait'=>$result['headimgurl'],
'type'=>1
);
if($result['newpassWord']){
$old_password=checkPassWord($re['salt'],$auth_key,$result['newpassWord']);
if($old_password!==$re['passWord']){
$salt=randoms(8);
$data['salt']=$salt;
$data['passWord']=checkPassWord($salt,$auth_key,$result['newpassWord']);
Yii::$app->session->set('userInfo','');
}
}
$data=filter_array($data);
$arr=[];
$arr=Yii::$app->session->get('userInfo');
if($data['userName']){
$arr['userName']=$data['userName'];
}
if($data['portrait']){
$arr['portrait']=$data['portrait'];
}
if($data['phone']){
$arr['phone']=$data['phone'];
}
Yii::$app->session->set('userInfo',$arr);
$res = YII::$app->db->createCommand()->update('{{%ybwm_users}}', $data, 'id=:id', ['id'=>$result['userId']])->execute();
}else{
$count=(new \yii\db\Query())
->from('{{%ybwm_users}}')
->where(['userName'=>$result['user_name']])
->one();
$adminInfo=(new \yii\db\Query())
->from('{{%ybwm_admin}}')
->where(['userName'=>$result['user_name']])
->one();
if($count||$adminInfo){
return $this->result(2,'用户名已存在');die;
}
$salt=randoms(8);
$password=checkPassWord($salt,$auth_key,$result['password']);
$data=array(
'uniacid'=>$result['id'],
'userName'=>$result['user_name'],
'phone'=>$result['user_phone'],
'createdAt'=>time(),
'salt'=>$salt,
'portrait'=>$result['headimgurl'],
'passWord'=>$password,
'type'=>1
);
$res=YII::$app->db->createCommand()->insert('{{%ybwm_users}}', $data)->execute();
}
if ($res!==false) {
return $this->result(1,'成功');
} else {
return $this->result(2,'网络异常,请稍后再试');
}
}
return $this->result(2,'请求异常');
}
//修改用户状态 放入回收站 删除
public function actionUserset(){
$request = Yii::$app->request;
if ($request->isPost){
$result=axios_request();
$status=$result['status'];
$res=YII::$app->db->createCommand()->delete('{{%ybwm_users}}','id=:id', ['id' =>$result['id']])->execute();
//$res=YII::$app->db->createCommand()->update('{{%ybwm_users}}',['status'=>$status,'up_operdate'=>date('Y-m-d H:i:s',time())], 'id=:id', ['id' =>$result['id']])->execute();
if($res!==false){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
return $this->result(2,'请求异常');
}
public function actionAdministration_user(){
$sessionData=Yii::$app->session->get('adminInfo');
if($sessionData['type']){
return $this->renderpartial('error');
}
return $this->renderpartial('administration_user');
}
public function actionAdministration_usermsg()
{
return $this->renderpartial('administration_usermsg');
}
}