add clear

This commit is contained in:
as2252258@163.com
2019-12-02 17:53:08 +08:00
parent f7893e08ae
commit 4ed83cb6d8
3 changed files with 610 additions and 2 deletions
+500
View File
@@ -0,0 +1,500 @@
<?php
namespace qq;
use common\Help;
use common\Result;
use qq\result\RedHatResult;
class Redhat extends SmallProgram
{
private $charset = 'UTF8';
private $nonce_str = '';
private $sign = '';
private $mch_billno = '';
private $mch_id = '';
private $mch_name = '';
private $qqappid = '';
private $re_openid = '';
private $total_amount = '';
private $total_num = '';
private $wishing = '';
private $act_name = '';
private $icon_id = '';
private $banner_id = '';
private $notify_url = '';
private $not_send_msg = '';
private $min_value = '';
private $max_value = '';
private $key = '';
private $signType = '';
private $sendUrl = 'https://api.qpay.qq.com/cgi-bin/hongbao/qpay_hb_mch_send.cgi';
private $searchUrl = 'https://qpay.qq.com/cgi-bin/mch_query/qpay_hb_mch_list_query.cgi';
/**
* @return string
*/
public function getKey(): string
{
return $this->key;
}
/**
* @param string $key
* @return Redhat
*/
public function setKey(string $key): Redhat
{
$this->key = $key;
return $this;
}
/**
* @return string
*/
public function getSignType(): string
{
return $this->signType;
}
/**
* @param string $signType
* @return Redhat
*/
public function setSignType(string $signType): Redhat
{
$this->signType = $signType;
return $this;
}
/**
* @return string
*/
public function getCharset(): string
{
return $this->charset;
}
/**
* @param string $charset
* @return Redhat
*/
public function setCharset(string $charset): Redhat
{
$this->charset = $charset;
return $this;
}
/**
* @return string
*/
public function getNonceStr(): string
{
return $this->nonce_str;
}
/**
* @param string $nonce_str
* @return Redhat
*/
public function setNonceStr(string $nonce_str): Redhat
{
$this->nonce_str = $nonce_str;
return $this;
}
/**
* @return string
*/
public function getSign(): string
{
return $this->sign;
}
/**
* @param string $sign
* @return Redhat
*/
public function setSign(string $sign): Redhat
{
$this->sign = $sign;
return $this;
}
/**
* @return string
*/
public function getMchBillno(): string
{
if (empty($this->mch_billno)) {
return '1559179381' . date('Ymd') . mt_rand(11, 99) . date('His') . mt_rand(11, 99);
}
return $this->mch_billno;
}
/**
* @param string $mch_billno
* @return Redhat
*/
public function setMchBillno(string $mch_billno): Redhat
{
$this->mch_billno = $mch_billno;
return $this;
}
/**
* @return string
*/
public function getMchId(): string
{
return $this->mch_id;
}
/**
* @param string $mch_id
* @return Redhat
*/
public function setMchId(string $mch_id): Redhat
{
$this->mch_id = $mch_id;
return $this;
}
/**
* @return string
*/
public function getMchName(): string
{
return $this->mch_name;
}
/**
* @param string $mch_name
* @return Redhat
*/
public function setMchName(string $mch_name): Redhat
{
$this->mch_name = $mch_name;
return $this;
}
/**
* @return string
*/
public function getQqappid(): string
{
return $this->qqappid;
}
/**
* @param string $qqappid
* @return Redhat
*/
public function setQqappid(string $qqappid): Redhat
{
$this->qqappid = $qqappid;
return $this;
}
/**
* @return string
*/
public function getReOpenid(): string
{
return $this->re_openid;
}
/**
* @param string $re_openid
* @return Redhat
*/
public function setReOpenid(string $re_openid): Redhat
{
$this->re_openid = $re_openid;
return $this;
}
/**
* @return string
*/
public function getTotalAmount(): string
{
return $this->total_amount;
}
/**
* @param string $total_amount
* @return Redhat
*/
public function setTotalAmount(string $total_amount): Redhat
{
$this->total_amount = $total_amount;
return $this;
}
/**
* @return string
*/
public function getTotalNum(): string
{
return $this->total_num;
}
/**
* @param string $total_num
* @return Redhat
*/
public function setTotalNum(string $total_num): Redhat
{
$this->total_num = $total_num;
return $this;
}
/**
* @return string
*/
public function getWishing(): string
{
return $this->wishing;
}
/**
* @param string $wishing
* @return Redhat
*/
public function setWishing(string $wishing): Redhat
{
$this->wishing = $wishing;
return $this;
}
/**
* @return string
*/
public function getActName(): string
{
return $this->act_name;
}
/**
* @param string $act_name
* @return Redhat
*/
public function setActName(string $act_name): Redhat
{
$this->act_name = $act_name;
return $this;
}
/**
* @return string
*/
public function getIconId(): string
{
return $this->icon_id;
}
/**
* @param string $icon_id
* @return Redhat
*/
public function setIconId(string $icon_id): Redhat
{
$this->icon_id = $icon_id;
return $this;
}
/**
* @return string
*/
public function getBannerId(): string
{
return $this->banner_id;
}
/**
* @param string $banner_id
* @return Redhat
*/
public function setBannerId(string $banner_id): Redhat
{
$this->banner_id = $banner_id;
return $this;
}
/**
* @return string
*/
public function getNotifyUrl(): string
{
return $this->notify_url;
}
/**
* @param string $notify_url
* @return Redhat
*/
public function setNotifyUrl(string $notify_url): Redhat
{
$this->notify_url = $notify_url;
return $this;
}
/**
* @return string
*/
public function getNotSendMsg(): string
{
return $this->not_send_msg;
}
/**
* @param string $not_send_msg
* @return Redhat
*/
public function setNotSendMsg(string $not_send_msg): Redhat
{
$this->not_send_msg = $not_send_msg;
return $this;
}
/**
* @return string
*/
public function getMinValue(): string
{
return $this->min_value;
}
/**
* @param string $min_value
* @return Redhat
*/
public function setMinValue(string $min_value): Redhat
{
$this->min_value = $min_value;
return $this;
}
/**
* @return string
*/
public function getMaxValue(): string
{
return $this->max_value;
}
/**
* @param string $max_value
* @return Redhat
*/
public function setMaxValue(string $max_value): Redhat
{
$this->max_value = $max_value;
return $this;
}
/**
* @return mixed
* 构建请求参数
*/
private function generate()
{
$requestParam['charset'] = $this->getCharset();
$requestParam['nonce_str'] = $this->getNonceStr();
$requestParam['mch_billno'] = $this->getMchBillno();
$requestParam['mch_id'] = $this->getMchId();
$requestParam['mch_name'] = $this->getMchName();
$requestParam['qqappid'] = $this->getQqappid();
$requestParam['re_openid'] = $this->getReOpenid();
$requestParam['total_amount'] = $this->getTotalAmount() * 100;
$requestParam['min_value'] = $this->getMinValue() * 100;
$requestParam['max_value'] = $this->getMaxValue() * 100;
$requestParam['total_num'] = $this->getTotalNum();
$requestParam['wishing'] = $this->getWishing();
$requestParam['act_name'] = $this->getActName();
$requestParam['icon_id'] = $this->getIconId();
$requestParam['notify_url'] = $this->getNotifyUrl();
$requestParam['sign'] = $this->builderSign($requestParam);
return $requestParam;
}
/**
* @param $requestParam
* @return string
*/
private function builderSign($requestParam)
{
return Help::sign($requestParam, $this->getKey(), $this->getSignType());
}
/**
* @return string
* @throws
* 发红包
*/
public function redEnvelopes()
{
$callback = function ($response) {
$json = json_decode($response, true);
if (isset($json['return_code']) && $json['return_code'] != 'SUCCESS') {
$message = $json['return_msg'] ?? $json['retmsg'];
$param = ['code' => 500, 'message' => $message];
} else if ($json['retcode'] != 0) {
$param = ['code' => $json['retcode'], 'message' => $json['retmsg']];
} else {
$param = ['code' => 0, 'message' => $json['retmsg'], 'data' => ['listid' => $json['listid']]];
}
return new Result($param);
};
$this->request->setCallback($callback);
$response = $this->request->post($this->sendUrl, $this->generate());
if (!$response->isResultsOK()) {
throw new \Exception($response->getMessage());
}
return $response->getData('listid');
}
/**
* @param $listid
* @param null $orderNo
* @return RedHatResult
* @throws \Exception
*
* 查询红包状态
*/
public function searchByOrderNo($listid, $orderNo = null)
{
$requestParam['nonce_str'] = $this->getNonceStr();
$requestParam['mch_id'] = $this->getMchId();
$requestParam['listid'] = $listid;
if (!empty($orderNo)) {
$requestParam['mch_billno'] = $orderNo;
}
$requestParam['sign'] = $this->builderSign($requestParam);
$this->request->setCallback(function ($response) {
$response = json_decode($response, true);
if (!isset($response['result']) || $response['result'] != 0) {
$param = ['code' => 500, 'message' => $response['res_info'] ?? '订单查询失败!', 'data' => $response];
} else {
$param = ['code' => 0, 'message' => 'system success!', 'data' => $response];
}
return new Result($param);
});
$response = $this->request->get($this->searchUrl, $requestParam);
if (!$response->isResultsOK()) {
throw new \Exception($response->getMessage());
}
return new RedHatResult($response->getData());
}
}
+6 -2
View File
@@ -15,9 +15,10 @@ class SmallProgram extends Miniprogarampage
/**
* @param $get_token
* @return mixed|null
*/
public function generateAccess_token()
public function generateAccess_token($get_token = false)
{
if (!empty($this->config->getToken())) {
return $this->config->getToken();
@@ -31,6 +32,9 @@ class SmallProgram extends Miniprogarampage
if (!$param->isResultsOK()) {
return null;
}
return $param->getData('access_token');
if ($get_token) {
return $param->getData('access_token');
}
return $param->getData();
}
}
+104
View File
@@ -0,0 +1,104 @@
<?php
namespace qq\result;
/**
* Class RedHatResult
* @package qq\result
*/
class RedHatResult
{
public $result;
public $res_info;
public $listid;
public $state;
public $total_num;
public $recv_num;
public $total_amount;
public $recv_amount;
public $recv_details;
public $uin;
const RED_HAT_STATUS_PAID = 1;
const RED_HAT_STATUS_SNATCHED = 2;
const RED_HAT_STATUS_EXPIRED = 3;
const RED_HAT_STATUS_REFUNDED = 4;
/**
* RedHatResult constructor.
* @param array $array
*/
public function __construct(array $array)
{
$this->init($array);
}
/**
* @param $array
* @return $this
*/
private function init($array)
{
foreach ($array as $key => $value) {
if (!property_exists($this, $key)) {
continue;
}
$this->{$key} = $value;
}
return $this;
}
/**
* @param $field
* @return mixed
*/
public function get($field)
{
return $this->$field;
}
/**
* @return bool
* 是否已完成支付
*/
public function isPaid()
{
return $this->state == self::RED_HAT_STATUS_PAID;
}
/**
* @return bool
* 是否已抢完
*/
public function isSnatched()
{
return $this->state == self::RED_HAT_STATUS_SNATCHED;
}
/**
* @return bool
*/
public function isExpired()
{
return $this->state == self::RED_HAT_STATUS_EXPIRED;
}
/**
* @return bool
*/
public function isRefunded()
{
return $this->state == self::RED_HAT_STATUS_REFUNDED;
}
}