Share.php 8.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/24
 * Time: 14:09
 */

namespace app\home\controller;


use app\common\controller\WechatBase;
use EasyWeChat\Foundation\Application;
use think\Db;

class Share extends WechatBase
{
    protected $user_id;
    function _initialize()
    {
        parent::_initialize();
        //判断是否授权
        $user_id = get_current_user_id();
        if(empty($user_id)){
            $this->redirect('user/authorization_view');
        }
        $this->user_id = $user_id;
    }

    /**
     * 我的邀请
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>['in',[2,3]]])->select();
        $this->assign('user',$user);
        $this->assign('exp_ratio',$exp_ratio);
        $this->assign('title','我的邀请');
        return $this->fetch();
    }

    public function get_poster(){
        $domain_name = $this->request->domain();//域名
        $user_id = $this->request->param('user_id',0,'intval');
        $type = $this->request->param('type',0,'intval');
        if(empty($user_id) || empty($type)){
            $this->error('404');
        }
        $user = Db::name('user')->where(['id'=>$user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
        //获取带参二维码
        $code_img = $this->get_code_img($user_id);
        //图像处理
        $url = $this->processing($code_img,$user,$type);
        $this->success('SUCCESS','',['url'=>$domain_name.$url]);
    }

    /**
     * 图像处理
     * @param $url
     * @param $user
     * @param $type
     */
    private function processing($url,$user,$type){
        $user_id = $user['id'];
        //保存头像
        if(stripos($user['avatar'],'http')){
            $fileName = "avatar_$user_id";
            $avatar=$this->getImage($user['avatar'],$fileName);
            if($avatar['code']==1){
                $this->error($avatar['msg']);
            }
            $avatar_url = "./uploads/avatar/avatar_$user_id.jpeg";
        }else{
            $avatar_url = "./".$user['avatar'];
            $image = \think\Image::open($avatar_url);
            $image->thumb(65, 65,\think\Image::THUMB_SCALING)->save($avatar_url);
        }
        dump($avatar_url);
        $savePath = "./uploads/poster$type/";
        if(!file_exists($savePath)){
            mkdir ($savePath,0777,true);
        }
        //圆形的头像
        $image = \think\Image::open(ROOT_PATH."public/white.png");
        if($type == 1){
            $image->water($avatar_url,[311,46.5],100)
                ->water(ROOT_PATH."public/bg$type.png")
                ->water($url,[212,600],100)
                ->save($savePath."poster_$user[id].png");
        }else{
            $image->water($avatar_url,[311,46.5],100)
                ->water(ROOT_PATH."public/bg$type.png")
                ->water($url,[212,622],100)
                ->save($savePath."poster_$user[id].png");
        }
        return "./uploads/poster$type/poster_$user[id].png";
    }

    /**
     * 下载微信头像保存到本地
     * @param $url
     * @param $filename
     * @return array
     */
    private function getImage($url,$filename){
        $save_dir='./uploads/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= './uploads/avatar/'.$filename.'.'.$type;
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content)))) {
                //等比缩放二维码
                $image = \think\Image::open($save_path);
                $image->thumb(65, 65,\think\Image::THUMB_SCALING)->save($save_path);
                return ['code'=>0,'file_path'=>$new_file,'save_path'=>$save_path];
            }else{
                return ['code'=>1,'msg'=>'图片保存失败'];
            }
        }else{
            return ['code'=>1,'msg'=>'图片格式错误'];
        }
    }

    /**
     * 获取带参二维码
     * @param $user_id
     * @return string
     */
    private function get_code_img($user_id){
        $savePath = "./uploads/code_img/";
        if (!file_exists($savePath)){
            mkdir($savePath, 0777,true);
        }
        $file_name = "code_img$user_id.png";
        if(!file_exists($savePath."code_img$user_id.png")){
            $code_img = $this->code_img($user_id);
            $code = file_get_contents($code_img);
            file_put_contents($savePath.$file_name,$code);
            //等比缩放缩小二维码
            $image = \think\Image::open($savePath.$file_name);
            $image->thumb(260, 260,\think\Image::THUMB_SCALING)->save($savePath.$file_name);
        }
        return "./uploads/code_img/$file_name";
    }

    /**
     * 生成带参二维码
     * @param $user_id
     * @return string
     */
    private function code_img($user_id)
    {
        $options=config('wechat');
        $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' => "$user_id"]]
        ];
        $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;
    }

    /**
     * 获取二维码提交
     * @param $url
     * @param $data
     * @return mixed|resource
     */
    private 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;
        }
    }
}