RequestHelper.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace addons\QiDingDong\common\helper;
  3. use addons\QiDingDong\common\expand\sdk\qidingdong\ModelInterface;
  4. use addons\QiDingDong\common\expand\sdk\qidingdong\RequestInterface;
  5. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\ContentBaseModel;
  6. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\ContentModel;
  7. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\ContentValueModel;
  8. use addons\QiDingDong\common\models\QiDingDongError;
  9. use addons\QiDingDong\common\service\SettingService;
  10. use yii\helpers\Json;
  11. class RequestHelper
  12. {
  13. /**
  14. * @param int $mall_id
  15. * @return ContentModel
  16. * @throws \Exception
  17. */
  18. public static function contentModelInstance(int $mall_id): ContentModel
  19. {
  20. $mall_id = SettingService::getMasterMall();
  21. $config = (new SettingService())->get($mall_id);
  22. if (empty($config['app_key'])) throw new \Exception("请先配置AppKey");
  23. if (empty($config['token'])) throw new \Exception("请先配置Token");
  24. $time = time();
  25. $cParams = new ContentModel();
  26. $cParams->lock = "";
  27. $cParams->app_key = $config['app_key'];
  28. $cParams->time = $time;
  29. $cParams->content = new ContentValueModel();
  30. // base
  31. $base = new ContentBaseModel();
  32. $base->equipment = "";
  33. $base->time = $time;
  34. $base->source = 99;
  35. $base->version = "v3.2.1";
  36. $base->token = $config['token'];
  37. $cParams->content->base = $base;
  38. return $cParams;
  39. }
  40. /**
  41. * 加密
  42. * @param ContentModel $model
  43. * @param int $mall_id
  44. * @return ContentModel
  45. * @throws \Exception
  46. */
  47. public static function enCrypto(ContentModel $model, int $mall_id)
  48. {
  49. $mall_id = SettingService::getMasterMall();
  50. $json = Json::encode($model->content->toArray());
  51. $encrypt = AesHelper::encrypt($json);
  52. // $public_key = dirname(\Yii::$app->basePath) .'/'. (new SettingService())->get($mall_id)['public_key'];
  53. //
  54. // if (empty($public_key)) throw new \Exception("请上传Public Key");
  55. RsaHelper::setPublicKey($mall_id);
  56. $model->lock = RsaHelper::publicEncrypt($encrypt[1]);
  57. $model->content = $encrypt[0];
  58. return $model;
  59. }
  60. /**
  61. * 执行请求
  62. * @param RequestInterface $request
  63. * @param ModelInterface $model
  64. * @param int $mall_id
  65. * @return array|int|string
  66. * @throws \Exception
  67. */
  68. public static function send(RequestInterface $request, ModelInterface $model, $mall_id = 0)
  69. {
  70. $resp = $request->send($model);
  71. if (!empty($resp->getError())) throw new \Exception($resp->getError());
  72. $res = $resp->toArray();
  73. if (!isset($res['status']) || $res['status'] != 200) {
  74. QiDingDongError::setData([
  75. 'mall_id' => $mall_id,
  76. 'post' => $model->toArray(),
  77. 'content' => !is_array($res) ? [$res] : $res,
  78. 'route' => $request->getRoute(),
  79. ]);
  80. // throw new \Exception($res['msg']);
  81. return $res['msg'];
  82. }
  83. $mall_id = SettingService::getMasterMall();
  84. return self::decrypt($res, $mall_id);
  85. }
  86. /**
  87. * 解密
  88. * @param $res
  89. * @param $mall_id
  90. * @return array|int|string
  91. * @throws \Exception
  92. */
  93. public static function decrypt($res, $mall_id)
  94. {
  95. $mall_id = SettingService::getMasterMall();
  96. RsaHelper::setPublicKey($mall_id);
  97. $lock = RsaHelper::publicDecrypt($res['lock']);
  98. return AesHelper::decrypt($res['content'], $lock);
  99. }
  100. }