NoticeService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace addons\QiDingDong\common\service;
  3. use addons\QiDingDong\common\helper\RequestHelper;
  4. use addons\QiDingDong\common\models\QiDingDongEvent;
  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', 'content', $params['keyword']];
  27. }
  28. $ret = QiDingDongEvent::listPage([
  29. 'where' => $where,
  30. 'limit' => $params['limit'] ?? 10,
  31. 'order' => 'id desc'
  32. ]);
  33. if (!empty(QiDingDongEvent::getStaticError())) return QiDingDongEvent::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 QiDingDongEvent();
  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) || $config['app_key'] != $data['app_key']) {
  56. throw new \Exception("还未设置配置信息");
  57. }
  58. $model->content = RequestHelper::decrypt($data, $model->mall_id);
  59. switch($model->type) {
  60. case 'goods':
  61. (new GoodsService())->eventUpdate($model);
  62. break;
  63. case 'order':
  64. (new OrderService())->eventUpdate($model);
  65. break;
  66. }
  67. } catch (\Exception $e) {
  68. $status = 500;
  69. $message = $e->getMessage();
  70. $model->err = [
  71. 'code' => $e->getCode(),
  72. 'msg' => $e->getMessage(),
  73. 'file' => $e->getFile(),
  74. 'line' => $e->getLine(),
  75. ];
  76. }
  77. if (empty($model->content)) $model->content = [];
  78. if (!$is_compensate && !$model->save()) {
  79. \Yii::error($model->getErrorMessage() . " - " . __METHOD__);
  80. }
  81. // 记录数据
  82. return [
  83. 'status' => $status,
  84. 'msg' => 'OK',
  85. 'content' => [
  86. 'msg' => $message
  87. ]
  88. ];
  89. }
  90. public function compensate(int $mall_id, int $id)
  91. {
  92. try {
  93. $notice = QiDingDongEvent::findOne(['mall_id' => $mall_id, 'id' => $id]);
  94. if (empty($notice)) throw new \Exception("记录不存在");
  95. // 重新执行
  96. $ret = $this->exec($notice->type, $notice->post, true);
  97. if ($ret['status'] != 200) throw new \Exception($ret['content']['msg']);
  98. $notice->err = [];
  99. $notice->save();
  100. return true;
  101. } catch (\Exception $e) {
  102. return $e->getMessage();
  103. }
  104. }
  105. }