摘要:背景最近公司项目一直在围绕着支付宝做应用开发,为了能保证消息能够及时的给用户传递,因此需要开发模板消息的功能,而小程序的模板消息也是最快捷的通知方式事先准备请仔细阅读支付宝模板消息发送指引模板消息指引仔细阅读用户的授权文档,用户授权的详细的
背景
最近公司项目一直在围绕着支付宝做应用开发,为了能保证消息能够及时的给用户传递,因此需要开发模板消息的功能,而小程序的模板消息也是最快捷的通知方式
事先准备1、请仔细阅读支付宝模板消息发送指引:模板消息指引
2、仔细阅读用户的授权文档,用户授权的详细的实现步骤可以见我写的另外一篇文章:《PHP实现支付宝小程序用户授权的工具类》
3、在小程序中加入模板消息的权限,如下图
4、仔细阅读支付宝发送模板消息接口文档:alipay.open.app.mini.templatemessage.send
5、事先通过小程序的form组件收集好对一个的formid,formid组件文档
6、将小程序绑定到生活号上
7、配置一个模板消息编号,详细步骤:小程序--->模板消息,最终配置号的模板消息如下
1、通过客户端的form组件,收集好formid,并多带带开一个后端接口将formid通过http请求保存到后台,最好是尽可能多的收集formid,比如按钮的点击事件、tab的切换上都可以增加formid组件
2、通过调用alipay.open.app.mini.templatemessage.send接口,给客户端发送模板消息 ,注意支付宝所有的模板消息都是基于生活号进行分发的,所以事先一定要绑定好对应的生活号
//模板消息的接口方法名称 const API_METHOD_SEND_TPL_MSG = "alipay.open.app.mini.templatemessage.send"; //模板消息的结果返回节点名称 const RESPONSE_OUTER_NODE_SEND_TPL_MSG = "alipay_open_app_mini_templatemessage_send_response";入口方法
/** * 发送小程序模板消息 * @param $formId * @param $to 发送给用户的编号 * @param $tplId 模板编号 * @param $tplContent 模板内容 * @param $page 要跳转的页面 * @return array */ public static function sendAmpTplMsg($formId,$to,$tplId,$tplContent,$page = ""){ $param = self::getTplMsgBaseParam($formId,$to,$tplId,$tplContent,$page = ""); $url = self::buildRequestUrl($param); $response = self::getResponse($url,self::RESPONSE_OUTER_NODE_SEND_TPL_MSG); return $response; }获取基础参数方法
/** * 获取发送模板消息的接口参数 */ protected static function getTplMsgBaseParam($formId,$to,$tplId,$tplContent,$page = ""){ $baseParam = [ "to_user_id" => $to, "form_id" => $formId, "user_template_id" => $tplId, "page" => $page, "data" => $tplContent, ]; $bizContent = json_encode($baseParam,JSON_UNESCAPED_UNICODE); $busiParam = [ "biz_content" => $bizContent ]; $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_SEND_TPL_MSG); return $param; } /** * 构建业务参数 */ protected static function buildApiBuisinessParam($businessParam,$apiMethod){ $pubParam = self::getApiPubParam($apiMethod); $businessParam = array_merge($pubParam,$businessParam); $signContent = self::getSignContent($businessParam); error_log("sign_content ===========>".$signContent); $rsaHelper = new RsaHelper(); $sign = $rsaHelper->createSign($signContent); error_log("sign ===========>".$sign); $businessParam["sign"] = $sign; return $businessParam; } /** * 公共参数 * */ protected static function getApiPubParam($apiMethod){ $ampBaseInfo = BusinessHelper::getAmpBaseInfo(); $param = [ "timestamp" => date("Y-m-d H:i:s") , "method" => $apiMethod, "app_id" => formatArrValue($ampBaseInfo,"appid",config("param.amp.appid")), "sign_type" =>self::SIGN_TYPE_RSA2, "charset" =>self::FILE_CHARSET_UTF8, "version" =>self::VERSION, ]; return $param; } /** * 获取签名的内容 */ protected static function getSignContent($params) { ksort($params); $stringToBeSigned = ""; $i = 0; foreach ($params as $k => $v) { if (!empty($v) && "@" != substr($v, 0, 1)) { if ($i == 0) { $stringToBeSigned .= "$k" . "=" . "$v"; } else { $stringToBeSigned .= "&" . "$k" . "=" . "$v"; } $i++; } } unset ($k, $v); return $stringToBeSigned; } /** * 构建请求链接 */ protected static function buildRequestUrl($param){ $paramStr = http_build_query($param); return self::API_DOMAIN . $paramStr; }获取返回的结果值
/** * 获取返回的数据,对返回的结果做进一步的封装和解析 */ protected static function getResponse($url,$responseNode){ $json = curlRequest($url); error_log("result is =========>".$json); $response = json_decode($json,true); $responseContent = formatArrValue($response,$responseNode,[]); $errResponse = formatArrValue($response,self::RESPONSE_OUTER_NODE_ERROR_RESPONSE,[]); if($errResponse){ return $errResponse; } return $responseContent; }返回结果
如果返回的节点code为10000,则表示消息发送
{ "alipay_open_app_mini_templatemessage_send_response":{ "code":"10000", "msg":"Success" } ,"sign":"ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE" }调用
$formId = "MjA4ODMwMjI2MjE4Mzc4MF8xNTQ2ODQ0MTUyNzU0XzA1NQ=="; $openId = "2088302262183780"; $tplId = "Mzc4OTk2ODU1YzM4NTI3NmY5ZjI2OTdhNGNkZDE2NGQ="; $content = [ "keyword1" => [ "value" => "您的朋友【】偷去了你的能量啦~", ], "keyword2" => [ "value" => "朋友偷能量提醒", ], "keyword3" => [ "value" => "点我查看详情", ], ]; $result = AmpHelper::sendAmpTplMsg($formId,$openId,$tplId,$content,$page= "pages/index/index");效果图 附录:完整的工具类
".$json); $response = json_decode($json,true); $responseContent = formatArrValue($response,$responseNode,[]); $errResponse = formatArrValue($response,self::RESPONSE_OUTER_NODE_ERROR_RESPONSE,[]); if($errResponse){ return $errResponse; } return $responseContent; } /** * 开始所有的业务并获取相应的结果 */ protected static function doBusiness($busiParam,$reponseNode){ $url = self::buildRequestUrl($busiParam); $response = self::getResponse($url,$reponseNode); return $response; } /** * 获取请求的链接 */ protected static function buildQrRequestUrl($mpPage = "pages/index",$queryParam = []){ $paramStr = http_build_query(self::getQrBaseParam($mpPage,$queryParam)); return self::API_DOMAIN . $paramStr; } /** * 构建请求链接 */ protected static function buildRequestUrl($param){ $paramStr = http_build_query($param); return self::API_DOMAIN . $paramStr; } /** * 获取用户的基础信息接口 */ protected static function getAmpUserBaseParam($token){ $busiParam = [ "auth_token" => $token, ]; $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_GET_USER_INFO); return $param; } /** *获取二维码的基础参数 */ protected static function getQrcodeBaseParam($page= "pages/index/index",$queryParam = [],$describe = ""){ $busiParam = [ "biz_content" => self::getQrBizContent($page,$queryParam,$describe) ]; $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_GENERATE_QR); return $param; } /** *获取授权的基础参数 */ protected static function getAuthBaseParam($code,$refreshToken = ""){ $busiParam = [ "grant_type" => "authorization_code", "code" => $code, "refresh_token" => $refreshToken, ]; $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_AUTH_TOKEN); return $param; } /** * 获取发送模板消息的接口参数 */ protected static function getTplMsgBaseParam($formId,$to,$tplId,$tplContent,$page = ""){ $baseParam = [ "to_user_id" => $to, "form_id" => $formId, "user_template_id" => $tplId, "page" => $page, "data" => $tplContent, ]; $bizContent = json_encode($baseParam,JSON_UNESCAPED_UNICODE); $busiParam = [ "biz_content" => $bizContent ]; $param = self::buildApiBuisinessParam($busiParam,self::API_METHOD_SEND_TPL_MSG); return $param; } /** * 获取支付的基础参数接口 * @param $outTradeNo 第三方交易订单号 * @param $totalAmount 待支付的金额 * @param $subject 主题,可以是商品名称 * @param $body 描述,可以是商品简介 * @return array */ protected static function getTradePayBaseParam($outTradeNo,$totalAmount,$subject,$body = ""){ $baseParam = [ "out_trade_no" =>$outTradeNo, "total_amount" =>$totalAmount, "subject" =>$subject, "body" =>$body, "notify_url" => getDomain()."/alipay/notify" ]; return self::buildBusiBaseParam($baseParam, self::API_METHOD_TRADE_PAY); } /** * 构建基础的业务参数 */ protected static function buildBusiBaseParam($param, $apiMethod){ $bizContent = json_encode($param,JSON_UNESCAPED_UNICODE); $busiParam = [ "biz_content" => $bizContent ]; $param = self::buildApiBuisinessParam($busiParam,$apiMethod); return $param; } /** * 构建业务参数 */ protected static function buildApiBuisinessParam($businessParam,$apiMethod){ $pubParam = self::getApiPubParam($apiMethod); $businessParam = array_merge($pubParam,$businessParam); $signContent = self::getSignContent($businessParam); error_log("sign_content ===========>".$signContent); $rsaHelper = new RsaHelper(); $sign = $rsaHelper->createSign($signContent); error_log("sign ===========>".$sign); $businessParam["sign"] = $sign; return $businessParam; } /** * 公共参数 * */ protected static function getApiPubParam($apiMethod){ $ampBaseInfo = BusinessHelper::getAmpBaseInfo(); $param = [ "timestamp" => date("Y-m-d H:i:s") , "method" => $apiMethod, "app_id" => formatArrValue($ampBaseInfo,"appid",config("param.amp.appid")), "sign_type" =>self::SIGN_TYPE_RSA2, "charset" =>self::FILE_CHARSET_UTF8, "version" =>self::VERSION, ]; return $param; } /** * 获取签名的内容 */ protected static function getSignContent($params) { ksort($params); $stringToBeSigned = ""; $i = 0; foreach ($params as $k => $v) { if (!empty($v) && "@" != substr($v, 0, 1)) { if ($i == 0) { $stringToBeSigned .= "$k" . "=" . "$v"; } else { $stringToBeSigned .= "&" . "$k" . "=" . "$v"; } $i++; } } unset ($k, $v); return $stringToBeSigned; } protected static function convertArrToQueryParam($param){ $queryParam = []; foreach ($param as $key => $val){ $obj = $key."=".$val; array_push($queryParam,$obj); } $queryStr = implode("&",$queryParam); return $queryStr; } /** * 转换字符集编码 * @param $data * @param $targetCharset * @return string */ protected static function characet($data, $targetCharset) { if (!empty($data)) { $fileType = self::FILE_CHARSET_UTF8; if (strcasecmp($fileType, $targetCharset) != 0) { $data = mb_convert_encoding($data, $targetCharset, $fileType); } } return $data; } /** * 获取业务参数内容 */ protected static function getQrBizContent($page, $queryParam = [],$describe = ""){ if(is_array($queryParam)){ $queryParam = http_build_query($queryParam); } $obj = [ "url_param" => $page, "query_param" => $queryParam, "describe" => $describe ]; $bizContent = json_encode($obj,JSON_UNESCAPED_UNICODE); return $bizContent; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/29959.html
摘要:背景最近项目需要上线支付宝小程序,同时需要走用户的授权流程完成用户信息的存储,以前做过微信小程序的开发,本以为实现授权的过程是很简单的事情,但是再实现的过程中还是遇到了不少的坑,因此记录一下实现的过程学到的知识支付宝开放接口的调用模式以及实 背景 最近项目需要上线支付宝小程序,同时需要走用户的授权流程完成用户信息的存储,以前做过微信小程序的开发,本以为实现授权的过程是很简单的事情,但是...
摘要:在用户喜爱的众多功能中,使用率最高的是模版消息推送。模版消息推送数的量级也由早期每天几百条,变为后来的每天数百万条。平台支持少知晓云已经支持包括微信小程序和支付宝小程序在内的各大小程序平台的消息推送,对平台的支持也将在近期上线。 两年多前,为了让更多的人找到好玩、好用的小程序,我们成立了「知晓程序」。 再后来,我们推出了后端云服务平台——知晓云,帮助大家降低创业成本,提升开发效率。 「...
摘要:目前支持哪些平台的搬家目前对外开放版本释放了微信小程序转支付宝小程序的功能,这也是我们在调研中发现需求最多的。从笔者的了解来看,微信小程序框架原理更接近于,而支付宝小程序更接近于。 原文地址: https://ant-move.github.io/we... 蚂蚁搬家工具(Antmove)是一个小程序开发辅助工具,致力于解决小程序跨平台开发的难题,借助于 Antmove,你只需要编写...
阅读 3279·2021-11-23 09:51
阅读 2409·2021-11-09 09:46
阅读 1455·2019-08-30 15:54
阅读 3092·2019-08-30 14:22
阅读 2895·2019-08-29 12:40
阅读 1607·2019-08-26 10:33
阅读 1756·2019-08-23 17:09
阅读 1533·2019-08-23 16:11