当前位置: 首页 > wzjs >正文

网站建设宣传册内容广告设计的工作内容

网站建设宣传册内容,广告设计的工作内容,酒店宣传软文,企业建筑网站说明:​ 本插件是集成企业微信自定义应用开发时接收消息与事件处理的URL接口的msg_signature请求进行校验、对消息解密Encrypt和处理被动回复消息内容功能,让自建应用和企业微信进行双向通信,企业可以在应用的管理后台开启接收消息模式&…

说明:​

本插件是集成企业微信自定义应用开发时接收消息与事件处理的URL接口的msg_signature请求进行校验、对消息解密Encrypt和处理被动回复消息内容功能,让自建应用和企业微信进行双向通信,企业可以在应用的管理后台开启接收消息模式,方便开发企业微信时快速接入。

功能:

  • 验证URL有效性,通过参数msg_signature​​对请求进行校验​​,确认调用者的合法性。
  • 收到消息后对​​解密​​Encrypt,得到明文的消息结构体
  • 被动回复消息,构造被动响应包

使用代码:

插件把安装后会安装在wx模块下的work中,在work目录下有lib目录是处理接口相关功能。了解更多插件到社区查看,为地址:https://goflys.cn/codedetail?id=77

使用代码如下:

// 企业微信服务入口
package workimport ("encoding/xml""fmt""gofly/utils/gf""gofly/utils/tools/gconv""log""time"wxbizmsgcrypt "gofly/app/wx/work/lib"
)type Index struct {NoNeedLogin []string //忽略登录接口配置-忽略全部传[*]
}// 初始化路由
func init() {fpath := Index{NoNeedLogin: []string{"*"}}gf.Register(&fpath, fpath)
}// 读取wxwork插件配置文件
var (confobj, _ = gf.GetConfByFile("wxwork")confData   = gconv.Map(gconv.Map(confobj)["data"])
)/**
* url接口配置*/
func (api *Index) GetPostApi(c *gf.GinCtx) {CorpId := gf.String(confData["corpid"])                 // 企业微信 ID (在企业微信的“我的企业->企业信息-企业ID”)Token := gf.String(confData["token"])                   // 添加回调时自动生成的 TokenEncodingAESKey := gf.String(confData["encodingaeskey"]) // 添加回调时自动生成的 EncodingAESKey// 获取到请求参数msgSignature := c.Query("msg_signature")timestamp := c.Query("timestamp")nonce := c.Query("nonce")if c.Request.Method == "GET" {echostr := c.Query("echostr")//使用示例一:验证回调URL-调用企业微信官方提供的接口进行解析校验wxcpt := wxbizmsgcrypt.NewWXBizMsgCrypt(Token, EncodingAESKey, CorpId, wxbizmsgcrypt.XmlType)echoStr, cryptErr := wxcpt.VerifyURL(msgSignature, timestamp, nonce, echostr)if nil != cryptErr {fmt.Println("verifyUrl fail", cryptErr)return}// 将解密出来的字符串返回出去// c.String(200, string(echoStr))_, _ = c.Writer.WriteString(string(echoStr))} else { //post请求wxcpt := wxbizmsgcrypt.NewWXBizMsgCrypt(Token, EncodingAESKey, CorpId, wxbizmsgcrypt.XmlType)postdata, err := c.GetRawData()if err != nil {log.Fatalln(err)}//使用示例二:对用户回复的消息解密msgText, cryptErr := wxcpt.DecryptMsg(msgSignature, timestamp, nonce, []byte(postdata))if nil != cryptErr {fmt.Println("DecryptMsg fail", cryptErr)return}msgContent, err := ReceiveCommonMsg(msgText)if err != nil {fmt.Println("ReceiveMsg fail", err)return}fmt.Println("解析消息: ", msgContent)fmt.Println("解析消息内容: ", msgContent.Content)//使用示例三:企业回复用户消息的加密(被动回复)if msgContent.Content != "" { //有内容是在回复respData := fmt.Sprintf("<xml><ToUserName><![CDATA[%v]]></ToUserName><FromUserName><![CDATA[%v]]></FromUserName><CreateTime>%v</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[AI正在为您解答,请稍等]]></Content></xml>", msgContent.FromUsername, msgContent.ToUsername, time.Now().Unix())encryptMsg, cryptErr := wxcpt.EncryptMsg(respData, timestamp, nonce)if nil != cryptErr {fmt.Println("DecryptMsg fail", cryptErr)}sEncryptMsg := string(encryptMsg)_, _ = c.Writer.WriteString(sEncryptMsg)}}
}type MsgContent struct {ToUsername   string `xml:"ToUserName"`FromUsername string `xml:"FromUserName"`CreateTime   uint32 `xml:"CreateTime"`MsgType      string `xml:"MsgType"`Content      string `xml:"Content"`Msgid        string `xml:"MsgId"`Agentid      uint32 `xml:"AgentId"`
}type WxReceiveCommonMsg struct {ToUserName   string //接收者 开发者 微信号FromUserName string //发送者 发送方帐号(一个OpenID)Content      string //文本内容CreateTime   int64  //创建时间MsgType      string //消息类型MsgId        int64  //消息idAgentID      int64  //接收的应用id,可在应用的设置页面获取PicUrl       string //图片urlMediaId      string //媒体idEvent        string //事件类型,VIEWEventKey     string //事件KEY值,设置的跳转URLMenuId       stringFormat       stringRecognition  stringThumbMediaId string //缩略图媒体ID
}// MsgContentFunc (接收到消息之后,会将消息交于这个函数处理)
var MsgContentFunc func(msg MsgContent) error// 处理接口事件
func ReceiveCommonMsg(msgData []byte) (MsgContent, error) {fmt.Printf("received weixin msgData:\n%s\n", msgData)rootmsg := MsgContent{}err := xml.Unmarshal(msgData, &rootmsg)if MsgContentFunc == nil {return rootmsg, err}err = MsgContentFunc(rootmsg)return rootmsg, err
}

