//填写微信分配的公众账号ID * 'mch_id' => //填写微信支付分配的商户号 * 'notify_url'=> //填写微信支付结果回调地址 * 'key' => //填写微信商户支付密钥 * ); * 统一下单方法 * $WechatAppPay = new wechatAppPay($options); * $params['body'] = '商品描述'; //商品描述 * $params['out_trade_no'] = '1217752501201407'; //自定义的订单号,不能重复 * $params['total_fee'] = '100'; //订单金额 只能为整数 单位为分 * $params['trade_type'] = 'APP'; //交易类型 JSAPI | NATIVE |APP | WAP * $wechatAppPay->unifiedOrder( $params ); */ class weixin { private $appid; private $seceret; public function __construct($appid, $seceret) { $this->appid = $appid; $this->seceret = $seceret; } /** * code获取openid * @param $code * @return mixed */ public function code2Session($code) { $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->appid}&secret={$this->seceret}&js_code={$code}&grant_type=authorization_code"; $res = Curl::get($url,true); return json_decode($res,true); } public function getAccessToken() { $rs = Baseconfig::getInfoByKey(TOKEN_BASE_KEY); if (empty($rs) || (time() - $rs["lastupdate"]) > TOKEN_LIMIT_TIME) { $accessToken = self::refreshToken(); } else { $accessToken = $rs["value"]; } return $accessToken; } public function refreshToken() { $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->seceret; $res = file_get_contents($token_access_url); // $result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量s $access_token = $result['access_token']; $rs = Baseconfig::getInfoByKey(TOKEN_BASE_KEY); if ($rs) { $res = Baseconfig::updateByKey(TOKEN_BASE_KEY, array("value" => $access_token, "lastupdate" => time())); } else { $attrs = array( "key"=>TOKEN_BASE_KEY, "value"=>$access_token, "name"=>"token" ); $res = Baseconfig::add($attrs); } if ($res) { return $access_token; } } public function createBase64($path,$scene) { $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}"; // if(!file_exists(FILE_PATH.$destPath)) // { // mkdir(FILE_PATH.$destPath,0777,true); // } // $fileName = date("YmdHis").randcode(4).".jpg"; $resData = Curl::post($url,json_encode(array("page"=>$path,"scene"=>$scene))); return Image::BinaryToBase64($resData); // file_put_contents(FILE_PATH.$destPath.$fileName,$resData); } public function createQrCode($path,$scene,$destPath) { $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}"; if(!file_exists(FILE_PATH.$destPath)) { mkdir(FILE_PATH.$destPath,0777,true); } $fileName = date("YmdHis").randcode(4).".jpg"; $resData = Curl::post($url,json_encode(array("page"=>$path,"scene"=>$scene))); file_put_contents(FILE_PATH.$destPath.$fileName,$resData); return $destPath.$fileName; } public function sendBaomingInform($openId,$formId,$page,$name,$title){ $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$accessToken}"; $time = date("Y-m-d H:i:s"); $data = '{ "touser": "'.$openId.'", "template_id": "'.BAOMING_TEMPLATE_ID.'", "page": "'.$page.'", "form_id": "'.$formId.'", "data": { "keyword1": { "value": "'.$name.'" }, "keyword2": { "value": "'.$title.'" }, "keyword3": { "value": "'.$time.'" } } }'; $rs = Curl::post($url,$data); } public function sendSignSuccess($openId,$formId,$page,$name,$title){ $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$accessToken}"; $time = date("Y-m-d H:i:s"); $data = '{ "touser": "'.$openId.'", "template_id": "'.SIGN_SUCCESS_TEMPLATE_ID.'", "page": "'.$page.'", "form_id": "'.$formId.'", "data": { "keyword1": { "value": "'.$name.'" }, "keyword2": { "value": "'.$time.'" }, "keyword3": { "value": "'.$title.'" } } }'; $rs = Curl::post($url,$data); } /***发送动开始通知 订阅消息***/ public function sendBeginInform($openId,$page,$auth,$title,$time,$address){ $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$accessToken}"; $data = '{ "touser": "'.$openId.'", "template_id": "'.BAOMING_BEGIN_INFORM_TEMPLATE_ID.'", "page": "'.$page.'", "data": { "name1": { "value": "'.$auth.'" }, "thing4": { "value": "'.$title.'" }, "date5": { "value": "'.$time.'" }, "thing6": { "value": "'.$address.'" } } }'; $rs = Curl::post($url,$data); } /***发送奖品通知 订阅消息***/ public function sendAwardInform($openId,$page,$auth,$title,$phone,$awardName){ $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$accessToken}"; $data = '{ "touser": "'.$openId.'", "template_id": "'.BAOMING_AWARD_INFORM_TEMPLATE_ID.'", "page": "'.$page.'", "data": { "thing1": { "value": "'.$title.'" }, "thing2": { "value": "'.$awardName.'" }, "phone_number3": { "value": "'.$phone.'" }, "name4": { "value": "'.$auth.'" } } }'; $rs = Curl::post($url,$data); } }