国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Heim WeChat-Applet WeChat-Entwicklung Zusammenfassung von fünf Fallstricken, die bei der Entwicklung ?ffentlicher PHP-WeChat-Konten auftreten

Zusammenfassung von fünf Fallstricken, die bei der Entwicklung ?ffentlicher PHP-WeChat-Konten auftreten

Mar 21, 2017 pm 03:54 PM

In diesem Artikel werden haupts?chlich die fünf Fallstricke der Entwicklung ?ffentlicher PHP-WeChat-Konten vorgestellt. Interessierte Freunde k?nnen sich auf das Menü

beziehen XML-Datei Basierend auf der von WeChat zurückgegebenen XML-Datei k?nnen wir die eindeutige Kennung jedes WeChat-Benutzers relativ zum offiziellen WeChat-Konto erhalten. Der Mechanismus der ?ffentlichen WeChat-Plattform besteht einfach darin, dass wir selbst eine XML-Datei mit festem Format ausgeben und die WeChat-APP dann dafür verantwortlich ist, sie zu analysieren, die gewünschten Informationen abzurufen und die Informationen dann auf einheitliche Weise zu verarbeiten.

Die sechste Grube, Wenn Sie sich das WeChat-Dokument ansehen, wird es Sie definitiv umbringen, wie im Bild oben gezeigt. Denken Sie daran, dass ?ToUserName“ und ?FromUserName“ für WeChat niemals umgekehrt geschrieben werden sollten.

Zusammenfassung von fünf Fallstricken, die bei der Entwicklung ?ffentlicher PHP-WeChat-Konten auftreten


/// <summary>
 /// 接收微信發(fā)送的XML消息并且解析
 /// </summary>
 private void ReceiveXml()
 {
 try
 {
  Stream requestStream = System.Web.HttpContext.Current.Request.InputStream;
  byte[] requestByte = new byte[requestStream.Length];
  requestStream.Read(requestByte, 0, (int)requestStream.Length);
  string requestStr = Encoding.UTF8.GetString(requestByte);

  if (!string.IsNullOrEmpty(requestStr))
  { 

  //封裝請求類
  XmlDocument requestDocXml = new XmlDocument();
  requestDocXml.LoadXml(requestStr);
  XmlElement rootElement = requestDocXml.DocumentElement;
  WxXmlModel WxXmlModel = new WxXmlModel();
  if (rootElement != null)
  {
   WxXmlModel.ToUserName = rootElement.SelectSingleNode("ToUserName") == null ? "" : rootElement.SelectSingleNode("ToUserName").InnerText;
   WxXmlModel.FromUserName = rootElement.SelectSingleNode("FromUserName") == null ? "" : rootElement.SelectSingleNode("FromUserName").InnerText;
   WxXmlModel.CreateTime = rootElement.SelectSingleNode("CreateTime") == null ? "" : rootElement.SelectSingleNode("CreateTime").InnerText;
   WxXmlModel.MsgType = rootElement.SelectSingleNode("MsgType") == null ? "" : rootElement.SelectSingleNode("MsgType").InnerText;
   switch (WxXmlModel.MsgType)
   {

   case "text"://文本
    WxXmlModel.Content = rootElement.SelectSingleNode("Content") == null ? "" : rootElement.SelectSingleNode("Content").InnerText;
    break;
   case "image"://圖片
    WxXmlModel.PicUrl = rootElement.SelectSingleNode("PicUrl") == null ? "" : rootElement.SelectSingleNode("PicUrl").InnerText;
    break;
   case "event"://事件
    WxXmlModel.Event = rootElement.SelectSingleNode("Event") == null ? "" : rootElement.SelectSingleNode("Event").InnerText;
    if (WxXmlModel.Event != "TEMPLATESENDJOBFINISH")//關(guān)注類型
    {
    WxXmlModel.EventKey = rootElement.SelectSingleNode("EventKey") == null ? "" : rootElement.SelectSingleNode("EventKey").InnerText;
    }
    break;
   default:
    break;
   }
  }
  ResponseXML(WxXmlModel);//回復(fù)消息
  }

 

 }
 catch (Exception ee)
 {
  //記錄錯誤日志
 }
 }

 /// <summary>
 /// 回復(fù)消息
 /// </summary>
 /// <param name="WxXmlModel"></param>
 private void ResponseXML(WxXmlModel WxXmlModel)
 {
 string XML = "";
 switch (WxXmlModel.MsgType)
 {
  case "text"://文本回復(fù)
  var info = oauth.GetUserInfo(Tools.WA_GetAccess_Token.IsExistAccess_Token(), WxXmlModel.FromUserName);
  Tools.WAEntity.OAuthUser user = Tools.JsonHelper.ParseFromJson<Tools.WAEntity.OAuthUser>(info);
  var content = WxXmlModel.Content.ToUpper();
  string NcbActUrl = ConfigurationManager.AppSettings["NcbActUrl"];
  string appid = ConfigurationManager.AppSettings["AppID"];
  if (content.Contains("T"))//接受的文字如果包含T
  {
   //業(yè)務(wù)處理
  }
  else
  {
   XML = ResponseMessage.ReText(WxXmlModel.FromUserName, WxXmlModel.ToUserName, "/:rose農(nóng)場大數(shù)據(jù)歡迎你!/:rose");

  }
  break;
  case "event":
  switch (WxXmlModel.Event.ToLower())
  {
   case "subscribe":
   if (string.IsNullOrEmpty(WxXmlModel.EventKey))
   {
    XML = ResponseMessage.ReText(WxXmlModel.FromUserName, WxXmlModel.ToUserName, "關(guān)注成功!/:rose");

   }
   else
   {
    XML = ResponseMessage.SubScanQrcode(WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.EventKey);//掃描帶參數(shù)二維碼先關(guān)注后推送事件
   }
   break;
   case "scan":
   XML = ResponseMessage.ScanQrcode(WxXmlModel.FromUserName, WxXmlModel.ToUserName, WxXmlModel.EventKey);//掃描帶參數(shù)二維碼已關(guān)注 直接推送事件
   break;
   case "click"://處理單擊事件
   if (WxXmlModel.EventKey == "p1")
   {
    //自己的業(yè)務(wù)邏輯
   }
   else
   {
    //自己的業(yè)務(wù)邏輯
   }
   break;
   case "unsubscribe"://取消關(guān)注
   break;
  }
  break;
  default://默認(rèn)回復(fù)
  break;
 }
 Response.Write(XML);//輸出組織的XML信息

 }

