city.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // components/city/city.js
  2. const cityData = require('../../data/city').provinceObj;
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. // 省编码
  9. provinceCode: {
  10. type: String,
  11. value: '320000',
  12. },
  13. // 市编码
  14. cityCode: {
  15. type: String,
  16. value: '320200'
  17. },
  18. // 区编码
  19. countryCode: {
  20. type: String,
  21. value: '320214'
  22. }
  23. },
  24. /**
  25. * 组件的初始数据
  26. */
  27. data: {
  28. // 城市列表
  29. cityList: [],
  30. // 地区列表
  31. areaList: [],
  32. // 省名
  33. provinceName: '',
  34. // 市名
  35. cityName: '',
  36. // 地区名
  37. countryName: '',
  38. cityData: []
  39. },
  40. ready() {
  41. // 通过传进来的省市区编码 动态渲染列表
  42. cityData.forEach((value, index) => {
  43. value.cityList.forEach(val => {
  44. if (val.areaList) {
  45. val.areaList.unshift({
  46. "countryCode": "0",
  47. "countryName": "所有地区",
  48. })
  49. } else {
  50. val.areaList = [
  51. {
  52. "countryCode": "0",
  53. "countryName": "所有地区",
  54. }
  55. ]
  56. }
  57. })
  58. });
  59. this.setData({
  60. cityData
  61. });
  62. this.init(cityData)
  63. },
  64. /**
  65. * 组件的方法列表
  66. */
  67. methods: {
  68. init(cityData) {
  69. const that = this;
  70. // 获取城市列表 并设置默认值
  71. for (let index = 0; index < cityData.length; index++) {
  72. const element = cityData[index];
  73. const provinceCode = that.data.provinceCode || '320000'
  74. if (element.provinceCode === provinceCode) {
  75. const cityList = element.cityList;
  76. that.setData({
  77. cityList,
  78. provinceName: element.provinceName,
  79. provinceCode
  80. })
  81. // 获取区列表 并设置默认值
  82. for (let key = 0; key < cityList.length; key++) {
  83. const element = cityList[key];
  84. const cityCode = that.data.cityCode || '320200'
  85. if (element.cityCode === cityCode) {
  86. const areaList = element.areaList;
  87. that.setData({
  88. areaList,
  89. cityName: element.cityName,
  90. cityCode
  91. })
  92. // 获取区名默认值
  93. for (let i = 0; i < areaList.length; i++) {
  94. const element = areaList[i];
  95. const countryCode = that.data.countryCode || '320214'
  96. if (element.countryCode === countryCode) {
  97. that.setData({
  98. countryName: element.countryName,
  99. countryCode
  100. })
  101. break;
  102. }
  103. }
  104. break;
  105. }
  106. }
  107. break;
  108. }
  109. }
  110. },
  111. // 选择城市并且设置市列表与区列表 同时设置其他默认值
  112. chooseProvince(e) {
  113. const { index } = e.currentTarget.dataset;
  114. const { cityData } = this.data;
  115. this.setData({
  116. cityList: cityData[index].cityList,
  117. provinceCode: cityData[index].provinceCode,
  118. provinceName: cityData[index].provinceName,
  119. areaList: cityData[index].cityList[0].areaList,
  120. cityCode: cityData[index].cityList[0].cityCode,
  121. cityName: cityData[index].cityList[0].cityName,
  122. countryCode: cityData[index].cityList[0].areaList[0].countryCode,
  123. countryName: cityData[index].cityList[0].areaList[0].countryName
  124. })
  125. },
  126. // 选择城市并设置去列表 同时设置其他默认值
  127. chooseCity(e) {
  128. const { index } = e.currentTarget.dataset;
  129. const { cityList } = this.data;
  130. this.setData({
  131. areaList: cityList[index].areaList,
  132. cityCode: cityList[index].cityCode,
  133. cityName: cityList[index].cityName,
  134. countryCode: cityList[index].areaList[0].countryCode,
  135. countryName: cityList[index].areaList[0].countryName,
  136. })
  137. },
  138. // 选择区 并设置区名
  139. chooseArea(e) {
  140. const { index } = e.currentTarget.dataset;
  141. const { areaList } = this.data;
  142. this.setData({
  143. countryCode: areaList[index].countryCode,
  144. countryName: areaList[index].countryName
  145. })
  146. },
  147. // 提交获取的值
  148. commit() {
  149. this.triggerEvent('commit', { region: { code: [this.data.provinceCode, this.data.cityCode, this.data.countryCode], value: [this.data.provinceName, this.data.cityName, this.data.countryName] } })
  150. }
  151. }
  152. })