ตัวอย่างนี้คือการตระหนักถึงการเพิ่มและการจัดการการลบเมนู WeChat ในพื้นหลังการจัดการ
1. ก่อนอื่นเราต้องสร้างตารางฐานข้อมูลใหม่สำหรับการจัดเก็บรายการเมนู
ฟิลด์ที่สามารถรวมได้คือ ID, ID คลาสแม่, ชื่อ, เรียงลำดับไม่ว่าจะแสดง, พิมพ์ (ดู, คลิก), ลิงค์, addDate
โปรดทราบว่าเมื่อข้อมูลเมนูเมนูถูกเก็บไว้ในพื้นหลัง ParentId = -1 เป็นเมนูระดับแรกหรือ ParentID เป็น ID ของเมนูระดับแรกเป็นเมนูระดับที่สองภายใต้เมนูระดับแรก
2. ในระหว่างเมนูการตั้งค่าคุณต้องส่งสตริง menujson ไปยังอินเตอร์เฟส weChat ดังนั้นคุณต้องแยกสตริงก่อนและกำหนด creatmenu () ในพื้นหลัง
public bool creatmenu () {String menujson = ""; // ที่นี่ค่าเริ่มต้น parentid = -1 เป็นเมนูนอกสุด, isactive = 1 คือการแสดงผล, responsetype = 1 คือประเภทคลิก dataTable dtamenu = service.selectDatatable ("id, ชื่อ, responsetype, jsonstr, url", "weixinmenu", "parentid = -1 if (dtamenu.rows.count> 0) {menujson = "{/" ปุ่ม/": ["; สำหรับ (int i = 0; i <dtamenu.rows.count; i ++) {dataTable dtbmenu = service.selectDatatable ("id, ชื่อ, responsetype, jsonsters, url", wx, "parentid =" + dtamenu.rows [i] [id " if (dtbmenu.rows.count> 0) {menujson + = "{/" name/":/" " + dtamenu.rows [i] [" name "]. toString () +"/",/" sub_button/":"; สำหรับ (int j = 0; j <dtbmenu.rows.count; j ++) {ถ้า (convert.toint32 (dtbmenu.rows [j] ["responsetype"]) == 2) {menujson += "/" type/":/" view/",/" ชื่อ/":/" dtbmenu.rows [j] ["ชื่อ"]. toString () + "/",/"url/":/"" + dtbmenu.rows [j] ["jsonstr"]. toString () + "/"}, "; } else {menujson + = "{/" type/":/" คลิก/",/" name/":/" " + dtbmenu.rows [j] [" name "]. tostring () +"/",/" key/":/" eventkey_ " + dtbmenu.rows [j] [" }} menujson = menujson.trimend (','); menujson += "]},"; } else {// ถ้า (Convert.toint32 (dtamenu.rows [i] ["Responsetype"]) == 2) // {menujson + = "{/" type/":/" view/"/" name/":/" " + dtamenu.rows [i] [" name " dtamenu.rows [i] ["jsonstr"]. toString () + "/"}, "; //} // else // {// menujson + = "{/" type/":/" click/",/" name/":/" " + dtamenu.rows [i] [" name "]. toString () +"/",/" key/":/" eventkey_ " //}} dtbmenu.dispose (); } dtamenu.dispose (); menujson = menujson.trimend (','); menujson += "]}"; menujson = menujson.trim (); return requestzmtocreatment (menujson); } else {return false; -3. หลังจากได้รับสตริง menujson ให้โทรไปที่อินเตอร์เฟส WeChat เพื่อสร้างเมนู คุณต้องได้รับโทเค็นการประเมินก่อน สำหรับโทเค็นการประเมินโปรดดู: รับ AccessToken
/// <summary> /// ร้องขอไปยังเซิร์ฟเวอร์ WeChat เพื่อสร้างเมนูที่กำหนดเอง /// </summary> /// <param name = "JSONST"> </param> /// <return> </return> /// private bool requestzmTocreatment โดยทั่วไปคุณสามารถรับฐานข้อมูลที่จุดเริ่มต้นและรับจากฐานข้อมูลในครั้งต่อไป โปรดทราบว่า AccessToken นั้นใช้ได้เป็นเวลา 7200 วินาที // ประกาศ HTTPWEBREQUEST REQUEST String InterfaceUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + AccessToken; httpWebRequest Request = (httpWebRequest) webRequest.create (InterfaceUrl); // ตั้งค่าคำขอหมดเวลาการเชื่อมต่อ timeout = 30000; request.keepalive = true; การเข้ารหัส encodeType = encoding.getEncoding ("UTF-8"); request.headers.set ("pragma", "no-cache"); request.method = "post"; request.contentType = "แอปพลิเคชัน/x-www-form-urlencoded"; request.useragent = "mozilla/4.0 (เข้ากันได้; msie 6.0; windows nt 5.2; sv1; maxthon; .net clr 1.1.4322); http stdns"; request.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, แอปพลิเคชัน/msword, */ *"; request.cookieContainer = new CookieContainer (); ไบต์ [] bytes = encodetype.getBytes (jsonstr); request.contentLength = bytes.length; request.allowautoredirect = true; // ส่งข้อมูลโดยใช้ (สตรีม writer = request.getRequestStream ()) {writer.write (ไบต์, 0, bytes.length); Writer.close (); } StringBuilder strb = new StringBuilder (); // รับข้อมูลโดยใช้ (reader reader = request.getResponse (). getResponSestream ()) {streamReader sr = streamReader ใหม่ (reader, encodeType); strb.append (sr.readtoend ()); sr.close (); reader.close (); } if ((strb.toString (). indexof ("/" errcode/": 42001")! = -1) || (strb.toString (). indexof ("/" errcode/": 40001")! = -1) | | (strb.toString (). indexof ("/" errcode/": 41001")! = -1)) // ข้อผิดพลาด access_token {// accessToken = getAccessToken (); getzmaccessToken (); return requestzmtocreatment (jsonstr); } else {ถ้า (strb.toString () == "{/" errcode/": 0,/" errmsg/":/" ตกลง/"}") {return true; } else {return false; }}} catch (exp exp) {return false; -สรุป
ด้านบนเป็นรหัสตัวอย่างสำหรับการตั้งค่าเมนูที่กำหนดเองสำหรับการพัฒนาบัญชีสาธารณะ WeChat แนะนำให้คุณทราบโดยบรรณาธิการ ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!