123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- <?php
- namespace app\api\controller;
- use app\common\model\UserFacility;
- use app\common\model\UserGroup;
- use app\common\model\UserLevelRank;
- use app\common\model\UserLoginLog;
- use app\common\validate\UserVali;
- use app\common\model\User;
- use app\common\model\UserWallet;
- use Elasticsearch\Endpoints\License\Post;
- use library\service\CaptchaService;
- use library\tools\Data;
- use think\cache\driver\Redis;
- use think\Db;
- use EasyWeChat\Factory;
- use app\common\model\InviteInfo;
- use function AlibabaCloud\Client\value;
- /**
- * @title 用户登录
- * @controller Login
- */
- class Login extends Base
- {
- // 需要登录的
- protected $need_login = [
- 'addActive'
- ];
- /**
- * @title 用户统一登录
- * @desc 用户登录
- * @author qc
- * @url /api/Login/unifiedLogin
- * @method POST
- * @tag 登录 授权
- * @param name:account_type type:int require:1 default:1 desc:账号类型1企业2个人
- * @param name:phone_pre type:int require:1 default:86 desc:手机号前缀
- * @param name:account type:int require:1 default:-- desc:账号
- * @param name:code type:int require:1 default:-- desc:验证码
- * @param name:facility_type type:int require:1 default:-- desc:设备类型1:安卓手机设备,2:ios手机设备,3:安卓ipad,4:iosipad,5:h5,6pc电脑设备号
- * @param name:facility_code type:string require:1 default:-- desc:设备号
- * @return name:token type:string default:-- desc:用户登录成功后的token值
- * @return name:phone type:string default:-- desc:绑定的手机号
- */
- public function unifiedLogin()
- {
- $code = input('post.code','');
- $account = input('post.account','');
- $phone_pre = input('post.phone_pre','86');
- $account_type = input('post.account_type', 1);
- $facility_type = input('post.facility_type', 1);
- $facility_code = input('post.facility_code');
- $ret_data = ['code' => 200, 'token' => ''];
- $msg = '登录成功';
- Db::startTrans();
- try {
- $check_code = $this->checkPhoneCode($account,$code,$phone_pre);
- if(!in_array($account,config('power.phone'))) {
- // 上线需要去掉注释
- if(!$check_code) $this->exception('验证码错误');
- $this->updatePhoneCode($check_code);
- }
- $where = [];
- $where[] = $account_type == 1 ? ['email','=',$account] : ['phone','=',$account];
- if($account_type == 1) {
- $where[] = ['email','=',$account] ;
- }else{
- $where[] = ['phone','=',$account] ;
- $where[] = ['phone_pre','=',$phone_pre] ;
- }
- $where[] = ['is_deleted','=',0];
- $user_info = User::where($where)->find();
- if(!$facility_code) $this->exception('设备号不能为空');
- if(!$user_info) {
- $reg_data = [];
- $reg_data['account_type'] = $account_type;
- $reg_data['facility_'.$facility_type] = $facility_code;// 注册设备号
- if($account_type == 1){
- $group_title = explode('@',$account)[1];
- $group_info = UserGroup::where(['title'=>'@'.$group_title])->find();
- $reg_data['email'] = $account;
- $validate = new UserVali();
- if (!$validate->scene('email_login')->check(['email'=>$account])) $this->exception($validate->getError());
- if($group_info){
- $reg_data['name'] = 'G企业用户_'.$group_info->name.'_'.$group_title;
- $check_group = User::where(['group_id'=>$group_info->id,'is_first'=>1])->value('id');//该公司是否有第一个注册的
- $reg_data['group_id'] = $group_info->id;
- if(!$check_group) $reg_data['is_first'] =1;
- if($check_group)$reg_data['group_first'] = $check_group;
- }else{
- $this->exception('该企业邮箱ID无效');
- // $reg_data['name'] = 'G企业用户_'.$group_title;
- }
- }else{
- $validate = new UserVali();
- if (!$validate->scene('phone_login')->check(['phone'=>$account])) {
- $this->exception($validate->getError());
- }
- $reg_data['phone'] = $account;
- $reg_data['phone_pre'] = $phone_pre;
- $reg_data['name'] = 'G'. substr_replace($account,'****',3,4);
- }
- $user_info = User::create($reg_data);
- if($account_type == 1 && isset_full_check($reg_data,'is_first',1)) User::where('id',$user_info->id)->update(['group_first'=>$user_info->id]);
- $reg_level = sysconf('vip_experience');
- // 非企业会员注册,是否怎送会员
- if($reg_level && $account_type == 2) UserLevelRank::create(['user_id'=>$user_info->id,'level_id'=>$reg_level,'start_time'=>time(),'end_time'=>time()+86400*7,'end_date'=>date('Y-m-d H:i:s',time()+86400*7)]);
- $user_info = $user_info->toArray();
- }else{
- $user_info = $user_info->toArray();
- // 已设置登录设备号
- if($user_info['status'] != 1) $this->exception('用户被禁用');
- }
- // 设备号验证
- if(!UserFacility::checkFacility($user_info['id'],$facility_type,$facility_code,$account)) $this->exception('设备号错误');
- if(empty($user_info['hx_username']) || !$user_info['hx_username']) hx_register($user_info['id']);
- $ret_data['token'] = $this->createJwt($user_info['id'],$facility_code,$facility_type);
- if($user_info['account_type'] == 1) {
- if(empty($user_info['bind_id'])) {
- $ret_data['phone'] = null;
- }else{
- $ret_data['phone'] = User::where('id',$user_info['bind_id'])->value('phone');
- }
- }else{
- $ret_data['phone'] = $user_info['phone'];
- }
- $redis = new Redis();
- // 登录账号的token
- $redis->set('TOKEN_'.$user_info['id'].'_'.$facility_type,$ret_data['token'],86400 * 7);//保存redis 天
- UserLoginLog::creatLog($user_info['id'],$facility_type,$facility_code,$account,$ret_data['token']);
- Db::commit();
- }catch (\Exception $e){
- $ret_data['code'] = 201;
- $ret_data['phone'] = null;
- $msg =$e->getMessage();
- Db::rollback();
- }
- $ret_data['code'] == 200 ? $this->success($msg,$ret_data):$this->error($msg,$ret_data);
- }
- /**
- * @title 获取验证码
- * @desc 获取验证码
- * @author qc
- * @url /api/Login/getCaptcha
- * @method GET
- * @return name:image type:string default:-- desc:图片
- * @return name:uniqid type:string default:-- desc:uniqid
- */
- public function getCaptcha()
- {
- $image = CaptchaService::instance();
- $captcha = ['image' => $image->getData(), 'uniqid' => $image->getUniqid()];
- $this->success('生成验证码成功', $captcha);
- }
- public static function create_emchat_user($user_id,$nick_name) {
- $user_emchat_name = 'Act_'.$user_id;
- $user_emchat_password = 'abc123456';
- $base_set = [
- 'client_id' => 'YXA6NS5H0GDGEe20Q9VWb7Fpew',
- 'client_secret' => 'YXA6zkYKRQMJbxi_6cN1ERHuFn9QUpI',
- 'org_name' => '1414221110068467',
- 'app_name' => 'kefuchannelapp104968',
- ];
- $emchat = new Easemob($base_set);
- $result = $emchat->createUser($user_emchat_name, $user_emchat_password,$nick_name); //创建emchat用户
- if (isset($result['error']) && $result['error'] != '') {
- return false;
- }
- return ['user_emchat_name' => $user_emchat_name, 'user_emchat_password' => $user_emchat_password];
- }
- /**
- * @title 查看清除设备号次数
- * @desc 查看清除设备号次数
- * @author qc
- * @url /api/Login/facility_num
- * @method GET
- * @param name:account_type type:int require:1 default:1 desc:账号类型1企业2个人
- * @param name:phone_pre type:int require:1 default:86 desc:手机号前缀
- * @param name:account type:int require:1 default:-- desc:账号
- * @return name:image type:string default:-- desc:图片
- * @return name:uniqid type:string default:-- desc:uniqid
- */
- public function facility_num()
- {
- $data = input();
- $account_type = $data['account_type'];
- $account = $data['account'];
- $phone_pre = $data['phone_pre'];
- $where = [];
- if($account_type == 1) {
- $where[] = ['email','=',$account] ;
- }else{
- $where[] = ['phone','=',$account] ;
- $where[] = ['phone_pre','=',$phone_pre] ;
- }
- $user_info = User::where($where)->find();
- $data['user_id'] = $user_info['id'];
- $time = strtotime($user_info['create_at']); // 将日期转换为时间戳
- //当前年份
- $year = date('Y');
- $his = date('-m-d H:i:s',$time);
- $timestamp_e = $year.$his; // 格式化输出结果
- $time_e = strtotime($timestamp_e); // 将日期转换为时间戳
- $oneYearLaterTimestamp = $time_e - (60 * 60 * 24 * 365); // 减少一年的秒数
- $timestamp_s = date('Y-m-d H:i:s', $oneYearLaterTimestamp); // 格式化输出结果
- // $timestamp_e = date('Y-m-d H:i:s',mktime(23, 59, 59, 12, 31, $user_info['create_at']));
- $log = Db::name('facility_log')->where('user_id',$data['user_id'])
- ->where('operate','clean')
- ->where('create_at','>',$timestamp_s)
- ->where('create_at','<',$timestamp_e)->count();
- $num = 2 - $log;
- if($num < 0){
- $num = 0;
- }
- $arr = [
- 'timestamp_s' => $timestamp_s,
- 'timestamp_e' => $timestamp_e,
- 'num' => $num
- ];
- $this->success('操作成功,剩余'.$num.'次',$arr);
- }
- /**
- * @title 清除设备号
- * @desc 清除设备号
- * @author qc
- * @url /api/Login/facility
- * @method GET
- * @param name:account_type type:int require:1 default:1 desc:账号类型1企业2个人
- * @param name:phone_pre type:int require:1 default:86 desc:手机号前缀
- * @param name:account type:int require:1 default:-- desc:账号
- * @param name:facility_type type:int require:1 default:-- desc:设备类型1:安卓手机设备,2:ios手机设备,3:安卓ipad,4:iosipad,5:h5,6pc电脑设备号
- * @return name:image type:string default:-- desc:图片
- * @return name:uniqid type:string default:-- desc:uniqid
- */
- public function facility(){
- $data = input();
- // if(!$this->user_id){
- // $this->error('请先登录');
- // }
- $account_type = $data['account_type'];
- $account = $data['account'];
- $phone_pre = $data['phone_pre'];
- $facility_type = $data['facility_type'];
- $where = [];
- if($account_type == 1) {
- $where[] = ['email','=',$account] ;
- }else{
- $where[] = ['phone','=',$account] ;
- $where[] = ['phone_pre','=',$phone_pre] ;
- }
- $user_info = User::where($where)->find();
- $data['user_id'] = $user_info['id'];
- // $timestamp_s = date('Y-m-d H:i:s',mktime(0, 0, 0, 1, 1, date('Y')));
- // $timestamp_e = date('Y-m-d H:i:s',mktime(23, 59, 59, 12, 31, date('Y')));
- $time = strtotime($user_info['create_at']); // 将日期转换为时间戳
- //当前年份
- $year = date('Y');
- $his = date('-m-d H:i:s',$time);
- $timestamp_e = $year.$his; // 格式化输出结果
- $time_e = strtotime($timestamp_e); // 将日期转换为时间戳
- $oneYearLaterTimestamp = $time_e - (60 * 60 * 24 * 365); // 减少一年的秒数
- $timestamp_s = date('Y-m-d H:i:s', $oneYearLaterTimestamp); // 格式化输出结果
- // return $timestamp_s.'-'.$timestamp_e;
- $log = Db::name('facility_log')->where('user_id',$data['user_id'])
- ->where('operate','clean')
- ->where('create_at','>',$timestamp_s)
- ->where('create_at','<',$timestamp_e)->count();
- $num = 2 - $log;
- if($num < 0){
- $num = 0;
- }
- if($log >= 2){
- $this->error('今年的清除次数已耗尽');
- }
- // return json($data);
- $redis = new Redis();
- for ($i=1;$i<=4;$i++) {
- // $check_facility = UserFacility::where(['user_id'=>$data['user_id'],'type'=>$i,'facility'=>$data['facility_'.$i]])->value('id');
- $redis = new Redis();
- // if(isset($data['facility_'.$i])){
- Data::save('UserFacility',['user_id'=>$data['user_id'],'type'=>$i,'facility'=>'','set_time'=>date('Y-m-d H:i:s')], 'user_id',['user_id'=>$data['user_id'],'type'=>$i]);
- // if($data['facility_'.$i] == ''){
- $redis->rm('TOKEN_'.$data['user_id'].'_'.$i);
- // var_dump('TOKEN_'.$data['user_id'].'_'.$i);die;
- $aliases = $this->aliases($data['user_id'],[],'GET','?new_format=true');
- if(isset($aliases['data']) && $aliases['data'] != '' && $aliases['data'] != null){
- if(count($aliases['data']) > 0){
- //别名下有绑定的Registration ID
- $registration_ids = [];
- foreach ($aliases['data'] as $k => $v){
- if($i == 1 || $i == 3){
- //安卓
- if($facility_type == $i){
- if($v['platform'] == "android"){
- $registration_ids[] = $v['registration_id'];
- }
- }
- }else if($i == 2 || $i == 4){
- //ios
- if($facility_type == $i) {
- if ($v['platform'] == "ios") {
- $registration_ids[] = $v['registration_id'];
- }
- }
- }
- }
- if(count($registration_ids) > 0){
- $body = [
- "registration_ids"=>[
- "remove"=> $registration_ids
- ]
- ];
- //var_dump($body);die;
- // var_dump($registration_ids);die;
- $aliases_del = $this->aliases($data['user_id'],$body,'POST');
- // var_dump($aliases_del);die;
- }
- }
- }
- // }
- // }
- }
- $arr = [
- 'user_id' => $data['user_id'],
- 'create_at' => date("Y-m-d H:i:s"),
- 'operate' => 'clean'
- ];
- Db::name('facility_log')->insert($arr);
- $this->success('操作成功,剩余'.($num - 1).'次');
- }
- // /**
- // * @title 推送查询
- // * @desc 推送查询
- // */
- public function aliases($id = 66,$registration_ids = [],$type = 'GET',$new_format = ''){
- $data = input();
- // if(!$data['url'] || !$data['from'] || !$data['to'] || !$data['url']){
- //
- // }
- $arr = [
- 'url'=>'https://device.jpush.cn/v3/aliases/'.$id.$new_format,
- 'AppKey'=>config('app.jiguang')['AppKey'],
- 'MasterSecret'=>config('app.jiguang')['MasterSecret'],
- ];
- // $hx_voice = hx_voice(24);
- // $url= $arr['url'].$arr['org_name'].'/'.$arr['app_name'].'/'.'messages/users';
- // $body['from'] = $data['form']; //'member2200'
- // $body['to'] = $data['to']; //worker1000
- // $body['type'] = 'audio';
- // $body['body'] = [
- // 'filename' => $data['filename'], //'testaudio.amr', //语音文件的名称
- // 'url' => $data['url'] //'http://tmp/bQo7Udv1FETr01c4bb0ecee3a2edb86b00a393176fcc.durationTime=2790.aac', //语音文件的url地址
- // ];
- $body = $registration_ids;
- $options['type']="audio";
- $options['msg']='发送';
- $b=json_encode($body);
- // return $b;
- $str = $arr['AppKey'].':'.$arr['MasterSecret'];
- //$str = '7d431e42dfa6a6d693ac2d04:5e987ac6d2e04d95a9d8f0d1';
- $base64 = base64_encode($str);
- //$header=['Authorization' => ];
- $header = array("Authorization: Basic ".$base64);
- // return json($header);
- $result=$this->postCurl($arr['url'],$b,$header,$type);
- // $result=$this->postCurl($url,$body,$header);
- //dump($result);
- return $result;
- // return $hx_voice;
- }
- public function postCurl($url,$body,$header,$type="POST"){
- //1.创建一个curl资源
- $ch = curl_init();
- //2.设置URL和相应的选项
- curl_setopt($ch,CURLOPT_URL,$url);//设置url
- //1)设置请求头
- // array_push($header, 'Accept:application/json');
- // array_push($header,'Content-Type:application/json');
- // array_push($header, 'http:multipart/form-data');
- //设置为false,只会获得响应的正文(true的话会连响应头一并获取到)
- curl_setopt($ch,CURLOPT_HEADER,0);
- // curl_setopt ( $ch, CURLOPT_TIMEOUT,5); // 设置超时限制防止死循环
- //设置发起连接前的等待时间,如果设置为0,则无限等待。
- curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
- //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //2)设备请求体
- if (strlen($body)>0) {
- //$b=json_encode($body,true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $body);//全部数据使用HTTP协议中的"POST"操作来发送。
- }
- //设置请求头
- // if(count($header)>0){
- // curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
- // }
- curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
- //上传文件相关设置
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 对认证证书来源的检查
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);// 从证书中检查SSL加密算
- //3)设置提交方式
- switch($type){
- case "GET":
- curl_setopt($ch,CURLOPT_HTTPGET,true);
- break;
- case "POST":
- curl_setopt($ch,CURLOPT_POST,true);
- break;
- case "PUT"://使用一个自定义的请求信息来代替"GET"或"HEAD"作为HTTP请求。这对于执行"DELETE" 或者其他更隐蔽的HTT
- curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"PUT");
- break;
- case "DELETE":
- curl_setopt($ch,CURLOPT_CUSTOMREQUEST,"DELETE");
- break;
- }
- //4)在HTTP请求中包含一个"User-Agent: "头的字符串。-----必设
- // curl_setopt($ch, CURLOPT_USERAGENT, 'SSTS Browser/1.0');
- // curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
- //curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)' ); // 模拟用户使用的浏览器
- //5)
- //3.抓取URL并把它传递给浏览器
- $res=curl_exec($ch);
- $result=json_decode($res,true);
- //4.关闭curl资源,并且释放系统资源
- curl_close($ch);
- if(empty($result))
- return $res;
- else
- return $result;
- }
- /**
- * @title 增加活跃度
- * @desc 增加活跃度
- * @author qc
- * @url /api/Login/addActive
- * @method Post
- * @param name:type type:int require:1 default:-- desc:设备类型1:安卓手机设备,2:ios手机设备,3:安卓ipad,4:iosipad,5:h5,6pc电脑设备号
- * @return name:image type:string default:-- desc:图片
- * @return name:uniqid type:string default:-- desc:uniqid
- */
- public function addActive(){
- $startTime = date('Y-m-d') . ' 00:00:00'; // 获取今天的起始时间
- $endTime = date('Y-m-d') . ' 23:59:59'; // 获取今天的结束时间
- if(!$this->user_id){
- $this->error('请先登录');
- }
- $active_log = Db::name('user_active_log')
- ->where('create_at','>',$startTime)
- ->where('create_at','<',$endTime)
- ->where('user_id',$this->user_id)
- ->find();
- if(!$active_log){
- $data = input();
- $arr = [
- 'user_id' => $this->user_id,
- 'create_at' => date('Y-m-d H:i:s'),
- 'type' => $data['type']
- ];
- Db::name('user_active_log')->insert($arr);
- }
- $this->success('操作成功');
- }
- }
|