Wechat applet with Xiaobai interface, you can develop it without a server.

Here we will focus on how to directly call the Xiaobai interface in the WeChat applet without a server.

 

premise

Assuming you have already activated the WeChat Mini Program, if not, you can go to the WeChat public platform to activate it: https://mp.weixin.qq.com

Assuming you have already opened the Xiaobai interface, if you haven't, you can go to https://www.okayapi.com/?f=mwx to register for free

 

source code

The source code of this example can be downloaded from the code cloud, click to download: https://gitee.com/dogstar/okayapi-demo .

Final running effect:

key configuration

Modify the legitimate domain name of the request

First, log in to the WeChat official account platform, enter: Settings - Development Settings - Server Domain Name, modify the legitimate domain name of the request, and modify it to the domain name of the Xiaobai interface you are currently in. The Xiaobai interface already supports HTTPS access. as follows:

If you don't know the domain name you are in, you can log in to the backend of Xiaobai, enter: System Settings - My Package, and check the interface domain name. Such as:

Notice! Not all the domain names are https://api.okayapi.com. Only after this setting can the interface request in the applet be allowed.

Modify globalData in app.js

Then, download the code of this access example, use the WeChat developer tool ( click to download ) to open it, and modify the globalData configuration in app.js. as follows:

About APP_KEY and APP_SECRECT, you can find it in my previous package.

After the configuration is complete, we can start to develop functions in the applet. Let's see how to write code to request Xiaobai interface.

First add the two files okayapi.js and md5.js to the files in your utils directory. Then in your index.js file, first import okayapi.js, namely:

//index.js
let okayapi = require('../../utils/okayapi.js')

Request Xiaobai interface through wx.request

Then, use the wx.request provided by WeChat to initiate a request to the Xiaobai interface. It should be noted that the passed parameters must be encrypted by okayapi.enryptData(), and the wxRes.data in the returned result is the return result of the Xiaobai interface.

  /** * 小白接口请求示例 */
  okayApiHelloWorld: function(e) {
    /** * 准备接口参数 */
    let params = {
      s: "Hello.World",         // 必须,待请求的接口服务名称
      name: "dogstar"         // 可选,根据接口文档,补充更多接口参数
    };
    let _self = this

    /** * 对小白接口发起请求 */
    wx.request({
      url: app.globalData.okayapiHost,
      data: okayapi.enryptData(params),
      success: function (wxRes) {
        // TODO:实现你的梦想……
        let res = wxRes.data

        if (res.data && res.data.err_code == 0) {
          // TODO:请求成功
          console.log('ok: ', res.data)

          _self.setData({
            motto: res.data.title
          })
        } else {
          // TODO:当前操作失败
          console.log('fail: ', res)

          _self.setData({
            motto: res.data.err_msg
          })
        }

      }
    }) 
  }

If the call is successful, you will see debugging information like this:

Use PHP proxy to request Xiaobai interface

The above is the access guide for the WeChat applet directly connected to the Xiaobai interface without a server. If you have your own server and cannot modify the legitimate domain name of the request on the WeChat applet, you can use the PHP proxy described above . The use of PHP proxy is simpler, you only need to upload the PHP proxy file to your server, and then when the WeChat applet requests the interface, the link is changed to the link of the PHP proxy just now. This usage scenario is simpler and does not need to modify the legitimate domain name of the request, nor the globalData configuration in app.js, nor the okayapi.enryptData() encryption in wx.request. Because this is all moved to the PHP proxy implementation, it is also more secure.

That is, the call chain is as follows:

// 自己没有服务器(通过wx.request直接请求小白接口)
微信小程序 -> 小白接口

// 自已有服务器(通过PHP代理中转请求小白接口)
微信小程序 -> 自己服务器的PHP代理 -> 小白接口

If you have your own server, it is recommended to use a PHP proxy; if you don't have your own server, it doesn't matter, you can refer to the access guide above.

 

On the Xiaobai interface, you can store a lot of your own data, and develop it with WeChat applet, you can develop any application~~

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325069042&siteId=291194637