Dies ist die Informationsverarbeitung des Menüs. Menschen, die die Wahrheit nicht kennen, scheinen zu fragen, was das ist ?ResponseMessage“ bedeutet, dass ich mich über die ?ffentliche WeChat-Plattform, die ich in den letzten drei Tagen recherchiert habe, nicht mehr beschweren kann.


public class ResponseMessage

{

 #region 接收的類型
 /// <summary>
 /// 接收文本
 /// </summary>
 /// <param name="FromUserName"></param>
 /// <param name="ToUserName"></param>
 /// <param name="Content"></param>
 /// <returns></returns>
 public static string GetTextTest(string FromUserName, string ToUserName, string Content, string key)
 {
 CommonMethod.WriteTxt(Content);//接收的文本消息
 string XML = "";
 switch (Content)
 {
  case "關(guān)鍵字":
  XML = ReText(FromUserName, ToUserName, "關(guān)鍵詞回復(fù)測試——興農(nóng)豐華:" + key);
  break;
  case "單圖文":
  XML = ReArticle(FromUserName, ToUserName, "測試標(biāo)題", "測試詳情——興農(nóng)豐華:" + key, "http://www.xnfhtech.com/templets/boze/images/20120130083143544.gif", "http://www.xnfhtech.com/");
  break;
  default:
  XML = ReText(FromUserName, ToUserName, "無對應(yīng)關(guān)鍵字——興農(nóng)豐華:" + key);
  break;
 }
 return XML;

 }

 /// <summary>
 /// 未關(guān)注掃描帶參數(shù)二維碼
 /// </summary>
 /// <param name="FromUserName"></param>
 /// <param name="ToUserName"></param>
 /// <param name="EventKey"></param>
 /// <returns></returns>
 public static string SubScanQrcode(string FromUserName, string ToUserName, string EventKey)
 {
 return "";
 }
 

 /// <summary>
 /// 已關(guān)注掃描帶參數(shù)二維碼
 /// </summary>
 /// <param name="FromUserName"></param>
 /// <param name="ToUserName"></param>
 /// <param name="EventKey"></param>
 /// <returns></returns>
 public static string ScanQrcode(string FromUserName, string ToUserName, string EventKey)
 {
 return "";
 }
 #endregion
 

 #region 回復(fù)方式
 /// <summary>
 /// 回復(fù)文本
 /// </summary>
 /// <param name="FromUserName">發(fā)送給誰(openid)</param>
 /// <param name="ToUserName">來自誰(公眾賬號ID)</param>
 /// <param name="Content">回復(fù)類型文本</param>
 /// <returns>拼湊的XML</returns>

 public static string ReText(string FromUserName, string ToUserName, string Content)
 {
 string XML = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";//發(fā)送給誰(openid),來自誰(公眾賬號ID)
 XML += "<CreateTime>" + CommonMethod.ConvertDateTimeInt(DateTime.Now) + "</CreateTime>";//回復(fù)時間戳
 XML += "<MsgType><![CDATA[text]]></MsgType>";//回復(fù)類型文本
 XML += "<Content><![CDATA[" + Content + "]]></Content><FuncFlag>0</FuncFlag></xml>";//回復(fù)內(nèi)容 FuncFlag設(shè)置為1的時候,自動星標(biāo)剛才接收到的消息,適合活動統(tǒng)計(jì)使用
 return XML;

 }
 

