piaoyi-cityPicker.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="pupop">
  3. <view class="popup-box" :animation="animationData">
  4. <view class="pupop-btn">
  5. <view @tap="cancel">取消</view>
  6. <view @tap="confirm" style="color: #2979ff;">确定</view>
  7. </view>
  8. <picker-view :value="value" :indicator-style="indicatorStyle" @change="bindChange" class="picker-view">
  9. <picker-view-column>
  10. <view class="item" v-for="(item,index) in provinceList" :key="index">{{item.name}}</view>
  11. </picker-view-column>
  12. <picker-view-column>
  13. <view class="item" v-for="(item,index) in cityList" :key="index">{{item.name}}</view>
  14. </picker-view-column>
  15. <picker-view-column v-if="column == 3">
  16. <view class="item" v-for="(item,index) in areaList" :key="index">{{item.name}}</view>
  17. </picker-view-column>
  18. </picker-view>
  19. </view>
  20. <view @tap="close" @touchmove.stop.prevent :class="visible ? 'pupop-model' : 'pupop-models'"></view>
  21. </view>
  22. </template>
  23. <script>
  24. import {
  25. addressList
  26. } from './cityData.js'
  27. export default {
  28. data() {
  29. return {
  30. value: [],
  31. addressList,
  32. provinceList: [],
  33. cityList: [],
  34. areaList: [],
  35. indicatorStyle: `height: 50px;`,
  36. provinceIndex: 0,
  37. cityIndex: 0,
  38. areaIndex: 0,
  39. animationData: {}
  40. }
  41. },
  42. props: {
  43. column: {
  44. type: Number,
  45. default: 3
  46. },
  47. defaultValue: {
  48. default: () => ''
  49. },
  50. visible: {
  51. type: Boolean,
  52. default: () => false
  53. },
  54. maskCloseAble: {
  55. type: Boolean,
  56. default: () => true
  57. },
  58. },
  59. watch: {
  60. visible (val) {
  61. this.changeActive()
  62. },
  63. defaultValue() {
  64. this.init()
  65. }
  66. },
  67. created() {
  68. this.init()
  69. },
  70. methods: {
  71. init () {
  72. var provinceList = []
  73. addressList.filter(item => {
  74. provinceList.push({
  75. code: item.code,
  76. name: item.name
  77. })
  78. })
  79. this.provinceList = [...provinceList]
  80. if (!this.defaultValue) {
  81. this.cityList = addressList[0].children
  82. this.areaList = addressList[0].children[0].children
  83. } else {
  84. var city = {11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "}
  85. if (Array.isArray(this.defaultValue) && this.defaultValue.length >= 2) {
  86. console.log(this.defaultValue)
  87. var province = this.defaultValue[0]
  88. var city = this.defaultValue[1]
  89. this.provinceIndex = 0
  90. this.cityIndex = 0
  91. this.areaIndex = 0
  92. this.provinceIndex = addressList.findIndex(obj => {
  93. return obj.name == province
  94. })
  95. this.provinceIndex = this.provinceIndex >= 0 ? this.provinceIndex : 0
  96. this.cityList = addressList[this.provinceIndex].children
  97. this.cityIndex = this.cityList.findIndex(obj => {
  98. return obj.name == city
  99. })
  100. this.cityIndex = this.cityIndex >= 0 ? this.cityIndex : 0
  101. this.areaList = this.cityList[this.cityIndex].children
  102. if (this.defaultValue.length > 2) {
  103. this.areaIndex = this.areaList.findIndex(obj => {
  104. return obj.name == this.defaultValue[2]
  105. })
  106. }
  107. this.areaIndex = this.areaIndex >= 0 ? this.areaIndex : 0
  108. this.$nextTick(() => {
  109. if (this.column == 3) {
  110. this.value = JSON.parse(JSON.stringify([this.provinceIndex, this.cityIndex, this.areaIndex]))
  111. } else if (this.column == 2) {
  112. this.value = JSON.parse(JSON.stringify([this.provinceIndex, this.cityIndex]))
  113. }
  114. })
  115. } else if (/^\d{6}$/.test(this.defaultValue)) {
  116. var province = this.defaultValue.substr(0, 2)
  117. var city = this.defaultValue.substr(0, 4)
  118. this.provinceIndex = 0
  119. this.cityIndex = 0
  120. this.areaIndex = 0
  121. this.provinceIndex = addressList.findIndex(obj => {
  122. return obj.code == province
  123. })
  124. this.provinceIndex = this.provinceIndex >= 0 ? this.provinceIndex : 0
  125. this.cityList = addressList[this.provinceIndex].children
  126. this.cityIndex = this.cityList.findIndex(obj => {
  127. return obj.code == city
  128. })
  129. this.cityIndex = this.cityIndex >= 0 ? this.cityIndex : 0
  130. this.areaList = this.cityList[this.cityIndex].children
  131. this.areaIndex = this.areaList.findIndex(obj => {
  132. return obj.code == this.defaultValue
  133. })
  134. this.areaIndex = this.areaIndex >= 0 ? this.areaIndex : 0
  135. this.$nextTick(() => {
  136. if (this.column == 3) {
  137. this.value = JSON.parse(JSON.stringify([this.provinceIndex, this.cityIndex, this.areaIndex]))
  138. } else if (this.column == 2) {
  139. this.value = JSON.parse(JSON.stringify([this.provinceIndex, this.cityIndex]))
  140. }
  141. })
  142. } else {
  143. uni.showToast({
  144. title: '地区编码格式不正确',
  145. icon: 'none'
  146. })
  147. console.log('地区编码格式不正确')
  148. }
  149. }
  150. this.changeActive()
  151. },
  152. confirm () {
  153. if (this.column == 3) {
  154. this.$emit('confirm', {
  155. code: addressList[this.provinceIndex].children[this.cityIndex].children[this.areaIndex].code,
  156. name: addressList[this.provinceIndex].name + addressList[this.provinceIndex].children[this.cityIndex].name + addressList[this.provinceIndex].children[this.cityIndex].children[this.areaIndex].name,
  157. provinceName: addressList[this.provinceIndex].name,
  158. cityName: addressList[this.provinceIndex].children[this.cityIndex].name,
  159. areaName: addressList[this.provinceIndex].children[this.cityIndex].children[this.areaIndex].name
  160. })
  161. } else if (this.column == 2) {
  162. this.$emit('confirm', {
  163. code: addressList[this.provinceIndex].children[this.cityIndex].children[this.areaIndex].code.substring(0, 4) + '00',
  164. name: addressList[this.provinceIndex].name + addressList[this.provinceIndex].children[this.cityIndex].name,
  165. provinceName: addressList[this.provinceIndex].name,
  166. cityName: addressList[this.provinceIndex].children[this.cityIndex].name
  167. })
  168. } else {
  169. uni.showToast({
  170. title: '目前column只能传2和3',
  171. icon: 'none'
  172. })
  173. }
  174. },
  175. cancel () {
  176. this.$emit('cancel')
  177. },
  178. // 动画
  179. changeActive () {
  180. var active = '-415px'
  181. if (this.visible) {
  182. active = 0
  183. }
  184. var animation = uni.createAnimation({
  185. duration: 400,
  186. timingFunction: 'linear'
  187. })
  188. animation.bottom(active).step()
  189. this.animationData = animation.export()
  190. },
  191. bindChange(e) {
  192. e.detail.value[0] = e.detail.value[0] || 0
  193. e.detail.value[1] = e.detail.value[1] || 0
  194. e.detail.value[2] = e.detail.value[2] || 0
  195. if (e.detail.value[0] != this.provinceIndex) {
  196. // console.log('监听第一列')
  197. this.provinceChange(e.detail.value[0])
  198. } else if (e.detail.value[1] != this.cityIndex) {
  199. // console.log('监听第二列')
  200. this.cityChange(e.detail.value[1])
  201. } else if (e.detail.value[2] != this.areaIndex) {
  202. // console.log('监听第三列')
  203. this.areaChange(e.detail.value[2])
  204. }
  205. },
  206. // 监听第一列变化
  207. provinceChange (e) {
  208. this.provinceIndex = e
  209. this.cityIndex = 0
  210. this.areaIndex = 0
  211. this.value = [...[e, 0, 0]]
  212. this.cityList = addressList[e].children
  213. this.areaList = addressList[e].children[0].children
  214. },
  215. // 监听第二列变化
  216. cityChange (e) {
  217. this.cityIndex = e
  218. this.areaIndex = 0
  219. // console.log(this.cityIndex, this.areaIndex)
  220. this.value = [...[this.provinceIndex, e, 0]]
  221. this.cityList = addressList[this.provinceIndex].children
  222. this.areaList = addressList[this.provinceIndex].children[e].children
  223. },
  224. // 监听第三列变化
  225. areaChange (e) {
  226. this.areaIndex = e
  227. },
  228. // 点击模态框
  229. close () {
  230. if (this.maskCloseAble) {
  231. this.cancel()
  232. }
  233. }
  234. }
  235. }
  236. </script>
  237. <style scoped lang="scss">
  238. .pupop {
  239. .popup-box {
  240. position: fixed;
  241. left: 0;
  242. bottom: -315px;
  243. z-index: 99999;
  244. background: #fff;
  245. padding-bottom: 50px;
  246. .pupop-btn{
  247. height: 40px;
  248. display: flex;
  249. align-items: center;
  250. padding: 0 30upx;
  251. justify-content: space-between;
  252. }
  253. }
  254. .pupop-model {
  255. position: fixed;
  256. left: 0;
  257. top: 0;
  258. width: 100%;
  259. height: 100%;
  260. z-index: 9999;
  261. background: rgba(0, 0, 0, .5);
  262. }
  263. .pupop-models{
  264. display: none;
  265. }
  266. .picker-view {
  267. width: 750rpx;
  268. height: 225px;
  269. margin-top: 20rpx;
  270. }
  271. .item {
  272. height: 50px;
  273. align-items: center;
  274. justify-content: center;
  275. text-align: center;
  276. line-height: 50px;
  277. }
  278. }
  279. </style>