52 Commits

Author SHA1 Message Date
as2252258@163.com 8eb5922d10 add clear 2019-11-04 14:39:59 +08:00
as2252258@163.com dbfbbf75b0 add clear 2019-11-04 14:18:33 +08:00
as2252258@163.com ddaf80a14d add clear 2019-10-29 10:20:12 +08:00
as2252258@163.com af316b799b add clear 2019-10-25 15:29:10 +08:00
as2252258@163.com 4784354c22 add clear 2019-10-25 15:20:52 +08:00
as2252258@163.com 4a3a3a5091 add clear 2019-10-25 15:20:09 +08:00
as2252258@163.com 7011a4569d add clear 2019-10-22 14:02:44 +08:00
as2252258@163.com 27d8b60845 add clear 2019-10-22 13:57:09 +08:00
as2252258@163.com b2bf378b7f add clear 2019-10-18 10:40:38 +08:00
as2252258@163.com b2d477f0cf add clear 2019-09-29 17:10:14 +08:00
as2252258@163.com f2dc33fca4 add clear 2019-09-19 19:04:43 +08:00
as2252258@163.com d605600657 add clear 2019-09-19 18:58:46 +08:00
as2252258@163.com 6ff5ecc853 add clear 2019-09-19 18:34:06 +08:00
as2252258@163.com 09776fc906 add clear 2019-09-04 14:08:01 +08:00
as2252258@163.com c9d0f86620 add clear 2019-09-04 14:06:50 +08:00
as2252258@163.com 33de57b515 add clear 2019-08-29 18:25:42 +08:00
as2252258@163.com 678543151f add clear 2019-08-29 18:13:55 +08:00
as2252258@163.com e0925d86c5 add clear 2019-08-29 18:07:11 +08:00
as2252258@163.com 658b951fe9 add clear 2019-08-29 18:06:19 +08:00
as2252258@163.com 7aa22e2e62 add clear 2019-08-29 18:05:46 +08:00
as2252258@163.com 31d75529bc add clear 2019-08-29 18:00:53 +08:00
as2252258@163.com f13a0e80ff add clear 2019-08-29 17:59:29 +08:00
as2252258@163.com df23a0ecf4 add clear 2019-08-29 17:57:02 +08:00
as2252258@163.com ea3790579c add clear 2019-08-29 17:54:51 +08:00
as2252258@163.com aa513a397f add clear 2019-08-29 17:52:02 +08:00
as2252258@163.com 447390a87b add clear 2019-08-29 17:44:51 +08:00
as2252258@163.com b1524a7eaf add clear 2019-08-29 17:40:42 +08:00
as2252258@163.com a4071f1ad8 add clear 2019-08-29 16:14:46 +08:00
as2252258@163.com a376fe8f4f add clear 2019-08-29 15:53:21 +08:00
as2252258@163.com aaae917b72 add clear 2019-08-29 14:48:09 +08:00
as2252258@163.com d7953bc801 add clear 2019-08-29 11:32:58 +08:00
as2252258@163.com 26e691fec8 add clear 2019-08-21 13:37:26 +08:00
as2252258@163.com 00f046fba1 add clear 2019-08-21 13:34:03 +08:00
as2252258@163.com ad78b85ea0 add clear 2019-08-21 13:27:12 +08:00
as2252258@163.com a4e504e5e3 add clear 2019-08-21 13:23:11 +08:00
as2252258@163.com 706088998b add clear 2019-08-21 13:22:22 +08:00
as2252258@163.com cf52645c42 add clear 2019-08-21 13:20:56 +08:00
as2252258@163.com 8e27dc41e5 add clear 2019-08-21 12:13:02 +08:00
as2252258@163.com 5202270073 add clear 2019-08-21 12:11:36 +08:00
as2252258@163.com 93e763c880 add clear 2019-08-21 11:55:42 +08:00
as2252258@163.com 6136633a83 add clear 2019-08-21 11:54:20 +08:00
as2252258@163.com fa67c60088 add clear 2019-08-19 16:17:45 +08:00
as2252258@163.com fecac0e850 add clear 2019-08-19 16:14:36 +08:00
as2252258@163.com 1b89cd61ae add clear 2019-08-19 16:13:24 +08:00
as2252258@163.com a1c85376e1 add clear 2019-08-19 16:07:44 +08:00
as2252258@163.com c323b97112 add clear 2019-08-19 15:51:29 +08:00
as2252258@163.com 663bf23ebd add clear 2019-07-19 19:11:12 +08:00
as2252258@163.com d769ba0aaa add clear 2019-07-19 18:09:58 +08:00
as2252258@163.com e44b1c0989 add clear 2019-07-19 18:09:42 +08:00
as2252258@163.com b24d18dc55 add clear 2019-07-19 17:53:54 +08:00
as2252258@163.com cc87f8b440 Merge remote-tracking branch 'origin/master' 2019-07-19 17:50:59 +08:00
as2252258@163.com 4de90eff88 add clear 2019-07-19 17:50:49 +08:00
23 changed files with 3015 additions and 117 deletions
+2 -1
View File
@@ -13,7 +13,8 @@
],
"autoload": {
"psr-4": {
"wchat\\": "wx"
"wchat\\": "wx",
"qq\\": "qq"
}
},
"require": {
+215
View File
@@ -0,0 +1,215 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/19 0019
* Time: 16:12
*/
namespace qq;
use wchat\Result;
use wchat\WxClient;
use wchat\Miniprogarampage;
class Account extends Miniprogarampage
{
private $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=';
private $getwxacode = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=';
private $publicInfo = 'https://api.weixin.qq.com/cgi-bin/user/info?';
private $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=';
private $savePath = __DIR__ . '/../../../';
private $OK = 0;
private $IllegalAesKey = -41001;
private $IllegalIv = -41002;
private $IllegalBuffer = -41003;
private $DecodeBase64Error = -41004;
/**
* @param $path
*/
public function setSavePath($path)
{
$this->savePath = $path;
}
/**
* @param $code
* @return Result
*/
public function login($code)
{
$param['appid'] = $this->config->getAppid();
$param['secret'] = $this->config->getAppsecret();
$param['js_code'] = $code;
$param['grant_type'] = 'authorization_code';
if (empty($code)) {
return new Result(['code' => 404, 'message' => '临时登录凭证不能为空.']);
}
$this->request->setHost('api.q.qq.com');
$this->request->setMethod(WxClient::GET);
$this->request->addHeader('Content-Type', 'text/xml');
return $this->request->get('sns/jscode2session', $param);
}
/**
* @param $openid
* @return array|mixed|Result
* @throws \Exception
*/
public function getPublicUserInfo($openid)
{
$query = [
'access_token' => $this->getAccessToken(),
'openid' => $openid,
'lang' => 'zh_CN'
];
$this->request->setMethod(WxClient::GET);
$this->request->setIsSSL(true);
return $this->request->get($this->publicInfo, $query);
}
/**
* @param $encryptedData
* @param $iv
* @param $sessionKey
* @param $asArray
* @return object|array
* @throws
*
* * <li>-41001: encodingAesKey 非法</li>
* <li>-41003: aes 解密失败</li>
* <li>-41004: 解密后得到的buffer非法</li>
* <li>-41005: base64加密失败</li>
* <li>-41016: base64解密失败</li>
*/
public function decode($encryptedData, $iv, $sessionKey, $asArray = false)
{
$config = QqSDK::getMiniProGaRamPage()->getConfig();
if (strlen($sessionKey) != 24) {
throw new \Exception('encodingAesKey 非法', $this->IllegalAesKey);
}
$aesKey = base64_decode($sessionKey);
if (strlen($iv) != 24) {
throw new \Exception('base64解密失败', $this->IllegalIv);
}
$aesIV = base64_decode($iv);
$aesCipher = base64_decode($encryptedData);
$result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, OPENSSL_RAW_DATA, $aesIV);
if ($result === false) {
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
}
$dataObj = json_decode($result);
if ($dataObj->watermark->appid != $config->getAppid()) {
throw new \Exception('aes 解密失败', $this->IllegalBuffer);
}
if ($asArray) {
return get_object_vars($dataObj);
}
return $dataObj;
}
/**
* @param $path
* @param $width
* @return array|mixed|Result
* @throws \Exception
*/
public function createwxaqrcode($path, $width)
{
$url = $this->wxaqr . $this->getAccessToken();
$sendBody['path'] = $path;
$sendBody['width'] = $width;
$this->request->setMethod(WxClient::POST);
$this->request->setCallback([$this, 'saveByPath']);
return $this->request->post($url, $sendBody);
}
/**
* @param $path
* @param $width
* @param bool $is_hyaline
* @param bool $auto_color
* @param string $line_color
* @return array|mixed|Result
* @throws \Exception
*/
public function getwxacode($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
{
$sendBody['path'] = $path;
$sendBody['width'] = $width;
$sendBody['auto_color'] = $auto_color;
$sendBody['is_hyaline'] = $is_hyaline;
if ($auto_color) {
$sendBody['line_color'] = $line_color;
}
$url = $this->getwxacode . $this->getAccessToken();
$this->request->setMethod(WxClient::POST);
$this->request->setCallback([$this, 'saveByPath']);
return $this->request->post($url, $sendBody);
}
/**
* @param $path
* @param $width
* @param bool $is_hyaline
* @param bool $auto_color
* @param string $line_color
* @return array|mixed|Result
* @throws \Exception
*/
public function getwxacodeunlimit($path, $width, $is_hyaline = false, $auto_color = false, $line_color = '')
{
$sendBody['path'] = $path;
$sendBody['width'] = $width;
$sendBody['auto_color'] = $auto_color;
$sendBody['is_hyaline'] = $is_hyaline;
if ($auto_color) {
$sendBody['line_color'] = $line_color;
}
$url = $this->getwxacodeunlimit . $this->getAccessToken();
$this->request->setMethod(WxClient::POST);
$this->request->setCallback([$this, 'saveByPath']);
return $this->request->post($url, $sendBody);
}
/**
* @param mixed $body
* @return string
* @throws \Exception
*/
public function saveByPath($body)
{
if (!is_null($json = json_decode($body))) {
throw new \Exception($json['errmsg'], $json['errcode']);
}
$push = md5_file($body) . '.png';
file_put_contents($this->savePath . $push, $this->savePath);
return $this->savePath . $push;
}
}
+314
View File
@@ -0,0 +1,314 @@
<?php
namespace qq;
use wchat\Miniprogarampage;
use wchat\Result;
use wchat\WxClient;
class Message extends Miniprogarampage
{
const TEXT = 0;
const IMAGE = 1;
const VOICE = 2;
const NEWS = 3;
const VIDEO = 4;
const MUSIC = 5;
const MINIPROGRAMPAGE = 6;
const WXCARD = 7;
private $openid = '';
private $msgData = [];
/**
* @param string $openid
*/
public function setOpenid(string $openid)
{
$this->openid = $openid;
$this->msgData['touser'] = $openid;
}
/**
* @param string $content
* @return Result
* @throws \Exception
*/
public function sendTextNews(string $content)
{
$this->msgData['msgtype'] = 'text';
$this->msgData['text[content]'] = $content;
return $this->sendKefuMsg();
}
/**
* @param $media_id
* @return Result
* @throws \Exception
*/
public function sendImageNews(string $media_id)
{
$this->msgData['msgtype'] = 'image';
$this->msgData['image[media_id]'] = $media_id;
return $this->sendKefuMsg();
}
/**
* @param $media_id
* @return Result
* @throws \Exception
*/
public function sendVoiceNews(string $media_id)
{
$this->msgData['msgtype'] = 'voice';
$this->msgData['voice[media_id]'] = $media_id;
return $this->sendKefuMsg();
}
/**
* @param $media_id
* @return Result
* @throws \Exception
*/
public function sendMpNewsNews(string $media_id)
{
$this->msgData['msgtype'] = 'mpnews';
$this->msgData['mpnews[media_id]'] = $media_id;
return $this->sendKefuMsg();
}
/**
* @param string $title
* @param string $description
* @param string $url
* @param string $picurl
* @return Result
* @throws \Exception
*/
public function sendNewsNews(string $title, string $description, string $url, string $picurl)
{
$this->msgData['msgtype'] = 'news';
$this->msgData['news[articles][0][title]'] = $title;
$this->msgData['news[articles][0][description]'] = $description;
$this->msgData['news[articles][0][url]'] = $url;
$this->msgData['news[articles][0][picurl]'] = $picurl;
return $this->sendKefuMsg();
}
/**
* @param string $title
* @return Result
* @throws \Exception
*/
public function sendCardNews(string $title)
{
$this->msgData['msgtype'] = 'wxcard';
$this->msgData['wxcard[card_id]'] = $title;
return $this->sendKefuMsg();
}
/**
* @param string $media_id
* @param string $thumb_media_id
* @param string $title
* @param string $description
* @return Result
* @throws \Exception
*/
public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description)
{
$this->msgData['msgtype'] = 'video';
$this->msgData['video[media_id]'] = $media_id;
$this->msgData['video[thumb_media_id]'] = $thumb_media_id;
$this->msgData['video[title]'] = $title;
$this->msgData['video[description]'] = $description;
return $this->sendKefuMsg();
}
/**
* @param string $musicurl
* @param string $hqmusicurl
* @param string $thumb_media_id
* @param string $title
* @param string $description
* @return Result
* @throws \Exception
*/
public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description)
{
$this->msgData['msgtype'] = 'music';
$this->msgData['music[title]'] = $title;
$this->msgData['music[description]'] = $description;
$this->msgData['music[musicurl]'] = $musicurl;
$this->msgData['music[hqmusicurl]'] = $hqmusicurl;
$this->msgData['music[thumb_media_id]'] = $thumb_media_id;
return $this->sendKefuMsg();
}
/**
* @param string $head_content
* @param string $tail_content
* @param array $menus
* @return Result
* @throws \Exception
*/
public function sendMenuNews(string $head_content, string $tail_content, array $menus = [])
{
$this->msgData['msgtype'] = 'msgmenu';
$this->msgData['msgmenu[head_content]'] = $head_content;
$this->msgData['msgmenu[tail_content]'] = $tail_content;
if (empty($menus) || !is_array($menus) || count($menus) < 2) {
throw new \Exception('菜单选项必须有2个');
}
foreach ($menus as $key => $val) {
$this->addNewsMenu($val['id'], $val['name']);
}
return $this->sendKefuMsg();
}
private $index = 0;
/**
* @param $id
* @param $menuName
* @return $this
*/
public function addNewsMenu($id, $menuName)
{
$this->msgData['msgmenu[list][' . $this->index . '][id]'] = $id;
$this->msgData['msgmenu[list][' . $this->index . '][content]'] = $menuName;
++$this->index;
return $this;
}
/**
* @param $title
* @param $appid
* @param $pagepath
* @param $thumb_media_id
* @return Result
* @throws \Exception
*/
public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id)
{
$this->msgData['msgtype'] = 'msgmenu';
$this->msgData['miniprogrampage[title]'] = $title;
$this->msgData['miniprogrampage[appid]'] = $appid;
$this->msgData['miniprogrampage[pagepath]'] = $pagepath;
$this->msgData['miniprogrampage[thumb_media_id]'] = $thumb_media_id;
return $this->sendKefuMsg();
}
/**
* @param string $filePath
* @param string $type
* @param bool $isPermanent
* @param string $title
* @param string $introduction
* @return mixed
* @throws \Exception
*/
public function upload(string $filePath, string $type, $isPermanent = false, string $title = '', string $introduction = '')
{
if (!file_exists($filePath)) {
throw new \Exception('文件不存在');
}
if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) {
throw new \Exception('暂不支持的文件类型');
}
$token = $this->getAccessToken();
if ($isPermanent) {
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type={$type}";
} else {
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}";
}
$mime = mime_content_type($filePath);
$real_path = new \CURLFile(realpath($filePath));
$data = array("media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[content-type]' => $mime);
if ($isPermanent && $mime == 'video/mp3') {
$data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction];
}
$this->request->setMethod(WxClient::POST);
/** @var Result $body */
$data = $this->request->post($url, $data);
if (!$data->isResultsOK()) {
throw new \Exception($data->getMessage());
}
return $data->getData();
}
/**
* @param $mime
* @throws \Exception
*/
private function checkExtinfo($mime)
{
switch (strtolower($mime)) {
case 'image/bmp':
case 'image/png':
case 'image/jpeg':
case 'image/jpg':
case 'image/gif':
break;
case 'mp3/wma/wav/amr':
break;
case 'mp4';
break;
case 'jpg';
break;
default:
throw new \Exception('不支持的文件格式');
}
}
/**
* @param $data
* @return Result
* @throws \Exception
*/
private function sendKefuMsg()
{
$data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE);
$url = '/cgi-bin/message/custom/send?access_token=' . $this->getAccessToken();
$this->request->setMethod(WxClient::POST);
/** @var Result $body */
$body = $this->request->post($url, $data);
if (!$body->isResultsOK()) {
throw new \Exception($body->getMessage());
}
return $body;
}
}
+600
View File
@@ -0,0 +1,600 @@
<?php
namespace qq;
use wchat\Miniprogarampage;
use wchat\Help;
class Notify extends Miniprogarampage
{
public $appid = '';
public $mch_id = '';
public $device_info = '';
public $nonce_str = '';
public $sign = '';
public $sign_type = '';
public $result_code = '';
public $err_code = '';
public $err_code_des = '';
public $openid = '';
public $is_subscribe = '';
public $trade_type = '';
public $bank_type = '';
public $total_fee = '';
public $settlement_total_fee = '';
public $fee_type = '';
public $cash_fee = '';
public $cash_fee_type = '';
public $coupon_fee = '';
public $coupon_count = '';
public $coupon_type_n = '';
public $coupon_id_n = '';
public $coupon_fee_n = '';
public $transaction_id = '';
public $out_trade_no = '';
public $attach = '';
public $time_end = '';
public $return_code = '';
/**
* @return bool
* 判断是否完成支付
*/
public function isSuccess()
{
if ($this->getReturnCode() != "SUCCESS") {
return false;
}
if ($this->getResultCode() != 'SUCCESS') {
return false;
}
return true;
}
/**
* @param array $params
* @return $this
*/
public function setPayNotifyData(array $params)
{
if (!$this->validation($params)) {
$this->setResultCode('FAIL');
$this->setErrCodeDes('签名错误');
unset($params['result_code'], $params['err_code_des']);
}
foreach ($params as $key => $val) {
if (!property_exists($this, $key)) {
continue;
}
$this->$key = $val;
}
return $this;
}
/**
* @param array $params
* @return bool
*/
public function validation(array $params)
{
$sign = $params['sign'];
unset($params['sign']);
$signType = $this->config->getSignType();
$privateKey = $this->config->getKey();
$nowSign = Help::sign($params, $privateKey, $signType);
if ($sign === $nowSign) {
return true;
}
return false;
}
/**
* @return mixed
*/
public function getAppid()
{
return $this->appid;
}
/**
* @param mixed $appid
* @return Notify
*/
public function setAppid($appid)
{
$this->appid = $appid;
return $this;
}
/**
* @return mixed
*/
public function getMchId()
{
return $this->mch_id;
}
/**
* @param mixed $mch_id
* @return Notify
*/
public function setMchId($mch_id)
{
$this->mch_id = $mch_id;
return $this;
}
/**
* @return mixed
*/
public function getDeviceInfo()
{
return $this->device_info;
}
/**
* @param mixed $device_info
* @return Notify
*/
public function setDeviceInfo($device_info)
{
$this->device_info = $device_info;
return $this;
}
/**
* @return mixed
*/
public function getNonceStr()
{
return $this->nonce_str;
}
/**
* @param mixed $nonce_str
* @return Notify
*/
public function setNonceStr($nonce_str)
{
$this->nonce_str = $nonce_str;
return $this;
}
/**
* @return mixed
*/
public function getSign()
{
return $this->sign;
}
/**
* @param mixed $sign
* @return Notify
*/
public function setSign($sign)
{
$this->sign = $sign;
return $this;
}
/**
* @return mixed
*/
public function getSignType()
{
return $this->sign_type;
}
/**
* @param mixed $sign_type
* @return Notify
*/
public function setSignType($sign_type)
{
$this->sign_type = $sign_type;
return $this;
}
/**
* @return mixed
*/
public function getResultCode()
{
return $this->result_code;
}
/**
* @param mixed $result_code
* @return Notify
*/
public function setResultCode($result_code)
{
$this->result_code = $result_code;
return $this;
}
/**
* @return mixed
*/
public function getErrCode()
{
return $this->err_code;
}
/**
* @param mixed $err_code
* @return Notify
*/
public function setErrCode($err_code)
{
$this->err_code = $err_code;
return $this;
}
/**
* @return mixed
*/
public function getErrCodeDes()
{
return $this->err_code_des;
}
/**
* @param mixed $err_code_des
* @return Notify
*/
public function setErrCodeDes($err_code_des)
{
$this->err_code_des = $err_code_des;
return $this;
}
/**
* @return mixed
*/
public function getOpenid()
{
return $this->openid;
}
/**
* @param mixed $openid
* @return Notify
*/
public function setOpenid($openid)
{
$this->openid = $openid;
return $this;
}
/**
* @return mixed
*/
public function getIsSubscribe()
{
return $this->is_subscribe;
}
/**
* @param mixed $is_subscribe
* @return Notify
*/
public function setIsSubscribe($is_subscribe)
{
$this->is_subscribe = $is_subscribe;
return $this;
}
/**
* @return mixed
*/
public function getTradeType()
{
return $this->trade_type;
}
/**
* @param mixed $trade_type
* @return Notify
*/
public function setTradeType($trade_type)
{
$this->trade_type = $trade_type;
return $this;
}
/**
* @return mixed
*/
public function getBankType()
{
return $this->bank_type;
}
/**
* @param mixed $bank_type
* @return Notify
*/
public function setBankType($bank_type)
{
$this->bank_type = $bank_type;
return $this;
}
/**
* @return mixed
*/
public function getTotalFee()
{
return $this->total_fee;
}
/**
* @param mixed $total_fee
* @return Notify
*/
public function setTotalFee($total_fee)
{
$this->total_fee = $total_fee;
return $this;
}
/**
* @return mixed
*/
public function getSettlementTotalFee()
{
return $this->settlement_total_fee;
}
/**
* @param mixed $settlement_total_fee
* @return Notify
*/
public function setSettlementTotalFee($settlement_total_fee)
{
$this->settlement_total_fee = $settlement_total_fee;
return $this;
}
/**
* @return mixed
*/
public function getFeeType()
{
return $this->fee_type;
}
/**
* @param mixed $fee_type
* @return Notify
*/
public function setFeeType($fee_type)
{
$this->fee_type = $fee_type;
return $this;
}
/**
* @return mixed
*/
public function getCashFee()
{
return $this->cash_fee;
}
/**
* @param mixed $cash_fee
* @return Notify
*/
public function setCashFee($cash_fee)
{
$this->cash_fee = $cash_fee;
return $this;
}
/**
* @return mixed
*/
public function getCashFeeType()
{
return $this->cash_fee_type;
}
/**
* @param mixed $cash_fee_type
* @return Notify
*/
public function setCashFeeType($cash_fee_type)
{
$this->cash_fee_type = $cash_fee_type;
return $this;
}
/**
* @return mixed
*/
public function getCouponFee()
{
return $this->coupon_fee;
}
/**
* @param mixed $coupon_fee
* @return Notify
*/
public function setCouponFee($coupon_fee)
{
$this->coupon_fee = $coupon_fee;
return $this;
}
/**
* @return mixed
*/
public function getCouponCount()
{
return $this->coupon_count;
}
/**
* @param mixed $coupon_count
* @return Notify
*/
public function setCouponCount($coupon_count)
{
$this->coupon_count = $coupon_count;
return $this;
}
/**
* @return mixed
*/
public function getCouponTypeN()
{
return $this->coupon_type_n;
}
/**
* @param mixed $coupon_type_n
* @return Notify
*/
public function setCouponTypeN($coupon_type_n)
{
$this->coupon_type_n = $coupon_type_n;
return $this;
}
/**
* @return mixed
*/
public function getCouponIdN()
{
return $this->coupon_id_n;
}
/**
* @param mixed $coupon_id_n
* @return Notify
*/
public function setCouponIdN($coupon_id_n)
{
$this->coupon_id_n = $coupon_id_n;
return $this;
}
/**
* @return mixed
*/
public function getCouponFeeN()
{
return $this->coupon_fee_n;
}
/**
* @param mixed $coupon_fee_n
* @return Notify
*/
public function setCouponFeeN($coupon_fee_n)
{
$this->coupon_fee_n = $coupon_fee_n;
return $this;
}
/**
* @return mixed
*/
public function getTransactionId()
{
return $this->transaction_id;
}
/**
* @param mixed $transaction_id
* @return Notify
*/
public function setTransactionId($transaction_id)
{
$this->transaction_id = $transaction_id;
return $this;
}
/**
* @return mixed
*/
public function getOutTradeNo()
{
return $this->out_trade_no;
}
/**
* @param mixed $out_trade_no
* @return Notify
*/
public function setOutTradeNo($out_trade_no)
{
$this->out_trade_no = $out_trade_no;
return $this;
}
/**
* @return mixed
*/
public function getAttach()
{
return $this->attach;
}
/**
* @param mixed $attach
* @return Notify
*/
public function setAttach($attach)
{
$this->attach = $attach;
return $this;
}
/**
* @return mixed
*/
public function getTimeEnd()
{
return $this->time_end;
}
/**
* @param mixed $time_end
* @return Notify
*/
public function setTimeEnd($time_end)
{
$this->time_end = $time_end;
return $this;
}
/**
* @return mixed
*/
public function getReturnCode()
{
return $this->return_code;
}
/**
* @param mixed $return_code
* @return Notify
*/
public function setReturnCode($return_code)
{
$this->return_code = $return_code;
return $this;
}
}
+157
View File
@@ -0,0 +1,157 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/8 0008
* Time: 9:49
*/
namespace qq;
use wchat\Miniprogarampage;
use wchat\Result;
class PublicTemplate extends Miniprogarampage
{
private $keywords = [];
private $templateId = '';
private $first = '';
private $remark = '';
private $openId = '';
private $defaultUrl = 'http://weixin.qq.com/download';
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send';
private $miniprogram = [];
/**
* @param array $keywords
*/
public function setKeywords(array $keywords)
{
$this->keywords = $keywords;
}
/**
* @param string $templateId
*/
public function setTemplateId(string $templateId)
{
$this->templateId = $templateId;
}
/**
* @param string $openId
*/
public function setOpenId(string $openId)
{
$this->openId = $openId;
}
/**
* @param string $defaultUrl
*/
public function setDefaultUrl(string $defaultUrl)
{
$this->defaultUrl = $defaultUrl;
}
/**
* @param $name
* @param $context
* @param string $color
*/
public function replaceKeyword($name, $context, $color = '')
{
$this->keywords[$name] = ['value' => $context, 'color' => $color];
}
/**
* @param $name
* @param $context
* @param null $color
*/
public function addKeyword($name, $context, $color = null)
{
if (empty($color)) {
$color = '#000';
}
$this->keywords[$name] = [
'value' => $context,
'color' => $color
];
}
/**
* @param $context
* @param string $color
*/
public function setFirst($context, $color = '#f00')
{
$this->first = [
'value' => $context,
'color' => $color
];
}
/**
* @param $context
* @param string $color
*/
public function setRemark($context, $color = '#000')
{
$this->remark = [
'value' => $context,
'color' => $color
];
}
/**
* @param $appid
* @param $pagepath
*/
public function setMiniprogram($appid, $pagepath)
{
$this->miniprogram = [
'appid' => $appid,
'pagepath' => $pagepath
];
}
/**
* @return Result
* @throws \Exception
*
* 奴隶交易通知
*/
public function sendTemplate()
{
$url = $this->sendUrl . '?access_token=' . $this->getAccessToken();
$keywords = $this->keywords;
$keywords['first'] = $this->first;
$keywords['remark'] = $this->remark;
$default = [
"touser" => $this->openId,
"template_id" => $this->templateId,
"url" => $this->defaultUrl,
"data" => $keywords,
];
if (!empty($this->miniprogram)) {
$default['miniprogram'] = $this->miniprogram;
}
$params = json_encode($default, JSON_UNESCAPED_UNICODE);
$this->request->setIsSSL(true);
$this->request->addHeader('content-type', 'application/json');
$result = $this->request->post($url, $params);
$result->append('postBody', $params);
return $result;
}
}
+116
View File
@@ -0,0 +1,116 @@
<?php
namespace qq;
use wchat\WxClient;
use wchat\Config;
class QqSDK
{
/** @var static $instance */
private static $instance = null;
/** @var Config $config */
private $config = null;
/**
* @return static
*/
public static function getMiniProGaRamPage()
{
if (static::$instance === null) {
static::$instance = new QqSDK();
}
return static::$instance;
}
/**
* @return Config
*/
public function getConfig()
{
return $this->config;
}
/**
* @param Config $config
* @return $this
*/
public function setConfig(Config $config)
{
$this->config = $config;
return $this;
}
/**
* @return Template
*/
public function getTemplate()
{
return Template::getInstance($this->config);
}
/**
* @return PublicTemplate
*/
public function getPublicTemplate()
{
return PublicTemplate::getInstance($this->config);
}
/**
* @return Account
*/
public function getAccount()
{
return Account::getInstance($this->config);
}
/**
* @return Message
*/
public function getMessage()
{
return Message::getInstance($this->config);
}
/**
* @return Recharge
*/
public function getRecharge()
{
return Recharge::getInstance($this->config);
}
/**
* @return Notify
*/
public function getNotify()
{
return Notify::getInstance($this->config);
}
/**
* @return WxClient
*/
public function getClient()
{
$client = WxClient::getInstance();
$client->setAgent($this->config->getAgent());
return $client;
}
/**
* @return Token
*/
public function getAccessToken()
{
return Token::getInstance($this->config);
}
}
+182
View File
@@ -0,0 +1,182 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/26 0026
* Time: 10:22
*/
namespace qq;
use wchat\Miniprogarampage;
use wchat\Result;
use wchat\Help;
use wchat\WxClient;
class Recharge extends Miniprogarampage
{
private $money = 0;
private $orderNo;
private $data = [];
private $transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
private $unifiedorder = 'https://qpay.qq.com/cgi-bin/pay/qpay_unified_order.cgi';
/**
* @param int $money
* @param string $orderNo
* @param string $openId
* @return array|mixed|Result
* @throws
*/
public function recharge(int $money, string $orderNo, $openId = '')
{
if ($money < 0) {
return new Result(['code' => 500, 'message' => '充值金额不能小于0.']);
}
$this->money = $money;
$this->orderNo = $orderNo;
$this->data['openid'] = $openId;
$config = $this->config;
$this->request->setCallback(function ($result, $body) use ($config) {
$data = Help::toArray($result);
if (isset($data['sign'])) {
$sign = $data['sign'];
unset($data['sign']);
}
$return = [];
$_sign = Help::sign($data, $config->getKey(), $config->getSignType());
if (!isset($sign) || $sign != $_sign) {
$return['code'] = -1;
$return['message'] = $data['return_msg'] ?? '返回数据签名验证失败';
} else {
$return['code'] = 0;
$return['data'] = $data;
$return['data']['postBody'] = $body;
if ($data['return_code'] == 'FAIL') {
$return['code'] = -1;
$return['message'] = $data['return_msg'];
}
}
return $return;
});
return $this->send($this->unifiedorder, $this->builder());
}
/**
* 'appId' => $result['appid'],
* 'nonceStr' => $result['nonce_str'],
* 'package' => 'prepay_id=' . $result['prepay_id'],
* 'signType' => 'MD5',
* 'timeStamp' => (string)time(),
* @param $prepay_id
* @return array
*/
public function reception($prepay_id)
{
$array = [
'appId' => $this->config->getAppid(),
'nonceStr' => Help::random(32),
'package' => 'prepay_id=' . $prepay_id,
'signType' => 'MD5',
'timeStamp' => (string)time(),
];
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$array['paySign'] = Help::sign($array, $key, $sign_type);
$array['appId'] = $this->config->getAppid();
return $array;
}
/**
* @return string
*/
protected function builder()
{
$data = [
'appid' => $this->config->getAppid(),
'mch_id' => $this->config->getMchId(),
'nonce_str' => Help::random(32),
'body' => $this->config->getBody(),
'out_trade_no' => $this->orderNo,
'total_fee' => $this->money,
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
'notify_url' => $this->config->getNotifyUrl(),
'trade_type' => $this->config->getTradeType(),
];
$this->data = array_merge($data, $this->data);
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$this->data['sign_type'] = $this->config->getSignType();
$this->data['sign'] = Help::sign($this->data, $key, $sign_type);
return Help::toXml($this->data);
}
public function getSignData()
{
$data = $this->data;
$data['key'] = $this->config->getKey();
return $data;
}
/**
* @param $money
* @param $openid
* @param $order
* @param $desc
* @return Result
* @throws
*
* 提现
*/
public function cashWithdrawal($money, $openid, $order, $desc = '零钱提现')
{
$array = [
'nonce_str' => Help::random(32),
'partner_trade_no' => $order,
'mchid' => $this->config->getMchId(),
'mch_appid' => $this->config->getAppid(),
'openid' => $openid,
'check_name' => 'NO_CHECK',
'amount' => $money * 100,
'spbill_create_ip' => $this->config->getRemoteAddr(),
'desc' => $desc,
];
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$array['sign'] = Help::sign($array, $key, $sign_type);
$this->request->setCallback(function ($data) {
$array = Help::toArray($data);
if ($array['result_code'] != 'SUCCESS') {
$data = ['code' => $array['err_code'], 'message' => $array['err_code_des']];
} else {
$data = ['code' => 0, 'message' => '支付成功'];
}
return new Result($data);
});
return $this->send($this->transfers, Help::toXml($array));
}
/**
* @param $url
* @param $data
* @return array|mixed|Result
* @throws \Exception
*/
private function send($url, $data)
{
$this->request->setIsSSL(true);
$this->request->setMethod(WxClient::POST);
$this->request->addHeader('Content-Type', 'text/xml');
return $this->request->send($url, $data);
}
}
+155
View File
@@ -0,0 +1,155 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/8 0008
* Time: 9:49
*/
namespace qq;
use wchat\Miniprogarampage;
use wchat\Result;
use wchat\Help;
use wchat\WxClient;
class Template extends Miniprogarampage
{
private $keywords = [];
private $templateId = '';
private $formId = '';
private $openId = '';
private $defaultUrl = '';
private $page = 'pages/index/index';
private $emphasis_keyword = '';
private $sendUrl = 'https://api.q.qq.com/api/json/template/send';
/**
* @param array $keywords
*/
public function setKeywords(array $keywords)
{
$this->keywords = $keywords;
}
/**
* @param string $templateId
*/
public function setTemplateId(string $templateId)
{
$this->templateId = $templateId;
}
/**
* @param string $formId
*/
public function setFormId(string $formId)
{
$this->formId = $formId;
}
/**
* @param string $openId
*/
public function setOpenId(string $openId)
{
$this->openId = $openId;
}
/**
* @param string $defaultUrl
*/
public function setDefaultUrl(string $defaultUrl)
{
$this->defaultUrl = $defaultUrl;
}
/**
* @param string $page
*/
public function setPage(string $page)
{
$this->page = $page;
}
/**
* @param string $emphasis_keyword
*/
public function setEmphasisKeyword(string $emphasis_keyword)
{
$this->emphasis_keyword = $emphasis_keyword;
}
/**
* @param $index
* @param $context
* @param $color
*/
public function replaceKeyword($index, $context, $color = '')
{
if (empty($color)) {
$color = '#000';
}
$this->keywords['keyword' . $index] = [
'value' => $context,
'color' => $color
];
}
/**
* @param $color
* @param $context
*/
public function addKeyword($context, $color = null)
{
if (empty($color)) {
$color = '#000';
}
$this->keywords['keyword' . (count($this->keywords) + 1)] = [
'value' => $context,
'color' => $color
];
}
/**
* @return Result
* @throws \Exception
*
* 奴隶交易通知
*/
public function sendTemplate()
{
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
$params = json_encode([
"touser" => $this->openId,
"template_id" => $this->templateId,
"page" => $this->page,
"form_id" => $this->formId,
"data" => $this->keywords,
], JSON_UNESCAPED_UNICODE);
if (!empty($this->emphasis_keyword)) {
$params['emphasis_keyword'] = $this->emphasis_keyword;
}
$this->request->setIsSSL(true);
$this->request->addHeader('content-type', 'application/json');
$result = $this->request->post($url, $params);
$result->append('postBody', $params);
$this->openId = '';
$this->keywords = [];
$this->formId = '';
$this->templateId = '';
$this->page = '';
return $result;
}
}
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace qq;
use wchat\Miniprogarampage;
class Token extends Miniprogarampage
{
private $url = 'https://api.q.qq.com/api/getToken';
/**
* @return mixed|null
*/
public function generateAccess_token()
{
$query = [
'grant_type' => 'client_credential',
'appid' => $this->config->getAppid(),
'secret' => $this->config->getAppsecret()
];
$param = $this->request->get($this->url, $query);
if (!$param->isResultsOK()) {
return null;
}
return $param->getData('access_token');
}
}
+20 -1
View File
@@ -12,6 +12,7 @@ class Account extends Miniprogarampage
private $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=';
private $getwxacode = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=';
private $publicInfo = 'https://api.weixin.qq.com/cgi-bin/user/info?';
private $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=';
private $savePath = __DIR__ . '/../../../';
@@ -45,7 +46,25 @@ class Account extends Miniprogarampage
$this->request->setMethod(WxClient::GET);
$this->request->addHeader('Content-Type', 'text/xml');
return $this->request->get('/sns/jscode2session', $param);
return $this->request->get('sns/jscode2session', $param);
}
/**
* @param $openid
* @return array|mixed|Result
* @throws \Exception
*/
public function getPublicUserInfo($openid)
{
$query = [
'access_token' => $this->getAccessToken(),
'openid' => $openid,
'lang' => 'zh_CN'
];
$this->request->setMethod(WxClient::GET);
$this->request->setIsSSL(true);
return $this->request->get($this->publicInfo, $query);
}
+44
View File
@@ -107,6 +107,27 @@ class Config
*/
private $key = '';
private $access_token = '';
private $agent = '';
/**
* @return string
*/
public function getAgent(): string
{
return $this->agent;
}
/**
* @param string $agent
* @return Config
*/
public function setAgent(string $agent): Config
{
$this->agent = $agent;
return $this;
}
/**
* @return string
@@ -396,4 +417,27 @@ class Config
return $this->key;
}
/**
* @param array $configs
* @return $this
*/
public function setConfigs(array $configs)
{
if (empty($configs)) {
return $this;
}
foreach ($configs as $key => $val) {
if (empty($val) || is_array($val)) {
continue;
}
if (!property_exists($this, $key)) {
continue;
}
$this->$key = $val;
}
return $this;
}
}
+8 -9
View File
@@ -73,17 +73,16 @@ class Help extends Miniprogarampage
*/
public static function sign(array $array, $key, $type)
{
ksort($array, SORT_STRING);
$string = '';
foreach ($array as $key => $val) {
if (empty($string)) {
$string = $key . '=' . $val;
} else {
$string .= '&' . $key . '=' . $val;
ksort($array, SORT_ASC);
$string = [];
foreach ($array as $hashKey => $val) {
if (empty($val)) {
continue;
}
$string[] = $hashKey . '=' . $val;
}
$string .= '&key=' . $key;
$string[] = 'key=' . $key;
$string = implode('&', $string);
if ($type == 'MD5') {
return strtoupper(md5($string));
} else {
+42
View File
@@ -116,6 +116,48 @@ class Message extends Miniprogarampage
}
/**
* @param string $media_id
* @param string $thumb_media_id
* @param string $title
* @param string $description
* @return Result
* @throws \Exception
*/
public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description)
{
$this->msgData['msgtype'] = 'video';
$this->msgData['video[media_id]'] = $media_id;
$this->msgData['video[thumb_media_id]'] = $thumb_media_id;
$this->msgData['video[title]'] = $title;
$this->msgData['video[description]'] = $description;
return $this->sendKefuMsg();
}
/**
* @param string $musicurl
* @param string $hqmusicurl
* @param string $thumb_media_id
* @param string $title
* @param string $description
* @return Result
* @throws \Exception
*/
public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description)
{
$this->msgData['msgtype'] = 'music';
$this->msgData['music[title]'] = $title;
$this->msgData['music[description]'] = $description;
$this->msgData['music[musicurl]'] = $musicurl;
$this->msgData['music[hqmusicurl]'] = $hqmusicurl;
$this->msgData['music[thumb_media_id]'] = $thumb_media_id;
return $this->sendKefuMsg();
}
/**
* @param string $head_content
* @param string $tail_content
+12 -7
View File
@@ -14,8 +14,8 @@ abstract class Miniprogarampage
/** @var Config */
protected $config;
/** @var mixed $instance */
private static $instance = null;
/** @var Miniprogarampage $instance */
protected static $instance = null;
/** @var WxClient */
protected $request = null;
@@ -25,9 +25,7 @@ abstract class Miniprogarampage
*/
private function __construct()
{
if (!($this->request instanceof WxClient)) {
$this->request = new WxClient();
}
$this->request = WxClient::getInstance();
$this->request->setIsSSL(true);
}
@@ -41,6 +39,13 @@ abstract class Miniprogarampage
static::$instance = new static();
}
static::$instance->config = $config;
static::$instance->request->setAgent($config->getAgent());
$request = static::$instance->request;
if ($request->getIsSSL()) {
$request->addHeader('ssl_cert_file', $config->getSslCert());
$request->addHeader('ssl_key_file', $config->getSslKey());
}
return static::$instance;
}
@@ -58,8 +63,8 @@ abstract class Miniprogarampage
$this->request->setMethod(WxClient::GET);
$data = $this->request->get('/cgi-bin/token', [
'grant_type' => 'client_credential',
'appid' => $this->config->getAppid(),
'secret' => $this->config->getAppsecret(),
'appid' => $this->config->getAppid(),
'secret' => $this->config->getAppsecret(),
]);
if (!$data->isResultsOK()) {
throw new \Exception($data->getMessage());
+598
View File
@@ -0,0 +1,598 @@
<?php
namespace wchat;
class Notify extends Miniprogarampage
{
public $appid = '';
public $mch_id = '';
public $device_info = '';
public $nonce_str = '';
public $sign = '';
public $sign_type = '';
public $result_code = '';
public $err_code = '';
public $err_code_des = '';
public $openid = '';
public $is_subscribe = '';
public $trade_type = '';
public $bank_type = '';
public $total_fee = '';
public $settlement_total_fee = '';
public $fee_type = '';
public $cash_fee = '';
public $cash_fee_type = '';
public $coupon_fee = '';
public $coupon_count = '';
public $coupon_type_n = '';
public $coupon_id_n = '';
public $coupon_fee_n = '';
public $transaction_id = '';
public $out_trade_no = '';
public $attach = '';
public $time_end = '';
public $return_code = '';
/**
* @return bool
* 判断是否完成支付
*/
public function isSuccess()
{
if ($this->getReturnCode() != "SUCCESS") {
return false;
}
if ($this->getResultCode() != 'SUCCESS') {
return false;
}
return true;
}
/**
* @param array $params
* @return $this
*/
public function setPayNotifyData(array $params)
{
if (!$this->validation($params)) {
$this->setResultCode('FAIL');
$this->setErrCodeDes('签名错误');
unset($params['result_code'], $params['err_code_des']);
}
foreach ($params as $key => $val) {
if (!property_exists($this, $key)) {
continue;
}
$this->$key = $val;
}
return $this;
}
/**
* @param array $params
* @return bool
*/
public function validation(array $params)
{
$sign = $params['sign'];
unset($params['sign']);
$signType = $this->config->getSignType();
$privateKey = $this->config->getKey();
$nowSign = Help::sign($params, $privateKey, $signType);
if ($sign === $nowSign) {
return true;
}
return false;
}
/**
* @return mixed
*/
public function getAppid()
{
return $this->appid;
}
/**
* @param mixed $appid
* @return Notify
*/
public function setAppid($appid)
{
$this->appid = $appid;
return $this;
}
/**
* @return mixed
*/
public function getMchId()
{
return $this->mch_id;
}
/**
* @param mixed $mch_id
* @return Notify
*/
public function setMchId($mch_id)
{
$this->mch_id = $mch_id;
return $this;
}
/**
* @return mixed
*/
public function getDeviceInfo()
{
return $this->device_info;
}
/**
* @param mixed $device_info
* @return Notify
*/
public function setDeviceInfo($device_info)
{
$this->device_info = $device_info;
return $this;
}
/**
* @return mixed
*/
public function getNonceStr()
{
return $this->nonce_str;
}
/**
* @param mixed $nonce_str
* @return Notify
*/
public function setNonceStr($nonce_str)
{
$this->nonce_str = $nonce_str;
return $this;
}
/**
* @return mixed
*/
public function getSign()
{
return $this->sign;
}
/**
* @param mixed $sign
* @return Notify
*/
public function setSign($sign)
{
$this->sign = $sign;
return $this;
}
/**
* @return mixed
*/
public function getSignType()
{
return $this->sign_type;
}
/**
* @param mixed $sign_type
* @return Notify
*/
public function setSignType($sign_type)
{
$this->sign_type = $sign_type;
return $this;
}
/**
* @return mixed
*/
public function getResultCode()
{
return $this->result_code;
}
/**
* @param mixed $result_code
* @return Notify
*/
public function setResultCode($result_code)
{
$this->result_code = $result_code;
return $this;
}
/**
* @return mixed
*/
public function getErrCode()
{
return $this->err_code;
}
/**
* @param mixed $err_code
* @return Notify
*/
public function setErrCode($err_code)
{
$this->err_code = $err_code;
return $this;
}
/**
* @return mixed
*/
public function getErrCodeDes()
{
return $this->err_code_des;
}
/**
* @param mixed $err_code_des
* @return Notify
*/
public function setErrCodeDes($err_code_des)
{
$this->err_code_des = $err_code_des;
return $this;
}
/**
* @return mixed
*/
public function getOpenid()
{
return $this->openid;
}
/**
* @param mixed $openid
* @return Notify
*/
public function setOpenid($openid)
{
$this->openid = $openid;
return $this;
}
/**
* @return mixed
*/
public function getIsSubscribe()
{
return $this->is_subscribe;
}
/**
* @param mixed $is_subscribe
* @return Notify
*/
public function setIsSubscribe($is_subscribe)
{
$this->is_subscribe = $is_subscribe;
return $this;
}
/**
* @return mixed
*/
public function getTradeType()
{
return $this->trade_type;
}
/**
* @param mixed $trade_type
* @return Notify
*/
public function setTradeType($trade_type)
{
$this->trade_type = $trade_type;
return $this;
}
/**
* @return mixed
*/
public function getBankType()
{
return $this->bank_type;
}
/**
* @param mixed $bank_type
* @return Notify
*/
public function setBankType($bank_type)
{
$this->bank_type = $bank_type;
return $this;
}
/**
* @return mixed
*/
public function getTotalFee()
{
return $this->total_fee;
}
/**
* @param mixed $total_fee
* @return Notify
*/
public function setTotalFee($total_fee)
{
$this->total_fee = $total_fee;
return $this;
}
/**
* @return mixed
*/
public function getSettlementTotalFee()
{
return $this->settlement_total_fee;
}
/**
* @param mixed $settlement_total_fee
* @return Notify
*/
public function setSettlementTotalFee($settlement_total_fee)
{
$this->settlement_total_fee = $settlement_total_fee;
return $this;
}
/**
* @return mixed
*/
public function getFeeType()
{
return $this->fee_type;
}
/**
* @param mixed $fee_type
* @return Notify
*/
public function setFeeType($fee_type)
{
$this->fee_type = $fee_type;
return $this;
}
/**
* @return mixed
*/
public function getCashFee()
{
return $this->cash_fee;
}
/**
* @param mixed $cash_fee
* @return Notify
*/
public function setCashFee($cash_fee)
{
$this->cash_fee = $cash_fee;
return $this;
}
/**
* @return mixed
*/
public function getCashFeeType()
{
return $this->cash_fee_type;
}
/**
* @param mixed $cash_fee_type
* @return Notify
*/
public function setCashFeeType($cash_fee_type)
{
$this->cash_fee_type = $cash_fee_type;
return $this;
}
/**
* @return mixed
*/
public function getCouponFee()
{
return $this->coupon_fee;
}
/**
* @param mixed $coupon_fee
* @return Notify
*/
public function setCouponFee($coupon_fee)
{
$this->coupon_fee = $coupon_fee;
return $this;
}
/**
* @return mixed
*/
public function getCouponCount()
{
return $this->coupon_count;
}
/**
* @param mixed $coupon_count
* @return Notify
*/
public function setCouponCount($coupon_count)
{
$this->coupon_count = $coupon_count;
return $this;
}
/**
* @return mixed
*/
public function getCouponTypeN()
{
return $this->coupon_type_n;
}
/**
* @param mixed $coupon_type_n
* @return Notify
*/
public function setCouponTypeN($coupon_type_n)
{
$this->coupon_type_n = $coupon_type_n;
return $this;
}
/**
* @return mixed
*/
public function getCouponIdN()
{
return $this->coupon_id_n;
}
/**
* @param mixed $coupon_id_n
* @return Notify
*/
public function setCouponIdN($coupon_id_n)
{
$this->coupon_id_n = $coupon_id_n;
return $this;
}
/**
* @return mixed
*/
public function getCouponFeeN()
{
return $this->coupon_fee_n;
}
/**
* @param mixed $coupon_fee_n
* @return Notify
*/
public function setCouponFeeN($coupon_fee_n)
{
$this->coupon_fee_n = $coupon_fee_n;
return $this;
}
/**
* @return mixed
*/
public function getTransactionId()
{
return $this->transaction_id;
}
/**
* @param mixed $transaction_id
* @return Notify
*/
public function setTransactionId($transaction_id)
{
$this->transaction_id = $transaction_id;
return $this;
}
/**
* @return mixed
*/
public function getOutTradeNo()
{
return $this->out_trade_no;
}
/**
* @param mixed $out_trade_no
* @return Notify
*/
public function setOutTradeNo($out_trade_no)
{
$this->out_trade_no = $out_trade_no;
return $this;
}
/**
* @return mixed
*/
public function getAttach()
{
return $this->attach;
}
/**
* @param mixed $attach
* @return Notify
*/
public function setAttach($attach)
{
$this->attach = $attach;
return $this;
}
/**
* @return mixed
*/
public function getTimeEnd()
{
return $this->time_end;
}
/**
* @param mixed $time_end
* @return Notify
*/
public function setTimeEnd($time_end)
{
$this->time_end = $time_end;
return $this;
}
/**
* @return mixed
*/
public function getReturnCode()
{
return $this->return_code;
}
/**
* @param mixed $return_code
* @return Notify
*/
public function setReturnCode($return_code)
{
$this->return_code = $return_code;
return $this;
}
}
+154
View File
@@ -0,0 +1,154 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/8 0008
* Time: 9:49
*/
namespace wchat;
class PublicTemplate extends Miniprogarampage
{
private $keywords = [];
private $templateId = '';
private $first = '';
private $remark = '';
private $openId = '';
private $defaultUrl = 'http://weixin.qq.com/download';
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send';
private $miniprogram = [];
/**
* @param array $keywords
*/
public function setKeywords(array $keywords)
{
$this->keywords = $keywords;
}
/**
* @param string $templateId
*/
public function setTemplateId(string $templateId)
{
$this->templateId = $templateId;
}
/**
* @param string $openId
*/
public function setOpenId(string $openId)
{
$this->openId = $openId;
}
/**
* @param string $defaultUrl
*/
public function setDefaultUrl(string $defaultUrl)
{
$this->defaultUrl = $defaultUrl;
}
/**
* @param $name
* @param $context
* @param string $color
*/
public function replaceKeyword($name, $context, $color = '')
{
$this->keywords[$name] = ['value' => $context, 'color' => $color];
}
/**
* @param $name
* @param $context
* @param null $color
*/
public function addKeyword($name, $context, $color = null)
{
if (empty($color)) {
$color = '#000';
}
$this->keywords[$name] = [
'value' => $context,
'color' => $color
];
}
/**
* @param $context
* @param string $color
*/
public function setFirst($context, $color = '#f00')
{
$this->first = [
'value' => $context,
'color' => $color
];
}
/**
* @param $context
* @param string $color
*/
public function setRemark($context, $color = '#000')
{
$this->remark = [
'value' => $context,
'color' => $color
];
}
/**
* @param $appid
* @param $pagepath
*/
public function setMiniprogram($appid, $pagepath)
{
$this->miniprogram = [
'appid' => $appid,
'pagepath' => $pagepath
];
}
/**
* @return Result
* @throws \Exception
*
* 奴隶交易通知
*/
public function sendTemplate()
{
$url = $this->sendUrl . '?access_token=' . $this->getAccessToken();
$keywords = $this->keywords;
$keywords['first'] = $this->first;
$keywords['remark'] = $this->remark;
$default = [
"touser" => $this->openId,
"template_id" => $this->templateId,
"url" => $this->defaultUrl,
"data" => $keywords,
];
if (!empty($this->miniprogram)) {
$default['miniprogram'] = $this->miniprogram;
}
$params = json_encode($default, JSON_UNESCAPED_UNICODE);
$this->request->setIsSSL(true);
$this->request->addHeader('content-type', 'application/json');
$result = $this->request->post($url, $params);
$result->append('postBody', $params);
return $result;
}
}
+62 -48
View File
@@ -35,41 +35,56 @@ class Recharge extends Miniprogarampage
$this->orderNo = $orderNo;
$this->data['openid'] = $openId;
$this->request->setCallback([$this, 'payCallback']);
$config = $this->config;
$this->request->setCallback(function ($result, $body) use ($config) {
$data = Help::toArray($result);
if (isset($data['sign'])) {
$sign = $data['sign'];
unset($data['sign']);
}
$return = [];
$_sign = Help::sign($data, $config->getKey(), $config->getSignType());
if (!isset($sign) || $sign != $_sign) {
$return['code'] = -1;
$return['message'] = $data['return_msg'] ?? '返回数据签名验证失败';
} else {
$return['code'] = 0;
$return['data'] = $data;
$return['data']['postBody'] = $body;
if ($data['return_code'] == 'FAIL') {
$return['code'] = -1;
$return['message'] = $data['return_msg'];
}
}
return $return;
});
return $this->send($this->unifiedorder, $this->builder());
}
/**
* @param $result
* @param $body
* 'appId' => $result['appid'],
* 'nonceStr' => $result['nonce_str'],
* 'package' => 'prepay_id=' . $result['prepay_id'],
* 'signType' => 'MD5',
* 'timeStamp' => (string)time(),
* @param $prepay_id
* @return array
*/
public function payCallback($result, $body)
public function reception($prepay_id)
{
$data = Help::toArray($result);
if (isset($data['sign'])) {
$sign = $data['sign'];
unset($data['sign']);
}
$return = [];
$_sign = Help::sign($data, $this->config->getKey(), $this->config->getSignType());
if (!isset($sign) || $sign != $_sign) {
$return['code'] = -1;
$return['message'] = $data['return_msg'] ?? '返回数据签名验证失败';
} else {
$return['code'] = 0;
$return['data'] = $data;
$return['data']['postBody'] = $body;
if ($data['return_code'] == 'FAIL') {
$return['code'] = -1;
$return['message'] = $data['return_msg'];
}
}
return $return;
$array = [
'appId' => $this->config->getAppid(),
'nonceStr' => Help::random(32),
'package' => 'prepay_id=' . $prepay_id,
'signType' => 'MD5',
'timeStamp' => (string)time(),
];
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$array['paySign'] = Help::sign($array, $key, $sign_type);
return $array;
}
/**
* @return string
*/
@@ -82,19 +97,27 @@ class Recharge extends Miniprogarampage
'body' => $this->config->getBody(),
'out_trade_no' => $this->orderNo,
'total_fee' => $this->money,
'sign_type' => $this->config->getSignType(),
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
'notify_url' => $this->config->getNotifyUrl(),
'trade_type' => $this->config->getTradeType(),
];
$data = array_merge($data, $this->data);
$this->data = array_merge($data, $this->data);
$key = $this->config->getKey();
$sign_type = $this->config->getSignType();
$data['sign'] = Help::sign($data, $key, $sign_type);
return Help::toXml($data);
$this->data['sign_type'] = $this->config->getSignType();
$this->data['sign'] = Help::sign($this->data, $key, $sign_type);
return Help::toXml($this->data);
}
public function getSignData()
{
$data = $this->data;
$data['key'] = $this->config->getKey();
return $data;
}
/**
@@ -125,7 +148,15 @@ class Recharge extends Miniprogarampage
$sign_type = $this->config->getSignType();
$array['sign'] = Help::sign($array, $key, $sign_type);
$this->request->setCallback([$this, 'txCallback']);
$this->request->setCallback(function ($data) {
$array = Help::toArray($data);
if ($array['result_code'] != 'SUCCESS') {
$data = ['code' => $array['err_code'], 'message' => $array['err_code_des']];
} else {
$data = ['code' => 0, 'message' => '支付成功'];
}
return new Result($data);
});
return $this->send($this->transfers, Help::toXml($array));
}
@@ -142,21 +173,4 @@ class Recharge extends Miniprogarampage
$this->request->addHeader('Content-Type', 'text/xml');
return $this->request->send($url, $data);
}
/**
* @param $data
* @return Result
* 提现回调
*/
public function txCallback($data)
{
$array = Help::toArray($data);
if ($array['result_code'] != 'SUCCESS') {
$data = ['code' => $array['err_code'], 'message' => $array['err_code_des']];
} else {
$data = ['code' => 0, 'message' => '支付成功'];
}
return new Result($data);
}
}
+3
View File
@@ -91,6 +91,9 @@ class Result
public function isResultsOK()
{
if (!is_numeric($this->code)) {
return false;
}
return $this->code == 0;
}
+134
View File
@@ -0,0 +1,134 @@
<?php
namespace wchat;
class Subject extends Miniprogarampage
{
private $keywords = [];
private $templateId = '';
private $openId = '';
private $defaultUrl = '';
private $page = 'pages/index/index';
private $emphasis_keyword = '';
private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send';
/**
* @param array $keywords
*/
public function setKeywords(array $keywords)
{
$this->keywords = $keywords;
}
/**
* @param string $templateId
*/
public function setTemplateId(string $templateId)
{
$this->templateId = $templateId;
}
/**
* @param string $openId
*/
public function setOpenId(string $openId)
{
$this->openId = $openId;
}
/**
* @param string $defaultUrl
*/
public function setDefaultUrl(string $defaultUrl)
{
$this->defaultUrl = $defaultUrl;
}
/**
* @param string $page
*/
public function setPage(string $page)
{
$this->page = $page;
}
/**
* @param string $emphasis_keyword
*/
public function setEmphasisKeyword(string $emphasis_keyword)
{
$this->emphasis_keyword = $emphasis_keyword;
}
/**
* @param $index
* @param $context
* @param $color
*/
public function replaceKeyword($index, $context, $color = '')
{
if (empty($color)) {
$color = '#000';
}
$this->keywords['keyword' . $index] = [
'value' => $context,
'color' => $color
];
}
/**
* @param $color
* @param $context
*/
public function addKeyword($context, $color = null)
{
if (empty($color)) {
$color = '#000';
}
$this->keywords['keyword' . (count($this->keywords) + 1)] = [
'value' => $context,
'color' => $color
];
}
/**
* @return Result
* @throws \Exception
*
* 奴隶交易通知
*/
public function sendTemplate()
{
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
$params = json_encode([
"touser" => $this->openId,
"template_id" => $this->templateId,
"page" => $this->page,
"data" => $this->keywords,
], JSON_UNESCAPED_UNICODE);
if (!empty($this->emphasis_keyword)) {
$params['emphasis_keyword'] = $this->emphasis_keyword;
}
$this->request->setIsSSL(true);
$this->request->addHeader('content-type', 'application/json');
$result = $this->request->post($url, $params);
$result->append('postBody', $params);
$this->openId = '';
$this->keywords = [];
$this->templateId = '';
$this->page = '';
return $result;
}
}
+12 -7
View File
@@ -104,7 +104,7 @@ class Template extends Miniprogarampage
if (empty($color)) {
$color = '#000';
}
$this->keywords['keyword' . count($this->keywords)] = [
$this->keywords['keyword' . (count($this->keywords) + 1)] = [
'value' => $context,
'color' => $color
];
@@ -118,14 +118,13 @@ class Template extends Miniprogarampage
*/
public function sendTemplate()
{
$url = $this->sendUrl . '?access_token=' . $this->getAccessToken();
$url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken();
$params = json_encode([
"touser" => $this->openId,
"touser" => $this->openId,
"template_id" => $this->templateId,
"page" => $this->page,
"form_id" => $this->formId,
"data" => $this->keywords,
"page" => $this->page,
"form_id" => $this->formId,
"data" => $this->keywords,
], JSON_UNESCAPED_UNICODE);
if (!empty($this->emphasis_keyword)) {
@@ -138,6 +137,12 @@ class Template extends Miniprogarampage
$result = $this->request->post($url, $params);
$result->append('postBody', $params);
$this->openId = '';
$this->keywords = [];
$this->formId = '';
$this->templateId = '';
$this->page = '';
return $result;
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace wchat;
class Token extends Miniprogarampage
{
private $url = 'https://api.q.qq.com/cgi-bin/token';
/**
* @return mixed|null
*/
public function generateAccess_token()
{
$query = [
'grant_type' => 'client_credential',
'appid' => $this->config->getAppid(),
'secret' => $this->config->getAppsecret()
];
$param = $this->request->get($this->url, $query);
if (!$param->isResultsOK()) {
return null;
}
return $param->getData('access_token');
}
}
+46
View File
@@ -51,6 +51,15 @@ class Wx
return Template::getInstance($this->config);
}
/**
* @return PublicTemplate
*/
public function getPublicTemplate()
{
return PublicTemplate::getInstance($this->config);
}
/**
* @return Account
*/
@@ -74,4 +83,41 @@ class Wx
{
return Recharge::getInstance($this->config);
}
/**
* @return Notify
*/
public function getNotify()
{
return Notify::getInstance($this->config);
}
/**
* @return Token
*/
public function getToken()
{
return Token::getInstance($this->config);
}
/**
* @return WxClient
*/
public function getClient()
{
$client = WxClient::getInstance();
$client->setAgent($this->config->getAgent());
return $client;
}
/**
* @return Subject
*/
public function getSubject()
{
return Subject::getInstance($this->config);
}
}
+57 -25
View File
@@ -3,6 +3,7 @@
namespace wchat;
use Swoole\Coroutine\Http\Client;
use Swoole\Coroutine\System;
class WxClient
{
@@ -15,6 +16,7 @@ class WxClient
private $url = '';
private $isSSL = false;
private $agent = '';
const POST = 'post';
const GET = 'get';
@@ -22,6 +24,22 @@ class WxClient
const DELETE = 'delete';
const OPTIONS = 'option';
private function __construct()
{
}
/**
* @return WxClient
*/
public static function getInstance()
{
static $client = null;
if (!($client instanceof WxClient)) {
$client = new WxClient();
}
return $client;
}
/**
* @param string $host
*/
@@ -71,17 +89,22 @@ class WxClient
$this->url = $url;
}
public function setAgent(string $agent)
{
$this->agent = $agent;
}
/**
* @param bool $isSSL
*/
public function setIsSSL(bool $isSSL)
{
$this->isSSL = $isSSL;
if ($this->isSSL) {
$ssl = Wx::getMiniProGaRamPage()->getConfig();
$this->header['ssl_cert_file'] = $ssl->getSslCert();
$this->header['ssl_key_file'] = $ssl->getSslKey();
}
}
public function getIsSSL()
{
return $this->isSSL;
}
@@ -93,6 +116,16 @@ class WxClient
*/
private function request($url, $data = [])
{
if (function_exists('getIsCli') && getIsCli()) {
if (strpos($url, 'http://') !== FALSE) {
$url = str_replace('http://', '', $url);
} else if (strpos($url, 'https://') !== FALSE) {
$url = str_replace('https://', '', $url);
}
$explode = explode('/', $url);
$ip = System::gethostbyname(array_shift($explode));
return $this->coroutine($ip, $url, $data);
}
if (
strpos($url, 'http://') === 0 ||
strpos($url, 'https://') === 0
@@ -100,10 +133,6 @@ class WxClient
return $this->curl($url, $data);
}
if (function_exists('getIsCli') && getIsCli()) {
return $this->coroutine($url, $data);
}
if ($this->isSSL) {
return $this->curl('https://' . $this->host . '/' . $url, $data);
} else {
@@ -112,23 +141,22 @@ class WxClient
}
/**
* @param $ip
* @param $url
* @param array $data
* @return array|mixed|Result
* @throws \Exception
*
* 使用swoole协程方式请求
*/
private function coroutine($url, $data = [])
private function coroutine($ip, $url, $data = [])
{
$_data = $this->paramEncode($data);
if ($this->method == 'get' && is_array($_data)) {
$url .= '?' . http_build_query($_data);
}
$client = $this->getClient($this->getHostPort(), $url, $data);
$client = $this->getClient($ip, $this->getHostPort(), $url, $data);
if ($client->statusCode < 0) {
throw new \Exception('连接错误!');
throw new \Exception($client->errMsg);
}
$body = $client->body;
$client->close();
@@ -141,7 +169,8 @@ class WxClient
*/
private function getHostIp()
{
return array_shift(\Co::getAddrInfo($this->host));
$host = \Co::getAddrInfo($this->host);
return array_shift($host);
}
/**
@@ -155,20 +184,21 @@ class WxClient
}
/**
* @param $host
* @param $port
* @param $url
* @param $data
* @return Client
*/
private function getClient($port, $url, $data)
private function getClient($host, $port, $url, $data)
{
$host = $this->getHostIp();
$client = new Client($host, $port, $this->isSSL);
if (!empty($this->agent)) {
$this->header['User-Agent'] = $this->agent;
}
if (!empty($this->header)) {
$client->setHeaders($this->header);
}
switch (strtolower($this->method)) {
case self::GET:
$client->get($url);
@@ -214,27 +244,29 @@ class WxClient
{
$ch = curl_init();
if ($this->method == self::GET) {
if (is_array($_data)) {
$_data = http_build_query($_data);
}
$url = $url . '?' . $_data;
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);// 超时设置
curl_setopt($ch, CURLOPT_HEADER, FALSE);
if (!empty($this->header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
}
if (!empty($this->agent)) {
curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
}
if ($this->isSSL) {
curl_setopt($ch, CURLOPT_SSLCERT, $this->header['ssl_cert_file']);
curl_setopt($ch, CURLOPT_SSLKEY, $this->header['ssl_key_file']);
}
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);// 超时设置
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // 超时设置
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
return $ch;
}
@@ -247,7 +279,7 @@ class WxClient
private function structure($body, $_data)
{
$this->setIsSSL(false);
$this->setHeaders(null);
$this->setHeaders([]);
if ($this->callback !== NULL) {
$result = call_user_func($this->callback, $body, $_data);