123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\server;
-
-
- use app\common\enum\NoticeEnum;
- use app\common\logic\MessageNoticeLogic;
- use app\common\model\Client_;
- use app\common\model\NoticeSetting;
- use app\common\model\user\UserAuth;
- use EasyWeChat\Factory;
-
- class WxMessageServer
- {
- protected $config = null;
- protected $app = null;
- protected $openid = null;
- protected $template_id = null;
- protected $platform = null;
- protected $notice = null;
-
-
- public function __construct($user_id, $platform)
- {
-
- $this->platform = $platform;
- $where = ['user_id' => (int)$user_id, 'client' => $platform];
- $user_model = UserAuth::where($where)->find();
- $this->openid = $user_model['openid'];
-
- if ($this->platform === Client_::oa) {
- $this->config = WeChatServer::getOaConfig();
- $this->app = Factory::officialAccount($this->config);
- } else if ($this->platform === Client_::mnp) {
- $this->config = WeChatServer::getMnpConfig();
- $this->app = Factory::miniProgram($this->config);
- }
- }
-
-
- public function send($params)
- {
- if (empty($this->openid)) {
- return false;
- }
-
-
- $scene = NoticeSetting::where(['scene' => $params['scene']])->find()->toArray();
- if ($this->platform == Client_::oa) {
- $scene_model = $scene['oa_notice'];
- $send_type = NoticeEnum::OA_NOTICE;
- } else {
- $scene_model = $scene['mnp_notice'];
- $send_type = NoticeEnum::MNP_NOTICE;
- }
-
- if (!$scene_model || $scene_model['status'] == 0 || $scene_model['template_id'] == '') {
- return false;
- } else {
- $this->template_id = $scene_model['template_id'];
- }
-
- if ($this->platform == Client_::oa) {
- $template = $this->oaTemplate($params, $scene_model);
- } else {
- $template = $this->mnpTemplate($params, $scene_model);
- }
-
- $this->notice = MessageNoticeLogic::addNoticeLog($params, $scene_model, $send_type, json_encode($template, true));
-
-
- try {
- if ($this->platform === Client_::oa) {
- $res = $this->app->template_message->send($template);
- } else if ($this->platform === Client_::mnp) {
- $res = $this->app->subscribe_message->send($template);
- }
- MessageNoticeLogic::updateNotice($this->notice['id'], json_encode($res, true));
- return true;
-
- } catch (\Exception $e) {
- MessageNoticeLogic::updateNotice($this->notice['id'], $e->getMessage());
- return false;
- }
- }
-
-
-
- public function oaTemplate($params, $scene_model)
- {
- $domain = request()->domain();
- $tpl = [
- 'touser' => $this->openid,
- 'template_id' => $this->template_id,
- 'url' => $domain.$params['url'],
- 'data' => [
- 'first' => $scene_model['first'],
- 'remark' => $scene_model['remark']
- ]
- ];
- return $this->tplformat($scene_model, $params, $tpl);
- }
-
-
- public function mnpTemplate($params, $scene_model)
- {
- $tpl = [
- 'touser' => $this->openid,
- 'template_id' => $this->template_id,
- 'page' => $params['page']
- ];
- return $this->tplformat($scene_model, $params, $tpl);
- }
-
-
-
- public function tplformat($scene_model, $params, $tpl)
- {
- foreach ($scene_model['tpl'] as $item) {
- foreach ($params['params'] as $k => $v) {
- $search_replace = '{'.$k.'}';
- $item['tpl_content'] = str_replace($search_replace, $v, $item['tpl_content']);
- }
- $tpl['data'][$item['tpl_keyword']] = $item['tpl_content'];
- }
- return $tpl;
- }
-
- }
|