-

微信公众号开发(图文教程)

微信公众号是开发者或商家在微信公众平台上申请的应用账号,该帐号与QQ账号互通,平台上实现和特定群体的文字、图片、语音、视频的全方位沟通、互动 ,形成了一种主流的线上线下微信互动营销方式。

微信公众号注册登录
在浏览器中输入:https://mp.weixin.qq.com/

微信公众号设置
进入公众号后台 >> 设置与开发 >> 公众号设置 >> 帐号详情

微信公众号聊天界面底部,自定义菜单
进入公众号后台 >> 设置与开发 >> 接口权限 >> 自定义菜单 为已获得

微信开放文档:
进入公众号后台 >> 自定义菜单 >> 创建接口
网址:https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html

进入微信公众平台接口调试工具 >> 创建公众号底部菜单
网址:https://mp.weixin.qq.com/debug?token=1352861311&lang=zh_CN

{
    "button": [
        {
            "type": "view", 
            "name": "首页", 
            "url": "https://xiyueta.com/"
        }, 
        {
            "type": "view", 
            "name": "扫一扫", 
            "url": "https://xiyueta.com/api/scan/weixin.asp?redirect_uri=https://xiyueta.com/api/scan/demo.asp"
        }, 
        {
            "type": "view", 
            "name": "关于作者", 
            "url": "https://xiyueta.com/about/author/"
        }, 
         
    ]
}
            

微信公众号底部菜单案例
调用微信扫一扫功能,案例代码


<%
dim qr,nLen
qr=request("qrresult")
if qr<>"" then
    nLen=instr(qr,"?cxid=")
    if nLen>0 then
        qr=mid(qr,nLen+6)
    end if
    ' response.write("qr=" & qr)
    ' response.write("<a href='fw.asp?cxid="& qr &"'>点击查看结束</a>")
    response.write("<script>window.location.href='fw.asp?act=saoma&cxid="& qr &"';</script>")
    ' response.redirect("fw.asp?cxid="&qr)
    response.end()
end if
%>
<a href="//xiyueta.com/api/scan/weixin.asp?redirect_uri=<%=getThisUrlNoParam()%>" style="font-size:30px;line-height:40px" id="scanclick">点击调用微信扫一扫功能</a>
    <script type="text/javascript" src="js/jquery.js"></script>
<script>
$(function(){
    window.location.href=$("#scanclick").attr('href');
})
</script>


 

微信扫一扫跳转到网页设置,参数act不等于saoma则跳转到公众号里


<%

if request("act")><"saoma" then
    response.write("跳转")
    response.redirect("//mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzkxMTQxNjgyOQ==&scene=110#wechat_redirect")

end if
%>


 

跳转到微信公众号里的 “__biz”参数如何获得
进入公众号后台 >> 右键 >> 检测 >> 网络 >> 搜索 uin_base64

微信授权
如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑。 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0 点击帮助文档
注意:跳转网址可以用 http:// 开头的,不一定必须要以https://安全方式访问的

代码部分:
 
<% 
dim url,canshu,gotourl
gotourl="https://xiyueta.com/getwxopenid.asp" 
url="https://open.weixin.qq.com/connect/oauth2/authorize"
canshu=canshu & "?appid=wxca2d47267c61c58f"
canshu=canshu & "&redirect_uri=" & server.URLEncode(gotourl)
canshu=canshu & "&response_type=code"
canshu=canshu & "&scope=snsapi_base"   'snsapi_userinfo 获得用户信息'
canshu=canshu & "&state=STATE#wechat_redirect"

' &scope=SCOPE // 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
url = url &canshu 
response.redirect(url)
%> 
 

获得微信唯一标识:
 
<!--#Include file = "../inc/config.asp"--><%

