| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace addons\QiShangTong\common\service;
- use addons\QiShangTong\common\helper\RequestHelper;
- use addons\QiShangTong\common\models\QiShangTongEvent;
- 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', 'post', $params['keyword']];
- }
- $ret = QiShangTongEvent::listPage([
- 'where' => $where,
- 'limit' => $params['limit'] ?? 10,
- 'order' => 'id desc'
- ]);
- if (!empty(QiShangTongEvent::getStaticError())) return QiShangTongEvent::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 QiShangTongEvent();
- $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['secret_key'])) {
- throw new \Exception("还未配置密钥");
- }
- if (RequestHelper::sign($data, $config['secret_key']) != $data['sign']) {
- throw new \Exception("签名错误");
- }
- $model->content = $data;
- switch($model->type) {
- case 'goods':
- (new GoodsService())->eventUpdate($model);
- break;
- case 'ready':
- // (new OrderService())->eventUpdate($model, $model->type);
- // break;
- case 'delivery':
- (new OrderService())->eventUpdate($model, $model->type);
- break;
- case 'refund':
- (new RefundService())->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' => $message,
- ];
- }
- public function compensate(int $mall_id, int $id)
- {
- try {
- $notice = QiShangTongEvent::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['msg']);
- $notice->err = [];
- $notice->save();
- return true;
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- }
- }
|