'wx.tplAudit', 'transfer' => 'wx.tplTransfer', ]; /** * 发送订阅消息 * * @param int $userId * @param string $scene audit | transfer * @param array $data ['thing1' => '...', 'phrase2' => '...'] * @param string $page 点击消息跳转的小程序页面 * @return bool */ public static function send($userId, $scene, array $data, $page = 'pages/index/index') { if (!isset(self::SCENES[$scene]) || !WxMini::isConfigured()) { return false; } $tpl = trim((string)WxMini::config(self::SCENES[$scene], '')); if ($tpl === '') { return false; } $ext = Db::name('fc_user_ext')->where('user_id', (int)$userId)->find(); $openid = $ext ? trim((string)$ext['openid']) : ''; // 派生 openid(本地联调/h5)不是真实微信用户,发不出去 if ($openid === '' || strpos($openid, 'openid_') === 0 || strpos($openid, 'h5_') === 0) { return false; } try { $res = WxMini::app()->subscribe_message->send([ 'touser' => $openid, 'template_id' => $tpl, 'page' => $page, 'data' => $data, ]); } catch (\Throwable $e) { Log::record('订阅消息发送异常:' . $e->getMessage(), 'error'); return false; } $arr = $res instanceof \EasyWeChat\Kernel\Support\Collection ? $res->all() : (array)$res; if (isset($arr['errcode']) && (int)$arr['errcode'] !== 0) { Log::record('订阅消息发送失败:' . json_encode($arr, JSON_UNESCAPED_UNICODE), 'error'); return false; } return true; } }