<?php

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Libs\Classes\WxSign;
use App\Modules\WechatInner\Services\WechatInnerService;

class WechatBaseApisController extends Controller
{
	public function __construct($_param){
		$this->param = $_param;
		$this->WxSign = new WxSign();
	}
	
	/**
	 * api请求封装
	 *
	 */
	public function do_api_post($data,$api_event)
	{
		$api_event = strtolower($api_event);
		
		$inner_api_events = [
			'get_force_wx_user',
			'update_user_interaction_time',
			'get_event_content',
// 			'get_material_force_subscribe_mapping'
		];
		// 部分接口直接查
		if(in_array($api_event,$inner_api_events)){
			$event_datas =  WechatInnerService::get_event_data($data,$api_event);
			return $event_datas;
		}
		
		v('do_api_post_start:'.$api_event);
		try{
			$search_url = $this->param['group']['domain'].$this->param['group_api'][$api_event]['api_url'];
			$post_data  =  $this->WxSign->get_sign_arr($data,$this->param['group']['secret_key']);
			$result = $this->do_api_post_base($search_url,$post_data);
		}
		catch(\Exception $e){
			v('do_api_post_ept:'.$e->getMessage());
			$result = null;
		}
	
		return $result;
	}
	
	/**
	 * api请求接口基础
	 *
	 */
	public function do_api_post_base($search_url,$post_data)
	{
		$post_results = null;
		try{
			v('do_api_post_base_start, search_url:'.$search_url);
			v($post_data);
			$post_results = hpost($search_url,$post_data);
			if(isset($post_results['code']) && $post_results['code'] == 0){
				$post_results = $post_results['data'];
			}else{
				$code = isset($post_results['code'])?$post_results['code']:'';
				$msg = isset($post_results['msg'])?$post_results['msg']:'';
				v('post_error:code'.$code.' msg:'.$msg);
				$post_results = null;
			}
		}
		catch(\Exception $e){
			v('do_api_post_base_ept:'.$e->getMessage());
			
		}

		// 	 	$result = array();
		// 	 	$one_book1 = array('title'=>'搜索结果','description'=>'描述1','url'=>'www.baidu.com','image'=>'https://mmbiz.qpic.cn/mmbiz_jpg/U3h620I4Tu1wicDy9Lv9Il4xDOibuxsPl5cyhz2sR0FrSKzmtKcYSC3HKHHBIIWUzWPXX3rcXqw1eT69hicBrjl8Q/640.jpeg?tp=webp&wxfrom=5&wx_lazy=1');
		// 	 	$one_book2 = array('title'=>'搜索的图文消息','description'=>'描述2','url'=>'www.baidu.com','image'=>'https://mmbiz.qpic.cn/mmbiz_jpg/U3h620I4Tu1wicDy9Lv9Il4xDOibuxsPl5cyhz2sR0FrSKzmtKcYSC3HKHHBIIWUzWPXX3rcXqw1eT69hicBrjl8Q/640.jpeg?tp=webp&wxfrom=5&wx_lazy=1');
		// 	 	$one_book3 = array('title'=>'阅读云搜索图文消息','description'=>'阅读云搜索图文消息','url'=>'www.baidu.com','image'=>'https://gitee.com/uploads/88/1043488_yuelooll.png?1495765918');
	
		// 	 	$result[] = $one_book1;
		// 	 	$result[] = $one_book2;
		// 	 	$result[] = $one_book3;
// 		v('post_results');v($post_results);
		return $post_results;
	}
}