lib工具代码:

package libimport ("bytes""crypto/aes""crypto/cipher""crypto/sha1""encoding/base64""encoding/binary""encoding/xml""fmt""math/rand""sort""strings"
)const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"const (ValidateSignatureError int = -40001ParseXmlError          int = -40002ComputeSignatureError  int = -40003IllegalAesKey          int = -40004ValidateCorpidError    int = -40005EncryptAESError        int = -40006DecryptAESError        int = -40007IllegalBuffer          int = -40008EncodeBase64Error      int = -40009DecodeBase64Error      int = -40010GenXmlError            int = -40010ParseJsonError         int = -40012GenJsonError           int = -40013IllegalProtocolType    int = -40014
)type ProtocolType intconst (XmlType ProtocolType = 1
)type CryptError struct {ErrCode intErrMsg  string
}func NewCryptError(err_code int, err_msg string) *CryptError {return &CryptError{ErrCode: err_code, ErrMsg: err_msg}
}type WXBizMsg4Recv struct {Tousername string `xml:"ToUserName"`Encrypt    string `xml:"Encrypt"`Agentid    string `xml:"AgentID"`
}type CDATA struct {Value string `xml:",cdata"`
}type WXBizMsg4Send struct {XMLName   xml.Name `xml:"xml"`Encrypt   CDATA    `xml:"Encrypt"`Signature CDATA    `xml:"MsgSignature"`Timestamp string   `xml:"TimeStamp"`Nonce     CDATA    `xml:"Nonce"`
}func NewWXBizMsg4Send(encrypt, signature, timestamp, nonce string) *WXBizMsg4Send {return &WXBizMsg4Send{Encrypt: CDATA{Value: encrypt}, Signature: CDATA{Value: signature}, Timestamp: timestamp, Nonce: CDATA{Value: nonce}}
}type ProtocolProcessor interface {parse(src_data []byte) (*WXBizMsg4Recv, *CryptError)serialize(msg_send *WXBizMsg4Send) ([]byte, *CryptError)
}type WXBizMsgCrypt struct {token              stringencoding_aeskey    stringreceiver_id        stringprotocol_processor ProtocolProcessor
}type XmlProcessor struct {
}func (self *XmlProcessor) parse(src_data []byte) (*WXBizMsg4Recv, *CryptError) {var msg4_recv WXBizMsg4Recverr := xml.Unmarshal(src_data, &msg4_recv)if nil != err {return nil, NewCryptError(ParseXmlError, "xml to msg fail")}return &msg4_recv, nil
}func (self *XmlProcessor) serialize(msg4_send *WXBizMsg4Send) ([]byte, *CryptError) {xml_msg, err := xml.Marshal(msg4_send)if nil != err {return nil, NewCryptError(GenXmlError, err.Error())}return xml_msg, nil
}func NewWXBizMsgCrypt(token, encoding_aeskey, receiver_id string, protocol_type ProtocolType) *WXBizMsgCrypt {var protocol_processor ProtocolProcessorif protocol_type != XmlType {panic("unsupport protocal")} else {protocol_processor = new(XmlProcessor)}return &WXBizMsgCrypt{token: token, encoding_aeskey: (encoding_aeskey + "="), receiver_id: receiver_id, protocol_processor: protocol_processor}
}func (self *WXBizMsgCrypt) randString(n int) string {b := make([]byte, n)for i := range b {b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]}return string(b)
}func (self *WXBizMsgCrypt) pKCS7Padding(plaintext string, block_size int) []byte {padding := block_size - (len(plaintext) % block_size)padtext := bytes.Repeat([]byte{byte(padding)}, padding)var buffer bytes.Bufferbuffer.WriteString(plaintext)buffer.Write(padtext)return buffer.Bytes()
}func (self *WXBizMsgCrypt) pKCS7Unpadding(plaintext []byte, block_size int) ([]byte, *CryptError) {plaintext_len := len(plaintext)if nil == plaintext || plaintext_len == 0 {return nil, NewCryptError(DecryptAESError, "pKCS7Unpadding error nil or zero")}if plaintext_len%block_size != 0 {return nil, NewCryptError(DecryptAESError, "pKCS7Unpadding text not a multiple of the block size")}padding_len := int(plaintext[plaintext_len-1])return plaintext[:plaintext_len-padding_len], nil
}func (self *WXBizMsgCrypt) cbcEncrypter(plaintext string) ([]byte, *CryptError) {aeskey, err := base64.StdEncoding.DecodeString(self.encoding_aeskey)if nil != err {return nil, NewCryptError(DecodeBase64Error, err.Error())}const block_size = 32pad_msg := self.pKCS7Padding(plaintext, block_size)block, err := aes.NewCipher(aeskey)if err != nil {return nil, NewCryptError(EncryptAESError, err.Error())}ciphertext := make([]byte, len(pad_msg))iv := aeskey[:aes.BlockSize]mode := cipher.NewCBCEncrypter(block, iv)mode.CryptBlocks(ciphertext, pad_msg)base64_msg := make([]byte, base64.StdEncoding.EncodedLen(len(ciphertext)))base64.StdEncoding.Encode(base64_msg, ciphertext)return base64_msg, nil
}func (self *WXBizMsgCrypt) cbcDecrypter(base64_encrypt_msg string) ([]byte, *CryptError) {aeskey, err := base64.StdEncoding.DecodeString(self.encoding_aeskey)if nil != err {return nil, NewCryptError(DecodeBase64Error, err.Error())}encrypt_msg, err := base64.StdEncoding.DecodeString(base64_encrypt_msg)if nil != err {return nil, NewCryptError(DecodeBase64Error, err.Error())}block, err := aes.NewCipher(aeskey)if err != nil {return nil, NewCryptError(DecryptAESError, err.Error())}if len(encrypt_msg) < aes.BlockSize {return nil, NewCryptError(DecryptAESError, "encrypt_msg size is not valid")}iv := aeskey[:aes.BlockSize]if len(encrypt_msg)%aes.BlockSize != 0 {return nil, NewCryptError(DecryptAESError, "encrypt_msg not a multiple of the block size")}mode := cipher.NewCBCDecrypter(block, iv)mode.CryptBlocks(encrypt_msg, encrypt_msg)return encrypt_msg, nil
}func (self *WXBizMsgCrypt) calSignature(timestamp, nonce, data string) string {sort_arr := []string{self.token, timestamp, nonce, data}sort.Strings(sort_arr)var buffer bytes.Bufferfor _, value := range sort_arr {buffer.WriteString(value)}sha := sha1.New()sha.Write(buffer.Bytes())signature := fmt.Sprintf("%x", sha.Sum(nil))return string(signature)
}func (self *WXBizMsgCrypt) ParsePlainText(plaintext []byte) ([]byte, uint32, []byte, []byte, *CryptError) {const block_size = 32plaintext, err := self.pKCS7Unpadding(plaintext, block_size)if nil != err {return nil, 0, nil, nil, err}text_len := uint32(len(plaintext))if text_len < 20 {return nil, 0, nil, nil, NewCryptError(IllegalBuffer, "plain is to small 1")}random := plaintext[:16]msg_len := binary.BigEndian.Uint32(plaintext[16:20])if text_len < (20 + msg_len) {return nil, 0, nil, nil, NewCryptError(IllegalBuffer, "plain is to small 2")}msg := plaintext[20 : 20+msg_len]receiver_id := plaintext[20+msg_len:]return random, msg_len, msg, receiver_id, nil
}func (self *WXBizMsgCrypt) VerifyURL(msg_signature, timestamp, nonce, echostr string) ([]byte, *CryptError) {signature := self.calSignature(timestamp, nonce, echostr)if strings.Compare(signature, msg_signature) != 0 {return nil, NewCryptError(ValidateSignatureError, "signature not equal")}plaintext, err := self.cbcDecrypter(echostr)if nil != err {return nil, err}_, _, msg, receiver_id, err := self.ParsePlainText(plaintext)if nil != err {return nil, err}if len(self.receiver_id) > 0 && strings.Compare(string(receiver_id), self.receiver_id) != 0 {fmt.Println(string(receiver_id), self.receiver_id, len(receiver_id), len(self.receiver_id))return nil, NewCryptError(ValidateCorpidError, "receiver_id is not equil")}return msg, nil
}func (self *WXBizMsgCrypt) EncryptMsg(reply_msg, timestamp, nonce string) ([]byte, *CryptError) {rand_str := self.randString(16)var buffer bytes.Bufferbuffer.WriteString(rand_str)msg_len_buf := make([]byte, 4)binary.BigEndian.PutUint32(msg_len_buf, uint32(len(reply_msg)))buffer.Write(msg_len_buf)buffer.WriteString(reply_msg)buffer.WriteString(self.receiver_id)tmp_ciphertext, err := self.cbcEncrypter(buffer.String())if nil != err {return nil, err}ciphertext := string(tmp_ciphertext)signature := self.calSignature(timestamp, nonce, ciphertext)msg4_send := NewWXBizMsg4Send(ciphertext, signature, timestamp, nonce)return self.protocol_processor.serialize(msg4_send)
}func (self *WXBizMsgCrypt) DecryptMsg(msg_signature, timestamp, nonce string, post_data []byte) ([]byte, *CryptError) {msg4_recv, crypt_err := self.protocol_processor.parse(post_data)if nil != crypt_err {return nil, crypt_err}signature := self.calSignature(timestamp, nonce, msg4_recv.Encrypt)if strings.Compare(signature, msg_signature) != 0 {return nil, NewCryptError(ValidateSignatureError, "signature not equal")}plaintext, crypt_err := self.cbcDecrypter(msg4_recv.Encrypt)if nil != crypt_err {return nil, crypt_err}_, _, msg, receiver_id, crypt_err := self.ParsePlainText(plaintext)if nil != crypt_err {return nil, crypt_err}if len(self.receiver_id) > 0 && strings.Compare(string(receiver_id), self.receiver_id) != 0 {return nil, NewCryptError(ValidateCorpidError, "receiver_id is not equil")}return msg, nil
}


