<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 用户推荐
 */
class Userintro extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    //二维码海报
    private function mini_code($scene, $page_url, $introcode)
    {
        $value = config('wxMiniProgram');
        $appid = $value['appid'];
        $secret = $value['secret'];
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
        $res = file_get_contents($url);

        $token = json_decode($res, true)['access_token'];
        $URL = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $token;
        $data = [
            'scene' => 'introcode='.$introcode, //二维码传入参数
            'page' => $page_url, //扫码后进入页面
            'env_version' => 'trial', //要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"。默认是正式版。
            'width' => 280,  //二维码的宽度,单位 px,最小 280px,最大 1280px
            'auto_color' => false, //自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调,默认 false
            'is_hyaline' => false, //是否需要透明底色,为 true 时,生成透明底色的小程序
        ];
        $json = json_encode($data); //数组加密
        //$result = $this->api_notice_increment($URL, $json); //用CURL 进行POST请求
        $result = curl_post($URL,$json);

        $path  = ROOT_PATH . 'public/uploads/minicode'; //ROOT_PATH 我使用的是TP5框架
        $path2 =                   '/uploads/minicode';

        if (!file_exists($path)) { //判断目录是否存在
            mkdir($path, 0777, true);
        }
        $path  =  $path . '/' . $scene . '.png'; //最后要写入的目录及文件名
        $path2 = $path2 . '/' . $scene . '.png'; //最后要写入的目录及文件名

        file_put_contents($path, $result);

        return $path2;
    }

    //生成视频分享海报
    public function shareposter() {
        if($this->auth->group_id != 2){$this->error('你是不是来错地方了?');}

        $inviteimage = $this->mini_code($this->auth->id,'',$this->auth->username);

        $data = [

            [
                "left"=> "170px",
                "top"=> "500px",
                "type"=> "nickname",
                "width"=> "166px",
                "height"=> "38px",
                "size"=> "11px",
                "color"=> "#FFFFFF",
                "content" => '识别微信小程序',
            ],
            [
                "left"=> "170px",
                "top"=> "525px",
                "type"=> "nickname",
                "width"=> "166px",
                "height"=> "38px",
                "size"=> "11px",
                "color"=> "#FFFFFF",
                "content" => '更多游艇等你来~',
            ],
            [
                "left"=> "30px",
                "top"=> "460px",
                "type"=> "img",
                "width"=> "120px",
                "height"=> "120px",
                "src"=> httpurllocal($inviteimage)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
            ],

        ];


        $data = json_encode($data, 320);

        $poster = [
            'id' => 1,
            'title' => '测试',
            'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
            'bg_image' => '/assets/img/posteruserbg.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;

        //echo '<html><body><img src="'.$imgurl.'"></body></html>';

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


    //我邀请的人列表
    public function myintro_list(){
        if($this->auth->group_id != 2){$this->error('你是不是来错地方了?');}

        $list = Db::name('user')->field('id,nickname,avatar,createtime')->where('intro_uid',$this->auth->id)->autopage()->select();
        $list = list_domain_image($list,['avatar']);

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

    //////////////////////////////////////////////////////////////////////

    //邀请页多个信息
    public function introsite_info(){
        //本周
        $thisweek_start = strtotime(date('Y-m-d')) - ((date('w')==0?7:date('w'))-1)*86400;
        $thisweek_end   = $thisweek_start + 604799;
        //上周
        $lastweek_start = $thisweek_start - 604800;
        $lastweek_end   = $thisweek_end - 604800;

        //本周邀请人数
        $intro_num_thisweek = Db::name('user')->where('intro_uid',$this->auth->id)->where('createtime','BETWEEN',[$thisweek_start,$thisweek_end])->count();
        //上周邀请人数
        $intro_num_lastweek = Db::name('user')->where('intro_uid',$this->auth->id)->where('createtime','BETWEEN',[$lastweek_start,$lastweek_end])->count();
        //总邀请人数
        $intro_num_all      = Db::name('user')->where('intro_uid',$this->auth->id)->count();
        //本周奖励
        $money_sum_thisweek = Db::name('user_money_log')->where(['user_id'=>$this->auth->id,'log_type'=>63,'createtime'=>['BETWEEN',[$thisweek_start,$thisweek_end]]])->sum('change_value');
        //上周奖励
        $money_sum_lastweek = Db::name('user_money_log')->where(['user_id'=>$this->auth->id,'log_type'=>63,'createtime'=>['BETWEEN',[$lastweek_start,$lastweek_end]]])->sum('change_value');
        //我邀请的人
        $user_list = Db::name('user')->field('id,username,nickname,mobile,avatar,createtime')->where('intro_uid',$this->auth->id)->limit(3)->select();

        $rs = [
            'introsite_activeinfo' => config('site.introsite_activeinfo'),
            'introsite_introrule' => config('site.introsite_introrule'),
            'introcode' => $this->auth->introcode,
            'user_list' => $user_list,
            'money_sum_thisweek' => $money_sum_thisweek,
            'money_sum_lastweek' => $money_sum_lastweek,
            'intro_num_thisweek' => $intro_num_thisweek,
            'intro_num_lastweek' => $intro_num_lastweek,
            'intro_num_all'      => $intro_num_all,
        ];

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


}