 /// <summary>
 /// 回復(fù)單圖文
 /// </summary>
 /// <param name="FromUserName">發(fā)送給誰(openid)</param>
 /// <param name="ToUserName">來自誰(公眾賬號ID)</param>
 /// <param name="Title">標(biāo)題</param>
 /// <param name="Description">詳情</param>
 /// <param name="PicUrl">圖片地址</param>
 /// <param name="Url">地址</param>
 /// <returns>拼湊的XML</returns>

 public static string ReArticle(string FromUserName, string ToUserName, string Title, string Description, string PicUrl, string Url)
 {

 string XML = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";//發(fā)送給誰(openid),來自誰(公眾賬號ID)
 XML += "<CreateTime>" + CommonMethod.ConvertDateTimeInt(DateTime.Now) + "</CreateTime>";//回復(fù)時間戳
 XML += "<MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content><ArticleCount>1</ArticleCount><Articles>";
 XML += "<item><Title><![CDATA[" + Title + "]]></Title><Description><![CDATA[" + Description + "]]></Description><PicUrl><![CDATA[" + PicUrl + "]]></PicUrl><Url><![CDATA[" + Url + "]]></Url></item>";
 XML += "</Articles><FuncFlag>0</FuncFlag></xml>";
 return XML;

 }


 /// <summary>
 /// 多圖文回復(fù)
 /// </summary>
 /// <param name="FromUserName">發(fā)送給誰(openid)</param>
 /// <param name="ToUserName">來自誰(公眾賬號ID)</param>
 /// <param name="ArticleCount">圖文數(shù)量</param>
 /// <param name="dtArticle"></param>
 /// <returns></returns>

 public static string ReArticle(string FromUserName, string ToUserName, int ArticleCount, System.Data.DataTable dtArticle)
 {

 string XML = "<xml><ToUserName><![CDATA[" + FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + ToUserName + "]]></FromUserName>";//發(fā)送給誰(openid),來自誰(公眾賬號ID)
 XML += "<CreateTime>" + CommonMethod.ConvertDateTimeInt(DateTime.Now) + "</CreateTime>";//回復(fù)時間戳
 XML += "<MsgType><![CDATA[news]]></MsgType><Content><![CDATA[]]></Content><ArticleCount>" + ArticleCount + "</ArticleCount><Articles>";
 foreach (System.Data.DataRow Item in dtArticle.Rows)

 {
  XML += "<item><Title><![CDATA[" + Item["Title"] + "]]></Title><Description><![CDATA[" + Item["Description"] + "]]></Description><PicUrl><![CDATA[" + Item["PicUrl"] + "]]></PicUrl><Url><![CDATA[" + Item["Url"] + "]]></Url></item>";
 }
 XML += "</Articles><FuncFlag>0</FuncFlag></xml>";
 return XML;

 }

 #endregion

 }

OK, ist die Antwort mit Ihrem eigenen Logikcode perfekt implementiert?

Die siebte Grube, Ich m?chte wirklich nicht mehr z?hlen, bist du sicher, dass diese Antwort in Ordnung ist? Ehrlich gesagt bin ich mir nicht sicher, denn nachdem Sie es geschrieben haben, wissen Sie, wo Sie es nennen sollen, verdammt, es ist am sichersten, die Antwort hinzuzufügen, nachdem der Server die überprüfung bestanden hat. Ich habe keine Moral mehr.

Was sollen wir als n?chstes sagen? Sprechen wir über die Beschaffung von Benutzerinformationen, da diese Dinge von uns im Allgemeinen auf H5-Seiten basieren. Deshalb müssen wir das

Zusammenfassung von fünf Fallstricken, die bei der Entwicklung ?ffentlicher PHP-WeChat-Konten auftreten

-Zeug verwenden, das wir zuvor konfiguriert haben. Tats?chlich weist dieses im Vergleich zum vorherigen zumindest viel weniger Fallstricke auf. Mit freundlichen Grü?en, ich werde es erst einmal belassen, ganz zu schweigen davon, dass er betrogen wurde. Lassen Sie uns etwas Code einfügen.


//微信網(wǎng)頁授權(quán)2.0
public class Oauth2
{
 JavaScriptSerializer Jss = new JavaScriptSerializer();
 public Oauth2() { }
 

