<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 用户海报
 */
class Userposter extends Api
{


    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    public function testhaibao(){
        $haibao = $this->haibao($this->auth->id,['introcode'=>$this->auth->introcode]);
//        dump($haibao);
        $this->success('success', $haibao);
    }

    //海报
    public function haibao($player_id,$data){

        //下载页二维码,没必要保留
        $params = [
            'text' => config('img_url').'?code=' . $data['introcode'],
            'size' => 90,
            'logo' => false,
            'label' => false,
            'padding' => 0,
        ];
        $qrCode = \addons\qrcode\library\Service::qrcode($params);
        $qrcode_path = ROOT_PATH . 'public/uploads/hbplayer/'.date('Ymd');
        if (!is_dir($qrcode_path)) {
            @mkdir($qrcode_path);
        }
        if (is_really_writable($qrcode_path)) {
            $filename = 'download'.$player_id.'.png';
            $download_qrcode = $qrcode_path.'/'.$filename;
            $qrCode->writeFile($download_qrcode);
        }
        //海报
        $filepath = '/uploads/hbplayer/'.date('Ymd') .'/'. $filename;
        $haibao = $this->createhaibao($filepath,$player_id,$data);

        return $haibao;
    }

    /*public function createhaibao($download_qrcode,$player_id,$sub_data){

        //背景图
        $background = $sub_data['background'] ? $sub_data['background'] : config('img_url').'/assets/img/haibao.png';
        //海报图片路径
        $new_path = 'uploads/hbplayer/'.date("Ymd").'/';
        mk_dir($new_path);
        $wap_file_name  = $new_path .'wap_player_'. $player_id . '.png';

        //二维码
        $download_qrcode= config('img_url').'/'.$download_qrcode;


        //合成wap图片
        $image = new \addons\poster\library\Image2();
        $imgurl = $image->createPosterImage($background,$download_qrcode,$sub_data['introcode'],$wap_file_name);

        return '/'.$wap_file_name;
    }*/

    public function createhaibao($download_qrcode,$player_id,$sub_data){
        //二维码

        $data = [
            [
                "left" => "15px",
                "top" => "296px",
                "type" => "img",
                "width" => "58px",
                "height" => "58px",
                "src" => one_domain_image($this->auth->avatar)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
            ],
            [
                "left" => "81px",
                "top" => "300px",
                "type" => "nickname",
                "width" => "80px",
                "height" => "24px",
                "size" => "12px",
                "color" => "#000",
                "content" => $this->auth->nickname
            ],
            [
                "left" => "81px",
                "top" => "327px",
                "type" => "nickname",
                "width" => "80px",
                "height" => "24px",
                "size" => "12px",
                "color" => "#000",
                "content" => $this->auth->introcode
            ],
            [
                "left" => "227px",
                "top" => "283px",
                "type" => "img",
                "width" => "80px",
                "height" => "80px",
                "src" => httpurllocal($download_qrcode)
            ]
        ];

        $data = json_encode($data, 320);

        $poster = [
            'id' => $this->auth->id,
            'title' => '星链',
            'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
            'bg_image' => $sub_data['background'] ? localpath_to_netpath($sub_data['background']) : '/assets/img/inviteposter.png',
            'data' => $data,
            'status' => 'normal',
            'weigh' => 0,
            'createtime' => 1653993709,
            'updatetime' => 1653994259,
        ];

        $image = new \addons\poster\library\Image();
        $imgurl = $image->createPosterImage($poster, $this->auth->getUser());

        if (!$imgurl) {
            $this->error('生成海报出错');
        }
//        $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
        return '/' . $imgurl;
    }

    //海报背景图
    public function posterlist() {
        $list = config('site.intro_images');
        if (!$list) {
            $this->success('success', (object)[]);
        }

        foreach ($list as &$v) {
            $v = config('img_url') . $this->createposter(localpath_to_netpath($v));
        }

        $this->success('success', $list);
    }

    //生成海报
    public function createposter($image = '') {
//        $image = input('image', '', 'trim');
//        if (!$image) {
//            $this->error('您的网络开小差啦~');
//        }

        $haibao = $this->haibao($this->auth->id,['introcode'=>$this->auth->introcode, 'background' => $image]);
        return $haibao;
//        $this->success('success', $haibao);
    }




}