WxTplMsg = new WxTplMsg($config['app_id'], $config['app_secret']); } /** * @param $data * @return bool * @throws \app\common\exception\BaseException * @throws \think\exception\DbException */ public function send($data) { // 用户id集 $userIdsArr = !strstr($data['user_id'], self::SEPARATOR) ? [$data['user_id']] : explode(self::SEPARATOR, $data['user_id']); // 批量发送 foreach ($userIdsArr as $userId) { $this->sendTemplateMessage($userId, $data); } return true; } /** * 发送模板消息 * @param $userId * @param $data * @return bool * @throws \app\common\exception\BaseException * @throws \think\exception\DbException */ private function sendTemplateMessage($userId, $data) { // 获取formid if (!$formId = FormIdService::getAvailableFormId($userId)) { $this->recordState("用户[ID:$userId] 无可用formid,无法发送模板消息!"); return false; } // 获取用户信息 $user = UserModel::detail($data['user_id']); // 构建模板消息参数 $params = [ 'touser' => $user['open_id'], 'template_id' => $data['template_id'], 'page' => $data['page'], 'form_id' => $formId['form_id'], 'data' => [] ]; // 格式化模板内容 foreach (array_filter($data['content']) as $key => $item) { $params['data']['keyword' . ($key + 1)] = $item; } // 请求微信api:发送模板消息 if ($status = $this->WxTplMsg->sendTemplateMessage($params)) { $this->recordState("用户[ID:$userId] 发送成功!"); } // 标记formid已使用 FormIdService::setIsUsed($formId['id']); return $status; } /** * 获取状态集 * @return array */ public function getStateSet() { return $this->stateSet; } /** * 记录状态集 * @param $content */ private function recordState($content) { $this->stateSet[] = $content; } }