 /// <summary>
 /// 對頁面是否要用授權(quán)
 /// </summary>
 /// <param name="Appid">微信應(yīng)用id</param>
 /// <param name="redirect_uri">回調(diào)頁面</param>
 /// <param name="scope">應(yīng)用授權(quán)作用域snsapi_userinfo(不彈出授權(quán)頁面,直接跳轉(zhuǎn),只能獲取用戶openid),snsapi_userinfo (彈出授權(quán)頁面,可通過openid拿到昵稱、性別、所在地。并且,即使在未關(guān)注的情況下,只要用戶授權(quán),也能獲取其信息)</param>
 /// <returns>授權(quán)地址</returns>

 public string GetCodeUrl(string Appid, string redirect_uri, string scope)
 {
 return string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state=STATE#wechat_redirect", Appid, redirect_uri, scope);
 }

 

 /// <summary>
 /// 對頁面是否要用授權(quán)
 /// </summary>
 /// <param name="Appid">微信應(yīng)用id</param>
 /// <param name="redirect_uri">回調(diào)頁面</param>
 /// <param name="scope">應(yīng)用授權(quán)作用域snsapi_userinfo(不彈出授權(quán)頁面,直接跳轉(zhuǎn),只能獲取用戶openid),snsapi_userinfo (彈出授權(quán)頁面,可通過openid拿到昵稱、性別、所在地。并且,即使在未關(guān)注的情況下,只要用戶授權(quán),也能獲取其信息)</param>
 /// <returns>授權(quán)地址</returns>
 public string GetCodeUrl(string Appid, string redirect_uri, string scope,string state)
 {
 return string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={2}&state={3}#wechat_redirect", Appid, redirect_uri, scope, state);
 }

 

 /// <summary>
 /// 用code換取openid 此方法一般是不獲取用戶昵稱時候使用
 /// </summary>
 /// <param name="Appid"></param>
 /// <param name="Appsecret"></param>
 /// <param name="Code">回調(diào)頁面帶的code參數(shù)</param>
 /// <returns>微信用戶唯一標(biāo)識openid</returns>
 public string CodeGetOpenid(string Appid, string Appsecret, string Code)
 {
 string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", Appid, Appsecret, Code);
 string ReText = CommonMethod.WebRequestPostOrGet(url, "");//post/get方法獲取信息
 Dictionary<string, object> DicText = (Dictionary<string, object>)Jss.DeserializeObject(ReText);
 if (!DicText.ContainsKey("openid"))
  return "";
 return DicText["openid"].ToString();
 }

 

 /// <summary>
 ///用code換取獲取用戶信息(包括非關(guān)注用戶的)
 /// </summary>
 /// <param name="Appid"></param>
 /// <param name="Appsecret"></param>
 /// <param name="Code">回調(diào)頁面帶的code參數(shù)</param>
 /// <returns>獲取用戶信息(json格式)</returns>

 public string GetUserInfo(string Appid, string Appsecret, string Code)
 {

 string url = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", Appid, Appsecret, Code);
 string ReText = CommonMethod.WebRequestPostOrGet(url, "");//post/get方法獲取信息
 Dictionary<string, object> DicText = (Dictionary<string, object>)Jss.DeserializeObject(ReText);
 if (!DicText.ContainsKey("openid"))

 {

  log.Error("獲取openid失敗,錯誤碼:" + DicText["errcode"].ToString());

 return "";

 }
 else

 {

  return CommonMethod.WebRequestPostOrGet("https://api.weixin.qq.com/sns/userinfo?access_token=" + DicText["access_token"] + "&openid=" + DicText["openid"] + "&lang=zh_CN", "");

 }

 }

 

 

 /// <summary>
 /// 通過openId獲取用戶信息
 /// </summary>
 /// <param name="accesstoken"></param>
 /// <param name="openid"></param>
 /// <returns></returns>
 public string GetUserInfo(string accesstoken, string openid)

 {
 string url = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", accesstoken, openid);
 return CommonMethod.WebRequestPostOrGet(url, "");//post/get方法獲取信息

 }

}

Wenn wir es aufrufen müssen, k?nnen wir die darin enthaltene Methode direkt verwenden, um die Autorisierung der WeChat-Webseite zu erhalten. Beispielsweise müssen wir eine Autorisierung für Ansicht B einholen unter Ein Controller, und Um benutzerbezogene Informationen zu erhalten, k?nnen wir sie direkt aufrufen, z. B. GetCodeUrl(appid, "http://" + Url + "/A/B", "snsapi_userinfo")

Hier beschwere ich mich besser.

Die achte Grube, WeChat-Menü JSON-URL-Splicing, gibt es vorne keine JS-Verifizierung, also was solls, fügen Sie einfach http:// hinzu.

