<?php
class Reward
{
public $appid;
public $mch_id;
public $key;
public $order_id;
public function __construct($appid = false,$mch_id = false,$key = false,$order_id = flase)
{
if($appid){
$this->appid = $appid;
$this->mch_id = $mch_id;
$this->key = $key;
$this->order_id = $order_id;
}else{
$this->appid = '2222';
$this->mch_id = '131331';
$this->key = '30815ymdvqi1lk';
}
}
public function bonus()
{
$orderinfo = D('Order')->where(['order_id'=>$this->order_id])->find();//获取订单信息
if($orderinfo){
$store_contact = D('Store_contact')->where(['store_id'=>$orderinfo['store_id']])->find();
$store = D('Store')->where(array('store_id' => $orderinfo['store_id']))->find();
$userinfo = D('User')->where(['uid'=>$store['uid']])->find();
$admin = D('Admin')->field('ep_id')->where(['id'=>$userinfo['invite_admin']])->find();
//$userinfo1 = D('User')->where(['uid'=>$userinfo['invite_admin']])->find();
//返回数据
/* json_return(1001,$this->order_id.'-'.$store_contact['long'].'-'.$store_contact['lat'].'-'.$orderinfo['order_id'].'-'.$orderinfo['total'].'-'.$orderinfo['store_id'].'-'.$admin['ep_id']);*/
$data = [];
$data['order_id'] = $this->order_id;
$data['lng'] = $store_contact['long'];
$data['lat'] = $store_contact['lat'];
$data['total'] = $orderinfo['total'];
$data['store_id'] = $orderinfo['store_id'];
$data['ep_id'] = $admin['ep_id'];
$this->apply($data);
}else{
json_return(1001,'无此订单');
}
}
public function apply($data)
{
$api = 'http://fen.51ao.com/wap/aaep_test.php';
$datas=[
'ep_id'=>$data['ep_id'],
'lng'=> $data['lng'],
'lat'=>$data['lat'],
'order_id'=>$data['order_id'],
'total'=>$data['total'],
'store_id'=>$data['store_id'],
'appid' => $this->appid,
'mch_id' => $this->mch_id,
];
$xml = $this->arr_xml($datas);
file_put_contents(PIGCMS_PATH.'/1.txt', $xml);
$ret = $this->post($api,$xml);
$ret = $this->xml_arr($ret);
if($ret['return_code'] == 'Error' ){
json_return(1001,$ret['msg']);
}else{
json_return(0,$ret['msg']);
}
}
//ep用户同步
public function ep_user($tel,$paw)
{
$user = $this->find_user($tel);
if($user['code'] == 1){
$user = $this->register($tel,$paw);
dump($user);die;
}
if($user['code'] == 0){
$data['code'] = 0;
$data['ep_id'] = $user['userId'];
$data['user_type'] = $user['userType'];
}else{
$data['code'] = 1;
}
return $data;
}
//查询api
public function find_user($tel)
{
//$api='https://api.epaikj.com/user/get';
$api='https://api.epaikj.com/user/get.html';
$data=[
'appid'=>$this->appid,
'mch_id'=>$this->mch_id,
'mobile'=>$tel,
'nonce_str'=>$this->randomkey(32),
];
return json_decode($this->post($api,$this->arr_xml($data)),true);
}
//注册api
public function register ($tel,$paw)
{
$api='https://api.epaikj.com/user/register.html';
$data=[
'appid'=>$this->appid,
'mch_id'=>$this->mch_id,
'mobile'=>$tel,
'password'=>$paw,
'nonce_str'=>$this->randomkey(32),
];
return json_decode($this->post($api,$this->arr_xml($data)),true);
}
public function randomkey($length)
{
$key = '';
$pattern = '1234567890abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
for($i=0;$i<$length;$i++){
$key .= $pattern{mt_rand(0,35)}; //生成php随机数
}
return $key;
}
public function arr_xml($data)
{
ksort($data);//以键名排序
$string='';
foreach($data as $k=>$v){
$string.="$k=$v&";
}
$string.='key='.$this->key;
$sign=strtoupper(md5($string));
$data['sign']=$sign;
$xml='<xml>';
foreach($data as $key=>$val){
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.='</xml>';
return $xml;
}
public function xml_arr($xml)
{
return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
public function post($url, $data, $isHttps = TRUE)
{
// 创建curl对象
$ch = curl_init ();
// 配置这个对象
curl_setopt ( $ch, CURLOPT_URL, $url); // 请求的URL地址
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
if($isHttps)
{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
}
curl_setopt ( $ch, CURLOPT_POST, true); // 是否是POST请求
curl_setopt ( $ch, CURLOPT_HEADER, 0); // 去掉HTTP协议头
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1); // 返回接口的结果,而不是输出
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data); // 提交的数据
// 发出请求
$return = curl_exec ( $ch );
// 关闭对象
curl_close ( $ch );
// 返回数据
return $return;
}
}