| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace addons\QiDingDong\common\helper;
- use addons\QiDingDong\common\expand\sdk\qidingdong\ModelInterface;
- use addons\QiDingDong\common\expand\sdk\qidingdong\RequestInterface;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\ContentBaseModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\ContentModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\ContentValueModel;
- use addons\QiDingDong\common\models\QiDingDongError;
- use addons\QiDingDong\common\service\SettingService;
- use yii\helpers\Json;
- class RequestHelper
- {
- /**
- * @param int $mall_id
- * @return ContentModel
- * @throws \Exception
- */
- public static function contentModelInstance(int $mall_id): ContentModel
- {
- $mall_id = SettingService::getMasterMall();
- $config = (new SettingService())->get($mall_id);
- if (empty($config['app_key'])) throw new \Exception("请先配置AppKey");
- if (empty($config['token'])) throw new \Exception("请先配置Token");
- $time = time();
- $cParams = new ContentModel();
- $cParams->lock = "";
- $cParams->app_key = $config['app_key'];
- $cParams->time = $time;
- $cParams->content = new ContentValueModel();
- // base
- $base = new ContentBaseModel();
- $base->equipment = "";
- $base->time = $time;
- $base->source = 99;
- $base->version = "v3.2.1";
- $base->token = $config['token'];
- $cParams->content->base = $base;
- return $cParams;
- }
- /**
- * 加密
- * @param ContentModel $model
- * @param int $mall_id
- * @return ContentModel
- * @throws \Exception
- */
- public static function enCrypto(ContentModel $model, int $mall_id)
- {
- $mall_id = SettingService::getMasterMall();
- $json = Json::encode($model->content->toArray());
- $encrypt = AesHelper::encrypt($json);
- // $public_key = dirname(\Yii::$app->basePath) .'/'. (new SettingService())->get($mall_id)['public_key'];
- //
- // if (empty($public_key)) throw new \Exception("请上传Public Key");
- RsaHelper::setPublicKey($mall_id);
- $model->lock = RsaHelper::publicEncrypt($encrypt[1]);
- $model->content = $encrypt[0];
- return $model;
- }
- /**
- * 执行请求
- * @param RequestInterface $request
- * @param ModelInterface $model
- * @param int $mall_id
- * @return array|int|string
- * @throws \Exception
- */
- public static function send(RequestInterface $request, ModelInterface $model, $mall_id = 0)
- {
- $resp = $request->send($model);
- if (!empty($resp->getError())) throw new \Exception($resp->getError());
- $res = $resp->toArray();
- if (!isset($res['status']) || $res['status'] != 200) {
- QiDingDongError::setData([
- 'mall_id' => $mall_id,
- 'post' => $model->toArray(),
- 'content' => !is_array($res) ? [$res] : $res,
- 'route' => $request->getRoute(),
- ]);
- // throw new \Exception($res['msg']);
- return $res['msg'];
- }
- $mall_id = SettingService::getMasterMall();
- return self::decrypt($res, $mall_id);
- }
- /**
- * 解密
- * @param $res
- * @param $mall_id
- * @return array|int|string
- * @throws \Exception
- */
- public static function decrypt($res, $mall_id)
- {
- $mall_id = SettingService::getMasterMall();
- RsaHelper::setPublicKey($mall_id);
- $lock = RsaHelper::publicDecrypt($res['lock']);
- return AesHelper::decrypt($res['content'], $lock);
- }
- }
|