address.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <template>
  2. <view class="simple-address" v-if="showPopup">
  3. <!-- 遮罩层 -->
  4. <view
  5. class="simple-address-mask"
  6. v-if="maskClick"
  7. :class="[ani + '-mask', animation ? 'mask-ani' : '']"
  8. :style="{
  9. 'background-color': maskBgColor
  10. }"
  11. @tap="hideMask(true)"
  12. ></view>
  13. <!-- 省市区 -->
  14. <view class="simple-address-content simple-address--fixed" :class="[type, ani + '-content', animation ? 'content-ani' : '']">
  15. <view class="simple-address__header">
  16. <view class="simple-address__header-btn-box" @click="pickerCancel">
  17. <text class="simple-address__header-text" :style="{ color: cancelColor, fontSize: btnFontSize }">取消</text>
  18. </view>
  19. <view class="simple-address__header-btn-box" @click="pickerConfirm">
  20. <text class="simple-address__header-text" :style="{ color: confirmColor, fontSize: btnFontSize }">确定</text>
  21. </view>
  22. </view>
  23. <view class="simple-address__box">
  24. <picker-view class="simple-address-view" :value="pickerValue" @change="pickerChange">
  25. <picker-view-column>
  26. <!-- #ifndef APP-NVUE -->
  27. <view class="picker-item" :style="{ fontSize: fontSize }" v-for="(item, index) in provinceDataList" :key="index">{{ item.name }}</view>
  28. <!-- #endif -->
  29. <!-- #ifdef APP-NVUE -->
  30. <text class="picker-item" :style="{ fontSize: fontSize }" v-for="(item, index) in provinceDataList" :key="index">{{ item.name }}</text>
  31. <!-- #endif -->
  32. </picker-view-column>
  33. <picker-view-column>
  34. <!-- #ifndef APP-NVUE -->
  35. <view class="picker-item" :style="{ fontSize: fontSize }" v-for="(item, index) in cityDataList" :key="index">{{ item.name }}</view>
  36. <!-- #endif -->
  37. <!-- #ifdef APP-NVUE -->
  38. <text class="picker-item" :style="{ fontSize: fontSize }" v-for="(item, index) in cityDataList" :key="index">{{ item.name }}</text>
  39. <!-- #endif -->
  40. </picker-view-column>
  41. <picker-view-column>
  42. <!-- #ifndef APP-NVUE -->
  43. <view class="picker-item" :style="{ fontSize: fontSize }" v-for="(item, index) in areaDataList" :key="index">{{ item.name }}</view>
  44. <!-- #endif -->
  45. <!-- #ifdef APP-NVUE -->
  46. <text class="picker-item" :style="{ fontSize: fontSize }" v-for="(item, index) in areaDataList" :key="index">{{ item.name }}</text>
  47. <!-- #endif -->
  48. </picker-view-column>
  49. </picker-view>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import { getProvince, getCityByPro, getDistrictByCity } from '@/api/data.js'
  56. import {getArea} from '@/api/data.js'
  57. export default{
  58. name: 'Address',
  59. props: {
  60. // 是否允许点击遮罩层关闭
  61. maskClick: {
  62. type: Boolean,
  63. default: true
  64. },
  65. // 是否开启动画
  66. animation: {
  67. type: Boolean,
  68. default: true
  69. },
  70. // 遮罩层背景颜色
  71. maskBgColor: {
  72. type: String,
  73. default: 'rgba(0, 0, 0, 0.4)' //背景颜色 rgba(0, 0, 0, 0.4) 为空则调用 uni.scss
  74. },
  75. /* 弹出层类型,可选值; 暂时只支持底部弹出
  76. bottom:底部弹出层
  77. */
  78. type: {
  79. type: String,
  80. default: 'bottom'
  81. },
  82. cancelColor: {
  83. type: String,
  84. default: '' // 取消按钮颜色
  85. },
  86. confirmColor: {
  87. type: String,
  88. default: '' // 确认按钮颜色
  89. },
  90. fontSize: {
  91. type: String,
  92. default: '28rpx' // picker-item字体大小
  93. },
  94. btnFontSize: {
  95. type: String,
  96. default: '' // 按钮的字体大小
  97. },
  98. },
  99. data(){
  100. return{
  101. showPopup: false, // 是否显示省市区选择
  102. ani: '',
  103. pickerValue: [0, 0, 0], // 默认值下标
  104. pickerId: [], // 省市区默认值id
  105. provinceDataList: [], // 省数据列表
  106. cityDataList: [], // 市数据列表
  107. areaDataList: [], // 区数据列表
  108. isFirst: false, // 是否首次进入组件
  109. }
  110. },
  111. methods: {
  112. // 初始化
  113. init(){
  114. this.provinceDataList = []
  115. this.cityDataList = []
  116. this.areaDataList = []
  117. this.isFirst = false
  118. },
  119. // 点击遮罩层关闭选择省市区
  120. hideMask() {
  121. this._$emit('onCancel')
  122. this.close()
  123. },
  124. // 打开省市区picker
  125. open(idArr) {
  126. // 初始化数据
  127. this.init()
  128. this.pickerId = idArr
  129. // idArr 省市区默认id
  130. if(this.pickerId && this.pickerId.length > 0){
  131. // 获取省下拉
  132. this.getProvinceList(idArr)
  133. }else{
  134. // 获取省下拉
  135. this.getProvinceList()
  136. }
  137. this.showPopup = true
  138. this.$nextTick(() => {
  139. setTimeout(() => {
  140. this.ani = 'simple-' + this.type
  141. }, 100)
  142. });
  143. },
  144. // 关闭省市区picker
  145. close(type) {
  146. if (!this.maskClick && type) return
  147. this.ani = ''
  148. this.$nextTick(() => {
  149. setTimeout(() => {
  150. this.showPopup = false
  151. }, 300)
  152. })
  153. },
  154. // 取消按钮
  155. pickerCancel() {
  156. this._$emit('onCancel')
  157. this.close()
  158. },
  159. // 确定按钮
  160. pickerConfirm() {
  161. this._$emit('onConfirm')
  162. if(!this.isFirst){
  163. // 首次
  164. this.isFirst = true
  165. }
  166. this.close()
  167. },
  168. // 省市区 change
  169. pickerChange(e) {
  170. const _this = this
  171. let changePickerValue = e.detail.value
  172. console.log(changePickerValue,'changePickerValue')
  173. if (this.pickerValue[0] !== changePickerValue[0]) {
  174. if(this.pickerId && this.pickerId.length > 0 && this.isFirst){
  175. // 获取市下拉
  176. this.getCityList(this.pickerId[0], this.pickerId)
  177. }else{
  178. // 第一级发生滚动,省 change重置市区下拉数据
  179. this.getCityList(this.provinceDataList[changePickerValue[0]].id)
  180. changePickerValue[1] = 0
  181. changePickerValue[2] = 0
  182. }
  183. } else if (this.pickerValue[1] !== changePickerValue[1]) {
  184. if(this.pickerId && this.pickerId.length > 0 && this.isFirst){
  185. // 获取市下拉
  186. this.getAreaList(this.pickerId[1], this.pickerId)
  187. }else{
  188. // 第二级滚动,市 change重置区县下拉数据
  189. this.getAreaList(this.cityDataList[changePickerValue[1]].id)
  190. changePickerValue[2] = 0
  191. }
  192. }
  193. this.pickerValue = changePickerValue
  194. // setTimeout解决由于省市区接口请求数据有延缓导致获值报错
  195. setTimeout(()=>{
  196. _this._$emit('onChange')
  197. },1000)
  198. },
  199. _$emit(emitName) {
  200. let pickObj = {
  201. label: this._getLabel(),
  202. value: this.pickerValue,
  203. cityCode: this._getCityCode(),
  204. areaCode: this._getAreaCode(),
  205. provinceCode: this._getProvinceCode(),
  206. labelArr: this._getLabel().split('-')
  207. }
  208. this.$emit(emitName, pickObj)
  209. },
  210. _getLabel() {
  211. // name报错未发现页面影响(由于省市区接口请求数据延缓)
  212. let pcikerLabel =
  213. this.provinceDataList[this.pickerValue[0]].name + '-' + this.cityDataList[this.pickerValue[1]].name + '-' + this.areaDataList[this.pickerValue[2]].name
  214. return pcikerLabel
  215. },
  216. _getCityCode() {
  217. return this.cityDataList[this.pickerValue[1]].id
  218. },
  219. _getProvinceCode() {
  220. return this.provinceDataList[this.pickerValue[0]].id
  221. },
  222. _getAreaCode() {
  223. return this.areaDataList[this.pickerValue[2]].id
  224. },
  225. // 获取省下拉数据
  226. getProvinceList(idArr){
  227. const _this = this
  228. getArea({type:"2"}).then(res => {
  229. if (res.code == 200 || res.status == 204 || res.status == 200) {
  230. _this.provinceDataList = res.data || [] // 省下拉数据
  231. // 判断是否有默认值
  232. if(idArr){
  233. _this.getCityList(idArr[0], idArr)
  234. _this.getAreaIndArr(idArr)
  235. }else{
  236. // 默认省数据第一条下的市下拉数据
  237. _this.getCityList(_this.provinceDataList[0].id)
  238. }
  239. } else {
  240. _this.provinceDataList = []
  241. }
  242. })
  243. },
  244. // 获取城市列表
  245. getCityList (val, idArr) {
  246. const _this = this
  247. _this.cityDataList = []
  248. _this.areaDataList = []
  249. if (val != null && val != '') {
  250. getArea({parentId: val,type:3}).then(res => {
  251. if (res.code == 200 || res.status == 204 || res.status == 200) {
  252. _this.cityDataList = res.data || [] // 市下拉数据
  253. // 判断是否有默认值
  254. if(idArr){
  255. _this.getAreaList(idArr[1], idArr)
  256. _this.getAreaIndArr(idArr)
  257. }else{
  258. // 默认市数据第一条下的区县下拉数据
  259. _this.getAreaList(_this.cityDataList[0].id)
  260. }
  261. } else {
  262. _this.cityDataList = []
  263. }
  264. })
  265. }
  266. },
  267. // 获取区县列表
  268. getAreaList (val, idArr) {
  269. const _this = this
  270. this.areaDataList = []
  271. if (val != null && val != '') {
  272. getArea({parentId: val,type:4}).then(res => {
  273. if (res.code == 200 || res.status == 204 || res.status == 200) {
  274. _this.areaDataList = res.data || []
  275. // 判断是否有默认值
  276. if(idArr){ _this.getAreaIndArr(idArr) }
  277. } else {
  278. _this.areaDataList = []
  279. }
  280. })
  281. }
  282. },
  283. // 根据id 获取省市区 下标值
  284. getAreaIndArr(idArr){
  285. const _this = this
  286. const provinceInd = _this.provinceDataList.findIndex(res => res['id'] == idArr[0])
  287. const cityInd = _this.cityDataList.findIndex(res => res['id'] == idArr[1])
  288. const districtInd = _this.areaDataList.findIndex(res => res['id'] == idArr[2])
  289. setTimeout(()=>{
  290. _this.pickerValue = [provinceInd, cityInd, districtInd]
  291. },100)
  292. // console.log(_this.pickerValue,'_this.pickerValue')
  293. }
  294. }
  295. }
  296. </script>
  297. <style lang="scss" scoped>
  298. .simple-address {
  299. /* #ifndef APP-NVUE */
  300. display: flex;
  301. /* #endif */
  302. flex-direction: column;
  303. }
  304. .simple-address-mask {
  305. position: fixed;
  306. bottom: 0;
  307. top: 0;
  308. left: 0;
  309. right: 0;
  310. transition-property: opacity;
  311. transition-duration: 0.3s;
  312. opacity: 0;
  313. /* #ifndef APP-NVUE */
  314. z-index: 99999;
  315. /* #endif */
  316. }
  317. .mask-ani {
  318. transition-property: opacity;
  319. transition-duration: 0.2s;
  320. }
  321. .simple-bottom-mask {
  322. opacity: 1;
  323. }
  324. .simple-center-mask {
  325. opacity: 1;
  326. }
  327. .simple-address--fixed {
  328. position: fixed;
  329. bottom: 0;
  330. left: 0;
  331. right: 0;
  332. width: 100%;
  333. transition-property: transform;
  334. transition-duration: 0.3s;
  335. transform: translateY(460rpx);
  336. /* #ifndef APP-NVUE */
  337. z-index: 999999;
  338. /* #endif */
  339. }
  340. .simple-address-content {
  341. background-color: #ffffff;
  342. }
  343. .simple-content-bottom {
  344. bottom: 0;
  345. left: 0;
  346. right: 0;
  347. transform: translateY(500rpx);
  348. }
  349. .content-ani {
  350. transition-property: transform, opacity;
  351. transition-duration: 0.2s;
  352. }
  353. .simple-bottom-content {
  354. transform: translateY(0);
  355. }
  356. .simple-center-content {
  357. transform: scale(1);
  358. opacity: 1;
  359. }
  360. .simple-address__header {
  361. position: relative;
  362. /* #ifndef APP-NVUE */
  363. display: flex;
  364. /* #endif */
  365. flex-direction: row;
  366. flex-wrap: nowrap;
  367. justify-content: space-between;
  368. border-bottom-color: #f2f2f2;
  369. border-bottom-style: solid;
  370. border-bottom-width: 1rpx;
  371. }
  372. .simple-address--fixed-top {
  373. /* #ifndef APP-NVUE */
  374. display: flex;
  375. /* #endif */
  376. flex-direction: row;
  377. justify-content: space-between;
  378. border-top-color: $uni-border-color;
  379. border-top-style: solid;
  380. border-top-width: 1rpx;
  381. }
  382. .simple-address__header-btn-box {
  383. /* #ifndef APP-NVUE */
  384. display: flex;
  385. /* #endif */
  386. flex-direction: row;
  387. align-items: center;
  388. justify-content: center;
  389. height: 70rpx;
  390. }
  391. .simple-address__header-text {
  392. text-align: center;
  393. font-size: $uni-font-size-base;
  394. color: #1aad19;
  395. line-height: 70rpx;
  396. padding-left: 40rpx;
  397. padding-right: 40rpx;
  398. }
  399. .simple-address__box {
  400. position: relative;
  401. }
  402. .simple-address-view {
  403. position: relative;
  404. bottom: 0;
  405. left: 0;
  406. /* #ifndef APP-NVUE */
  407. width: 100%;
  408. /* #endif */
  409. /* #ifdef APP-NVUE */
  410. width: 750rpx;
  411. /* #endif */
  412. height: 408rpx;
  413. background-color: rgba(255, 255, 255, 1);
  414. }
  415. .picker-item {
  416. text-align: center;
  417. line-height: 70rpx;
  418. text-overflow: ellipsis;
  419. font-size: 28rpx;
  420. }
  421. </style>