Aber nach der Autorisierung hier, da wir viele Benutzerinformationen verwenden müssen, ist dies das Problem der Wertübertragung auf der H5-Seite. Ich verwende Session im Projekt und schreibe direkt eine ?ffentliche Methode . Wenn ?If Session“ einen Wert hat, wird dieser direkt übernommen. Bezüglich einiger darin enthaltener Dinge m?chte ich erkl?ren, dass nicht der gesamte Code ver?ffentlicht werden muss. Der Code hier ist genau das, was meiner Meinung nach ver?ffentlicht werden muss. Daher gibt es m?glicherweise einige Methoden, die Sie nicht sehen k?nnen. Bei Bedarf k?nnen Sie auf dieser Seite eine Nachricht hinterlassen.


public string getSession()

{

 log.Error("GetSession");
 string oauthStr = "";
 try

 {
 if (Session != null && (Session["oauthStr"] == null || string.IsNullOrEmpty(Session["oauthStr"].ToString())))
 {
  if (!string.IsNullOrEmpty(Request.QueryString["code"]))
  {
  Oauth2 oauth = new Oauth2();
  string code = Convert.ToString(Request["code"]);
  oauthStr = oauth.GetUserInfo(ConfigurationManager.AppSettings["AppID"],
   ConfigurationManager.AppSettings["AppSecret"], code);
   Session["oauthStr"] = oauthStr;
  Tools.WAEntity.OAuthUser oAuthUser = new Tools.WAEntity.OAuthUser();
  oAuthUser = Tools.JsonHelper.ParseFromJson<Tools.WAEntity.OAuthUser>(oauthStr);
  }
  return oauthStr;

 }
 else

 {

  Tools.WAEntity.OAuthUser oAuthUser = new Tools.WAEntity.OAuthUser();
  oAuthUser = Tools.JsonHelper.ParseFromJson<Tools.WAEntity.OAuthUser>(Session["oauthStr"].ToString());
  return Session["oauthStr"].ToString();

 }

 }

 catch (Exception e) { log.Error(e.ToString()); return oauthStr; };

}

Jedes Mal, wenn ich dann auf eine Seite sto?e, die Informationen einholen muss, rufe ich normalerweise einfach diese auf.

Im Grunde ist der Rest die Gesch?ftslogik, mit der wir uns selbst auseinandersetzen müssen. Reden wir weiter über die Fallstricke.

Die neunte Gefahr: Das Hochladen von Bildern auf WeChat ist definitiv nicht die einzige Gefahr für Sie. Ich glaube wirklich an dieses Baby, ob Sie es glauben oder nicht. Spezielle Bilder k?nnen nicht in einer for-Schleife hochgeladen werden. Dies gilt natürlich nur für Apple-Modelle, bei Android gibt es noch kein Problem.
Ich habe das Problem der JS-Sicherheitsüberprüfung bereits erw?hnt. Hier rufen wir diese überprüfungen auf, fordern einige entsprechende Berechtigungen an und erhalten dann Bildinformationen und so weiter.

Zusammenfassung von fünf Fallstricken, die bei der Entwicklung ?ffentlicher PHP-WeChat-Konten auftreten

Keine Sorge, das Baby spricht über das Bild oben, nicht über einen kleinen Bruder. . . . .

Lass uns weiter zurückkommen und uns den Code ansehen.

Befassen wir uns zun?chst mit Json


public class JsApi

{
 JavaScriptSerializer Jss = new JavaScriptSerializer(); 

 public JsApi() { } 
 const string URL_FORMAT_TICKET = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi";

 #region 驗(yàn)證JsApi權(quán)限配置
 /// <summary>
 /// 獲取JsApi權(quán)限配置的數(shù)組/四個參數(shù)
 /// </summary>
 /// <param name="Appid">應(yīng)用id</param>
 /// <param name="Appsecret">密鑰</param>
 /// <returns>json格式的四個參數(shù)</returns>
 public string GetJsApiInfo(string Appid, string Appsecret)
 {
 string jsapi_ticket = "";

 //ticket 緩存7200秒

 if (System.Web.HttpContext.Current.Session["jsapi_ticket"] == null)

 {
  string ticketurl = string.Format(URL_FORMAT_TICKET, BasicApi.GetAccessToken(Appid, Appsecret));//"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + GetAccessToken(Appid, Appsecret) + "&type=jsapi"
  jsapi_ticket = CommonMethod.WebRequestPostOrGet(ticketurl, "");//BasicApi.GetTokenSession
  System.Web.HttpContext.Current.Session["jsapi_ticket"] = jsapi_ticket;
  System.Web.HttpContext.Current.Session.Timeout = 7200;
  BasicApi.WriteTxt("jsapi_ticket1:" + jsapi_ticket);


 }
 else
 {
  jsapi_ticket = System.Web.HttpContext.Current.Session["jsapi_ticket"].ToString();
  BasicApi.WriteTxt("jsapi_ticket2:" + jsapi_ticket);
 } 

 Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(jsapi_ticket);
 jsapi_ticket = respDic["ticket"].ToString();//獲取ticket
 string timestamp = CommonMethod.ConvertDateTimeInt(DateTime.Now).ToString();//生成簽名的時間戳
 string nonceStr = CommonMethod.GetRandCode(16);//生成簽名的隨機(jī)串
 string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri.ToString();//當(dāng)前的地址
 BasicApi.WriteTxt("url:" + url);
 string[] ArrayList = { "jsapi_ticket=" + jsapi_ticket, "timestamp=" + timestamp, "noncestr=" + nonceStr, "url=" + url };
 Array.Sort(ArrayList);
 string signature = string.Join("&", ArrayList);
 signature = FormsAuthentication.HashPasswordForStoringInConfigFile(signature, "SHA1").ToLower();
 string r = "{\"appId\":\"" + Appid + "\",\"timestamp\":" + timestamp + ",\"nonceStr\":\"" + nonceStr +
   "\",\"signature\":\"" + signature +
   "\",\"jsApiList\":[\"chooseImage\",\"previewImage\",\"uploadImage\",\"downloadImage\",\"scanQRCode\",\"onMenuShareQQ\"]}";
 BasicApi.WriteTxt("r:" + r.Replace(" ", ""));
 return r.Replace(" ", "");

 }

}

