大圣又归来 發表於 2022-2-27 14:25:00

unicloud开发公众号

<p>1、准备域名,申请ssl</p>
<p>2、绑定unicloud云空间-云函数-函数列表-云函数域名绑定</p>
<p>3、云函数-详情-云函数Url化-配置路径</p>
<p>4、公众号后台绑定域名</p>
<p>5、云函数代码示例:<span style="color: rgba(255, 0, 0, 1)"><strong>记得安装sha1依赖,这个很简单(官方有)</strong></span></p>
<div class="cnblogs_code">
<pre>const sha1=require('sha1'<span style="color: rgba(0, 0, 0, 1)">);

exports.main </span>= (event, context) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
   
    let {queryStringParameters} </span>=<span style="color: rgba(0, 0, 0, 1)"> event;
    let {signature,echostr,timestamp,nonce} </span>=<span style="color: rgba(0, 0, 0, 1)"> queryStringParameters;
    let token </span>= 'this is ok'<span style="color: rgba(0, 0, 0, 1)">;
   
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">字典排序</span>
    const str=.sort().join(''<span style="color: rgba(0, 0, 0, 1)">);
    const result</span>=<span style="color: rgba(0, 0, 0, 1)">sha1(str);
    let res;
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(result===<span style="color: rgba(0, 0, 0, 1)">signature){
      res </span>=<span style="color: rgba(0, 0, 0, 1)"> echostr;
    }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{
      res </span>= "fail"<span style="color: rgba(0, 0, 0, 1)">;
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回数据给客户端</span>
    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> res;
};</span></pre>
</div>
<p>6、至此接入成功。</p>
<p>7、准备开发开发 公众号和小程序的连接。此时发现,需要获取access_token才能获取用户信息包含unionid。公众号需要绑定固定ip才能得到access_token。</p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong>所以必须使用腾讯云付费版服务空间,又重新选择了腾讯云服务空间。</strong></span></p>
<p>8、不使用&nbsp;uni-cloud-router ,配置公众号服务器域名直接可以成功,使用之后 token一直失败,猜测是uni-cloud-router自己封装了返回的格式。</p>
<p>可通过验证的代码,直接在 index.js中</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> const sha1=require('sha1');</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> const xml = require('./utils/xml')</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> exports.main = async (event, context) =&gt; {</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   let {httpMethod,queryStringParameters,body} = event;</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   if (httpMethod=="POST")</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, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">         return await postFun(body);</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, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   else</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, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">         return getFun(queryStringParameters);</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, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> function getFun(params) {</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   let {signature,echostr,timestamp,nonce} = params;</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   let token = 'magichatdy';</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, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   const str=.sort().join('');</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   const result=sha1(str);</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   let res;</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   if(result===signature){</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">         res = echostr;</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   }else{</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">         res = "fail";</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, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   return res;</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, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> async function postFun (bodystr) {</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   let msg,MsgType,result;</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   let body = await xml.xmlToJson(bodystr)</span>
   
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   msg = body ? body['xml']:'';</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   if (!msg) {</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">         return 'error request.';</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, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   let {MsgType:mst,Event:evt} = msg;</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   MsgType = mst</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   Event = evt</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   if(MsgType=='text'){</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, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">         result = xml.jsonToXml({</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">               xml: {</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">                     ToUserName: msg.FromUserName,</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">                     FromUserName: msg.ToUserName,</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">                     CreateTime: Date.now(),</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">                     MsgType: msg.MsgType,</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">                     Content: msg.Content</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, 0, 1)">
//</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, 0, 1)">   }else if(MsgType=='event'){</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">         // if (Event=='subscribe')</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, 0, 1)">
//</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, 0, 1)">             result = xml.jsonToXml({</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">               xml: {</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">                     ToUserName: msg.FromUserName,</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">                     FromUserName: msg.ToUserName,</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">                     CreateTime: Date.now(),</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">                     MsgType: 'text',</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">                     Content: "感谢关注"</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, 0, 1)">
//</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, 0, 1)">         // }</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">         // if(Event=='unsubscribe')</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, 0, 1)">//</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, 0, 1)">   }else{</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, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">         result = 'success'</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, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)">   return result;</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> }</span></pre>
</div>
<p>但是项目需要使用:uni-cloud-router,所以目前的策略是,先以上边的方式验证通过,再使用&nbsp;uni-cloud-router书写其他接口代码(包含事件接受与接口书写)</p>
<p>注:xxx/wx/index/sayHello 也会进入&nbsp; xxx/wx 的 handle方法,所以可以这样解决。 不确定之后会不会出问题。</p>
<p>9、换成腾讯云以后,发现公众号那边发过来的消息需要 base64解码。。。。,阿里云就不用解码</p>
<p>let str = new Buffer(bodystr, 'base64').toString();</p>
<p>&nbsp;10、公众号微信支付:</p>
<p>  1、在您的js公共文件夹下创建wechat.js</p>
<div id="highlighter_529569" class="syntaxhighlighter">&nbsp;
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">import Vue from "vue";
var jweixin = require('jweixin-module');
export default {
    //调试模式
    debug:false,
    //权限
    jsApiList:[
      'getLocation'
    ],
    //判断是否在微信中
    isWechat: function() {
      var ua = window.navigator.userAgent.toLowerCase();
      if (ua.match(/micromessenger/i) == 'micromessenger') {
            return true;
      } else {
            return false;
      }
    },

    //初始化sdk配置
    initJssdk: function(callback, url) {
      if (!this.isWechat()) {return;}
      //服务端进行签名 ,可使用uni.request替换。 签名算法请看文档
      Vue.prototype.$func.api(Vue.prototype.$apiConfig.wxjssdk(),{
            url:url || location.href.split('#'),
      },res =&gt; {
            jweixin.config({
                debug: res.debug || this.debug,
                appId: res.appId,
                timestamp: res.timestamp,
                nonceStr: res.nonceStr,
                signature: res.signature,
                jsApiList: res.jsApiList || this.jsApiList
            });
            //配置完成后,再执行分享等功能
            if (typeof callback === 'function') {
                callback(res.data);
            }
      })
    },
   
    //准备就绪
    ready:function(callback, url)
    {
      if (!this.isWechat()) {return;}
      this.initJssdk(function(){
            jweixin.ready(function() {
                //配置完成后,再执行
                if (typeof callback === 'function') {
                  callback(jweixin);
                }
            })
      }, url)
    },
   
    closeWindow:function() {
      this.ready(function(wx) {
            wx.closeWindow();
      })
    },
   
    //在需要定位页面调用
    location: function(success, fail) {
      this.ready(function(wx) {
            wx.getLocation({
                type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
                success: function (res) {
                  if (typeof success === 'function') {
                        success(res);
                  }
                },
                fail:function(res){
                  if (typeof fail === 'function') {
                        fail(res);
                  }
                }
            });
      });
    }
}
</pre>
</div>
<p>  </p>
<p>&nbsp; &nbsp; &nbsp;2、main.js加载该文件,这样就可以全局使用wechat.js了</p>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">// #ifdef H5
import wechat from './static/js/wechat.js';
Vue.prototype.$wechat = wechat;
// #endif
</pre>
</div>
<p>  </p>
<p>&nbsp; &nbsp; &nbsp; 3、页面中使用</p>
<div class="cnblogs_Highlighter">
<pre class="brush:javascript;gutter:true;">// #ifdef H5
//获取定位经纬度
const that = this;
if (this.$wechat &amp;&amp; this.$wechat.isWechat()) {
    this.$wechat.location(function (res) {
      that.latitude = res.latitude;//纬度
      that.longitude = res.longitude; //经度
    });
}
// #endif
</pre>
</div>
<p>  </p>
<div id="highlighter_579504" class="syntaxhighlighter">
<div class="bar               show">
<div class="toolbar"><span style="color: rgba(0, 0, 255, 1)">&nbsp;11、公众号开发 <span style="font-size: 14pt"><strong>调试</strong> </span>及缓存处理策略参照:&nbsp;cnblogs.com/Mvloveyouforever/p/16217859.html</span></div>
</div>
<div class="lines">&nbsp;</div>
</div>
</div>
<p><span style="color: rgba(255, 0, 0, 1); font-size: 14pt"><strong>12、用云函数开发的时候,发现有时候是文件错误,看不出原因。这时候就用url访问接口,错误信息会明显很多了(需要把代码中try catch 去掉)。</strong>&nbsp;</span></p>
<p><span style="color: rgba(0, 0, 255, 1); font-size: 14pt">13、页面跳转传对象的正确写法,但官方有bug,第二次进入同一页面无法接收事件(官方尚未修复),所以目前用不了。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">uni.navigateTo({
            url: </span>''<span style="color: rgba(0, 0, 0, 1)">,
            success(res){
                console.log(res)
                res.eventChannel.emit(</span>'messageDetail'<span style="color: rgba(0, 0, 0, 1)">, {
                  data: {
info: </span>'uniapp操作,长按复制,请求携带cookie - 走看看uniapp操作,长按复制,请求携带cookie - 走看看uniapp操作,长按复制,请求携带cookie - 走看看uniapp操作,长按复制,请求携带cookie - 走看看'<span style="color: rgba(0, 0, 0, 1)">,
id: </span>'1'<span style="color: rgba(0, 0, 0, 1)">
}
                })
            }
      })


</span>&lt;template&gt;
    &lt;view v-<span style="color: rgba(0, 0, 255, 1)">if</span>="detail" class="message-detail"&gt;<span style="color: rgba(0, 0, 0, 1)">
      {{detail.info}}
    </span>&lt;/view&gt;
&lt;/template&gt;

&lt;script setup&gt;<span style="color: rgba(0, 0, 0, 1)">
    import {
      getCurrentInstance,
      ref
    } from </span>'vue'<span style="color: rgba(0, 0, 0, 1)">;

    const {
      proxy
    } </span>=<span style="color: rgba(0, 0, 0, 1)"> getCurrentInstance();
    const myEventChannel </span>=<span style="color: rgba(0, 0, 0, 1)"> proxy.getOpenerEventChannel();
    const detail </span>=<span style="color: rgba(0, 0, 0, 1)"> ref();
    console.log(myEventChannel)
    myEventChannel.on(</span>'messageDetail'<span style="color: rgba(0, 0, 0, 1)">, ({
      data
    }) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      console.log(data);
      detail.value </span>=<span style="color: rgba(0, 0, 0, 1)"> data;
    });
</span>&lt;/script&gt;

&lt;style lang="scss" scoped&gt;<span style="color: rgba(0, 0, 0, 1)">
    .message</span>-<span style="color: rgba(0, 0, 0, 1)">detail {
      padding: 30rpx;
    }
</span>&lt;/style&gt;</pre>
</div>
<p>&nbsp;14、使用地图选点组件:&nbsp;https://lbs.qq.com/webApi/component/componentGuide/componentPicker</p>
<p>&nbsp; &nbsp; &nbsp; 申请key的时候,要启用webserviceAPI。 添加白名单域名的时候 不能漏了&nbsp;<span style="color: rgba(255, 0, 0, 1)"><strong>qq.com</strong></span>&nbsp; 霍...</p>
<p>&nbsp; &nbsp; &nbsp; <span style="color: rgba(255, 0, 0, 1)">backurl不能设置成&nbsp; location.href 因为,第二次再选择的时候就乱了。 location.origin + '/...'</span></p><br><br>
来源:https://www.cnblogs.com/Mvloveyouforever/p/15942248.html
頁: [1]
查看完整版本: unicloud开发公众号