经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序实现三级联动代码
来源:cnblogs  作者:不会敲代码的老王  时间:2019/4/24 10:01:56  对本文有异议

项目中经常遇到要选择城市。用到三级联动的方式

  • 微信小程序的 picker 组件 mode=date 是三级联动的,但是无法自定义,这让我们心痛不已,值得我们欣慰的 picker-view 组件是可以自定义添加多个选项,但还是无法联动。既然这样那就自己写一个联动。
  • 做到如下图所示:
  • 分为动态获取地址
  • 引用静态文件获取地址

  • addressAdd.wxml
  1. <view class="add-address">
  2. <view class="add-form">
  3. <view class="form-item">
  4. <input class="input" bindinput="bindinputName" placeholder="姓名" value="{{address.name}}" />
  5. </view>
  6. <view class="form-item">
  7. <input class="input" bindinput="bindinputMobile" value="{{address.mobile}}" placeholder="手机号码" />
  8. </view>
  9. <view class="form-item">
  10. <input class="input" bindinput="bindinputAddress" value="{{address.address}}" placeholder="详细地址" />
  11. </view>
  12. <view class="form-item" bindtap='select'>
  13. <view class="weui-cell__bd">
  14. {{areaInfo}}
  15. </view>
  16. </view>
  17. <view class="form-default">
  18. <text bindtap="bindIsDefault" class="default-input {{address.isDefault == 1 ? 'selected' : ''}}">设为默认地址</text>
  19. </view>
  20. </view>
  21. <view class="btns">
  22. <button class="cannel" bindtap="cancelAddress">取消</button>
  23. <button class="save" bindtap="saveAddress">保存</button>
  24. </view>
  25. </view>
  26. <view class="bg-mask" bindtap="cancelSelectRegion" wx:if="{{openSelectRegion}}"></view>
  27. <view class="picker-view" animation="{{animationAddressMenu}}" style="visibility:{{addressMenuIsShow ? 'visible':'hidden'}}">
  28. <!-- 确认取消按钮 -->
  29. <view class='btn'>
  30. <text catchtap="cityCancel">取消</text>
  31. <text style="float: right" catchtap="citySure">确定</text>
  32. </view>
  33. <!-- 选择地址 -->
  34. <picker-view class='cont' bindchange="cityChange" value="{{value}}" wx:key="">
  35. <!-- 省 -->
  36. <picker-view-column>
  37. <view wx:for="{{provinces}}" class="picker-item" wx:key="{{index}}">{{item.area}}</view>
  38. </picker-view-column>
  39. <!-- 市 -->
  40. <picker-view-column>
  41. <view wx:for="{{citys}}" class="picker-item" wx:key="index">{{item.area}}</view>
  42. </picker-view-column>
  43. <!-- 区 -->
  44. <picker-view-column>
  45. <view wx:for="{{areas}}" class="picker-item" wx:key="index">{{item.area}}</view>
  46. </picker-view-column>
  47. </picker-view>
  48. </view>
  • addressAdd.wxss
  1. page{
  2. height: 100%;
  3. background: #f4f4f4;
  4. }
  5. .add-address .add-form{
  6. background: #fff;
  7. width: 100%;
  8. height: auto;
  9. overflow: hidden;
  10. }
  11. .add-address .form-item{
  12. height: 116rpx;
  13. padding-left: 31.25rpx;
  14. border-bottom: 1px solid #d9d9d9;
  15. display: flex;
  16. align-items: center;
  17. padding-right: 31.25rpx;
  18. }
  19. .add-address .input{
  20. flex: 1;
  21. height: 44rpx;
  22. line-height: 44rpx;
  23. overflow: hidden;
  24. }
  25. .add-address .form-default{
  26. border-bottom: 1px solid #d9d9d9;
  27. height: 96rpx;
  28. background: #fafafa;
  29. padding-top: 28rpx;
  30. font-size: 28rpx;
  31. }
  32. .default-input{
  33. margin: 0 auto;
  34. display: block;
  35. width: 240rpx;
  36. height: 40rpx;
  37. padding-left: 50rpx;
  38. line-height: 40rpx;
  39. background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/sprites/checkbox-sed825af9d3-a6b8540d42.png) 1rpx -448rpx no-repeat;
  40. background-size: 38rpx 486rpx;
  41. font-size: 28rpx;
  42. }
  43. .default-input.selected{
  44. background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/sprites/checkbox-sed825af9d3-a6b8540d42.png) 0 -192rpx no-repeat;
  45. background-size: 38rpx 486rpx;
  46. }
  47. .add-address .btns{
  48. position: fixed;
  49. bottom: 0;
  50. left: 0;
  51. overflow: hidden;
  52. display: flex;
  53. height: 100rpx;
  54. width: 100%;
  55. }
  56. .add-address .cannel,.add-address .save{
  57. flex: 1;
  58. height: 100rpx;
  59. text-align: center;
  60. line-height: 100rpx;
  61. font-size: 28rpx;
  62. color: #fff;
  63. border:none;
  64. border-radius: 0;
  65. }
  66. .add-address .cannel{
  67. background: #3F3F3F;
  68. }
  69. .add-address .save{
  70. background: #a78845;
  71. }
  72. .region-select{
  73. width: 100%;
  74. height: 600rpx;
  75. background: #fff;
  76. position: fixed;
  77. z-index: 10;
  78. left:0;
  79. bottom: 0;
  80. }
  81. .region-select .hd{
  82. height: 108rpx;
  83. width: 100%;
  84. border-bottom: 1px solid #f4f4f4;
  85. padding: 46rpx 30rpx 0 30rpx;
  86. }
  87. .region-select .region-selected{
  88. float: left;
  89. height: 60rpx;
  90. display: flex;
  91. }
  92. .region-select .region-selected .item{
  93. max-width: 140rpx;
  94. margin-right: 30rpx;
  95. text-align: left;
  96. line-height: 60rpx;
  97. height: 100%;
  98. color: #333;
  99. font-size: 28rpx;
  100. overflow: hidden;
  101. text-overflow: ellipsis;
  102. white-space: nowrap;
  103. }
  104. .region-select .region-selected .item.disabled{
  105. color: #999;
  106. }
  107. .region-select .region-selected .item.selected{
  108. color: #a78845;
  109. }
  110. .region-select .done{
  111. float: right;
  112. height: 60rpx;
  113. width: 60rpx;
  114. border: none;
  115. background: #fff;
  116. line-height: 60rpx;
  117. text-align: center;
  118. color: #333;
  119. font-size: 28rpx;
  120. }
  121. .region-select .done.disabled{
  122. color: #999;
  123. }
  124. .region-select .bd{
  125. height: 492rpx;
  126. width: 100%;
  127. padding: 0 30rpx;
  128. }
  129. .region-select .region-list{
  130. height: 492rpx;
  131. }
  132. .region-select .region-list .item{
  133. width: 100%;
  134. height: 104rpx;
  135. line-height: 104rpx;
  136. text-align: left;
  137. color: #333;
  138. font-size: 28rpx;
  139. }
  140. .region-select .region-list .item.selected{
  141. color: #b4282d;
  142. }
  143. .bg-mask{
  144. height: 100%;
  145. width: 100%;
  146. background: rgba(0, 0, 0, 0.4);
  147. position: fixed;
  148. top:0;
  149. left:0;
  150. z-index: 8;
  151. }
  152. .picker-view {
  153. width: 100%;
  154. display: flex;
  155. z-index:12;
  156. background-color: #fff;
  157. /* background: rgba(0, 0, 0, .2); */
  158. flex-direction: column;
  159. justify-content: center;
  160. align-items: center;
  161. position: fixed;
  162. bottom: 0;
  163. left: 0rpx;
  164. height: 40vh;
  165. }
  166. .btn {
  167. width: 100%;
  168. height: 90rpx;
  169. padding: 0 24rpx;
  170. box-sizing: border-box;
  171. line-height: 90rpx;
  172. text-align: center;
  173. display: flex;
  174. background: rgba(255,255,255,.8);
  175. justify-content: space-between;
  176. }
  177. .cont {
  178. width: 100%;
  179. height: 389rpx;
  180. }
  181. .picker-item {
  182. line-height: 70rpx;
  183. margin-left: 5rpx;
  184. margin-right: 5rpx;
  185. text-align: center;
  186. }
  187. .address {
  188. width: 100%;
  189. height: 90rpx;
  190. line-height: 90rpx;
  191. text-align: center;
  192. border-bottom: 1rpx solid #f1f1f1;
  193. }
  • addressAdd.js (分两个版本一个是动态获取的 就是选择的时候动态向后台获取内容 下方是动态获取的例子:)
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var app = getApp();
  4. Page({
  5. data: {
  6. addressId: 0,
  7. openSelectRegion: false,
  8. regionType: 1,
  9. selectRegionDone: false,
  10. szxqList: [],
  11. szxq: {
  12. id: "",
  13. name: "请选择小区"
  14. },
  15. szdsList: [],
  16. szds: {
  17. id: "",
  18. name: ""
  19. },
  20. fanghao: "",
  21. animationAddressMenu: {},
  22. addressMenuIsShow: false,
  23. value: [0, 0, 0],
  24. provinces: [],
  25. citys: [],
  26. areas: [],
  27. areaInfo: '',
  28. areaJson: {}
  29. },
  30. bindinputMobile(event) {
  31. let address = this.data.address;
  32. address.mobile = event.detail.value;
  33. this.setData({
  34. address: address
  35. });
  36. },
  37. bindinputName(event) {
  38. let address = this.data.address;
  39. address.name = event.detail.value;
  40. this.setData({
  41. address: address
  42. });
  43. },
  44. bindinputAddress(event) {
  45. let address = this.data.address;
  46. address.address = event.detail.value;
  47. this.setData({
  48. address: address
  49. });
  50. },
  51. bindIsDefault() {
  52. let address = this.data.address;
  53. address.isDefault = !address.isDefault;
  54. this.setData({
  55. address: address
  56. });
  57. },
  58. getAddressDetail() {
  59. let that = this;
  60. // util.request(api.AddressDetail, {
  61. // id: that.data.addressId
  62. // }).then(function(res) {
  63. // if (res.errno === 0) {
  64. // if (res.data) {
  65. // that.setData({
  66. // address: res.data
  67. // });
  68. // }
  69. // }
  70. // });
  71. },
  72. wxChooseAddress() {
  73. let that = this;
  74. let address = this.data.address;
  75. // 用户已经同意小程序使用地址功能
  76. wx.chooseAddress({
  77. success: function(res) {
  78. address.provinceId = 99999;
  79. address.cityId = 88888;
  80. address.areaId = 77777;
  81. address.name = res.userName;
  82. address.mobile = res.telNumber;
  83. address.provinceName = res.provinceName;
  84. address.cityName = res.cityName;
  85. address.areaName = res.countyName;
  86. address.address = res.provinceName + res.cityName + res.countyName + res.detailInfo;
  87. that.setData({
  88. address: address,
  89. });
  90. }
  91. });
  92. },
  93. wxAddress() {
  94. let that = this;
  95. // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.address" 这个 scope
  96. wx.getSetting({
  97. success(res) {
  98. if (!res.authSetting['scope.address']) {
  99. wx.authorize({
  100. scope: 'scope.address',
  101. success() {
  102. that.wxChooseAddress();
  103. }
  104. })
  105. } else {
  106. that.wxChooseAddress();
  107. }
  108. }
  109. })
  110. },
  111. onLoad: function(options) {
  112. let that = this;
  113. // 页面初始化 options为页面跳转所带来的参数
  114. console.log(options);
  115. if (options.id && options.id != 0) {
  116. this.setData({
  117. addressId: options.id
  118. });
  119. this.getAddressDetail();
  120. } else {
  121. that.wxAddress();
  122. }
  123. },
  124. onReady: function() {
  125. },
  126. cancelAddress() {
  127. wx.navigateBack();
  128. },
  129. saveAddress() {
  130. console.log(this.data.address);
  131. let address = this.data.address;
  132. if (address.name == '') {
  133. util.showErrorToast('请输入姓名');
  134. return false;
  135. }
  136. if (address.mobile == '') {
  137. util.showErrorToast('请输入手机号码');
  138. return false;
  139. }
  140. if (address.areaId == 0) {
  141. util.showErrorToast('请输入省市区');
  142. return false;
  143. }
  144. if (address.address == '') {
  145. util.showErrorToast('请输入详细地址');
  146. return false;
  147. }
  148. let that = this;
  149. },
  150. onShow: function() {
  151. // 获取所在栋数
  152. var animation = wx.createAnimation({
  153. duration: 500,
  154. timingFunction: 'linear',
  155. })
  156. this.animation = animation
  157. const that = this
  158. // 获取所在地区
  159. console.log()
  160. util.getAreaReq().then(provinces => {
  161. util.getAreaReq(provinces[0].code).then(citys => {
  162. util.getAreaReq(citys[0].code).then(areas => {
  163. that.setData({
  164. provinces: provinces,
  165. citys: citys,
  166. areas: areas,
  167. areaJson: {
  168. provinces: {
  169. id: 40,
  170. name: "广东省"
  171. },
  172. citys: {
  173. id: 4006,
  174. name: "河源市"
  175. },
  176. areas: {
  177. id: 400602,
  178. name: "源城区"
  179. }
  180. }
  181. })
  182. var areas = that.data.areaJson.areas.name == null ? "" : that.data.areaJson.areas.name
  183. var areaInfo = that.data.areaJson.provinces.name + '·' + that.data.areaJson.citys.name + '·' + areas
  184. that.setData({
  185. areaInfo: areaInfo,
  186. })
  187. })
  188. })
  189. })
  190. },
  191. // 点击所在地区弹出选择框
  192. select: function(e) {
  193. // 如果已经显示,不在执行显示动画
  194. if (this.data.addressMenuIsShow) {
  195. return false
  196. } else {
  197. // 执行显示动画
  198. this.startAddressAnimation(true)
  199. }
  200. },
  201. // 处理省市县联动逻辑
  202. cityChange: function(e) {
  203. // console.log(this.data.provinces)
  204. var value = e.detail.value
  205. var provinces = this.data.provinces
  206. var citys = this.data.citys
  207. var areas = this.data.areas
  208. var provinceNum = value[0]
  209. var cityNum = value[1]
  210. var countyNum = value[2]
  211. var that = this;
  212. // console.log(provinces)
  213. // 如果省份选择项和之前不一样,表示滑动了省份,此时市默认是省的第一组数据,
  214. if (this.data.value[0] != provinceNum) {
  215. var id = provinces[provinceNum].id
  216. // console.log(citys[cityNum])
  217. util.getAreaReq(provinces[provinceNum].code).then(citys => {
  218. util.getAreaReq(citys[0].code).then(areas => {
  219. this.setData({
  220. value: [provinceNum, 0, 0],
  221. citys: citys,
  222. areas: areas,
  223. areaJson: {
  224. provinces: {
  225. id: provinces[provinceNum].code,
  226. name: provinces[provinceNum].area
  227. },
  228. citys: {
  229. id: citys[0].code,
  230. name: citys[0].area
  231. },
  232. areas: {
  233. id: areas.length > 0 ? areas[0].code : null,
  234. name: areas.length > 0 ? areas[0].area : null,
  235. }
  236. }
  237. })
  238. })
  239. })
  240. } else if (this.data.value[1] != cityNum) {
  241. // 滑动选择了第二项数据,即市,此时区显示省市对应的第一组数据
  242. var id = citys[cityNum].id
  243. util.getAreaReq(citys[cityNum].code).then(areas => {
  244. this.setData({
  245. value: [provinceNum, cityNum, 0],
  246. areas: areas,
  247. areaJson: {
  248. provinces: {
  249. id: provinces[provinceNum].code,
  250. name: provinces[provinceNum].area
  251. },
  252. citys: {
  253. id: citys[cityNum].code,
  254. name: citys[cityNum].area
  255. },
  256. areas: {
  257. id: areas.length > 0 ? areas[0].code : null,
  258. name: areas.length > 0 ? areas[0].area : null,
  259. }
  260. }
  261. })
  262. })
  263. } else {
  264. // 滑动选择了区
  265. this.setData({
  266. value: [provinceNum, cityNum, countyNum],
  267. areaJson: {
  268. provinces: {
  269. id: provinces[provinceNum].code,
  270. name: provinces[provinceNum].area
  271. },
  272. citys: {
  273. id: citys[cityNum].code,
  274. name: citys[cityNum].area
  275. },
  276. areas: {
  277. id: areas[countyNum].code,
  278. name: areas[countyNum].area
  279. }
  280. }
  281. })
  282. // console.log(that.data.areaJson)
  283. }
  284. },
  285. // 执行动画
  286. startAddressAnimation: function(isShow) {
  287. if (isShow) {
  288. // vh是用来表示尺寸的单位,高度全屏是100vh
  289. this.animation.translateY(0 + 'vh').step()
  290. } else {
  291. this.animation.translateY(40 + 'vh').step()
  292. }
  293. this.setData({
  294. animationAddressMenu: this.animation.export(),
  295. addressMenuIsShow: isShow,
  296. })
  297. },
  298. // 点击地区选择取消按钮
  299. cityCancel: function(e) {
  300. this.startAddressAnimation(false)
  301. },
  302. // 点击地区选择确定按钮
  303. citySure: function(e) {
  304. var that = this
  305. var city = that.data.city
  306. var value = that.data.value
  307. this.startAddressAnimation(false)
  308. // console.log(that.data.areaJson)
  309. var areas = that.data.areaJson.areas.name == null ? "" : that.data.areaJson.areas.name
  310. // 将选择的城市信息显示到输入框
  311. var areaInfo = that.data.areaJson.provinces.name + '·' + that.data.areaJson.citys.name + '·' + areas
  312. that.setData({
  313. areaInfo: areaInfo,
  314. })
  315. },
  316. onHide: function() {
  317. // 页面隐藏
  318. },
  319. onUnload: function() {
  320. // 页面关闭
  321. }
  322. });
  • 需要使用外部js(utils) 自己封装的一个工具
  1. var api = require('../config/api.js');
  2. var app = getApp();
  3. var user = require('./user.js');
  4. /**
  5. * 封装微信的的request
  6. */
  7. function request(url, data = {}, method = "GET") {
  8. return new Promise(function(resolve, reject) {
  9. user.checkLogin().then(res => {
  10. }).catch(() => {
  11. wx.switchTab({
  12. url: '/pages/ucenter/index/index?show=true'
  13. });
  14. });
  15. wx.request({
  16. url: url,
  17. data: data,
  18. method: method,
  19. header: {
  20. 'Content-Type': 'application/x-www-form-urlencoded',
  21. 'Cookie': "token=" + wx.getStorageSync('token') + ";" + wx.getStorageSync('sessionid'),
  22. 'X-Requested-With': "XMLHttpRequest"
  23. },
  24. success: function(res) {
  25. if (res.statusCode == 400) {
  26. user.loginByWeixin().then(res => {
  27. app.globalData.hasLogin = true;
  28. });
  29. wx.redirectTo({
  30. url: '/pages/index/index'
  31. });
  32. wx.showToast({
  33. title: '已经重新登录',
  34. })
  35. }
  36. if (res.header["Set-Cookie"]) {
  37. wx.setStorageSync("sessionid", res.header["Set-Cookie"])
  38. }
  39. if (res.statusCode == 200) {
  40. if (res.data.errno == 501) {
  41. } else {
  42. resolve(res);
  43. }
  44. } else {
  45. reject(res);
  46. }
  47. },
  48. fail: function(err) {
  49. reject(err)
  50. }
  51. })
  52. });
  53. }
  54. function getAreaReq(id) {
  55. const that = this;
  56. return new Promise(function(resolve, reject) {
  57. that.request("****", JSON.stringify({
  58. }), "post").then(response => {
  59. console.log(response.data.rs_data)
  60. resolve(response.data.rs_data);
  61. })
  62. })
  63. }
  64. module.exports = {
  65. request,
  66. getAreaReq
  67. };
  • 使用静态获取的时候。js如下
  1. var util = require('../../../utils/util.js');
  2. var api = require('../../../config/api.js');
  3. var area = require('../../../config/area.js');
  4. var app = getApp();
  5. Page({
  6. data: {
  7. address: {
  8. id: 0,
  9. provinceId: 0,
  10. cityId: 0,
  11. areaId: 0,
  12. address: '',
  13. name: '',
  14. mobile: '',
  15. isDefault: 0,
  16. provinceName: '',
  17. cityName: '',
  18. areaName: ''
  19. },
  20. addressId: 0,
  21. openSelectRegion: false,
  22. regionType: 1,
  23. selectRegionDone: false,
  24. szxqList: [],
  25. szxq: {
  26. id: "",
  27. name: "请选择小区"
  28. },
  29. szdsList: [],
  30. szds: {
  31. id: "",
  32. name: ""
  33. },
  34. fanghao: "",
  35. animationAddressMenu: {},
  36. addressMenuIsShow: false,
  37. value: [0, 0, 0],
  38. provinces: [],
  39. citys: [],
  40. areas: [],
  41. areaInfo: '',
  42. areaJson: {}
  43. },
  44. bindinputMobile(event) {
  45. let address = this.data.address;
  46. address.mobile = event.detail.value;
  47. this.setData({
  48. address: address
  49. });
  50. },
  51. bindinputName(event) {
  52. let address = this.data.address;
  53. address.name = event.detail.value;
  54. this.setData({
  55. address: address
  56. });
  57. },
  58. bindinputAddress(event) {
  59. let address = this.data.address;
  60. address.address = event.detail.value;
  61. this.setData({
  62. address: address
  63. });
  64. },
  65. bindIsDefault() {
  66. let address = this.data.address;
  67. address.isDefault = !address.isDefault;
  68. this.setData({
  69. address: address
  70. });
  71. },
  72. getAddressDetail() {
  73. let that = this;
  74. util.request(api.AddressDetail, {
  75. id: that.data.addressId
  76. }).then(function(res) {
  77. if (res.errno === 0) {
  78. if (res.data) {
  79. that.setData({
  80. address: res.data
  81. });
  82. }
  83. }
  84. });
  85. },
  86. wxChooseAddress() {
  87. let that = this;
  88. let address = this.data.address;
  89. // 用户已经同意小程序使用地址功能
  90. wx.chooseAddress({
  91. success: function(res) {
  92. address.provinceId = 99999;
  93. address.cityId = 88888;
  94. address.areaId = 77777;
  95. address.name = res.userName;
  96. address.mobile = res.telNumber;
  97. address.provinceName = res.provinceName;
  98. address.cityName = res.cityName;
  99. address.areaName = res.countyName;
  100. address.address = res.provinceName + res.cityName + res.countyName + res.detailInfo;
  101. that.setData({
  102. address: address,
  103. });
  104. }
  105. });
  106. },
  107. wxAddress() {
  108. let that = this;
  109. // 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.address" 这个 scope
  110. wx.getSetting({
  111. success(res) {
  112. if (!res.authSetting['scope.address']) {
  113. wx.authorize({
  114. scope: 'scope.address',
  115. success() {
  116. that.wxChooseAddress();
  117. }
  118. })
  119. } else {
  120. that.wxChooseAddress();
  121. }
  122. }
  123. })
  124. },
  125. onLoad: function(options) {
  126. let that = this;
  127. // 页面初始化 options为页面跳转所带来的参数
  128. console.log(options);
  129. if (options.id && options.id != 0) {
  130. this.setData({
  131. addressId: options.id
  132. });
  133. this.getAddressDetail();
  134. } else {
  135. that.wxAddress();
  136. }
  137. },
  138. onReady: function() {
  139. },
  140. cancelAddress() {
  141. wx.navigateBack();
  142. },
  143. saveAddress() {
  144. console.log(this.data.address);
  145. let address = this.data.address;
  146. if (address.name == '') {
  147. util.showErrorToast('请输入姓名');
  148. return false;
  149. }
  150. if (address.mobile == '') {
  151. util.showErrorToast('请输入手机号码');
  152. return false;
  153. }
  154. if (address.areaId == 0) {
  155. util.showErrorToast('请输入省市区');
  156. return false;
  157. }
  158. if (address.address == '') {
  159. util.showErrorToast('请输入详细地址');
  160. return false;
  161. }
  162. if (!check.isValidPhone(address.mobile)) {
  163. util.showErrorToast('手机号不正确');
  164. return false;
  165. }
  166. },
  167. onShow: function() {
  168. // 获取所在栋数
  169. var animation = wx.createAnimation({
  170. duration: 500,
  171. timingFunction: 'linear',
  172. })
  173. this.animation = animation
  174. util.request("https://www.xaibox.com/czbb/interface/dataInfo.php", JSON.stringify({
  175. "param_key": {
  176. "info_mode": "getcity_jd"
  177. },
  178. "secret_key": "047709aaa7df22205d818bf4c1707458"
  179. }), "post").then(response => {
  180. console.log(response)
  181. that.setData({
  182. szxqList: response.data.rs_data
  183. })
  184. that.setData({
  185. szxq: response.data.data[0]
  186. })
  187. that.setData({
  188. szds: response.data.data[0]['buildingList']['0']
  189. })
  190. that.setData({
  191. szdsList: response.data.data[0]['buildingList']
  192. })
  193. })
  194. // 获取所在地区
  195. that.setData({
  196. provinces: areajs,
  197. citys: areajs[0].children,
  198. areas: areajs[0].children ? areajs[0].children[0].children : [],
  199. areaJson: {
  200. provinces: {
  201. id: 40,
  202. name: "广东省"
  203. },
  204. citys: {
  205. id: 4006,
  206. name: "河源市"
  207. },
  208. areas: {
  209. id: 400602,
  210. name: "源城区"
  211. }
  212. }
  213. })
  214. var areas = that.data.areaJson.areas.name == null ? "" : that.data.areaJson.areas.name
  215. var areaInfo = that.data.areaJson.provinces.name + '·' + that.data.areaJson.citys.name + '·' + areas
  216. that.setData({
  217. areaInfo: areaInfo,
  218. })
  219. },
  220. // 点击所在地区弹出选择框
  221. select: function(e) {
  222. // 如果已经显示,不在执行显示动画
  223. if (this.data.addressMenuIsShow) {
  224. return false
  225. } else {
  226. // 执行显示动画
  227. this.startAddressAnimation(true)
  228. }
  229. },
  230. // 处理省市县联动逻辑
  231. cityChange: function(e) {
  232. // console.log(this.data.provinces)
  233. var value = e.detail.value
  234. var provinces = this.data.provinces
  235. var citys = this.data.citys
  236. var areas = this.data.areas
  237. var provinceNum = value[0]
  238. var cityNum = value[1]
  239. var countyNum = value[2]
  240. var that = this;
  241. // console.log(provinces)
  242. // 如果省份选择项和之前不一样,表示滑动了省份,此时市默认是省的第一组数据,
  243. if (this.data.value[0] != provinceNum) {
  244. var id = provinces[provinceNum].id
  245. // console.log(citys[cityNum])
  246. this.setData({
  247. value: [provinceNum, 0, 0],
  248. citys: provinces[provinceNum].children,
  249. areas: provinces[provinceNum].children ? provinces[provinceNum].children[0].children : [],
  250. areaJson: {
  251. provinces: {
  252. id: provinces[provinceNum].code,
  253. name: provinces[provinceNum].area
  254. },
  255. citys: {
  256. id: provinces[provinceNum].children[0].code,
  257. name: provinces[provinceNum].children[0].area
  258. },
  259. areas: {
  260. id: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].code : null,
  261. name: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].area : null
  262. }
  263. }
  264. })
  265. } else if (this.data.value[1] != cityNum) {
  266. // 滑动选择了第二项数据,即市,此时区显示省市对应的第一组数据
  267. var id = citys[cityNum].id
  268. this.setData({
  269. value: [provinceNum, cityNum, 0],
  270. areas: citys[cityNum].children,
  271. areaJson: {
  272. provinces: {
  273. id: provinces[provinceNum].code,
  274. name: provinces[provinceNum].area
  275. },
  276. citys: {
  277. id: citys[cityNum].code,
  278. name: citys[cityNum].area
  279. },
  280. areas: {
  281. id: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].code : null,
  282. name: citys[cityNum].children.length > 0 ? citys[cityNum].children[0].area : null
  283. }
  284. }
  285. })
  286. } else {
  287. // 滑动选择了区
  288. this.setData({
  289. value: [provinceNum, cityNum, countyNum],
  290. areaJson: {
  291. provinces: {
  292. id: provinces[provinceNum].code,
  293. name: provinces[provinceNum].area
  294. },
  295. citys: {
  296. id: citys[cityNum].code,
  297. name: citys[cityNum].area
  298. },
  299. areas: {
  300. id: areas[countyNum].code,
  301. name: areas[countyNum].area
  302. }
  303. }
  304. })
  305. // console.log(that.data.areaJson)
  306. }
  307. },
  308. // 执行动画
  309. startAddressAnimation: function(isShow) {
  310. if (isShow) {
  311. // vh是用来表示尺寸的单位,高度全屏是100vh
  312. this.animation.translateY(0 + 'vh').step()
  313. } else {
  314. this.animation.translateY(40 + 'vh').step()
  315. }
  316. this.setData({
  317. animationAddressMenu: this.animation.export(),
  318. addressMenuIsShow: isShow,
  319. })
  320. },
  321. // 点击地区选择取消按钮
  322. cityCancel: function(e) {
  323. this.startAddressAnimation(false)
  324. },
  325. // 点击地区选择确定按钮
  326. citySure: function(e) {
  327. var that = this
  328. var city = that.data.city
  329. var value = that.data.value
  330. this.startAddressAnimation(false)
  331. // console.log(that.data.areaJson)
  332. var areas = that.data.areaJson.areas.name == null ? "" : that.data.areaJson.areas.name
  333. // 将选择的城市信息显示到输入框
  334. var areaInfo = that.data.areaJson.provinces.name + '·' + that.data.areaJson.citys.name + '·' + areas
  335. that.setData({
  336. areaInfo: areaInfo,
  337. })
  338. },
  339. onHide: function() {
  340. // 页面隐藏
  341. },
  342. onUnload: function() {
  343. // 页面关闭
  344. }
  345. });
  • 静态获取三级联动 的话则需要文件area.js点击下载

原文链接:http://www.cnblogs.com/rolandlee/p/10756542.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号