审查视图

application/home/controller/Share.php 7.4 KB
王晓刚 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<?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(){
王晓刚 authored
38 39 40 41
        $user = Db::name('user')->where(['id'=>$this->user_id])->find();
        if(empty($user)){
            $this->error('查无此人');
        }
王晓刚 authored
42
        $exp_ratio = Db::name('exp_ratio')->where(['id'=>['in',[2,3]]])->select();
王晓刚 authored
43
        $this->assign('user',$user);
王晓刚 authored
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        $this->assign('exp_ratio',$exp_ratio);
        $this->assign('title','我的邀请');
        return $this->fetch();
    }

    public function get_poster(){
        $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'=>$url]);
    }

    /**
     * 图像处理
     * @param $url
     * @param $user
     * @param $type
     */
    private function processing($url,$user,$type){
        $user_id = $user['id'];
        //保存头像
王晓刚 authored
75
        $fileName = "avatar_$user_id";
王晓刚 authored
76 77 78 79
        $avatar=$this->getImage($user['avatar'],$fileName);
        if($avatar['code']==1){
            $this->error($avatar['msg']);
        }
王晓刚 authored
80
        $avatar_url = "./uploads/avatar/avatar_$user_id.jpeg";
王晓刚 authored
81 82 83 84 85 86
        $savePath = "./uploads/poster$type/";
        if(!file_exists($savePath)){
            mkdir ($savePath,0777,true);
        }
        //圆形的头像
        $image = \think\Image::open(ROOT_PATH."public/white.png");
王晓刚 authored
87
        $image->water($avatar_url,[300,40],100)
王晓刚 authored
88
            ->water(ROOT_PATH."public/bg$type.png")
王晓刚 authored
89
            ->water($url,[213,600],100)
王晓刚 authored
90
            ->save($savePath."poster_$user[id].png");
王晓刚 authored
91
        return "./uploads/poster$type/poster_$user[id].png";
王晓刚 authored
92 93 94 95 96 97 98 99 100
    }

    /**
     * 下载微信头像保存到本地
     * @param $url
     * @param $filename
     * @return array
     */
    private function getImage($url,$filename){
王晓刚 authored
101
        $save_dir='./uploads/avatar/';
王晓刚 authored
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        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;
王晓刚 authored
125
            $save_path= './uploads/avatar/'.$filename.'.'.$type;
王晓刚 authored
126
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content)))) {
王晓刚 authored
127 128
                //等比放大缩小二维码
                $image = \think\Image::open($save_path);
王晓刚 authored
129
                $image->thumb(88, 90,\think\Image::THUMB_SCALING)->save($save_path);
王晓刚 authored
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                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);
王晓刚 authored
154 155
            //等比放大缩小二维码
            $image = \think\Image::open($savePath.$file_name);
王晓刚 authored
156
            $image->thumb(260, 260,\think\Image::THUMB_SCALING)->save($savePath.$file_name);
王晓刚 authored
157
        }
王晓刚 authored
158
        return "./uploads/code_img/$file_name";
王晓刚 authored
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    }

    /**
     * 生成带参二维码
     * @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;
        }
    }
}