هذا المثال هو إدراك إدارة إضافة وحذف قوائم WeChat في خلفية الإدارة.
1. أولاً نحتاج إلى إنشاء جدول قاعدة بيانات جديد لتخزين عناصر القائمة
الحقول التي يمكن تضمينها هي معرف ، معرف الفئة الأصل ، الاسم ، الفرز ، سواء كنت تعرض ، اكتب (عرض ، انقر) ، رابط ، الإضافة
لاحظ أنه عندما يتم تخزين بيانات القائمة في الخلفية ، فإن ParentId = -1 هي القائمة من المستوى الأول ، أو أن ParentID هو معرف القائمة من المستوى الأول كقائمة المستوى الثاني ضمن القائمة من المستوى الأول.
2. أثناء قائمة الإعداد ، تحتاج إلى تمرير سلسلة Menujson إلى واجهة WeChat ، لذلك يجب عليك أولاً تقسيم السلسلة وتحديد Creatmenu () في الخلفية.
BOOL BOOL CEARMENU () {String menusson = "" ؛ // هنا الافتراضي ParentId = -1 هو القائمة الخارجية ، IsActive = 1 هو العرض ، ResponseType = 1 هو نوع النقر DataTable dtamenu = service.selectDatatable ("ID ، name ، responseType ، jsonstr ، url" ، "weixinmenu" ، "parentid = -1 و iS-1 order by sube") ؛ if (dtamenu.rows.count> 0) {menujson = "{/" button/": [" ؛ لـ (int i = 0 ؛ i <dtamenu.rows.count ؛ i ++) {dataTable dtbmenu = service.selectDatatable ("id ، name ، responseType ، jsonstr ، url" ، wx ، "parentid =" + dtamenu.rows [i] ["id"]. if (dtbmenu.rows.count> 0) {menujson + = "{/" name/":/" " + dtamenu.rows [i] [" name "]. لـ (int j = 0 ؛ j <dtbmenu.rows.count ؛ j ++) {if (convert.toint32 (dtbmenu.rows [j] ["revieType"]) == 2) {menujson += "{/" type/":/" view/"،/ + "/" ،/"url/":/"" + dtbmenu.rows [j] ["jsonstr"]. toString () + "/"} ، "؛ } آخر {menujson + = "{/" type/":/" click/"،/" name/":/" " + dtbmenu.rows [j] [" name "]. }} menujson = menujson.trimend ('،') ؛ menujson += "]} ،" ؛ } آخر {// if (convert.toint32 (dtamenu.rows [i] ["responseType"]) == 2) // {menujson + = "{/" type/":/" view/"،/" name/":/" " + dtamenu.rows [i] [i] dtamenu.rows [i] ["jsonstr"]. toString () + "/"} ، "؛ //} // else // {// menujson + = "{/" type/":/" click/"،/" name/":/" " + dtamenu.rows [i] [" name "]. //}} dtbmenu.dispose () ؛ } dtamenu.dispose () ؛ menujson = menujson.trimend ('،') ؛ menujson += "]}" ؛ menujson = menujson.trim () ؛ Return requestzmtocreatment (menujson) ؛ } آخر {return false ؛ }}3. بعد الحصول على سلسلة Menujson ، اتصل بواجهة WeChat لإنشاء القائمة. تحتاج إلى الحصول على رمز التقييم أولا. لرمز التقييم ، يرجى الرجوع إلى: الحصول على AccessToken
/// <summary> /// طلب إلى خادم WeChat لإنشاء قائمة مخصصة /// </summary> /// <param name = "jsonstr"> </param> /// <returns> </returns> ///private bool requestzmtocreatment (string jsonstr) بشكل عام ، يمكنك الحصول على قاعدة البيانات في البداية والحصول عليها من قاعدة البيانات في المرة القادمة. لاحظ أن AccessToken صالحة لمدة 7200 ثانية // إعلان httpwebrequest request string interfaceurl = "https://api.weixin.qq.com/cgi-bin/menu/create؟access_token=" + AccessToken ؛ طلب httpwebrequest = (httpwebrequest) webrequest.create (interfaceurl) ؛ // قم بتعيين request.timeout termout. timeout = 30000 ؛ request.keepalive = true ؛ ترميز encodeType = encoding.getenCoding ("UTF-8") ؛ request.headers.set ("pragma" ، "no-cache") ؛ request.method = "post" ؛ request.ContentType = "Application/X-WWW-Form-urlencoded" ؛ request.userAgent = "Mozilla/4.0 (متوافق ؛ MSIE 6.0 ؛ Windows NT 5.2 ؛ SV1 ؛ Maxthon ؛ .RET 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 ، application/msword ، */ *" ؛ request.cookieContainer = new CookieContainer () ؛ byte [] bytes = encodeType.getBytes (jsonstr) ؛ request.contentLength = bytes.length ؛ request.allowautoredirect = true ؛ // إرسال البيانات باستخدام (Dream Writer = request.getRequestStream ()) {writer.write (bytes ، 0 ، bytes.length) ؛ الكاتب. } StringBuilder strb = new StringBuilder () ؛ // تلقي البيانات باستخدام (Dream Reader = request.getResponse (). getResPonsestream ()) {StreamReader Sr = new 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 () (strb.toString (). indexof ("/" errcode/": 41001")! = -1)) getzmaccesstoken () ؛ Return requestzmtocreatment (jsonstr) ؛ } آخر {if (strb.toString () == "{/" errcode/": 0 ،/" errmsg/":/" ok/"}") {return true ؛ } آخر {return false ؛ }}} catch (استثناء exp) {return false ؛ }}لخص
ما ورد أعلاه هو رمز المثال لإعداد قائمة مخصصة لتطوير حساب WeChat العام الذي قدمه لك المحرر. آمل أن يكون ذلك مفيدًا لك. إذا كان لديك أي أسئلة ، فيرجى ترك رسالة لي وسوف يرد المحرر إليك في الوقت المناسب. شكرا جزيلا لدعمكم لموقع wulin.com!