| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- namespace addons\WechatVideoShop\common\helper;
- use addons\WechatVideoShop\common\expand\sdk\weixin\ModelInterface;
- use addons\WechatVideoShop\common\expand\sdk\weixin\RequestInterface;
- use common\helpers\FormatHelper;
- class WechatRequestHelper
- {
- /**
- * @param RequestInterface $request
- * @param ModelInterface|null $model
- * @param bool $validResp
- * @return RequestInterface
- * @throws \Exception
- */
- public static function execute(RequestInterface $request, ModelInterface $model = null, bool $validResp = true)
- {
- try {
- $resp = $request->send($model);
- if (!empty($resp->getError())) throw new \Exception($resp->getError());
- // 如果code不为0
- if ((!empty($model) && $validResp) && !empty($resp->respError())) throw new \Exception($resp->respError());
- return $resp;
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- $msg = explode(" rid: ", $e->getMessage());
- throw new \Exception($msg[0] ?? '未知错误');
- }
- }
- /**
- * @param RequestInterface $request
- * @param ModelInterface|null $model
- * @param bool $validResp
- * @return array
- * @throws \Exception
- */
- public static function toArray(RequestInterface $request, ModelInterface $model = null, bool $validResp = true)
- {
- return self::execute($request, $model, $validResp)->toArray();
- }
- /**
- * @param RequestInterface $request
- * @param ModelInterface|null $model
- * @param bool $validResp
- * @return array
- * @throws \Exception
- */
- public static function accessTokenAndToArray(RequestInterface $request, ModelInterface $model = null, bool $validResp = true)
- {
- return self::execute($request->accessToken(), $model, $validResp)->toArray();
- }
- /**
- * @param RequestInterface $request
- * @param ModelInterface|null $model
- * @param bool $validResp
- * @return mixed
- * @throws \Exception
- */
- public static function accessTokenRawContent(RequestInterface $request, ModelInterface $model = null, bool $validResp = true)
- {
- return self::execute($request->accessToken(), $model, $validResp)->getRawContent();
- }
- }
|