使用第三方在线API模拟数据,进行微信小程序开发。不会后端开发也可以体验微信小程序。

 

1、下载安装微信 Web 开发者工具

2、本实例中使用的 appKey 为我个人账户,如需使用自己微信小程序俱乐部上的 appKey 可以进行替换

3、感兴趣的同学可以自己在微信小程序俱乐部进行注册,获取属于自己的 appKey

4、该 Demo 主要实践如何使用第三方 API 进行微信小程序的开发

1、程序结构

图片.png

2、实现思路

利用微信小程序俱乐部 API 提供的接口实现信息请求,并返回数据。

 

微信小程序俱乐部 API 主要配置:

  1. module.exports = {
  2. appKey: "9emcn7gs9wx8c5i200yzdqlad3tb1d2m",
  3. clubApi: {
  4. put: \'https://api.wxappclub.com/put\',
  5. get: \'https://api.wxappclub.com/get\',
  6. del: \'https://api.wxappclub.com/del\',
  7. match: \'https://api.wxappclub.com/match\',
  8. list: \'https://api.wxappclub.com/list\',
  9. wxUser: \'https://api.wxappclub.com/wxUser\'
  10. }
  11. }

 

 

 

以首页进行说明:

加载页面时,请求微信小程序俱乐部 API 获取轮播的图片,和文章,进行展示,其他几个界面类似

  1. Page({
  2. data: {
  3. imgUrls: [],
  4. indicatorDots: true,
  5. autoplay: true,
  6. interval: 2000,
  7. duration: 100
  8. },
  9. onLoad: function (options) {
  10. this.getShowImg()
  11. this.getLastNews()
  12. },
  13. //请求展示的图片
  14. getShowImg: function (options) {
  15. let that = this
  16. options = {
  17. url: config.clubApi.get,
  18. data: {
  19. appkey: config.appKey,
  20. key: "show",
  21. type: "picture"
  22. }
  23. }
  24. util.request(options, function (res) {
  25. let showPictures = res.data.result.value
  26. that.setData({ showPictures: showPictures })
  27. })
  28. },
  29. //请求最新新闻
  30. getLastNews: function (options) {
  31. let that = this
  32. options = {
  33. url: config.clubApi.get,
  34. data: {
  35. appkey: config.appKey,
  36. key: "lastnews",
  37. type: "article"
  38. }
  39. }
  40. util.request(options, function (res) {
  41. let lastnews = res.data.result.value
  42. that.setData({ lastnews: lastnews })
  43. })
  44. }
  45. })

 

4、配置文件说明

 

utils 文件夹下 config.js 里 appKey 为 微信小程序俱乐部 API 中心的 appKey,这里可以使用我的 appKey 进行测试,也可以使用你们自己的。

 

在解析 html 时使用了开源库 wxParse。

1、将项目导入到微信 web 开发者工具,点击编译即可运行

2、运行截图

 

1500990690476038683.png

1500990690376055820.png

1500990690571067127.png

1、微信小程序俱乐部地址

2、进入俱乐部后,点击 API 中心,可以根据自己需要进行创建,在附件中会将数据进行导出。可以将data里的数据导入到自己微信俱乐部 API 中。

版权声明:本文为demodashi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/demodashi/p/8486488.html