//填写微信分配的公众账号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_public { private $appid; private $seceret; private $uri; public function __construct($appid, $seceret, $uri) { $this->appid = $appid; $this->seceret = $seceret; $this->uri = $uri; } //获取code public function redirectWithCode() { $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid ."&redirect_uri=".$this->uri ."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"; header('Location: '.$url); exit; } 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; } } }