platform = $platform; if($platform == NoticeEnum::OA) { $terminal = UserTerminalEnum::WECHAT_OA; $this->config = [ 'app_id' => ConfigService::get('official_account','app_id'), 'secret' => ConfigService::get('official_account','app_secret') ]; $this->app = Factory::officialAccount($this->config); } if($platform == NoticeEnum::MNP) { $terminal = UserTerminalEnum::WECHAT_MMP; $this->config = [ 'app_id' => ConfigService::get('mini_program','app_id'), 'secret' => ConfigService::get('mini_program','app_secret') ]; $this->app = Factory::miniProgram($this->config); } $userAuth = UserAuth::where([ 'user_id' => $userId, 'terminal' => $terminal ?? -999 ])->findOrEmpty()->toArray(); $this->openid = $userAuth['openid'] ?? ''; } /** * @notes 发送消息 * @param $params * @return bool * @throws \GuzzleHttp\Exception\GuzzleException * @author Tab * @date 2021/8/20 16:42 */ public function send($params) { try { if(empty($this->openid)) { throw new \Exception('openid不存在'); } $noticeSetting = NoticeSetting::where('scene_id', $params['scene_id'])->findOrEmpty()->toArray(); if ($this->platform == NoticeEnum::OA) { $sceneConfig = $noticeSetting['oa_notice']; $sendType = NoticeEnum::OA; } else { $sceneConfig = $noticeSetting['mnp_notice']; $sendType = NoticeEnum::MNP; } if ($sceneConfig['template_id'] == '') { throw new \Exception('模板ID不存在'); } else { $this->templateId = $sceneConfig['template_id']; } if ($this->platform == NoticeEnum::OA) { $template = $this->oaTemplate($params, $sceneConfig); } else { $template = $this->mnpTemplate($params, $sceneConfig); } // 添加通知记录 $this->notice = NoticeLogic::addNotice($params, $noticeSetting, $sendType, json_encode($template, JSON_UNESCAPED_UNICODE)); if ($this->platform == NoticeEnum::OA) { $res = $this->app->template_message->send($template); } else if ($this->platform == NoticeEnum::MNP) { $res = $this->app->subscribe_message->send($template); } if(isset($res['errcode']) && $res['errcode'] != 0) { // 发送失败 throw new \Exception(json_encode($res, JSON_UNESCAPED_UNICODE)); } // 发送成功,记录消息结果 Notice::where('id', $this->notice->id)->update(['extra' => json_encode($res, JSON_UNESCAPED_UNICODE)]); return true; } catch (\Exception $e) { // 记录消息错误信息 if (! empty($this->notice->id)) { Notice::where('id', $this->notice->id)->update(['extra' => $e->getMessage()]); } Log::write("微信消息发送失败:{$e->__toString()}"); return true; } } /** * @notes 小程序消息模板 * @param $params * @param $sceneConfig * @return mixed * @author Tab * @date 2021/8/20 15:05 */ public function mnpTemplate($params, $sceneConfig) { $tpl = [ 'touser' => $this->openid, 'template_id' => $this->templateId, 'page' => $params['page'] ]; return $this->tplformat($sceneConfig, $params, $tpl); } /** * @notes 公众号消息模板 * @param $params * @param $sceneConfig * @return array * @author Tab * @date 2021/8/20 16:46 */ public function oaTemplate($params, $sceneConfig) { $domain = request()->domain(); $tpl = [ 'touser' => $this->openid, 'template_id' => $this->templateId, 'url' => $domain.$params['url'], 'data' => [ 'first' => $sceneConfig['first'], 'remark' => $sceneConfig['remark'] ] ]; return $this->tplformat($sceneConfig, $params, $tpl); } /** * @notes 提取并填充微信平台变量 * @param $sceneConfig * @param $params * @param $tpl * @return array * @author Tab * @date 2021/8/20 15:33 */ public function tplformat($sceneConfig, $params, $tpl) { foreach($sceneConfig['tpl'] as $item) { foreach($params['params'] as $k => $v) { $search = '{' . $k . '}'; $item['tpl_content'] = str_replace($search, $v, $item['tpl_content']); } $tpl['data'][$item['tpl_keyword']] = $item['tpl_content']; } return $tpl; } }