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); } }