<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['*'];

    public function index(){
        echo 'apisuccess';
        exit;
    }

    //好评有礼
    public function app_comment(){
        $images = input('images','');
        $platform = input('platform','android');

        $data = [
            'user_id' => $this->auth->id,
            'images'  => $images,
            'status'  => 0,
            'platform'  => $platform,
            'createtime'  => time(),
            'updatetime'  => time(),
        ];

        Db::name('app_comment')->insertGetId($data);
        $this->success();
    }


    /**
     * 获取邀请图片
     */
    public function getInviteImg() {
        $plat = $this->request->request("plat",1); //平台:1=小程序,2=app
        if(!in_array($plat,[1,2])) $this->error("参数错误");
        // 获取用户的邀请码
        $invitecode = \app\common\model\User::where(["id"=>$this->auth->id])->value("invite_no");

        // 文字图片合成
        $bigImgPath = $this->httpurlLocal('/assets/img/inviteimg.jpeg');
        $img = imagecreatefromstring(file_get_contents($bigImgPath));
        //字体文件
        $font = realpath('./assets/fonts/lato/lato-black.ttf');

        //字体颜色(RGB)
        $black = imagecolorallocate($img, 217, 76, 41);
        //字体大小
        $fontSize = 30;
        //旋转角度
        $circleSize = 0;
        //左边距
        $left = 275;
        //上边距
        $top = 540;
        imagefttext($img, $fontSize, $circleSize, $left, $top, $black, $font, $invitecode);
        $filename = date("YmdH").".jpeg";
        $path = "/uploads/qrcode/".$filename;
        $file = $_SERVER['DOCUMENT_ROOT'] . $path;//打开文件准备写入
        list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
        switch ($bgType) {
            case 1://gif
                header('Content-Type:image/gif');
                imagegif($img,$file);
                break;
            case 2://jpg
                header('Content-Type:image/jpg');
                imagejpeg($img,$file);
                break;
            case 3://jpg
                header('Content-Type:image/png');
                imagepng($img,$file);
                break;
            default:
                break;
        }
        //销毁照片
        imagedestroy($img);

        // 图片和二维码合成
        $qrcode = $plat == 1 ? config("site.miniqrcode"):config("site.qrcode");


        $background = $file;
        $target = $this->httpurl($qrcode);

        $background_iamge = imagecreatefromstring(file_get_contents($background));
        $target_image = imagecreatefromstring(file_get_contents($target));
        list($target_width, $target_height, $target_type) = getimagesize($target);
        imagecopymerge($background_iamge , $target_image , 250, 700, 0, 0, $target_width, $target_height, 100);
        list($background_width, $background_height, $background_type) = getimagesize($background);
        switch ($background_type) {
            case 1://gif
                header('Content-Type:image/gif');
                imagegif($background_iamge,$file);
                break;
            case 2://jpg
                header('Content-Type:image/jpg');
                imagejpeg($background_iamge,$file);
                break;
            case 3://jpg
                header('Content-Type:image/png');
                imagepng($background_iamge,$file);
                break;
            default:
                break;
        }
        $savepath = $this->httpurlLocal($path);

        $this->success("获取成功!",$savepath);
    }




    /**
     * 评论时间转换
     * @param null $time
     * @return false|string
     */
    private function get_last_time($time = NULL) {
        $text = '';
        $time = $time === NULL || $time > time() ? time() : intval($time);
        $t = time() - $time; //时间差 (秒)
        $y = date('Y', $time)-date('Y', time());//是否跨年
        switch($t){
            case $t == 0:
                $text = '刚刚';
                break;
            case $t < 60:
                $text = $t . '秒前'; // 一分钟内
                break;
            case $t < 60 * 60:
                $text = floor($t / 60) . '分钟前'; //一小时内
                break;
            case $t < 60 * 60 * 24:
                $text = floor($t / (60 * 60)) . '小时前'; // 一天内
                break;
            case $t < 60 * 60 * 24 * 3:
                $text = floor($time/(60*60*24)) ==1 ?'昨天 ' . date('H:i', $time) : '前天 ' . date('H:i', $time) ; //昨天和前天
                break;
            case $t < 60 * 60 * 24 * 30:
                $text = date('m月d日 H:i', $time); //一个月内
                break;
            case $t < 60 * 60 * 24 * 365&&$y==0:
                $text = date('m月d日', $time); //一年内
                break;
            default:
                $text = date('Y年m月d日', $time); //一年以前
                break;
        }

        return $text;
    }

}