文章转载自:

http://nd1OTH2g.yLtrf.cn
http://696gbDgr.yLtrf.cn
http://se3Hczad.yLtrf.cn
http://9r2a9kJj.yLtrf.cn
http://m9qmhmYH.yLtrf.cn
http://bWoE0Q2H.yLtrf.cn
http://CfS9PC7W.yLtrf.cn
http://bMv4kpcV.yLtrf.cn
http://Q6mtSsSZ.yLtrf.cn
http://8y21bSjW.yLtrf.cn
http://rgPh1lDs.yLtrf.cn
http://5ZOoNL37.yLtrf.cn
http://XhY3GF9W.yLtrf.cn
http://BJuVeQAg.yLtrf.cn
http://q06W8Cnz.yLtrf.cn
http://y9Tq0TKG.yLtrf.cn
http://RnZ6UFs8.yLtrf.cn
http://GEw5ZMcX.yLtrf.cn
http://Z1vlYOvt.yLtrf.cn
http://jbrzlgmF.yLtrf.cn
http://93NfMqa2.yLtrf.cn
http://FQgUHFZ3.yLtrf.cn
http://Id6fUOX3.yLtrf.cn
http://CGY9F2j3.yLtrf.cn
http://k96ikdy1.yLtrf.cn
http://luU22ejQ.yLtrf.cn
http://6e72kfFj.yLtrf.cn
http://Sh4gtBJF.yLtrf.cn
http://BUVE4IHG.yLtrf.cn
http://sasb6FP4.yLtrf.cn
http://www.dtcms.com/wzjs/706327.html

