address.vue 11 KB

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