und schauen uns dann die konkreten Aufrufe an.

Der Hintergrundcode ist eigentlich sehr einfach. Geben Sie einfach die Konfigurationsdatei direkt aus und rufen Sie dann die Front-End-JS direkt auf.


JsApi jsApi = new JsApi();
string config = jsApi.GetJsApiInfo(appId, appSecret);
ViewBag.config = config;

前臺代碼,其實(shí)也不難,這個有官方的例子的。


<script type="text/javascript">
 wx.config(@Html.Raw(ViewBag.config));//后臺傳遞的微信配置文件
 wx.ready(function () {
 $("#avatar").click(function () {
  wx.chooseImage({
  count: 1, // 圖片數(shù)量 默認(rèn)9
  sizeType: [&#39;compressed&#39;], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有&#39;original&#39;,
  sourceType: [&#39;album&#39;, &#39;camera&#39;], // 可以指定來源是相冊還是相機(jī),默認(rèn)二者都有
  success: function (res) {
   var localIds = res.localIds; // 返回選定照片的本地ID列表,localId可以作為img標(biāo)簽的src屬性顯示圖片
   wx.uploadImage({
   localId: &#39;&#39; + localIds,
   isShowProgressTips: 1,
   success: function (res) {
    serverId = res.serverId;
    getWxPhoto(serverId);

   }

   });

  }

  });

 });

 });

 wx.error(function (res) {
 alert("接口驗(yàn)證失敗,詳細(xì)信息:\n" + JSON.stringify(res));
 });
 var types = 1;
 function getWxPhoto(mediaId) {
 $.ajax({
  async: false,
  type: "post",
  url: "/ActivityRegistration/DownloadWxPhoto",//自己的處理方法
  data: { mediaId: mediaId, types: types },
  success: function (data) {
  $("#imageico").val(data.result);
  $("#hed_pic").attr(&#39;src&#39;, ".." + data.result);
  $("#hed_pic").attr(&#39;alt&#39;, "avatarImg");

  }

 });

 }

</script>

OK,后臺方法其實(shí)也很簡單,就是一個二進(jìn)制文件處理,不對,簡單個蛋蛋,特么的,因?yàn)槁窂降膯栴},坑了寶寶一個小時,特么的。還有這里建議,等微信圖片下載完成之后再給前臺加載圖片,保證每一個圖片都加載完成,保證后臺的圖片的上傳完成。


/// <summary>
/// 下載多媒體文件
/// </summary>
/// <param name="userName">公眾號</param>
/// <param name="mediaId">媒體ID</param>
/// <param name="data">返回下載是否成功</param>
/// <param name="types">添加的圖片類型</param>
/// <returns>返回多媒體文件數(shù)據(jù);如果下載失敗,返回null。</returns>
public JsonResult DownloadWxPhoto(string mediaId, int types)

{

 ErrorMessage errorMessage;
 string access_token = BasicApi.GetAccessToken(ConfigurationManager.AppSettings["AppID"], ConfigurationManager.AppSettings["AppSecret"]);
 byte[] data = MediaHelper.Download(access_token, mediaId, out errorMessage);
 string files = String.Empty, fileName = String.Empty;
 files = Server.MapPath("~/Wxinphoto/");
 if (!Directory.Exists(files))
 {
 Directory.CreateDirectory(files);
 }
 fileName = files + DateTime.Now.Ticks + ".jpg";
 if (data != null)
 {
 bool flag = writeFile(data, fileName);
 if (flag)
 {
  errorMessage = new ErrorMessage(ErrorMessage.SuccessCode, "下載多媒體文件成功。");
 }
 else
 {
  errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "從微信服務(wù)器下載多媒體文件失敗。");
 }

 }
 else
 errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "從微信服務(wù)器下載多媒體文件失敗。");
 return Json(new { result = "/" + urlconvertor(fileName), errorMessage = errorMessage });

}