call loadmin()
sub loadmin()
    dim isDebug  '调用'
    isDebug=false

    if isDebug then
        call echo("code",request("code"))
        call echo("state",request("state"))
        doevents
    end if

    dim url,canshu,s,refresh_token,access_token,useropenid
    url="https://api.weixin.qq.com/sns/oauth2/access_token"
    canshu="?appid=wxca2d47267c61c58f"
    canshu=canshu & "&secret=xxx"
    canshu=canshu & "&code=" &  request("code")
    canshu=canshu & "&scope=SCOPE"        'snsapi_base   snsapi_userinfo'
    canshu=canshu & "&grant_type=authorization_code"

    s=gethttpurl(url &canshu,"utf-8")
    if isDebug then call echo("s",s)
    refresh_token=getStrCut(s,"""refresh_token"":""",""",",0)
    if isDebug then call echo("取refresh_token",refresh_token)


    '取access_token
    url="https://api.weixin.qq.com/sns/oauth2/refresh_token"
    canshu="?appid=wxca2d47267c61c58f" 
    canshu=canshu & "&refresh_token=" & refresh_token  '填写通过access_token获取到的refresh_token参数'
    canshu=canshu & "&grant_type=refresh_token&"  '填写为refresh_token'
    s=gethttpurl(url &canshu,"utf-8")
    if isDebug then call echo("url",url & canshu)
    if isDebug then call echo("s",s)

     
    access_token=getStrCut(s,"""access_token"":""",""",",0)
    if isDebug then call echo("access_token",access_token)
    useropenid=getStrCut(s,"""openid"":""",""",",0)
    if isDebug then call echo("取useropenid",useropenid)

    call eerr("useropenid",useropenid)   '测试'
 

    call openconn()
    dim sql
    sql="select * from " & db_PREFIX & "member Where wxopenid='"& useropenid &"'"
    rs.open sql ,conn,1,3
    if rs.eof then  
        rs.addnew
        rs("username")="wx" & getrnd(8)  '用户名'
        if session("promotionUserId")<>"" then   '添加推广者id 20230421'
            rs("userid")=session("promotionUserId")
        end if  
    end if  
    rs("wxopenid")=useropenid 
    rs.update:rs.close
    
    rs.open sql,conn,1,1    
    if not rs.eof then
        session("memberid")=rs("id")
    end if
    if isDebug then call echo("提示","登录成功")
 
    response.redirect("/")

   if isDebug then  call die("ss")

end sub

%>
 

获得头像信息等:
 
<!--#Include file = "../inc/config.asp"--><%

call loadmin()
sub loadmin()
    dim isDebug  '调用'
    isDebug=true

    if isDebug then
        call echo("code",request("code"))
        call echo("state",request("state"))
        doevents
    end if

    dim url,canshu,s,refresh_token,access_token,useropenid
    url="https://api.weixin.qq.com/sns/oauth2/access_token"
    canshu="?appid=wxca2d47267c61c58f"
    canshu=canshu & "&secret=xx"
    canshu=canshu & "&code=" &  request("code")
    canshu=canshu & "&scope=snsapi_userinfo"
    canshu=canshu & "&grant_type=authorization_code"

    s=gethttpurl(url &canshu,"utf-8")
    if isDebug then call echo("s",s)
    refresh_token=getStrCut(s,"""refresh_token"":""",""",",0)
    if isDebug then call echo("取refresh_token",refresh_token)


    '取access_token
    url="https://api.weixin.qq.com/sns/oauth2/refresh_token"
    canshu="?appid=wxca2d47267c61c58f" 
    canshu=canshu & "&refresh_token=" & refresh_token  '填写通过access_token获取到的refresh_token参数'
    canshu=canshu & "&grant_type=refresh_token&"  '填写为refresh_token'
    s=gethttpurl(url &canshu,"utf-8")
    if isDebug then call echo("url",url & canshu)
    if isDebug then call echo("s",s)

     
    access_token=getStrCut(s,"""access_token"":""",""",",0)
    if isDebug then call echo("access_token",access_token)
    useropenid=getStrCut(s,"""openid"":""",""",",0)
    if isDebug then call echo("取useropenid",useropenid)

    if isDebug then call hr()
    url="https://api.weixin.qq.com/sns/userinfo"
    canshu="?access_token="  & access_token  '网页授权接口调用凭证'
    canshu=canshu & "&openid=" & useropenid
    canshu=canshu & "&lang=zh_CN&"  '填写为refresh_token'
     


    s=gethttpurl(url &canshu,"utf-8")
    if isDebug then call echo("url",url & canshu)
    if isDebug then call echo("s",s)

    call writetofile("txt/"& useropenid &".txt",s,"utf-8")  '保存信息,测试用'

    dim nickname,sex,province,city,headimgurl
    nickname=getStrCut(s,"""nickname"":""",""",",0)   '昵称'
    sex=getStrCut(s,",""sex"":",",",0)   '性别'
    province=getStrCut(s,"""province"":""",""",",0)   '省'
    city=getStrCut(s,"""city"":""",""",",0)   '城市'
    headimgurl=getStrCut(s,"""headimgurl"":""",""",",0)   '头像'
    headimgurl=replace(headimgurl,"\/","/")

    if isDebug then 
        call echo("nickname",nickname)
        call echo("sex",sex)
        call echo("province",province)
        call echo("city",city)
        call echo("headimgurl",headimgurl & "<img src='"& headimgurl & "'>")
    end if
    call eerr("提示","测试结束")


    call openconn()
    dim sql
    sql="select * from " & db_PREFIX & "member Where wxopenid='"& useropenid &"'"
    rs.open sql ,conn,1,3
    if rs.eof then  
        rs.addnew
        rs("username")="wx" & getrnd(8)  '用户名'
        if session("promotionUserId")<>"" then   '添加推广者id 20230421'
            rs("userid")=session("promotionUserId")
        end if      
    end if  
    rs("wxopenid")=useropenid
    rs("wxopenidtime")=now()  '获得openid时间'
    rs("pic")=headimgurl
    rs("nickname")=nickname
    rs("province")=province
    rs("city")=city
    rs("bodycontent")=s
    rs("sex")=IIF(sex="0","男","女")      
    rs.update:rs.close
    
    rs.open sql,conn,1,1    
    if not rs.eof then
        session("memberid")=rs("id")
    end if
    if isDebug then call echo("提示","登录成功")
    if session("wxgotourl")<>"" then
        dim gotourl2
        gotourl2=session("wxgotourl")
        session("wxgotourl")=""
        response.redirect(gotourl2)   '指定跳转
    else
        response.redirect("/")
    end if

   if isDebug then  call die("ss")

end sub

%>
 

微信分享接口
请注意,原有的 wx.onMenuShareTimeline、wx.onMenuShareAppMessage、wx.onMenuShareQQ、wx.onMenuShareQZone 接口,即将废弃。请尽快迁移使用客户端6.7.2及JSSDK 1.4.0以上版本支持的 wx.updateAppMessageShareData、wx.updateTimelineShareData接口。 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#10 点击帮助文档
注意:在公众号里点击链接,再分享才会生效成带图标框链接,或点击带图标框的链接再分享,也会生成带图标框链接的。
如果不是在公众号里点击的链接,再分享是不会有带图标框的链接的。

自定义“分享给朋友”及“分享到QQ”按钮的分享内容(1.4.0)
 
<script type="text/javascript">
 
        var url = "api/scan/getwxconfig.asp";
        var data = {current_url: encodeURIComponent(window.location.href)};
        
        $.getJSON(url, data, function (backdata){ 
            // alert(backdata.appId)
            wx.config({
              debug: false,
              appId: backdata.appId,
              timestamp: backdata.timestamp,
              nonceStr: backdata.nonceStr,
              signature: backdata.signature,
              jsApiList: [
               "getNetworkType","getLocation",
               "scanQRCode",'updateAppMessageShareData',
  'updateTimelineShareData',
  'onMenuShareAppMessage', //旧的接口,即将废弃
  'onMenuShareTimeline' //旧的接口,即将废弃
                ]
            }); 

            // wx.updateAppMessageShareData(share); 
            // wx.updateTimelineShareData(sharePenYou); //朋友

            wx.ready(function () {   //需在用户可能点击分享按钮前就先调用
              wx.updateAppMessageShareData({ 
                title: "标题", // 分享标题
                desc: "描述内容", // 分享描述
                link: "https://xiyueta.com/", // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: "https://xiyueta.com/logo.jpg", // 分享图标
                success: function () {
                  // 设置成功
                  // alert("updateAppMessageShareData OK")
                }
              }) 

              wx.updateTimelineShareData({ 
                title: "标题", // 分享标题
                link: "https://xiyueta.com/", // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: "https://xiyueta.com/logo.jpg", // 分享图标
                success: function () {
                  // 设置成功
                  // alert(" updateTimelineShareData OK")
                }
              }) 


            }); 

        }); 

        wx.error(function (res) {
          alert("出错提示:" + res.errMsg);
        }); 



 
</script>