| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace addons\QiShangTong\common\helper;
- use addons\QiShangTong\common\expand\sdk\qishangtong\ModelInterface;
- use addons\QiShangTong\common\expand\sdk\qishangtong\RequestInterface;
- use addons\QiShangTong\common\models\QiShangTongError;
- use addons\QiShangTong\common\service\SettingService;
- use yii\helpers\Json;
- class RequestHelper
- {
- /**
- * 加密
- * @param ModelInterface $model
- * @param int $mall_id
- * @return ModelInterface
- * @throws \Exception
- */
- public static function packCommonParams(ModelInterface $model, int $mall_id)
- {
- $mall_id = SettingService::getMasterMall();
- $config = (new SettingService())->get($mall_id);
- $model->app_key = $config['app_key'];
- $model->timestamp = time();
- $model->sign = self::sign($model->toArray(), $config['secret_key']);
- // 生成签名
- return $model;
- }
- public static function sign(array $data, $secret)
- {
- if (isset($data['sign'])) unset($data['sign']);
- foreach ($data as &$datum) {
- if (is_string($datum)) {
- $datum = trim($datum);
- }
- }
- ksort($data);
- $query = http_build_query($data);
- $query .= "&secret=$secret";
- return strtoupper(md5(strtoupper($query)));
- }
- /**
- * 执行请求
- * @param RequestInterface $request
- * @param ModelInterface $model
- * @param int $mall_id
- * @return array
- * @throws \Exception
- */
- public static function send(RequestInterface $request, ModelInterface $model, int $mall_id)
- {
- $mall_id1 = SettingService::getMasterMall();
- $resp = $request->send(self::packCommonParams($model, $mall_id1));
- if (!empty($resp->getError())) throw new \Exception($resp->getError());
- $res = $resp->toArray();
- if (!isset($res['status']) || $res['status'] != 200) {
- QiShangTongError::setData([
- 'mall_id' => $mall_id,
- 'post' => $model->toArray(),
- 'content' => !is_array($res) ? [$res] : $res,
- 'route' => $request->getRoute(),
- ]);
- // throw new \Exception($res['message']);
- return $res['message'];
- }
- return $res['data'] ?? [];
- }
- }
|