<?php
/**
 * 门店小程序
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaopp <913519476@qq.com>
 */

namespace Common\Vendor;
class WeixinShops{
	/**
	 * 获取保存的accesstoken
	 */
	private function getToken() {
		static $stoken = null;
        $accesstoken='';
        if(C('BOUND_STATUS')==2){//扫码授权
            $wx = new \Common\Vendor\Weixinopen;
            $accesstoken=$wx->get_access_token(C('appid'));
        }else{
			$stoken = S('WX_S_TOKEN');
			if (is_array($stoken)) {
				$nowtime = time();
				// 判断缓存里面的TOKEN保存了多久
				$difftime = $nowtime - $stoken ['tokentime'];
				// TOKEN有效时间7200 判断超过7000就重新获取;
				if ($difftime > 7000) {
					// 去微信获取最新ACCESS_TOKEN
					$accesstoken = $this->getAcessToken();
					$stoken ['tokentime'] = time();
					$stoken ['token'] = $accesstoken;
					// 放进缓存
					S('WX_S_TOKEN', $stoken);
				} else {
					$accesstoken = $stoken ['token'];
				}
			} else {
				// 去微信获取最新ACCESS_TOKEN
				$accesstoken = $this->getAcessToken();
				$stoken ['tokentime'] = time();
				$stoken ['token'] = $accesstoken;
				S('WX_S_TOKEN', $stoken); // 放进缓存
			}
		}
		return $accesstoken;
	}

	/**
	 * 重新从微信获取accesstoken
	 */
	private function getAcessToken() {		
	   $wx = new \Common\Vendor\Jssdk();
       return $wx->getAccessToken();
	}
	/**
	 * 拉取门店小程序类目
	 */
	public function getMerchantCategory() {
		$access_token = $this->getToken();
		$url = "https://api.weixin.qq.com/wxa/get_merchant_category?access_token=".$access_token;
		$httpstr = curlget($url);
		$harr = json_decode($httpstr, true);
		return $harr;
	}	
	/**
	 * 获取城市信息
	 * @return array
	 */
	public function get_city(){
		$access_token = $this->getToken();
		/*$old_f_s = format_url($post['thumb'],2);
		//上传门店头像
		$type = "image";
		$filedata = array("media"=>"@".$old_f_s);
		$url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=$access_token&type=$type";
		$result = $this->https_request($url,$filedata);//print_r($result);exit;
		$json = json_decode($result,true);*/
		// $json['media_id']='A_27edKkRW7rZ8r68TEIPnQiaLXGvUjuToSzkZDMWVS1CmdDlcuLE6cvJXVX2qAa';
		$url = "https://api.weixin.qq.com/wxa/get_district?access_token=".$access_token;
		$httpstr = curlget($url);
		$harr = json_decode($httpstr, true);
		return $harr;
	}
	/**
	 *添加门店
	 *
	 * @param array $postdata 参数
	 * @return array
	 */
	public function add_store($postdata){
		$access_token = $this->getToken();
		$post = array(
			"map_poi_id" =>$postdata['map_poi_id'],
			"pic_list"=>$postdata['pic_list'],
			"contract_phone"=>$postdata['contract_phone'],
			"hour"=>$postdata['hour'],
			"credential"=>$postdata['credential'],
			"card_id"=>'pK-1212323123'
		);
		$post = json_encode($post, JSON_UNESCAPED_UNICODE);
		$url = "https://api.weixin.qq.com/wxa/add_store?access_token=".$access_token;
		$datas = curlget($url,$post,'POST',array("Content-type: text/html; charset=utf-8"),true);
		$harr = json_decode($datas, true);
		return $harr;
	}
	/**
	 *更新门店信息
	 *
	 * @param array $post 参数
	 * @return array
	 */
	public function update_store($post){
		$access_token = $this->getToken();
		$post = array(
			"poi_id" =>$post['poi_id'],
			"hour"=>$post['hour'],
			"contract_phone"=>$post['contract_phone'],
			"pic_list"=>$post['pic_list'],
			"card_id"=>''
		);
		$post = json_encode($post, JSON_UNESCAPED_UNICODE);
		$url = "https://api.weixin.qq.com/wxa/update_store?access_token=".$access_token;
		$datas = curlget($url,$post,'POST',array("Content-type: text/html; charset=utf-8"),true);
		$harr = json_decode($datas, true);
		if($harr['errmsg'] == 'ok'){
			$return['status'] = 1;
		}else{
			$return['status'] = 0;
			$return['msg'] = $datas;
		}
		return $return;
	}
	/**
	 *获取单个门店信息
	 *
	 * @param string $poi_id 参数
	 * @return array
	 */
	public function get_store_info($poi_id){
		$access_token = $this->getToken();
		$post = array(
			"poi_id" =>$poi_id,
		);
		$post = json_encode($post, JSON_UNESCAPED_UNICODE);
		$url = "https://api.weixin.qq.com/wxa/get_store_info?access_token=".$access_token;
		$datas = curlget($url,$post,'POST',array("Content-type: text/html; charset=utf-8"),true);
		$harr = json_decode($datas, true);
		if($harr['errmsg'] == 'ok'){
			$return['status'] = 1;
		}
		return $return;
	}
	/**
	 *获取门店信息列表
	 * @return array
	 */
	public function get_store_list(){
		$access_token = $this->getToken();
		$post = array(
			"offset"=>0,
			"limit"=>10
		);
		$post = json_encode($post, JSON_UNESCAPED_UNICODE);
		$url = "https://api.weixin.qq.com/wxa/get_store_list?access_token=".$access_token;
		$datas = curlget($url,$post,'POST',array("Content-type: text/html; charset=utf-8"),true);
		$harr = json_decode($datas, true);
		if($harr['errmsg'] == 'ok'){
			$return['status'] = 1;
		}
		return $return;
	}
	/**
	 *删除门店
	 *@param string $poi_id 参数
	 * @return array
	 */
	public function del_store($poi_id){
		$access_token = $this->getToken();
		$post = array(
			"poi_id"=>$poi_id
		);
		$post = json_encode($post, JSON_UNESCAPED_UNICODE);
		$url = "https://api.weixin.qq.com/wxa/del_store?access_token=".$access_token;
		$datas = curlget($url,$post,'POST',array("Content-type: text/html; charset=utf-8"),true);
		$harr = json_decode($datas, true);
		if($harr['errmsg'] == 'ok'){
			$return['status'] = 1;
		}
		return $return;
	}
}
?>