前言

本教程是基于 “apifm-wxapi” 模块,教你快速实现小程序开发,所以你可能需要先了解以下知识点:

《创建 HelloWorld 项目》
《使用 “apifm-wxapi” 快速开发小程序》
《免费注册开通后台,获得专属域名》

功能说明

本案例行政区数据采用国家统计局2017年公布的权威数据,地区编码为国标;
小程序实现5级联动,实现 省/市/区/街道/社区 以及 镇/村委会 级别的数据联动;

小程序实现

效果演示

「省/市/区/街道/社区」5级联动 - 图1

wxml代码

  1. <view class="page">
  2. <view class="page__bd">
  3. <view class="weui-cells__title">选择</view>
  4. <view class="weui-cells weui-cells_after-title">
  5. <view class="weui-cell weui-cell_select">
  6. <view class="weui-cell__hd weui-cell__hd_in-select-after">
  7. <view class="weui-label">省份</view>
  8. </view>
  9. <view class="weui-cell__bd">
  10. <picker bindchange="provinceChange" value="{{pIndex}}" range="{{provinces}}" range-key="name">
  11. <view class="weui-select weui-select_in-select-after">{{provinces[pIndex].name}}</view>
  12. </picker>
  13. </view>
  14. </view>
  15. <view wx:if="{{cities}}" class="weui-cell weui-cell_select">
  16. <view class="weui-cell__hd weui-cell__hd_in-select-after">
  17. <view class="weui-label">城市</view>
  18. </view>
  19. <view class="weui-cell__bd">
  20. <picker bindchange="cityChange" value="{{cIndex}}" range="{{cities}}" range-key="name">
  21. <view class="weui-select weui-select_in-select-after">{{cities[cIndex].name}}</view>
  22. </picker>
  23. </view>
  24. </view>
  25. <view wx:if="{{areas}}" class="weui-cell weui-cell_select">
  26. <view class="weui-cell__hd weui-cell__hd_in-select-after">
  27. <view class="weui-label">区县</view>
  28. </view>
  29. <view class="weui-cell__bd">
  30. <picker bindchange="areaChange" value="{{aIndex}}" range="{{areas}}" range-key="name">
  31. <view class="weui-select weui-select_in-select-after">{{areas[aIndex].name}}</view>
  32. </picker>
  33. </view>
  34. </view>
  35. <view wx:if="{{streets}}" class="weui-cell weui-cell_select">
  36. <view class="weui-cell__hd weui-cell__hd_in-select-after">
  37. <view class="weui-label">街道/镇</view>
  38. </view>
  39. <view class="weui-cell__bd">
  40. <picker bindchange="streetChange" value="{{sIndex}}" range="{{streets}}" range-key="name">
  41. <view class="weui-select weui-select_in-select-after">{{streets[sIndex].name}}</view>
  42. </picker>
  43. </view>
  44. </view>
  45. <view wx:if="{{communities}}" class="weui-cell weui-cell_select">
  46. <view class="weui-cell__hd weui-cell__hd_in-select-after">
  47. <view class="weui-label">社区/村委会</view>
  48. </view>
  49. <view class="weui-cell__bd">
  50. <picker bindchange="communityChange" value="{{ccIndex}}" range="{{communities}}" range-key="name">
  51. <view class="weui-select weui-select_in-select-after">{{communities[ccIndex].name}}</view>
  52. </picker>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. </view>

js代码

  1. const WXAPI = require('apifm-wxapi')
  2. WXAPI.init('gooking')
  3. Page({
  4. data: {
  5. provinces: undefined,// 省份数据数组
  6. pIndex: 0,//选择的省下标
  7. cities: undefined,// 城市数据数组
  8. cIndex: 0,//选择的市下标
  9. areas: undefined,// 区县数数组
  10. aIndex: 0,//选择的区下标
  11. streets: undefined,// 街道/镇数据数组
  12. sIndex: 0,// 选择的街道下标
  13. communities: undefined,//社区/村委会数据数组
  14. ccIndex: 0,// 选择的社区下标
  15. },
  16. onLoad: function (options) {
  17. WXAPI.province().then(res => {
  18. if (res.code == 0) {
  19. this.setData({
  20. provinces: res.data
  21. })
  22. }
  23. })
  24. },
  25. provinceChange(e){
  26. const index = e.detail.value
  27. this.setData({
  28. pIndex: index
  29. })
  30. const pid = this.data.provinces[index].id
  31. WXAPI.nextRegion(pid).then(res => {
  32. console.log(res)
  33. if (res.code == 0) {
  34. this.setData({
  35. cities: res.data
  36. })
  37. }
  38. })
  39. },
  40. cityChange(e){
  41. const index = e.detail.value
  42. this.setData({
  43. cIndex: index
  44. })
  45. const pid = this.data.cities[index].id
  46. WXAPI.nextRegion(pid).then(res => {
  47. console.log(res)
  48. if (res.code == 0) {
  49. this.setData({
  50. areas: res.data
  51. })
  52. }
  53. })
  54. },
  55. areaChange(e){
  56. const index = e.detail.value
  57. this.setData({
  58. aIndex: index
  59. })
  60. const pid = this.data.areas[index].id
  61. WXAPI.nextRegion(pid).then(res => {
  62. console.log(res)
  63. if (res.code == 0) {
  64. this.setData({
  65. streets: res.data
  66. })
  67. }
  68. })
  69. },
  70. streetChange(e){
  71. const index = e.detail.value
  72. this.setData({
  73. sIndex: index
  74. })
  75. const pid = this.data.streets[index].id
  76. WXAPI.nextRegion(pid).then(res => {
  77. console.log(res)
  78. if (res.code == 0) {
  79. this.setData({
  80. communities: res.data
  81. })
  82. }
  83. })
  84. },
  85. communityChange(e){
  86. const index = e.detail.value
  87. this.setData({
  88. ccIndex: index
  89. })
  90. const pid = this.data.communities[index].id
  91. WXAPI.nextRegion(pid).then(res => {
  92. console.log(res)
  93. // 似乎没有下级了
  94. })
  95. },
  96. })

WXAPI.init(‘gooking’) 这句代码是将你的小程序链接到你的后台,其中 gooking 这个是你的专属域名(请查看前言中关于专属域名的章节说明);

apifm-wxapi 最多可实现5级数据的获取,在实际应用中已经足够了(和某宝的地址体系详细程度一致)

通常的业务场景下,一般只需要2-3级就可以,你可以根据自己的实际需要修改上述的小程序代码,够用原则,注重用户体验!

拓展:

微信自带读取收货地址的api接口,开动脑筋,试着动手去实现微信自带读取用户收货地址和本案例地址系的整合兼容,期待你的作品。

期待你的进步!
感谢!