diff --git a/qq/Redhat.php b/qq/Redhat.php new file mode 100644 index 0000000..ce24e81 --- /dev/null +++ b/qq/Redhat.php @@ -0,0 +1,500 @@ +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()); + } + +} diff --git a/qq/SmallProgram.php b/qq/SmallProgram.php index 037afc6..e4b2b30 100644 --- a/qq/SmallProgram.php +++ b/qq/SmallProgram.php @@ -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(); } } diff --git a/qq/result/RedHatResult.php b/qq/result/RedHatResult.php new file mode 100644 index 0000000..462027a --- /dev/null +++ b/qq/result/RedHatResult.php @@ -0,0 +1,104 @@ +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; + } + +}