//讀filename到byte[] 
private byte[] ReadFile(string fileName)
{
 FileStream pFileStream = null;
 byte[] pReadByte = new byte[0];
 try
 {
 pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
 BinaryReader r = new BinaryReader(pFileStream);
 r.BaseStream.Seek(0, SeekOrigin.Begin); //將文件指針設(shè)置到文件開
 pReadByte = r.ReadBytes((int)r.BaseStream.Length);
 return pReadByte;
 }
 catch
 {
 return pReadByte;
 }
 finally
 {
 if (pFileStream != null)
  pFileStream.Close();

 }

}

 

//寫byte[]到fileName

private bool writeFile(byte[] pReadByte, string fileName)

{

 FileStream pFileStream = null;

 try

 {
 pFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
 pFileStream.Write(pReadByte, 0, pReadByte.Length);
 }
 catch

 {
 return false;
 }
 finally

 {
 if (pFileStream != null)

  pFileStream.Close();

 }

 return true;

}
/// <summary>
/// 判斷目標(biāo)字節(jié)數(shù)組是否位于源字節(jié)數(shù)組的開始
/// </summary>
/// <param name="source">源字節(jié)數(shù)組</param>
/// <param name="target">目標(biāo)字節(jié)數(shù)組</param>
/// <returns>返回目標(biāo)字節(jié)數(shù)組是否位于源字節(jié)數(shù)組的開始</returns>

private bool StartsWithBytes(byte[] source, byte[] target)

{

 if (source == null && target == null)

 return true;

 if (source == null && target != null || source != null && target == null)
 return false;
 if (source.Length < target.Length)
 return false;
 bool startsWith = true;
 for (int i = 0; i < target.Length; i++)
 {
 if (source[i] != target[i])

 {
  startsWith = false;

  break;

 }

 }

 return startsWith;

}

是不是以為這就算完事了,我的乖乖,頭像上傳了,微信攝像頭也特么該調(diào)用的調(diào)用了,寶寶好幸福,寶寶也是牛人一個了,記住前面的東東,寶寶還沒有說坑呢。
來重復(fù)我們的第九個坑,特么的,你JS寫個for循環(huán)要是能循環(huán)把圖片上傳到后臺,寶寶也服氣,真的,寶寶服氣。

直接說吧,最后我自己想了下,也和隊(duì)友討論了下,可能是因?yàn)槲⑿庞惺裁打?yàn)證,導(dǎo)致之后一張圖片上傳成功之后,才能進(jìn)行一張,但是我們Iphone就是特么的特例,大Android沒用問題的,就是Iphone有了問題,而且問題不小,上傳四張圖片吧,老特么是最后一張,最后,找到了萬能的網(wǎng)友,感謝你,不過寶寶已經(jīng)忘記了在哪里找到的了,尷尬了。。。。。。。。。。。


