NoticeService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace addons\QiShangTong\common\service;
  3. use addons\QiShangTong\common\helper\RequestHelper;
  4. use addons\QiShangTong\common\models\QiShangTongEvent;
  5. use common\enums\StatusEnum;
  6. use common\models\mall\Mall;
  7. use yii\helpers\Json;
  8. class NoticeService
  9. {
  10. /**
  11. * 通知记录
  12. * @param int $mall_id
  13. * @param array $params
  14. * @return array|false|mixed|string|null
  15. */
  16. public function page(int $mall_id, array $params)
  17. {
  18. $where = [
  19. ['mall_id' => $mall_id],
  20. ['status' => StatusEnum::ENABLED]
  21. ];
  22. if (!empty($params['type'])) {
  23. $where[] = ['type' => $params['type']];
  24. }
  25. if (!empty($params['keyword'])) {
  26. $where[] = ['like', 'post', $params['keyword']];
  27. }
  28. $ret = QiShangTongEvent::listPage([
  29. 'where' => $where,
  30. 'limit' => $params['limit'] ?? 10,
  31. 'order' => 'id desc'
  32. ]);
  33. if (!empty(QiShangTongEvent::getStaticError())) return QiShangTongEvent::getStaticError();
  34. if (!empty($ret['list'])) {
  35. foreach ($ret['list'] as &$item) {
  36. if (empty($item['err'])) $item['err'] = '[]';
  37. $item['err'] = Json::decode($item['err']);
  38. $item['err_message'] = $item['err']['msg'] ?? '无错误信息';
  39. }
  40. }
  41. return $ret;
  42. }
  43. public function exec(string $type, array $data, $is_compensate = false)
  44. {
  45. $model = new QiShangTongEvent();
  46. $status = 200;
  47. $message = 'ok';
  48. $model->err = [];
  49. $model->post = $data;
  50. $model->type = $type;
  51. try {
  52. $model->mall_id = SettingService::getMasterMall();
  53. if (empty($model->mall_id)) throw new \Exception("还未设置主商城");
  54. $config = (new SettingService())->get($model->mall_id);
  55. if (empty($config['secret_key'])) {
  56. throw new \Exception("还未配置密钥");
  57. }
  58. if (RequestHelper::sign($data, $config['secret_key']) != $data['sign']) {
  59. throw new \Exception("签名错误");
  60. }
  61. $model->content = $data;
  62. switch($model->type) {
  63. case 'goods':
  64. (new GoodsService())->eventUpdate($model);
  65. break;
  66. case 'ready':
  67. // (new OrderService())->eventUpdate($model, $model->type);
  68. // break;
  69. case 'delivery':
  70. (new OrderService())->eventUpdate($model, $model->type);
  71. break;
  72. case 'refund':
  73. (new RefundService())->eventUpdate($model);
  74. break;
  75. }
  76. } catch (\Exception $e) {
  77. $status = 500;
  78. $message = $e->getMessage();
  79. $model->err = [
  80. 'code' => $e->getCode(),
  81. 'msg' => $e->getMessage(),
  82. 'file' => $e->getFile(),
  83. 'line' => $e->getLine(),
  84. ];
  85. }
  86. if (empty($model->content)) $model->content = [];
  87. if (!$is_compensate && !$model->save()) {
  88. \Yii::error($model->getErrorMessage() . " - " . __METHOD__);
  89. }
  90. // 记录数据
  91. return [
  92. 'status' => $status,
  93. 'msg' => $message,
  94. ];
  95. }
  96. public function compensate(int $mall_id, int $id)
  97. {
  98. try {
  99. $notice = QiShangTongEvent::findOne(['mall_id' => $mall_id, 'id' => $id]);
  100. if (empty($notice)) throw new \Exception("记录不存在");
  101. // 重新执行
  102. $ret = $this->exec($notice->type, $notice->post, true);
  103. if ($ret['status'] != 200) throw new \Exception($ret['msg']);
  104. $notice->err = [];
  105. $notice->save();
  106. return true;
  107. } catch (\Exception $e) {
  108. return $e->getMessage();
  109. }
  110. }
  111. }