SpotRepository.php 10.9 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/17
 * Time: 11:06
 */

namespace App\Repositories\Backend\Spot;


use EasyWeChat\Factory;
use App\Modules\Models\Spot\Spot;
use Illuminate\Support\Facades\DB;
use App\Exceptions\GeneralException;
use App\Events\Backend\Access\Spot\SpotCreated;
use App\Events\Backend\Access\Spot\SpotDeleted;
use App\Events\Backend\Access\Spot\SpotUpdated;
use App\Modules\Repositories\Spot\BaseSpotRepository;

use Illuminate\Support\Facades\Log;
use Intervention\Image\ImageManagerStatic as Image;
class SpotRepository extends BaseSpotRepository
{

    private  $img_url;
    private  $img_page;
    private  $img_code;
    private  $img_pageupdate;
    const MODEL = Spot::class;

    public function getForDataTable()
    {
        return $this->query()->get();
    }

     public function getinfo(){

         return   $this->query()->select('spotname', 'listen_img','detail_img','business_name','spot.id','go_url','wx_logo','vic_title','main_title')->leftjoin('business','spot.business_id','=','business.id')->get();

     }
    public function create($input)
    {
          
         $this->img($input);
           //微信图片不是放在data里面的
         
        $key =array_keys($input['data']);
        $res=in_array('detail_img',$key);
        $res1=in_array('listen_img',$key);

        if($res){
            $backet="detail_img";
        } else{
            throw new GeneralException(trans('alerts.backend.finance.backet_error'));
            die();
        }

        if($res1){
            $backet1="listen_img";
        } else{
            throw new GeneralException(trans('alerts.backend.finance.backet_error'));
            die();
        }

        $img=$this->imgup($backet);
        $img1=$this->imgup($backet1);
        $data = $input;
        $data['data']['detail_img']  = $img['path'];
        $data['data']['listen_img']  = $img1['path'];

         $spot = $this->createUserStub($data);
        
         DB::transaction(function () use ($spot, $data) {
            if ($spot->save()) {
                //Attach new roles
                event(new SpotCreated($spot));

                return true;
            }

            throw new GeneralException(trans('exceptions.backend.spot.create_error'));
        });

    }


    public function img($input){
        //图片上传
        $allow_file_type =['jpg','png','gif','jpeg'];
       if(!$_FILES||!$_FILES['wx_logo']){
           throw new GeneralException(trans('exceptions.backend.spot.create_error'));  //请选上传文件
       }
        $file_name = $_FILES['wx_logo']['name'];
        $tmp_file_extend =  explode('.',$file_name);
        $file_type  = strtolower(end($tmp_file_extend));
        if(!in_array(strtolower(end($tmp_file_extend)),$allow_file_type)) {
            throw new GeneralException(trans('exceptions.backend.spot.type_error'));     // '请选择指定上传的图片类型'
        }
        $img = Image::make($_FILES['wx_logo']['tmp_name']);       // Image::make() 支持这种方式
        $img->fit(200, 200);
        $name = public_path().'/img/spot/';
        $folder_name = uniqid();
        $upload_dir = $name;

        Log::info('directory is '.$upload_dir);

        if(!file_exists($upload_dir)){
            Log::info('directory is noExists : '.$upload_dir);
            mkdir($upload_dir,0777);
            chmod($upload_dir,0777);
        } ;
        $pic=$upload_dir.$folder_name.".".$file_type;
        $img->save($pic);
        //todo 以后 要清除数据本地
        $wx_config=config('wechat.official_account.default');
        $app = Factory::officialAccount($wx_config);
        $shakearound = $app->shake_around;
        //上传微信
        $result = $shakearound->material->uploadImage($pic);
            
        if(!$result){
            throw new GeneralException(trans('exceptions.backend.spot.wx_error'));
        }

        //获取微信的图片
        $pic_url = $result['data']['pic_url'];
        $this->img_url=$pic_url;
        $code =uniqid();
        $this->img_code =$code;
        //微信上传素材
        $page_data =[
            'title'=>$input['data']['main_title'],
            'description'=>$input['data']['vic_title'],
            'icon_url'=> $pic_url,
            'comment'=>$input['data']['spotname'],
            'page_url' => 'http://www.playxx.cn/mpview' . '?code=' . $code,
            //注意这是正式服,测试服没有写
        ];

        $result_pic = $shakearound->page->create($page_data);
        if(!$result_pic){
            throw new GeneralException(trans('exceptions.backend.spot.wxpic_error'));
        }
         $page_id =$result_pic['data']['page_id'];
         $this->img_page=$page_id;

       


    }
     //修改图片
     public function updateimg($input){
           
         if(isset($input['wx_logo'])){

             $allow_file_type =['jpg','png','gif','jpeg'];
             $file_name = $_FILES['wx_logo']['name'];
             $tmp_file_extend =  explode('.',$file_name);
             $file_type  = strtolower(end($tmp_file_extend));
             if(!in_array(strtolower(end($tmp_file_extend)),$allow_file_type)) {
                 throw new GeneralException(trans('exceptions.backend.spot.type_error'));     // '请选择指定上传的图片类型'
             }

             $img = Image::make($_FILES['wx_logo']['tmp_name']);       // Image::make() 支持这种方式
             $img->fit(200, 200);
             $name = public_path().'\\img\\spot\\';
             $folder_name = uniqid();
             $upload_dir = $name;
             if(!file_exists($upload_dir)){
                 mkdir($upload_dir,0777);
                 chmod($upload_dir,0777);
             } ;
             $pic=$upload_dir.$folder_name.".".$file_type;
             $img->save($pic);

             $wx_config=config('wechat.official_account.default');
             $app = Factory::officialAccount($wx_config);
             $shakearound = $app->shake_around;
             //上传微信
             $result = $shakearound->material->uploadImage($pic);
//             var_dump($result);exit();

             if(!$result){
                 throw new GeneralException(trans('exceptions.backend.spot.wx_error'));
             }
             $pic_url = $result['data']['pic_url'];
             $this->img_pageupdate =$pic_url;
             $page_Id=$input['page_id'];
             $data=[
                 'title' =>$input['main_title'],
                 'description'=>$input['vic_title'],
                 'page_url'=>'http://www.playxx.cn/mpview'.'?code='.$input['code'],
                 'comment'=>'修改',
                 'icon_url'=>$pic_url
             ];
             //调用 微信接口
            $resultupdate= $shakearound->page->update( $page_Id, $data);

            if(!$resultupdate){
                throw new GeneralException(trans('exceptions.backend.spot.wxupdate_error'));
                return false;
            }

         }else{
                 
             $wx_config=config('wechat.official_account.default');
             $app = Factory::officialAccount($wx_config);
             $shakearound = $app->shake_around;
             $page_Id=$input['page_id'];
             $data=[

                 'title' =>$input['main_title'],
                 'description'=>$input['vic_title'],
                 'page_url'=>'http://www.playxx.cn/mpview' . '?code=' . $input['code'],
                 'comment'=>'修改',
                 'icon_url'=>$input['wx_logo'],
             ];
             //调用 微信接口
             $resultupdate= $shakearound->page->update( $page_Id, $data);

             if(!$resultupdate){
                 throw new GeneralException(trans('exceptions.backend.spot.wxupdate_error'));
                 return false;
             }
             $this->img_pageupdate=$input['wx_logo'];
         }


     }
    
