<?php
/**
 * 会员中心
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <1070309178@qq.com>
 */
namespace Mobile\Controller;
use Think\Controller;
class ExtensionMatterController extends BaseController {
	public function _initialize(){
		parent::_initialize ();
		if(!MID){
			cookie('forward',U('Mobile/'.CONTROLLER_NAME.'/'.ACTION_NAME));
			$this->redirect('Mobile/Login/index');
		}else{
			// $map = array();$map['id'] = MID;$map['status'] = 1;
			// $rank_id = M('member')->where($map)->getField('rank_id');
			// if($rank_id > 0){
			// 	$map = array();$map['id'] = $rank_id;
			// 	$is_matter = M('MemberRank')->where($map)->getField('is_matter');
			// 	if($is_matter != 1){
			// 		$this->error('没有权限查看推广素材库',U('Mobile/Member/index',array('eid'=>MID)));exit;
			// 	}
			// }else{
			// 	$this->error('没有权限查看推广素材库',U('Mobile/Member/index',array('eid'=>MID)));exit;
			// }
		}
	}
	
	public function matter_index(){
		$goods_id = I('goods_id');$uid = MID;
		if($goods_id && $uid){
			if(cookie('lshop_id')){
                $lshop_id=cookie('lshop_id');
            }
			$tuijian_code = U('Mobile/goods/goods_cont',array('id'=>$goods_id,'eid'=>$uid,'lshop_id'=>$lshop_id),true,true);
			$tj_code = U('Home/Qrcode/index',array('data'=>urlencode($tuijian_code)),true,true);
			$this->assign('tj_code', $tj_code);
			$map = array();$map['id'] = $goods_id;
			$goods_info = M('Goods')->where($map)->field('goods_name,market_price,shop_price')->find();
			$list = array();$big_pic = '';$matter_img_id = $is_author = 0;
			$map = array();$map['goods_id'] = $goods_id;
			$list = M("GoodsMatter")->where($map)->field(true)->order('sort desc')->select();
			if($list){
				$big_pic = format_url($list[0]['matter_img']);
				$matter_img_id = $list[0]['id'];
			}
			$this->assign('list',$list);
			$this->assign('is_author',$is_author);
			$this->assign('big_pic',$big_pic);
			$this->assign('matter_img_id',$matter_img_id);
			$this->assign('goods_id', $goods_id);
			$this->assign($goods_info);
			
			$map=array();$map['goods_id']=$goods_id;
			$content=M("GoodsContent")->where($map)->field('content,seodescription')->find();
			$description = $goods_info['goods_name'];
			if($content['seodescription']){
				$description=strip_tags(h($content['seodescription']));
			}elseif($content['content']){
				$description=strip_tags(h($content['content']));
			}
			$share_url=U('Mobile/Goods/goods_cont',array('id'=>$goods_id,'eid'=>MID,'lshop_id'=>$lshop_id));
			$this->assign('title',$goods_info['goods_name']);
			$this->assign('description',$goods_info['goods_name']);
			$this->assign('share_url',$share_url);
	        //微信分享相关配置
	        $wx = new \Common\Vendor\Jssdk();
	        $signPackage = $wx->getSignPackage();
	        $this->assign('signPackage',$signPackage);
		}
        $this->display();
    }
	public function save_matter(){
		$return = array();$return['status'] = 0;
		$uid = MID;
		$matter_img_id = I('matter_img_id');
		$goods_id = I('goods_id');
		if($matter_img_id && $goods_id && $uid){
			$map = array();$map['goods_id'] = $goods_id;$map['id'] = $matter_img_id;
			$matter_img = M("GoodsMatter")->where($map)->getField('matter_img');
			if($matter_img){
				$ret = matter_code($uid,1,$goods_id,$matter_img,$matter_img_id);
				if($ret['status'] == 1){
					$img_url = $ret['url'];
					if($img_url){
						$return['status'] = 1;
						$return['pic_url'] = $img_url;
					}else{
						$return['msg'] = '生成失败';
					}
				}else{
					$return['msg'] = $ret['msg'];
				}
				$this->ajaxReturn($return);
			}
		}else{
			$return['msg'] = '参数错误';
		}
		$this->ajaxReturn($return);
	}
	// 商品截图
	public function save_goods_shots(){
		$return = array();$return['status'] = 0;
		$uid = MID;
		$goods_id = I('goods_id');
		if($goods_id){
			$ret = goods_shots($uid,1,$goods_id);
			if($ret['status'] == 1){
				$img_url = $ret['url'];
				if($img_url){
					$return['status'] = 1;
					$return['pic_url'] = $img_url;
				}else{
					$return['msg'] = '生成失败';
				}
			}else{
				$return['msg'] = $ret['msg'];
			}
			$this->ajaxReturn($return);
		}else{
			$return['msg'] = '参数错误';
		}
		$this->ajaxReturn($return);
	}
}