相关文章:

  • idea做网站登录网站建设开发软件有哪些方面
  • PS怎么布局网站结构物流平台运营
  • 佛山公司做网站中国建筑app下载
  • 专业建站公司费用做app需要什么技术
  • 网站集约化平台网站建设企业需要准备资料
  • 浙江省建设信息港网站自助建站营销招商
  • 重庆网站设计优化seo招聘
  • 佛山公司建网站昆明做网站优化的公司
  • 心雨在线高端网站建设网站建设质量体系审核指导
  • 深圳做手机的企业网站查看邮箱注册过的网站
  • 用手机怎么做免费网站公众号小程序怎么做
  • 电商网站变化做网站背景图片浪漫爱情
  • 用dedecms 做门户网站百度搜索网站
  • 域名访问网站在哪里找国外网站国内做二维码
  • 百度站长平台论坛网站服务器是注册域名平台吗
  • 有合作社做网站得不山东网站备案拍照
  • 学校网站html模板中国建设银行手机网站首页
  • 舟山网站建设代理关键词是什么意思
  • 商城网站建设排名wordpress开发工具
  • 西宁市城乡规划和建设局网站最近军事新闻
  • 企业网站模板源代码专门做图片的网站cms
  • 网站方案书免费济南房产网官网首页
  • 无证做音频网站违法吗呼和浩特百度公司
  • 百度高级搜索怎么用宝安网站优化
  • 本地网站404错误服务器网站建设流程
  • 为什么推荐企业做网站太原网站设计公司
  • 苏州建设职业培训中心网站常州孟河镇建设工程交易网站
  • 免费地图制作网站建站系统主要包括企业网站系统
  • 国外著名的网站设计公司一直免费的服务器下载安装
  • 网站建设技术手段免费行情软件app网站直播