HuifupayPayRequestLog.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace addons\HuifuPay\common\models;
  3. use common\enums\StatusEnum;
  4. use RuntimeException;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_huifupay_pay_request_log".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property string $url Url
  12. * @property string $path 请求路径
  13. * @property string $method 请求方式
  14. * @property string|null $request_data 请求数据
  15. * @property string|null $response_data 响应数据
  16. * @property string|null $headers 请求头
  17. * @property int $http_state_code http状态码
  18. * @property string $err_msg 错误信息
  19. * @property int $status 状态[-1:删除;0:禁用;1启用]
  20. * @property int $created_at
  21. * @property int $updated_at
  22. */
  23. class HuifupayPayRequestLog extends \common\models\base\BaseActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return 'qimall_addons_huifupay_pay_request_log';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id', 'http_state_code', 'status', 'created_at', 'updated_at'], 'integer'],
  39. [['request_data', 'headers'], 'safe'],
  40. [['response_data'], 'string'],
  41. [['path'], 'string', 'max' => 50],
  42. [['err_msg'], 'string', 'max' => 500],
  43. [['url'], 'string', 'max' => 2000],
  44. [['method'], 'string', 'max' => 7],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => 'Mall ID',
  55. 'url' => 'Url',
  56. 'path' => 'Path',
  57. 'method' => 'Method',
  58. 'request_data' => 'Request Data',
  59. 'response_data' => 'Response Data',
  60. 'headers' => 'Headers',
  61. 'http_state_code' => 'Http State Code',
  62. 'err_msg' => 'Err Msg',
  63. 'status' => 'Status',
  64. 'created_at' => 'Created At',
  65. 'updated_at' => 'Updated At',
  66. ];
  67. }
  68. public static function setData(int $mall_id, array $data)
  69. {
  70. if (empty($data['id'])) {
  71. $model = new self();
  72. $model->loadDefaultValues();
  73. $model->mall_id = $mall_id;
  74. } else {
  75. $model = self::findOne(['mall_id' => $mall_id, 'id' => $data['id'], 'status' => StatusEnum::ENABLED]);
  76. if (!$model) throw new RuntimeException('数据记录异常');
  77. }
  78. foreach ($data as $key => $val) {
  79. if ($key === 'response_data' && is_array($val)) {
  80. $val = json_encode($val, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  81. }
  82. $model->$key = $val;
  83. }
  84. if (!$model->save()) {
  85. self::$static_error = '保存数据失败:' . $model->getErrorMessage();
  86. return false;
  87. }
  88. return $model;
  89. }
  90. }