AyrShareLogic.php 6.6 KB
<?php

namespace App\Http\Logic\Bside\AyrShare;

use App\Http\Logic\Bside\BaseLogic;
use App\Models\AyrShare\AyrShare;
use App\Models\File\File as FileModel;
use App\Models\File\Image as ImageModel;
/**
 * @name:社交发布图片数量
 * Facebook Pages and Groups: 10 images, including a carousel post.
 *   Instagram: 10 images.
 *   Google : 1 image.
 *   LinkedIn: 9 images.
 *   Pinterest: 1 image.
 *   Reddit: 1 image.
 *   Telegram: 1 image.
 *   Twitter:4 image
 */
class AyrShareLogic extends BaseLogic
{
    /**
     * @var :发布图片数量
     */
    public $send_num = [
        'facebook' => 10,
        'instagram' => 10,
        'google' => 1,
        'linkedin'=>9,
        'reddit'=>1,
        'pinterest'=>1,
        'telegram'=>1,
        'Twitter'=>1,
    ];

    public function __construct()
    {
        parent::__construct();
        $this->model = new AyrShare();
        $this->param = $this->requestAll;
    }

    /**
     * @name   :(创建账号并绑定写入数据库)ayr_add
     * @author :lyh
     * @method :post
     * @time   :2023/5/6 9:19
     */
    public function ayr_share_add($res){
        //插入数据库
        $param = [
            'title'=>$res['title'],
            'ref_id'=>$res['refId'],
            'profile_key'=>$res['profileKey'],
            'operator_id'=>$this->user['id'],
            'project_id'=>$this->user['project_id'],
            'name'=>$this->param['name'],
        ];
        $rs = $this->model->add($param);
        if($rs === false){
            $this->fail('error');
        }
        return $this->success();
    }

    /**
     * @name   :(获取当前数据详情)ayr_share_del
     * @author :lyh
     * @method :post
     * @time   :2023/5/6 10:16
     */
    public function ayr_share_info($share_id = ''){
        if(isset($this->param['id'])){
            $share_id = $this->param['id'];
        }
        $info = $this->model->read(['id'=>$share_id]);
        if($info === false){
            $this->fail('当前数据不存在或已被删除');
        }
        return $this->success($info);
    }

    /**
     * @name   :(更新)ayr_share_edit
     * @author :lyh
     * @method :post
     * @time   :2023/5/9 14:44
     */
    public function ayr_share_edit($param,$id = ''){
        $rs = $this->model->edit($param,['id'=>$id]);
        if($rs === false){
            $this->fail('error');
        }
        return $this->success();
    }
    /**
     * @name   :(删除ayr数据并同步删除)
     * @author :lyh
     * @method :post
     * @time   :2023/5/6 10:18
     */
    public function ayr_share_del(){
        $rs = $this->model->del($this->param);
        if($rs === false){
            $this->fail('删出失败');
        }
        return $this->success();
    }
    /**
     * @name   :(根据hash获取图片详情)save_info_info
     * @author :lyh
     * @method :post
     * @time   :2023/5/10 15:01
     */
    public function save_img_info($hash){
        $imageModel = new ImageModel();
        $info = $imageModel->read(['hash'=>$hash]);
        if($info === false){
            $this->fail('error');
        }
        return $this->success($info);
    }

    /**
     * @name   :(根据hash视频详情)save_info_info
     * @author :lyh
     * @method :post
     * @time   :2023/5/10 15:01
     */
    public function save_file_info($hash){
        $fileModel = new FileModel();
        $info = $fileModel->read(['hash'=>$hash]);
        if($info === false){
            $this->fail('error');
        }
        return $this->success($info);
    }

    /**
     * @name   :(更新图片库)save_img
     * @author :lyh
     * @method :post
     * @time   :2023/5/10 15:01
     */
    public function save_img($param_data){
        //根据文件hash存入数据库
        $ayr_data = [
            'ayr_id'=>$param_data['id'],
            'ayr_url'=>$param_data['url'],
            'ayr_url_link'=>$param_data['url_1080']
        ];
        $imageModel = new ImageModel();
        $rs = $imageModel->edit($ayr_data,['hash'=>$this->param['hash']]);
        if($rs === false){
            $this->fail('更新失败');
        }
        return $this->success();
    }

    /**
     * @name   :(获取图片的base64)base_img_content
     * @author :lyh
     * @method :post
     * @time   :2023/5/12 9:28
     */
    public function base_img_content($hash){
        $imageModel = new ImageModel();
        $info = $imageModel->read(['hash'=>$hash]);
        if($info === false){
            $this->fail('当前数据不存在');
        }
        $content = file_get_contents($info['path']);
        $img_type = $info['type'];
        $content = base64_encode($content);
        $img_base64 = 'data:image/' . $img_type . ';base64,' . $content;
        return $img_base64;
    }
    /**
     * @name   :(更新文件库)save_img
     * @author :lyh
     * @method :post
     * @time   :2023/5/10 15:01
     */
    public function save_file($param_data){
        //根据文件hash存入数据库
        $ayr_data = [
            'ayr_id'=>$param_data['id'],
            'ayr_url'=>$param_data['url'],
            'ayr_url_link'=>$param_data['url_1080']
        ];
        $imageModel = new FileModel();
        $rs = $imageModel->edit($ayr_data,['hash'=>$this->param['hash']]);
        if($rs === false){
            $this->fail('更新失败');
        }
        return $this->success();
    }

    /**
     * @name   :(验证平台参数)verify_param
     * @author :lyh
     * @method :post
     * @time   :2023/5/16 9:19
     * ["facebook","fbg","gmb","instagram","linkedin","pinterest","reddit","telegram","tiktok","twitter","youtube"]
     */
    public function verify_param($info){
        //验证发送平台
        foreach ($this->param['platforms'] as $k => $v){
            if(!in_array($v,json_decode($info['bind_platforms']))){
                $this->fail('未绑定平台');
            }
            if($v == 'reddit' && isset($this->param['video'])){
                $this->fail('不支持视频');
            }
            //验证图片数
            if(isset($this->param['images']) && !empty($this->param['images'])){
                $img_num = count($this->param['images']);
                if($img_num > $this->send_num[$v]){
                    $this->fail('发布图片数量超过最大限制,'.$v.'只允许'.$this->send_num[$v].'张图');
                }
            }
            //验证图片数
//            $img_num = count($this->param['video']);
//            if($img_num > 1){
//                $this->fail('发布视频数量超过最大限制,'.$v.'只允许'.$this->send_num[$v].'个视频');
//            }
        }
        return $this->success();
    }
}