<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/6/8 * Time: 18:59 */ namespace app\portal\controller; use cmf\controller\WeChatBaseController; use EasyWeChat\Foundation\Application; use think\Db; class ShareController extends WeChatBaseController { /** * 我的推广页面 * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function index(){ //判断用户是否填写手机号 $user_id = cmf_get_current_user_id(); $my_user = Db::name('my_user')->where('uid',$user_id)->find(); $user = Db::name('user')->where('id',$user_id)->find(); if(empty($my_user)){ $this->error('查询为空','','',''); } if($my_user['is_pro'] != 1){ $this->error('无权限','','',''); } /* if(empty($my_user['phone'])){ $this->redirect('information'); }*/ //判断有没有生成过 // if(!file_exists('./upload/poster/poster_'.$user_id.".png")){ $url = $this->get_url($my_user['id'],$my_user['status']); $webPath = 'qrcode/code_img'.$user_id.$my_user['status'].'.png'; $savePath = './upload/'.$webPath; //生成带参二维码 if(!file_exists($savePath)){ $prcode_url = $this->code_img($my_user['id'],$my_user['status']); } $savePath = './upload/poster'; if(!file_exists($savePath)){ mkdir ($savePath,0777,true); } if($my_user['status'] == 3){ //老师 $image = \think\Image::open(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png"); $image->thumb(300,300,\think\Image::THUMB_FIXED)->save('./upload/qrcode/code_img'.$user_id.$my_user['status'].".png"); $image = \think\Image::open(ROOT_PATH."public/bg2.png"); $image->water(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png",[410,1015],100) // ->water(ROOT_PATH."public/bg2.png",\think\Image::WATER_CENTER,100) ->save('./upload/poster/poster_'.$user_id.".png"); }else{ //业务员 //保存头像 $avatar=$this->getImage($user['avatar'],'avatar_'.$user['id']); if($avatar['code']==1){ $this->error($avatar['msg']); } $image = \think\Image::open(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".jpeg"); $image->thumb(173,173,\think\Image::THUMB_FIXED)->save('./upload/avatar/avatar_'.$user_id.".png"); $image = \think\Image::open(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png"); $image->thumb(360,360,\think\Image::THUMB_FIXED)->save('./upload/qrcode/code_img'.$user_id.$my_user['status'].".png"); $image = \think\Image::open(ROOT_PATH."public/white.png"); $image->water(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png",[195,725],100) ->water(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".png",[289,478],100) ->water(ROOT_PATH."public/bg.png",\think\Image::WATER_CENTER,100) ->save('./upload/poster/poster_'.$user_id.".png"); } // } $share_image = cmf_get_image_url('poster/poster_'.$user_id.".png"); $this->assign('share_image',$share_image); //分享 $options = config('wechat_config'); $app = new Application($options); $js = $app->js; $jssdk = $js->config(array('onMenuShareAppMessage', 'onMenuShareTimeline','onMenuShareQQ'), false, false, true); $this->assign('jssdk',$jssdk); //分享内容 $share_url = url('to_user',array('user_id'=>$user_id),false,true); $share['share_title'] = "$user[user_nickname]的海报"; $share['share_desc'] = '快来加入我的团队'; $share['share_image'] = $share_image; $share['share_url'] = $share_url; $this->assign('status',$my_user['status']); $this->assign('share',$share); return $this->fetch(); } public function to_user(){ $user_id = $this->request->param('user_id',0,'intval'); if(empty($user_id)){ $this->error('缺少必要参数'); } $my_user = Db::name('my_user')->where('uid',$user_id)->find(); $user = Db::name('user')->where('id',$user_id)->find(); if(empty($my_user)){ $this->error('查询为空','','',''); } if($my_user['is_pro'] != 1){ $this->error('无权限','','',''); } $url = $this->get_url($my_user['id'],$my_user['status']); $webPath = 'qrcode/code_img'.$user_id.$my_user['status'].'.png'; $savePath = './upload/'.$webPath; //生成带参二维码 if(!file_exists($savePath)){ $prcode_url = $this->get_code($my_user['id'],$my_user['status']); } $savePath = './upload/poster'; if(!file_exists($savePath)){ mkdir ($savePath,0777,true); } if($my_user['status'] == 3){ //老师 $image = \think\Image::open(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png"); $image->thumb(300,300,\think\Image::THUMB_FIXED)->save('./upload/qrcode/code_img'.$user_id.$my_user['status'].".png"); $image = \think\Image::open(ROOT_PATH."public/bg2.png"); $image->water(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png",[410,1015],100) // ->water(ROOT_PATH."public/bg2.png",\think\Image::WATER_CENTER,100) ->save('./upload/poster/poster_'.$user_id.".png"); }else{ //业务员 //保存头像 $avatar=$this->getImage($user['avatar'],'avatar_'.$user['id']); if($avatar['code']==1){ $this->error($avatar['msg']); } $image = \think\Image::open(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".jpeg"); $image->thumb(173,173,\think\Image::THUMB_FIXED)->save('./upload/avatar/avatar_'.$user_id.".png"); $image = \think\Image::open(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png"); $image->thumb(360,360,\think\Image::THUMB_FIXED)->save('./upload/qrcode/code_img'.$user_id.$my_user['status'].".png"); $image = \think\Image::open(ROOT_PATH."public/white.png"); $image->water(ROOT_PATH."public/upload/qrcode/code_img".$user_id.$my_user['status'].".png",[195,725],100) ->water(ROOT_PATH."public/upload/avatar/avatar_".$user_id.".png",[289,478],100) ->water(ROOT_PATH."public/bg.png",\think\Image::WATER_CENTER,100) ->save('./upload/poster/poster_'.$user_id.".png"); } $share_image = cmf_get_image_url('poster/poster_'.$user_id.".png"); $this->assign('status',$my_user['status']); $this->assign('share_image',$share_image); return $this->fetch(); } public function information(){ $user_id = cmf_get_current_user_id(); $data = Db::name('my_user')->where(['uid'=>$user_id])->find(); $this->assign('data',$data); return $this->fetch(); } public function information_save(){ $param = $this->request->param(); if(empty($param['name'])){ $this->error('姓名不能为空'); } if(empty($param['phone'])){ $this->error('手机号不能为空'); } $data['name'] = $param['name']; $data['phone'] = $param['phone']; $user_id = cmf_get_current_user_id(); $result = Db::name('my_user')->where('uid',$user_id)->update($data); if(empty($result)){ $this->error('保存失败'); }else{ $this->success('保存成功'); } } /** * 分享链接 * @param $user_id * @param $status * @return string */ public function get_url($user_id,$status){ //http://xkeasy.w.bronet.cn/portal/index/index/my_user_id/用户id/status_id/身份id $url = url('portal/index/index',['my_user_id'=>$user_id,'status_id'=>$status],true,true); return $url; } /** * 生成二维码 * @param $user_id * @param $qrData * @return bool|string */ public function get_code($user_id,$qrData){ /*存储本地*/ $webPath = 'qrcode/'; $savePath = './upload/'.$webPath; if(!file_exists($savePath)){ mkdir ($savePath,0777,true); } $saveName = "share_".$user_id; $qrLevel = 'H'; $qrSize = '8'; $savePrefix = ""; if($filename = createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix,$saveName)){ $pic = $webPath .$savePrefix. $saveName . '.png'; return $pic; }else{ return false; } } public function code_img($my_user_id,$status){ //生成带参二维码 $savePath=ROOT_PATH.'public/upload/qrcode/'; if (!file_exists($savePath)){ mkdir($savePath, 0777,true); } $code_img = $this->code_img1($my_user_id,$status); $code = file_get_contents($code_img); $user_id = Db::name('my_user')->where('id',$my_user_id)->value('uid'); file_put_contents(ROOT_PATH."public/upload/qrcode/code_img$user_id$status.png",$code); return $savePath."code_img$user_id$status.png"; } /** * 生成带参二维码 */ public function code_img1($admin_id,$status) { $options=config('wechat_config'); $app = new Application($options); $accessToken = $app->access_token; // EasyWeChat\Core\AccessToken 实例 $token = $accessToken->getToken(false); $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=' . $token; $param = [ 'expire_seconds' => '', 'action_name' => 'QR_LIMIT_STR_SCENE', 'action_info' => ['scene' => ['scene_str' => "$admin_id"."-"."$status"]] ]; $result = $this->api_notice_increment($url, json_encode($param)); $data = json_decode($result, true); $ticket = urlencode($data['ticket']); $qr_url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . $ticket; return $qr_url; } /** * 获取二维码提交 */ public function api_notice_increment($url, $data) { // $data=json_encode($data); $ch = curl_init(); $header = array("Accept-Charset: utf-8"); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { curl_close($ch); return $ch; } else { curl_close($ch); return $tmpInfo; } } /** * 下载微信头像保存到本地 * @param $url * @param $filename * @return array */ function getImage($url,$filename){ $save_dir='./upload/avatar/'; if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){ return ['code'=>1,'msg'=>'图片路径错误']; } $header = array( 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0', 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept-Encoding: gzip, deflate',); $curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);curl_setopt($curl, CURLOPT_ENCODING, 'gzip'); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); $data = curl_exec($curl); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($code == 200) {//把URL格式的图片转成base64_encode格式的! $imgBase64Code = "data:image/jpeg;base64," . base64_encode($data); }else{ return ['code'=>1,'msg'=>'图片转换失败']; } $img_content=$imgBase64Code;//图片内容 if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result)) { $type = $result[2]; $new_file = $save_dir.$filename.'.'.$type; $save_path= 'avatar/'.$filename.'.'.$type; if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content)))) { return ['code'=>0,'file_path'=>$new_file,'save_path'=>$save_path]; }else{ return ['code'=>1,'msg'=>'图片保存失败']; } }else{ return ['code'=>1,'msg'=>'图片格式错误']; } } }