address.vue 12 KB

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