$merchant_id, $XcxMarkModel::TYPE => $type, $XcxMarkModel::STATUS => 1, ); $config = $XcxMarkModel->getFind($map); if (!empty($config) && !empty($config[$XcxMarkModel::MODEL_ID]) && !empty($config[$XcxMarkModel::FIELDS_JSON])) { try { $fields_json = json_decode($config[$XcxMarkModel::FIELDS_JSON], true); } catch (Exception $e) { return; } $keywordList = $fields_json['keyword_list']; $tempData = array(); $emphasis_keyword = ''; foreach ($keywordList as $key => $value) { $index = $key + 1; $tempData['keyword' . $index] = array( 'value' => $sendData[$value['field']] ); if ($value['emphasis_state'] == 1) { $emphasis_keyword = 'keyword' . $index . '.DATA'; } } //获取access_token $access_token = get_authorizer_access_tokens($merchant_id); //模板数据,微信要的是json数据,我这里先构建数组再转成json //文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-message/templateMessage.send.html $data = array( 'touser' => strval($openid),//要发送的用户 'template_id' => $config[$XcxMarkModel::MODEL_ID],//模板id,从模板库中获取,可通过接口获取或直接从小程序后台复制 "page" => $fields_json['page'] ? $fields_json['page'] : '',//跳转小程序的页面 'form_id' => '' . $form_id,//表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id 'data' => $tempData, 'emphasis_keyword' => $emphasis_keyword,//需要放大显示的关键词 ); // $time = date('Y-m-d H:i:s:ss'); // file_put_contents('./xcxMark.txt', $time . ' item: ' . json_encode($item) . "\r\n", FILE_APPEND); // file_put_contents('./xcxMark.txt', $time . ' config: ' . json_encode($config) . "\r\n", FILE_APPEND); // file_put_contents('./xcxMark.txt', $time . ' data: ' . json_encode($data) . "\r\n", FILE_APPEND); // file_put_contents('./xcxMark.txt', $time . ' access_token: ' . $access_token . "\r\n", FILE_APPEND); //发送模板消息 $result = $this->sendMsg(json_encode($data), $access_token); // file_put_contents('./xcxMark.txt', $time . ' result: ' . json_encode($result) . "\r\n", FILE_APPEND); } } //发送模板消息 protected function sendMsg_old($data, $access_token) { //这个方法里面的代码来源于百度复制,能否用于生产有待考究 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" . $access_token); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } curl_close($ch); return $tmpInfo; } /* * ******************************2020-1-11最新小程序模板************************************* */ public function sendMark($item) { $openid = $item['openid']; $type = $item['type']; $merchant_id = $item['merchant_id']; $sendData = $item['sendData']; $XcxMarkModel = D('XcxMark'); $map = array( $XcxMarkModel::MERCHANT_ID => $merchant_id, $XcxMarkModel::TYPE => $type, $XcxMarkModel::STATUS => 1, ); $config = $XcxMarkModel->getFind($map); if (!empty($config) && !empty($config[$XcxMarkModel::MODEL_ID]) && !empty($config[$XcxMarkModel::FIELDS_JSON])) { try { $fields_json = json_decode($config[$XcxMarkModel::FIELDS_JSON], true); } catch (Exception $e) { return; } $keywordList = $fields_json['keyword_list']; $tempData = array(); foreach ($keywordList as $key => &$value) { if (($value['field'] == 'user_address' || $value['field'] == 'store_address') && mb_strlen($sendData[$value['field']], 'UTF8') > 20) { $sendData[$value['field']] = mb_substr($sendData[$value['field']], 0, 16, 'utf-8') . '...'; } $tempData[$value['key']] = array('value' => strval($sendData[$value['field']])); } //获取access_token $access_token = get_authorizer_access_tokens($merchant_id); //模板数据,微信要的是json数据,我这里先构建数组再转成json $data = array( 'touser' => strval($openid),//要发送的用户 'template_id' => trim($config[$XcxMarkModel::MODEL_ID]),//模板id,从模板库中获取,可通过接口获取或直接从小程序后台复制 "page" => $sendData['page'] ? $sendData['page'] : '',//跳转小程序的页面 'data' => $tempData, ); //发送模板消息 $result = $this->sendMsg(json_encode($data), $access_token); return $result; } } //发送模板消息 protected function sendMsg($data, $access_token) { //这个方法里面的代码来源于百度复制,能否用于生产有待考究 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $access_token); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return curl_error($ch); } curl_close($ch); return $tmpInfo; } }