微信公众号开发--使用公众号后台进行游戏激活码
<div>项目要发内测激活码</div><p>使用前年做的公众号相关开发来达成解放客户精力。程序改变世界的小小理想。</p>
<p>相关开发文档 https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html</p>
<p>基本需求 我方生成一批不重复的字符串组合-激活码.参考之前礼包码的工作方式实现后续内容。</p>
<p>发放问题使用微信公众号 自定义菜单 ->调用API 自定义消息回复按照对方微信userId返回一个对应的激活码</p>
<p>效果示例</p>
<p><img src="https://img2020.cnblogs.com/blog/2029362/202006/2029362-20200601173635447-1176836258.png"></p>
<p> </p>
<p> </p>
<h2>公众号接口调试</h2>
<p>使用开发平台接口调试工具 https://mp.weixin.qq.com/debug/</p>
<p>前置条件需要设置公众号API白名单 API相关配置 取得AppId secret token等</p>
<p><img src="https://img2020.cnblogs.com/blog/2029362/202006/2029362-20200601174428996-495911002.png"></p>
<p> </p>
<p> </p>
<p>使用获取token调试接口拿取token<img src="https://img2020.cnblogs.com/blog/2029362/202006/2029362-20200601174530806-2026153968.png"></p>
<p> </p>
<p> </p>
<p>后续部分接口需要使用token来验证身份</p>
<p> </p>
<p> </p>
<h2>自定义菜单定义部分 </h2>
<p> </p>
<p>使用自定义菜单后 公众号后台大部分交互设置会完全托管给API</p>
<p><img src="https://img2020.cnblogs.com/blog/2029362/202006/2029362-20200601174011117-1102396725.png"></p>
<p> </p>
<p> </p>
<p>如示例的菜单定义json</p>
<div class="cnblogs_Highlighter">
<pre class="brush:java;gutter:true;">{
"menu": {
"button": [
{
"type": "click",
"name": "官方Q群",
"key": "btn1",
"sub_button": [ ]
},
{
"name": "测试账号",
"sub_button": [
{
"type": "click",
"name": "steam激活码",
"key": "sub1",
"sub_button": [ ]
},
{
"type": "click",
"name": "官方激活码",
"key": "sub2",
"sub_button": [ ]
}
]
},
{
"type": "click",
"name": "礼包领取",
"key": "btn3",
"sub_button": [ ]
}
]
}
}
</pre>
</div>
<p> </p>
<p>使用create get接口修改自定义菜单 测试结果</p>
<p><img src="https://img2020.cnblogs.com/blog/2029362/202006/2029362-20200601174630615-32298792.png"></p>
<p> </p>
<p> </p>
<h2>使用API接收并恢复自定义消息</h2>
<p>需要先查阅文档 区分每一种消息的接收和回复格式</p>
<p> 按照MsgType 区分具体的消息类型 然后解读消息内容。并返回消息</p>
<div class="cnblogs_Highlighter">
<pre class="brush:php;gutter:true;">//接受普通消息
public function responseMsg()
{
// $timestamp = input('get.timestamp');
// $nonce = input('get.nonce');
$ret = '';
$postStr = Request::instance()->getContent();
$xmlArrayInput = $this->parseXml($postStr);
if (isset($xmlArrayInput)) {
Log::record('Type: ' . $xmlArrayInput['MsgType'], 'debug');
switch ($xmlArrayInput['MsgType']) {
case 'event':
$ret = $this->eventResponHandler($xmlArrayInput);
break;
case 'text':
$ret = $this->txtResponHandler($xmlArrayInput);
break;
case 'image':
$ret = $this->imgResponHandler($xmlArrayInput);
break;
case 'video':
// $ret = $this->videoResponHandler($xmlArray);
$ret = $this->txtResponBack($xmlArrayInput,'您刚发的是视频');
break;
case 'voice':
$ret = $this->voiceResponHandler($xmlArrayInput);
break;
case 'shortvideo':
// $ret = $this->shortVideoResponHandler($xmlArray);
$ret = $this->txtResponBack($xmlArrayInput, '您刚发的是小视频');
break;
case 'link':
// $ret = $this->linkResponHandler($xmlArray);
$ret = $this->txtResponBack($xmlArrayInput, '您刚发的是链接');
break;
}
} else
echo '';
echo $ret;
return true;
}
</pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">event 关注 取消关注事件回复处理</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">function</span> eventResponHandler(<span style="color: rgba(128, 0, 128, 1)">$xmlArray</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(128, 0, 128, 1)">$ret</span> = ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(128, 0, 128, 1)">$contentStr</span>='按钮应答还未实现'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(128, 0, 128, 1)">$back</span> = "<span style="color: rgba(0, 0, 0, 1)"><xml>
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)"> <ToUserName><!]></ToUserName>
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)"> <FromUserName><!]></FromUserName>
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 0, 1)"> <CreateTime>%s</CreateTime>
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)"> <MsgType><!]></MsgType>
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)"> <Content><!]></Content>
</span><span style="color: rgba(0, 128, 128, 1)">12</span> </xml>"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['Event'] == 'subscribe'<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(128, 0, 128, 1)">$contentStr</span> = "这是一个价值1个亿的AI演示公众号.有多厉害你跟我聊几句试试!\n还可以玩图片抽奖哦!不信你发几张图片试试"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">15</span>
<span style="color: rgba(0, 128, 128, 1)">16</span> } <span style="color: rgba(0, 0, 255, 1)">elseif</span> (<span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['Event'] == 'unsubscribe'<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">todo 取消订阅 为保护用户数据隐私,开发者收到用户取消关注事件时需要删除该用户的所有信息。</span>
<span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 0, 255, 1)">elseif</span> (<span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['Event'] == 'CLICK' && <span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['EventKey']=='btn1'<span style="color: rgba(0, 0, 0, 1)"> ) {
</span><span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义菜单官方Q群</span>
<span style="color: rgba(0, 128, 128, 1)">21</span> <span style="color: rgba(128, 0, 128, 1)">$path</span> = APP_PATH .'WeChatAnswer.xml'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">22</span> <span style="color: rgba(128, 0, 128, 1)">$answer</span> = readXml(<span style="color: rgba(128, 0, 128, 1)">$path</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">23</span> <span style="color: rgba(128, 0, 128, 1)">$contentStr</span> = <span style="color: rgba(128, 0, 128, 1)">$answer</span>['btn1'<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">\cache('wxuser', $xmlArray['FromUserName']);</span>
<span style="color: rgba(0, 128, 128, 1)">25</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">26</span> <span style="color: rgba(0, 0, 255, 1)">elseif</span> (<span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['Event'] == 'CLICK' && <span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['EventKey']=='sub1'<span style="color: rgba(0, 0, 0, 1)"> ) {
</span><span style="color: rgba(0, 128, 128, 1)">27</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义菜单 steam激活码</span>
<span style="color: rgba(0, 128, 128, 1)">28</span> <span style="color: rgba(128, 0, 128, 1)">$code</span> = AccountCode::getCodeByWxuser(<span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['FromUserName'],0<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(128, 0, 128, 1)">$answer</span> = readXml(APP_PATH ."/WeChatAnswer.xml"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">30</span> <span style="color: rgba(128, 0, 128, 1)">$contentStr</span> = <span style="color: rgba(0, 128, 128, 1)">sprintf</span>(<span style="color: rgba(128, 0, 128, 1)">$answer</span>['sub1'],<span style="color: rgba(128, 0, 128, 1)">$code</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">31</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">32</span> <span style="color: rgba(0, 0, 255, 1)">elseif</span> (<span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['Event'] == 'CLICK' && <span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['EventKey']=='sub2'<span style="color: rgba(0, 0, 0, 1)"> ) {
</span><span style="color: rgba(0, 128, 128, 1)">33</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 自定义菜单 官方激活码</span>
<span style="color: rgba(0, 128, 128, 1)">34</span> <span style="color: rgba(128, 0, 128, 1)">$code</span> = AccountCode::getCodeByWxuser(<span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['FromUserName'],1<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">35</span> <span style="color: rgba(128, 0, 128, 1)">$answer</span> = readXml(APP_PATH ."/WeChatAnswer.xml"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">36</span> <span style="color: rgba(128, 0, 128, 1)">$contentStr</span> = <span style="color: rgba(0, 128, 128, 1)">sprintf</span>(<span style="color: rgba(128, 0, 128, 1)">$answer</span>['sub2'],<span style="color: rgba(128, 0, 128, 1)">$code</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">37</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">38</span> <span style="color: rgba(0, 0, 255, 1)">elseif</span> (<span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['Event'] == 'CLICK' && <span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['EventKey']=='btn3'<span style="color: rgba(0, 0, 0, 1)"> ) {
</span><span style="color: rgba(0, 128, 128, 1)">39</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">自定义礼包码菜单</span>
<span style="color: rgba(0, 128, 128, 1)">40</span> <span style="color: rgba(128, 0, 128, 1)">$path</span> = APP_PATH .'WeChatAnswer.xml'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">41</span> <span style="color: rgba(128, 0, 128, 1)">$answer</span> = readXml(<span style="color: rgba(128, 0, 128, 1)">$path</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">42</span> <span style="color: rgba(128, 0, 128, 1)">$contentStr</span> = <span style="color: rgba(128, 0, 128, 1)">$answer</span>['btn3'<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 128, 128, 1)">43</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">44</span> <span style="color: rgba(0, 0, 255, 1)">else</span>
<span style="color: rgba(0, 128, 128, 1)">45</span> <span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">46</span> <span style="color: rgba(128, 0, 128, 1)">$contentStr</span> .= ' ' .<span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['EventKey'<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 128, 128, 1)">47</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">48</span>
<span style="color: rgba(0, 128, 128, 1)">49</span> <span style="color: rgba(128, 0, 128, 1)">$ret</span> = <span style="color: rgba(0, 128, 128, 1)">sprintf</span>(<span style="color: rgba(128, 0, 128, 1)">$back</span>, <span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['FromUserName'], <span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['ToUserName'], <span style="color: rgba(0, 128, 128, 1)">time</span>(), 'text', <span style="color: rgba(128, 0, 128, 1)">$contentStr</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">50</span> <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$ret</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">51</span>
<span style="color: rgba(0, 128, 128, 1)">52</span> }</pre>
</div>
<p> </p>
<p> 再演示了一个皮一下斗图的公众号</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">function</span> imgResponHandler(<span style="color: rgba(128, 0, 128, 1)">$xmlArray</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(128, 0, 128, 1)">$ret</span> = ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(128, 0, 128, 1)">$back</span> = "<span style="color: rgba(0, 0, 0, 1)"><xml>
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 0, 1)"> <ToUserName><!]></ToUserName>
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 0, 1)"> <FromUserName><!]></FromUserName>
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)"> <CreateTime>%s</CreateTime>
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 0, 1)"> <MsgType><!]></MsgType>
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 0, 1)"> <Image>
</span><span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 0, 1)"> <MediaId><!]></MediaId>
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)"> </Image>
</span><span style="color: rgba(0, 128, 128, 1)">12</span> </xml>"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(128, 0, 128, 1)">$imgId</span> = <span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['MediaId'<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">$imgId = 2223;</span>
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 128, 128, 1)">Log</span>::record('input imgId ' . <span style="color: rgba(128, 0, 128, 1)">$imgId</span>, 'debug'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">16</span> WeChatTmpZsetManger::insertImg(<span style="color: rgba(128, 0, 128, 1)">$imgId</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(128, 0, 128, 1)">$getImgid</span> = WeChatTmpZsetManger::<span style="color: rgba(0, 0, 0, 1)">getImg();
</span><span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 128, 128, 1)">Log</span>::record('output imgId ' . <span style="color: rgba(128, 0, 128, 1)">$getImgid</span>, 'debug'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(128, 0, 128, 1)">$ret</span> = <span style="color: rgba(0, 128, 128, 1)">sprintf</span>(<span style="color: rgba(128, 0, 128, 1)">$back</span>, <span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['FromUserName'], <span style="color: rgba(128, 0, 128, 1)">$xmlArray</span>['ToUserName'], <span style="color: rgba(0, 128, 128, 1)">time</span>(), 'image', <span style="color: rgba(128, 0, 128, 1)">$getImgid</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">20</span> <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$ret</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">21</span> }</pre>
</div>
<p> </p>
<p> </p>
<p>以上就基本实现了改变世界的一小步</p>
<p>还是能便捷服务于大众的东西做起来更快也更有成就感</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/xiloweiEVE/p/13026481.html
頁:
[1]