| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace addons\QiDingDong\common\service;
- use addons\QiDingDong\common\helper\RequestHelper;
- use addons\QiDingDong\common\models\QiDingDongEvent;
- use common\enums\StatusEnum;
- use common\models\mall\Mall;
- use yii\helpers\Json;
- class NoticeService
- {
- /**
- * 通知记录
- * @param int $mall_id
- * @param array $params
- * @return array|false|mixed|string|null
- */
- public function page(int $mall_id, array $params)
- {
- $where = [
- ['mall_id' => $mall_id],
- ['status' => StatusEnum::ENABLED]
- ];
- if (!empty($params['type'])) {
- $where[] = ['type' => $params['type']];
- }
- if (!empty($params['keyword'])) {
- $where[] = ['like', 'content', $params['keyword']];
- }
- $ret = QiDingDongEvent::listPage([
- 'where' => $where,
- 'limit' => $params['limit'] ?? 10,
- 'order' => 'id desc'
- ]);
- if (!empty(QiDingDongEvent::getStaticError())) return QiDingDongEvent::getStaticError();
- if (!empty($ret['list'])) {
- foreach ($ret['list'] as &$item) {
- if (empty($item['err'])) $item['err'] = '[]';
- $item['err'] = Json::decode($item['err']);
- $item['err_message'] = $item['err']['msg'] ?? '无错误信息';
- }
- }
- return $ret;
- }
- public function exec(string $type, array $data, $is_compensate = false)
- {
- $model = new QiDingDongEvent();
- $status = 200;
- $message = 'OK';
- $model->err = [];
- $model->post = $data;
- $model->type = $type;
- try {
- $model->mall_id = SettingService::getMasterMall();
- if (empty($model->mall_id)) throw new \Exception("还未设置主商城");
- $config = (new SettingService())->get($model->mall_id);
- if (empty($config) || $config['app_key'] != $data['app_key']) {
- throw new \Exception("还未设置配置信息");
- }
- $model->content = RequestHelper::decrypt($data, $model->mall_id);
- switch($model->type) {
- case 'goods':
- (new GoodsService())->eventUpdate($model);
- break;
- case 'order':
- (new OrderService())->eventUpdate($model);
- break;
- }
- } catch (\Exception $e) {
- $status = 500;
- $message = $e->getMessage();
- $model->err = [
- 'code' => $e->getCode(),
- 'msg' => $e->getMessage(),
- 'file' => $e->getFile(),
- 'line' => $e->getLine(),
- ];
- }
- if (empty($model->content)) $model->content = [];
- if (!$is_compensate && !$model->save()) {
- \Yii::error($model->getErrorMessage() . " - " . __METHOD__);
- }
- // 记录数据
- return [
- 'status' => $status,
- 'msg' => 'OK',
- 'content' => [
- 'msg' => $message
- ]
- ];
- }
- public function compensate(int $mall_id, int $id)
- {
- try {
- $notice = QiDingDongEvent::findOne(['mall_id' => $mall_id, 'id' => $id]);
- if (empty($notice)) throw new \Exception("记录不存在");
- // 重新执行
- $ret = $this->exec($notice->type, $notice->post, true);
- if ($ret['status'] != 200) throw new \Exception($ret['content']['msg']);
- $notice->err = [];
- $notice->save();
- return true;
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- }
|