WeChat small program network package (simple and efficient)

A nonsense introduction
Although it was born for a long time, it has not been exposed to a small program development. The guitar interest class wants to get a small program to publish course information and card function. As an IT member, I voluntarily joined this small program development team. Although the small procedure is a front-end engineer, it is very friendly as a mobile program, plus a bit of front-end foundation, and it feels very easy.

WeChat applet network request is very convenient, and it can be called directly. But it is best to deal with some parameter callback information, carry out the secondary package, and directly provide the method for the entire small program application to provide a method of adjustment. Isn’t it better?

Using the consistent thinking of the network request, divided into three callbacks: onStart: Start request, onSuccess: request success callback, onfailed: request failure callback, meaning literally ~

netutil .js network request tool package

Create a netutil.js file

/ ** * For external POST request call * / function post (URL, PARAMS, ONSTART, Onsuccess, onfailed) {Request (URL, PARAMS, “Post”, onstart, onsuccess, onfailed;} / ** * For external GET request call * / Function Get (URL, PARAMS, ONSTART, Onsuccess, onfailed) {Request (URL, Params, “Get”, onstart, onsuccess, onfailed;} / ** * function: Packaging network request * @url URL address * @Params request Parameter * @Method request method: get / post * @OnStart start request, initial loading loading, etc. * @OnSuccess success callback * @onfailed failed callback * / Function Request (URL, Params, Method, onstart, onsuccess, onfailed) {onStart (); // Request Start Wx.Request ({URL: URL, DATA: DEALPARAMS (params), Method: Method, Header: {‘Content-Type’: ‘Application / Json’}, Success: Function (RES) {IF (RES .DATA) {/ ** START processes * / if (res. orerror_code == 0) {onSuccess (res. er); // request success} else {onfailed (res. div); {onfailed . msg); // request failed} / ** END processing end * /}}}} {onfailed (“”); // failure for other reasons}}} / ** * function: Demand Process Request Parameters: Add fixed parameter configuration, etc. * @PARAMS request parameters * / function dealparams (params) {return params;} Module.exports = {PostRequest: post, getRequest: get,}
   Small case instructions: Joke Daquan Interface 
Recommended: User Registration Interface

Request URL: http: //v.juhe. CN / Joke / Content / List.php


Parameter name


Type No String int Default per page load 20 Data Returns example Small program page JS file Netutil
Descending “DES”, ascending “ASC” Page No
, default 1 No String
TIME is String timestamp 10 bits
Key Yes String
{“ERROR_CODE”: 0, “Reason”: “Success”, “result”: {“data”: [{“Content”: “Mr. is a place in the place. One day, he is like usually in the study On the day of the newspaper, suddenly shouted with his wife: Hey, Anna, do you see the rumors on this morning? It’s a laugh! They said, you have a packing of the plaques. Have you heard? Anna, where are you? ? Anna? Ah! “,” Hashid “:” 90b182fc7f74865b40b1e5807cfebf41 “,” unixtime “: 1418745227,” Updatetime “:” 2014-12-16 23:53:47 “}, {” content “:” One day I looked at me Newspapers, small mutters, the topic of the article, the birds, there are foreign languages, and the husband listened to a sentence: the birds, of course, ‘外 外’. It turned out that her husband said wrong, I laughed forward. “,” Hashid “:” 206f52fd2ed94772cbc66c8ac61f2a ” “Unixtime”: 1418745227, “Updatetime”: “2014-12-16 23:53:47”}]}}

var Netutil = Require (“../../ utils / network.js”); // Require introduces PAGE ({Data: {Jokelist: {}}, online: function (options) {var URL = “http://v.juhe.cn/joke/content/list.php”; var params = {sort: “” Page: 1. PageSize: 5, Time: “1418816972”, Key: “746DFDB4CD8445D30A8F915FD2B5F76B”,} Netutil.getRequest (URL, Params, this.onfailed); // Calling the GET method is Number of households}, onstart: function () {// onstart callback wx.showloading ({Title: ‘is loading’,})}, onsuccess: function (res) {// onsuccess callback wx.hideloading (); this.setdata ({Jokelist: res. dec.data // Request results data})}}} {// onfailed callback wx.hideloading (); if (msg) {wx.showtoast ({Title: msg,} )}},})

Joke Daquan List in WXML file: JokeList

The above is all the content of this article, I hope to help everyone’s learning, I hope everyone will support Tumi Clouds.
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment