<?php
class cls_weixin {
static $perms;
static $co
nfig = array();
static function get_perms($key , $site_id = null) {
if( empty(self::$perms) || ($site_id !== null && $site_id != self::$perms['site_id']) ) {
self::$perms = fun_kj::get_site( $site_id );
}
$arr_return = array();
if(isset(self::$perms[$key])) $arr_return = self::$perms[$key];
return $arr_return;
}
static function get_access_token() {
if(!defined('cls_klkkdj::KJ_VERISION')) {
fun_b
ase::url_jump("http://www.kjcms.com/api.php?app=copy&app_act=verify.none");
exit;
}
$site_id = self::get_perms('site_id');
$token = cls_cache::get('access_token' , 'weixin/' . $site_id , 3);
if(empty($token)) {
$appid = self::get_perms('appid');
$appsecret = self::get_perms('appsecret');
if(empty($appid) || empty($appsecret)) return '';
$cont = file_get_co
ntents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret);
if(empty($cont)) return '';
$arr = fun_format::toarray($cont);
if(isset($arr['access_token'])) {
$token = $arr['access_token'];
cls_cache::set($token ,'access_token' , 'weixin/' . $site_id);
}
}
return $token;
}
//上传媒体文件
static function on_media_upload($path , $type , $site_id = 0) {
$arr_type = self::get_perms('mediatype');
if(!isset($arr_type[$type])) return array('code' => 500 , 'msg' => '上传类型不存在');
$real_path = fun_get::real_path($path);
$ext = strtolower(end(explode("." , $real_path)));
$exts = implode(',' , $arr_type[$type]);
if(!in_array($ext , $arr_type[$type])) return array('code' => 500 , 'msg' => '只允许上传扩展名为' . $exts . '的文件');
if(!is_file($real_path)) return array("code" => 500 , "msg" => "上传失败");
$access_token = self::get_access_token();
if(empty($access_token)) return array('code' => 500 , 'msg' => '微信权限不够');
$url = 'http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=' . $access_token . '&type=' . $type;
$arr_return = fun_b
ase::post($url , array() , array(b
asename($real_path) => $real_path) );
if($arr_return['code'] == 0) {
$arr_media = fun_format::toarray($arr_return['cont']);
if(isset($arr_media['media_id'])) {
$size = filesize($real_path);
//保存进数据库媒体表
$arr_fields = array(
'media_id' => $arr_media['media_id'],
'media_type' => $arr_media['type'] ,
'media_time' => $arr_media['created_at'],
'media_uid' => cls_obj::get('cls_user')->uid,
'media_file' => $path,
'media_size' => $size,
'media_site_id' => $site_id,
);
$arr_save = cls_obj::db_w()->on_insert(cls_config::DB_PRE."weixin_media",$arr_fields);
$arr_media['code'] = 0;
} else {
$arr_media['msg'] = (isset($arr_media['errmsg'])) ? $arr_media['errmsg'] : '';
$arr_media['code'] = 500;
}
} else {
$arr_media = $arr_return;
}
return $arr_media;
}
//下载媒体文件
static function get_media($mid) {
$access_token = self::get_access_token();
if(empty($access_token)) return '';
$cont = file_get_co
ntents('http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . $access_token . '&media_id=' . $mid);
return $cont;
}
//接收消息
static function get_msg() {
$arr_return = array();
$cont = file_get_co
ntents("php://input");
//$cont = file_get_co
ntents(KJ_DIR_ROOT . "/test.txt");
if(empty($cont)) return $arr_return;
$request = simplexml_load_string($cont , 'SimpleXmlElement' , LIBXML_NOCDATA);
$arr_return = fun_format::toarray($request);
return $arr_return;
}
//验证
static function check_signature() {
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$no
nce = $_GET["nonce"];
$token = self::get_access_token();
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return fun_get::get("echostr");
} else {
return '';
}
}
//处理消息
static function on_exe() {
$arr_msg = self::get_msg();
//$arr_msg = array('ToUserName'=>'dkd','FromUserName'=>'bbb','MsgType'=>'text','Content'=>'d');
if(empty($arr_msg['ToUserName'])) return;
self::$config['wx_id'] = $arr_msg['FromUserName'];
$arr_message = $arr_cont = array();
if($arr_msg['MsgType'] == 'text') {
$arr = explode(",",str_replac
e("," , "," , $arr_msg['Content']));
if(is_numeric($arr[0])) {
//是店铺用户
$obj_shop = cls_obj::db()->get_one("select shop_id from " . cls_config::DB_PRE . "meal_shop wher
e shop_weixin_id='" . $arr_msg['FromUserName'] . "'");
if(!empty($obj_shop)) {
$oid = $arr[0];
if(count($arr)>1) {
unset($arr[0]);
$beta = implode("," , $arr);
} else {
$beta = '';
}
$state = (empty($beta)) ? 1 : -1;
$wher
e = " order_shop_id='" . $obj_shop['shop_id'] . "' and righ
t(order_id," . strlen($oid) . ")='" . $oid . "'";
//查看订单是否存在
$obj_order = cls_obj::db()->get_one("selec
t order_id,order_state from " . cls_config::DB_PRE . "meal_order wher
e" . $wher
e);
if(empty($obj_order)) {
$arr_cont['cont'] = '订单不存在';
} else {
if($obj_order['order_state']!=0) {
$arr_cont['cont'] = '无效订单或已处理过';
} else {
$wher
e .= " and order_state=0";
$arr_re = tab_meal_order::on_state('' , $state , $beta , $wher
e);
$arr_cont = array();
if($arr_re['code'] == 0) {
$arr_cont['cont'] = ($state==-1) ? '取消订单成功' : '成功接收订单,请尽快配送';
} else {
$arr_cont['cont'] = '处理失败,原因:' . $arr_msg['msg'];
}
}
}
}
}
if(empty($arr_cont)) {
$msgmode = self::get_perms('msgmode');
$site_id = self::get_perms('site_id');
$shop_id = self::get_perms('shop_id');
//取自动回复消息或关键词消息
$arr_1 = tab_weixin_message::get_rekeywords($arr_msg['Content'] , $site_id);
$count = count($arr_1)-1;
if($count>0) {
$ii = rand(0,$count);
$arr_message = $arr_1[$ii];
} else if(!empty($arr_1)) {
$arr_message = $arr_1[0];
} else if(!empty($msgmode)){
//主站才搜索店铺
$arr_message = array('message_type' => 'news' , 'news' => array());
if(empty($site_id)) {
$obj_result = cls_obj::db()->selec
t("select shop_id,shop_name,shop_desc,shop_area,shop_pic,shop_pic_small from " . cls_config::DB_PRE . "meal_shop wher
e shop_name like '%" . $arr_msg['Content'] . "%' or shop_jian like '" . strtolower($arr_msg['Content']) . "%' limit 0,10");
while($obj_rs = cls_obj::db()->fetch_array($obj_result)) {
if(empty($obj_rs['shop_pic_small'])) $obj_rs['shop_pic_small'] = $obj_rs['shop_pic'];
if(empty($arr_message['news'])) {
if(empty($obj_rs['shop_pic'])) $obj_rs['shop_pic'] = $obj_rs['shop_pic_small'];
$pic = fun_get::html_url($obj_rs['shop_pic'] , 1);
} else {
$pic = fun_get::html_url($obj_rs['shop_pic_small'] , 1);
}
$url = cls_config::get('url') . '/index.php?app_weixin=1&app_act=shop&id=' . $obj_rs['shop_id'];
$shop_id = $obj_rs['shop_id'];
$arr_message['news'][] = array(
'title' => $obj_rs['shop_name'],
"desc" => strip_tags(fun_get::filter($item['shop_desc'],true)),
"pic" => $pic,
"url" => $url,
);
}
if(count($arr_message['news']) == 1) {
//搜索该店菜品
$obj_result = cls_obj::db()->selec
t("select menu_title,menu_price,menu_pic,menu_pic_small,menu_intro from " . cls_config::DB_PRE . "meal_menu wher
e menu_shop_id='" . $shop_id . "' and menu_tj=1 and menu_state>0 and menu_isdel=0 order by menu_price limit 0,4");
while($obj_rs = cls_obj::db()->fetch_array($obj_result)) {