最新服务器上的版本,以后用这个
zhangmeng
2023-04-19 e3f5aa12f58d986098a9de0f5cb38060e403036d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
 * Created by PhpStorm.
 * User: 风哀伤
 * Date: 2019/2/20
 * Time: 14:11
 * @copyright: ©2019 浙江禾匠信息科技
 * @link: http://www.zjhejiang.com
 */
 
namespace app\jobs;
 
 
use app\core\response\ApiCode;
use app\forms\common\platform\PlatformConfig;
use app\forms\common\template\TemplateSender;
use app\forms\common\template\tplmsg\BaseTemplate;
use app\models\Formid;
use app\models\Mall;
use app\models\TemplateRecord;
use app\models\User;
use app\models\UserInfo;
use yii\base\BaseObject;
use yii\queue\JobInterface;
 
class TemplateSendJob extends BaseJob implements JobInterface
{
    public $user;
    public $templateTpl;
    public $templateId;
    public $page;
    public $data;
    public $token;
    public $titleStyle;
    public $dataKey;
    public $tplClass;
 
    /* @var TemplateSender */
    public $sender;
    private $platform;
 
    /**
     * @param \yii\queue\Queue $queue
     * @throws \Exception
     */
    public function execute($queue)
    {
        try {
            \Yii::warning('---订阅消息发送提醒---');
            $this->setRequest();
            $userList = $this->getUserList();
            $mall = Mall::findOne(['id' => $this->user->mall_id]);
            \Yii::$app->setMall($mall);
            $userListId = [];
            foreach ($userList as $item) {
                $userListId[] = $item->id;
            }
            $formList = $this->getFormList($userListId);
 
            $formIdList = [];
            $templateRecord = [];
            foreach ($userList as $item) {
                $platform = PlatformConfig::getInstance()->getPlatform($item);
                $platformList = explode('_', $platform);
                foreach ($platformList as $value) {
                    $this->platform = $value;
                    $templateRecord[] = $this->send($item, $formList, $formIdList);
                }
            }
            if (!empty($formIdList)) {
                // 模板消息发送成功相应的form_id剩余发放次数减1
                Formid::updateAll(['remains' => '`remains`-1'], ['id' => $formIdList]);
            }
            if (count($templateRecord) > 0) {
                // 批量存储发送模板消息数据
                \Yii::$app->db->createCommand()->batchInsert(
                    TemplateRecord::tableName(),
                    ['user_id', 'mall_id', 'status', 'data', 'error', 'created_at', 'token'],
                    $templateRecord
                )->execute();
            }
        } catch (\Exception $e) {
            \Yii::warning($e);
            throw $e;
        }
    }
 
    /**
     * @return mixed
     * @throws \Exception
     */
    private function sender()
    {
        $plugin = \Yii::$app->plugin->getPlugin($this->platform);
        return $plugin->templateSender();
    }
 
    private function getUserList()
    {
        /* @var User[] $userList */
        if (isset($this->user)) {
            if (is_array($this->user)) {
                // 群发模板消息
                $userList = $this->user;
            } else {
                // 单发模板消息
                $userList = [$this->user];
            }
        } else {
            throw new \Exception('缺少参数user');
        }
        return $userList;
    }
 
    private function getFormList($userListId)
    {
        // 获取对应用户可用的form_id
        $form = Formid::find()->where(['user_id' => $userListId])
            ->andWhere(['>=', 'created_at', date('Y-m-d H:i:s', time() - 7 * 86400)])
            ->andWhere(['>', 'remains', 0])->orderBy(['created_at' => SORT_ASC])->groupBy('user_id')->all();
 
        $formList = [];
        /* @var Formid[] $form */
        foreach ($form as $item) {
            $formList[$item->user_id] = $item;
        }
        return $formList;
    }
 
    /**
     * @param User $item
     * @param $formList
     * @param $formIdList
     * @return array
     */
    private function send($item, $formList, &$formIdList)
    {
        $data = $this->data;
        if ($this->tplClass && $this->tplClass instanceof BaseTemplate) {
            $data = $this->tplClass->adaptive($this->platform);
        }
        // 发送模板消息需要的数据
        $arg = [
            'user' => $item,
            'templateId' => $this->templateId,
            'templateTpl' => $this->templateTpl,
            'page' => $this->page,
            'data' => $data,
            'dataKey' => $this->dataKey,
            'formId' => '',
            'titleStyle' => $this->titleStyle == 2 ? 'keyword1.DATA' : '',
        ];
 
        // 存储发送模板消息的数据
        $newItem = [
            'user_id' => $item->id,
            'mall_id' => $item->mall_id,
            'status' => 1,
            'data' => \Yii::$app->serializer->encode($arg),
            'error' => '',
            'created_at' => mysql_timestamp(),
            'token' => $this->token
        ];
        try {
            $this->sender = $this->sender();
            if ($this->sender->is_need_form_id) {
                if (!isset($formList[$item->id]) || !$formList[$item->id]) {
                    throw new \Exception('没有有效的form_id');
                }
                $form = $formList[$item->id];
                $arg['formId'] = $form->form_id;
                $formIdList[] = $form->id;
            }
            $this->sender->sendTemplate($arg);
        } catch (\Exception $e) {
            $newItem['error'] = $e->getMessage();
            $newItem['status'] = 0;
        }
        return $newItem;
    }
}