address.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. 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. _this.getAreaIndArr(idArr)
  233. }else{
  234. // 默认省数据第一条下的市下拉数据
  235. _this.getCityList(_this.provinceDataList[0].id)
  236. }
  237. } else {
  238. _this.provinceDataList = []
  239. }
  240. })
  241. },
  242. // 获取城市列表
  243. getCityList (val, idArr) {
  244. const _this = this
  245. _this.cityDataList = []
  246. _this.areaDataList = []
  247. if (val != null && val != '') {
  248. getCityByPro({id: val}).then(res => {
  249. if (res.code == 200 || res.status == 204 || res.status == 200) {
  250. _this.cityDataList = res.data || [] // 市下拉数据
  251. // 判断是否有默认值
  252. if(idArr){
  253. _this.getAreaList(idArr[1], idArr)
  254. _this.getAreaIndArr(idArr)
  255. }else{
  256. // 默认市数据第一条下的区县下拉数据
  257. _this.getAreaList(_this.cityDataList[0].id)
  258. }
  259. } else {
  260. _this.cityDataList = []
  261. }
  262. })
  263. }
  264. },
  265. // 获取区县列表
  266. getAreaList (val, idArr) {
  267. const _this = this
  268. this.areaDataList = []
  269. if (val != null && val != '') {
  270. getDistrictByCity({id: val}).then(res => {
  271. if (res.code == 200 || res.status == 204 || res.status == 200) {
  272. _this.areaDataList = res.data || []
  273. // 判断是否有默认值
  274. if(idArr){ _this.getAreaIndArr(idArr) }
  275. } else {
  276. _this.areaDataList = []
  277. }
  278. })
  279. }
  280. },
  281. // 根据id 获取省市区 下标值
  282. getAreaIndArr(idArr){
  283. const _this = this
  284. const provinceInd = _this.provinceDataList.findIndex(res => res['id'] == idArr[0])
  285. const cityInd = _this.cityDataList.findIndex(res => res['id'] == idArr[1])
  286. const districtInd = _this.areaDataList.findIndex(res => res['id'] == idArr[2])
  287. setTimeout(()=>{
  288. _this.pickerValue = [provinceInd, cityInd, districtInd]
  289. },100)
  290. // console.log(_this.pickerValue,'_this.pickerValue')
  291. }
  292. }
  293. }
  294. </script>
  295. <style lang="scss" scoped>
  296. .simple-address {
  297. /* #ifndef APP-NVUE */
  298. display: flex;
  299. /* #endif */
  300. flex-direction: column;
  301. }
  302. .simple-address-mask {
  303. position: fixed;
  304. bottom: 0;
  305. top: 0;
  306. left: 0;
  307. right: 0;
  308. transition-property: opacity;
  309. transition-duration: 0.3s;
  310. opacity: 0;
  311. /* #ifndef APP-NVUE */
  312. z-index: 99999;
  313. /* #endif */
  314. }
  315. .mask-ani {
  316. transition-property: opacity;
  317. transition-duration: 0.2s;
  318. }
  319. .simple-bottom-mask {
  320. opacity: 1;
  321. }
  322. .simple-center-mask {
  323. opacity: 1;
  324. }
  325. .simple-address--fixed {
  326. position: fixed;
  327. bottom: 0;
  328. left: 0;
  329. right: 0;
  330. width: 100%;
  331. transition-property: transform;
  332. transition-duration: 0.3s;
  333. transform: translateY(460rpx);
  334. /* #ifndef APP-NVUE */
  335. z-index: 999999;
  336. /* #endif */
  337. }
  338. .simple-address-content {
  339. background-color: #ffffff;
  340. }
  341. .simple-content-bottom {
  342. bottom: 0;
  343. left: 0;
  344. right: 0;
  345. transform: translateY(500rpx);
  346. }
  347. .content-ani {
  348. transition-property: transform, opacity;
  349. transition-duration: 0.2s;
  350. }
  351. .simple-bottom-content {
  352. transform: translateY(0);
  353. }
  354. .simple-center-content {
  355. transform: scale(1);
  356. opacity: 1;
  357. }
  358. .simple-address__header {
  359. position: relative;
  360. /* #ifndef APP-NVUE */
  361. display: flex;
  362. /* #endif */
  363. flex-direction: row;
  364. flex-wrap: nowrap;
  365. justify-content: space-between;
  366. border-bottom-color: #f2f2f2;
  367. border-bottom-style: solid;
  368. border-bottom-width: 1rpx;
  369. }
  370. .simple-address--fixed-top {
  371. /* #ifndef APP-NVUE */
  372. display: flex;
  373. /* #endif */
  374. flex-direction: row;
  375. justify-content: space-between;
  376. border-top-color: $uni-border-color;
  377. border-top-style: solid;
  378. border-top-width: 1rpx;
  379. }
  380. .simple-address__header-btn-box {
  381. /* #ifndef APP-NVUE */
  382. display: flex;
  383. /* #endif */
  384. flex-direction: row;
  385. align-items: center;
  386. justify-content: center;
  387. height: 70rpx;
  388. }
  389. .simple-address__header-text {
  390. text-align: center;
  391. font-size: $uni-font-size-base;
  392. color: #1aad19;
  393. line-height: 70rpx;
  394. padding-left: 40rpx;
  395. padding-right: 40rpx;
  396. }
  397. .simple-address__box {
  398. position: relative;
  399. }
  400. .simple-address-view {
  401. position: relative;
  402. bottom: 0;
  403. left: 0;
  404. /* #ifndef APP-NVUE */
  405. width: 100%;
  406. /* #endif */
  407. /* #ifdef APP-NVUE */
  408. width: 750rpx;
  409. /* #endif */
  410. height: 408rpx;
  411. background-color: rgba(255, 255, 255, 1);
  412. }
  413. .picker-item {
  414. text-align: center;
  415. line-height: 70rpx;
  416. text-overflow: ellipsis;
  417. font-size: 28rpx;
  418. }
  419. </style>