    public function update(Spot $spot, $input)
    {

//        echo 'update.wx_logo';
//        var_dump($input);
//        echo '------------------'.'</br>';
//        exit();
        if(isset($input['wx_logo'])){

            $this->updateimg($input);
            $input['wx_logo']= $this->img_pageupdate;
        }

        if(isset($input['detail_img'])){
            $key =array_keys($input);
            $res=in_array('detail_img',$key);
            if($res){
                $backet="detail_img";
                $img=$this->imgup($backet);
                $input['detail_img'] = $img['path'];
            } else{
                throw new GeneralException(trans('alerts.backend.finance.backet_error'));
                die();
            }
        }

        if(isset($input['listen_img'])){
            $key1 =array_keys($input);
            $res1=in_array('listen_img',$key1);
            if($res1){
                $backet1="listen_img";
                $img1=$this->imgup($backet1);
                $input['listen_img'] = $img1['path'];
            } else{
                throw new GeneralException(trans('alerts.backend.finance.backet_error'));
                die();
            }
        }

        DB::transaction(function () use ($spot, $input) {
            if ($spot->update($input)) {
                event(new SpotUpdated($spot));
                return true;
            }

            throw new GeneralException(trans('exceptions.backend.spot.update_error'));
        });
    }


    public function delete(Spot $spot,$input)
    {

        DB::transaction(function () use ($spot, $input) {
            if ($spot->delete($input)) {
                event(new SpotDeleted($spot));

                return true;
            }

            throw new GeneralException(trans('exceptions.backend.business.update_error'));
        });

    }

    protected function createUserStub($input)
    {

     
        $over = $input['data']['time'].":00";
        $overtime = $input['data']['overtime'].":00";
        $spot = self::MODEL;
        $spot = new $spot;
        $spot->spotname = $input['data']['spotname'];
        $spot->business_id = $input['data']['business_id'];
        $spot->main_title =$input['data']['main_title'];
        $spot->vic_title =$input['data']['vic_title'];
        $spot->go_url =$input['data']['go_url'];
        $spot->detail_img =$input['data']['detail_img'];
        $spot->listen_img =$input['data']['listen_img'];
        $spot->address =$input['data']['address'];
        $spot->wx_logo=$this->img_url;
        $spot->page_id=$this->img_page;
        $spot->code=$this->img_code;
        $spot->time= $over;
        $spot->overtime = $overtime ;



        return $spot;
    }

    /**
     * @param  $input
     * @param  $user
     *
     * @throws GeneralException
     */
    protected function checkUserByUsername($input, $business)
    {

        if ($business->business_name != $input['business_name']) {
            if ($this->query()->where('business_name', '=', $input['business_name'])->first()) {
                throw new GeneralException(trans('exceptions.backend.business.business_name_error'));
            }
        }
    }


}