<script type="text/javascript">

 var types = 2;

 var urlList="";

 var i = 0;

 function up(resurl) {

  if (i < resurl.localIds.length) {

  // 上傳照片resu.localIds[i]
  wx.uploadImage({
   localId: &#39;&#39; + resurl.localIds[i],
   isShowProgressTips: 1,
   success: function (res) {
   // alert("res.serverId:" + res.serverId);
   mediaId = res.serverId;
   $.ajax({
    async: false,
    type: "post",
    url: "/ActivityRegistration/DownloadWxPhoto",
    data: { mediaId: mediaId, types: types },
    success: function (data) {
    $("#picPath").append(&#39;<li><p class="imgbox"><img src="/static/imghw/default1.png"  data-src="/img/cechanadd.png"  class="lazy"   id="picture&#39; + i + &#39;" alt="" /></p></li>&#39;);

    $("#picture" + i).attr(&#39;src&#39;, data.result);
    $("#picPath").append(&#39;<input value=&#39; + data.result + &#39; type="hidden" id="picurl&#39; + i + &#39;" class="picclass" />&#39;);

    i++;

    if (i == resurl.localIds.length - 1) {

     $("#picPath").append(&#39;<li><p class="imgbox"><img src="/static/imghw/default1.png"  data-src="/img/cechanadd.png"  class="lazy"   id="picture" alt="" /></p></li>&#39;);

    }

    up(resurl);
    }
   });

   }
  });
  } else {

  i = 0;
  }

 }

 

 
 //上傳圖片
 wx.config(@Html.Raw(ViewBag.config));
 wx.ready(function () {
  $("#picPath").click(function () {
  wx.chooseImage({
   count: 3, // 默認(rèn)9
   sizeType: [&#39;compressed&#39;], // 可以指定是原圖還是壓縮圖,默認(rèn)二者都有&#39;original&#39;,
   sourceType: [&#39;album&#39;, &#39;camera&#39;], // 可以指定來源是相冊還是相機(jī),默認(rèn)二者都有
   success: function (resu) {
   var localIds = resu.localIds; // 返回選定照片的本地ID列表,localId可以作為img標(biāo)簽的src屬性顯示圖片
   if (localIds.indexOf("wxlocalresource") != -1) {    
   localIds = localIds.replace("wxlocalresource", "wxLocalResource");
   }
 
   @(index += 1)
   if (localIds != &#39;&#39;) {
    $("#picPath").html("");
   var sear = new RegExp(&#39;,&#39;);
    if (sear.test(localIds)) {
    up(resu);
    }

    else {
    $("#picPath").append(&#39; <li><p class="imgbox"><img src="/static/imghw/default1.png"  data-src="/img/cechanadd.png"  class="lazy"   id="picture&#39; + &#39;@index&#39; + &#39;" alt="" " /></p></li>&#39;);
    $("#picture" + "@index").attr(&#39;src&#39;, localIds);
    // 上傳照片
    wx.uploadImage({
     localId: &#39;&#39; + localIds,

    isShowProgressTips: 1,
     success: function (res) {
     mediaId = res.serverId;

     $.ajax({
      async: false,

      type: "post",
      url: "/ActivityRegistration/DownloadWxPhoto",

      data: { mediaId: mediaId, types: types },

      success: function (data) {

      $("#picPath").append(&#39;<input value=&#39; + data.result + &#39; type="hidden" id="picurl&#39; + @index + &#39;" class="picclass" />&#39;);

      $("#picPath").append(&#39;<li><p class="imgbox"><img src="/static/imghw/default1.png"  data-src="/img/cechanadd.png"  class="lazy"   id="picture" alt="" /></p></li>&#39;);
      }
     });

    }
    });
    }
    // $("#picPath").append(&#39;<li><p class="imgbox"><img src="/static/imghw/default1.png"  data-src="/img/cechanadd.png"  class="lazy"   id="picture" alt="" /></p></li>&#39;);

   }
   }
  });
  });
 });
 wx.error(function (res) {
  alert("接口驗(yàn)證失敗,詳細(xì)信息:\n" + JSON.stringify(res));
 });
 </script>

請記住,遞歸就特么可以了。

說到這里,寶寶已經(jīng)不想多說什么了,特么的產(chǎn)品你能不能不裝逼,你特么見過那個微信能回復(fù)一個信息直接跳轉(zhuǎn)網(wǎng)頁的,你咋不去屎呢,聯(lián)想到前幾天大阿里的月餅時間,突然感覺我們程序員挺悲劇的,成功的都是特么的產(chǎn)品,然后出問題的都是我們程序員的鍋?試問一下,這個鍋真心我們程序員該背么。

算了,還是不吐槽了,已經(jīng)無力了。。。。寶寶92年降臨,現(xiàn)在確實(shí)82年的皮膚呀,唉,寶寶累了,真的。

順便給點(diǎn)H5頁面的建議吧。比如當(dāng)點(diǎn)擊返回鍵的時候,我們需要刷新頁面的時候,就是所謂的判斷頁面要不要刷新,這里有很多種方法,但是微信里面寶寶還是覺得這么干靠譜。


<script type="text/javascript">
 if (window.name != "hasLoad") {
 location.reload();
 window.name = "hasLoad";
 } else {
 window.name = "";
 }
</script>

還有,那個微信執(zhí)行完成之后想直接退出當(dāng)前界面進(jìn)入微信公眾號界面的,直接調(diào)用微信的一個內(nèi)置的方法即可。記得寫到<script></script>里面。?

WeixinJSBridge.call('closeWindow'); //這是微信關(guān)閉當(dāng)前網(wǎng)頁

這么自信的以為自己搞定了所有,你跑呀,你要跑起來,嗯哼,別不服氣。?

微信公眾賬號指第十坑,我自己加的,哈哈,就是前面的JS驗(yàn)證的時候,你不要頭文件,怎么搞定這些事情,哈哈。是不是寶寶贏了。Oh? perfect,I like it。

Das obige ist der detaillierte Inhalt vonZusammenfassung von fünf Fallstricken, die bei der Entwicklung ?ffentlicher PHP-WeChat-Konten auftreten. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Erkl?rung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Hei?e KI -Werkzeuge

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem v?llig kostenlosen KI-Gesichtstausch-Tool aus!

Hei?e Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Hei?e Themen

PHP-Tutorial
1502
276