simple-address.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <view class="simple-address" v-if="showPopup" @touchmove.stop.prevent="clear">
  3. <!-- 遮罩层 -->
  4. <view class="simple-address-mask" @touchmove.stop.prevent="clear" v-if="maskClick" :class="[ani+'-mask', animation ? 'mask-ani' : '']" :style="{
  5. 'background-color': maskBgColor
  6. }"
  7. @tap="hideMask(true)"></view>
  8. <view class="simple-address-content simple-address--fixed" :class="[type,ani+'-content', animation ? 'content-ani' : '']">
  9. <view class="simple-address__header">
  10. <view class="simple-address__header-btn-box" @click="pickerCancel">
  11. <text class="simple-address__header-text">取消</text>
  12. </view>
  13. <view class="simple-address__header-btn-box" @click="pickerConfirm">
  14. <text class="simple-address__header-text confirm-text">确定</text>
  15. </view>
  16. </view>
  17. <view class="simple-address__box">
  18. <picker-view indicator-style="height: 70rpx;" class="simple-address-view" :value="pickerValue" @change="pickerChange">
  19. <picker-view-column>
  20. <!-- #ifndef APP-NVUE -->
  21. <view class="picker-item" v-for="(item,index) in region" :key="index">{{item.name}}</view>
  22. <!-- #endif -->
  23. <!-- #ifdef APP-NVUE -->
  24. <!-- <text class="picker-item" v-for="(item,index) in provinceDataList" :key="index">{{item.name}}</text> -->
  25. <!-- #endif -->
  26. </picker-view-column>
  27. <picker-view-column>
  28. <!-- #ifndef APP-NVUE -->
  29. <view class="picker-item" v-for="(item,index) in region[pickerValue[0]].children" :key="index">{{item.name}}</view>
  30. <!-- #endif -->
  31. <!-- #ifdef APP-NVUE -->
  32. <!-- <text class="picker-item" v-for="(item,index) in cityDataList" :key="index">{{item.name}}</text> -->
  33. <!-- #endif -->
  34. </picker-view-column>
  35. <!-- <picker-view-column> -->
  36. <!-- #ifndef APP-NVUE -->
  37. <!-- <view class="picker-item" v-for="(item,index) in areaDataList" :key="index">{{item.name}}</view> -->
  38. <!-- #endif -->
  39. <!-- #ifdef APP-NVUE -->
  40. <!-- <text class="picker-item" v-for="(item,index) in areaDataList" :key="index">{{item.name}}</text> -->
  41. <!-- #endif -->
  42. <!-- </picker-view-column> -->
  43. </picker-view>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. // import provinceData from './city-data/province.js'
  50. // import cityData from './city-data/city.js'
  51. // import areaData from './city-data/area.js'
  52. export default {
  53. name: 'simpleAddress',
  54. props: {
  55. // 开启动画
  56. animation: {
  57. type: Boolean,
  58. default: true
  59. },
  60. /* 弹出层类型,可选值;
  61. bottom:底部弹出层
  62. */
  63. type: {
  64. type: String,
  65. default: 'bottom'
  66. },
  67. // maskClick
  68. maskClick: {
  69. type: Boolean,
  70. default: true
  71. },
  72. show: {
  73. type: Boolean,
  74. default: true
  75. },
  76. maskBgColor: {
  77. type: String,
  78. default: 'rgba(0, 0, 0, 0.4)' // 背景颜色 rgba(0, 0, 0, 0.4) 为空则调用 uni.scss
  79. },
  80. /* 默认值 */
  81. pickerValueDefault: {
  82. type: Array,
  83. default() {
  84. return [0, 0]
  85. }
  86. },
  87. // 省市区数据
  88. region: {
  89. type: Array,
  90. default() {
  91. return []
  92. }
  93. }
  94. },
  95. data() {
  96. return {
  97. ani: '',
  98. showPopup: false,
  99. pickerValue: [0, 0],
  100. provinceDataList: [],
  101. cityDataList: [],
  102. areaDataList: []
  103. }
  104. },
  105. watch: {
  106. show(newValue) {
  107. if (newValue) {
  108. this.open()
  109. } else {
  110. this.close()
  111. }
  112. },
  113. pickerValueDefault() {
  114. this.init()
  115. }
  116. },
  117. created() {
  118. this.init()
  119. },
  120. methods: {
  121. init() {
  122. // this.handPickValueDefault() // 对 pickerValueDefault 做兼容处理
  123. // this.provinceDataList = JSON.parse(JSON.stringify(this.region))
  124. // this.cityDataList = []
  125. },
  126. handPickValueDefault() {
  127. // if (this.pickerValueDefault !== [0, 0]) {
  128. // if (this.pickerValueDefault[0] > provinceData.length - 1) {
  129. // this.pickerValueDefault[0] = provinceData.length - 1
  130. // }
  131. // if (this.pickerValueDefault[1] > cityData[this.pickerValueDefault[0]].length - 1) {
  132. // this.pickerValueDefault[1] = cityData[this.pickerValueDefault[0]].length - 1
  133. // }
  134. // if (this.pickerValueDefault[2] > areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1) {
  135. // this.pickerValueDefault[2] = areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1
  136. // }
  137. // }
  138. },
  139. pickerChange(e) {
  140. this.pickerValue = e.detail.value
  141. this._$emit('onChange')
  142. },
  143. _$emit(emitName) {
  144. let pickObj = {
  145. label: this._getLabel(),
  146. value: this.pickerValue,
  147. provinceName: this.region[this.pickerValue[0]].name,
  148. cityName: this.region[this.pickerValue[0]].children[this.pickerValue[1]].name,
  149. provinceCode: this._getProvinceCode(),
  150. cityCode: this._getCityCode()
  151. // areaCode: this._getAreaCode(),
  152. }
  153. console.log(pickObj)
  154. this.$emit(emitName, pickObj)
  155. },
  156. _getLabel() {
  157. let pcikerLabel = this.region[this.pickerValue[0]].name + '-' + this.region[this.pickerValue[0]].children[this.pickerValue[1]].name
  158. return pcikerLabel
  159. },
  160. _getProvinceCode() {
  161. return this.region[this.pickerValue[0]].id
  162. },
  163. _getCityCode() {
  164. return this.region[this.pickerValue[0]].children[this.pickerValue[1]].id
  165. },
  166. _getAreaCode() {
  167. return this.region[this.pickerValue[0]].children[this.pickerValue[1]].children[this.pickerValue[2]].id
  168. },
  169. clear() {
  170. },
  171. hideMask() {
  172. this._$emit('onCancel')
  173. this.close()
  174. },
  175. pickerCancel() {
  176. this._$emit('onCancel')
  177. this.close()
  178. },
  179. pickerConfirm() {
  180. this._$emit('onConfirm')
  181. this.close()
  182. },
  183. open() {
  184. this.showPopup = true
  185. this.$nextTick(() => {
  186. setTimeout(() => {
  187. this.ani = 'simple-' + this.type
  188. }, 100)
  189. })
  190. },
  191. close(type) {
  192. if (!this.maskClick && type) return
  193. this.ani = ''
  194. this.$nextTick(() => {
  195. setTimeout(() => {
  196. this.showPopup = false
  197. }, 300)
  198. })
  199. }
  200. }
  201. }
  202. </script>
  203. <style lang="scss" scoped>
  204. .simple-address {
  205. /* #ifndef APP-NVUE */
  206. display: flex;
  207. /* #endif */
  208. flex-direction: column;
  209. }
  210. .simple-address-mask {
  211. position: fixed;
  212. bottom: 0;
  213. top: 0;
  214. left: 0;
  215. right: 0;
  216. transition-property: opacity;
  217. transition-duration: 0.3s;
  218. opacity: 0;
  219. /* #ifndef APP-NVUE */
  220. z-index: 996;
  221. /* #endif */
  222. }
  223. .mask-ani {
  224. transition-property: opacity;
  225. transition-duration: 0.2s;
  226. }
  227. .simple-bottom-mask {
  228. opacity: 1;
  229. }
  230. .simple-center-mask {
  231. opacity: 1;
  232. }
  233. .simple-address--fixed {
  234. position: fixed;
  235. bottom: 0;
  236. left: 0;
  237. right: 0;
  238. transition-property: transform;
  239. transition-duration: 0.3s;
  240. transform: translateY(460rpx);
  241. /* #ifndef APP-NVUE */
  242. z-index: 999;
  243. /* #endif */
  244. }
  245. .simple-address-content {
  246. background-color: #FFFFFF;
  247. }
  248. .simple-content-bottom {
  249. bottom: 0;
  250. left: 0;
  251. right: 0;
  252. transform: translateY(500rpx);
  253. }
  254. .content-ani {
  255. transition-property: transform, opacity;
  256. transition-duration: 0.2s;
  257. }
  258. .simple-bottom-content {
  259. transform: translateY(0);
  260. }
  261. .simple-center-content {
  262. transform: scale(1);
  263. opacity: 1;
  264. }
  265. .simple-address__header {
  266. position: relative;
  267. /* #ifndef APP-NVUE */
  268. display: flex;
  269. /* #endif */
  270. flex-direction: row;
  271. flex-wrap: nowrap;
  272. justify-content: space-between;
  273. border-bottom-color: #f2f2f2;
  274. border-bottom-style: solid;
  275. border-bottom-width: 1rpx;
  276. }
  277. .simple-address--fixed-top {
  278. /* #ifndef APP-NVUE */
  279. display: flex;
  280. /* #endif */
  281. flex-direction: row;
  282. justify-content: space-between;
  283. border-top-color: $uni-border-color;
  284. border-top-style: solid;
  285. border-top-width: 1rpx;
  286. }
  287. .simple-address__header-btn-box {
  288. /* #ifndef APP-NVUE */
  289. display: flex;
  290. /* #endif */
  291. flex-direction: row;
  292. align-items: center;
  293. justify-content: center;
  294. height: 70rpx;
  295. }
  296. .simple-address__header-text {
  297. text-align: center;
  298. font-size: $uni-font-size-base;
  299. color: $fontColor2;
  300. line-height: 70rpx;
  301. padding-left: 40rpx;
  302. padding-right: 40rpx;
  303. }
  304. .confirm-text {
  305. color: $mainColor;
  306. }
  307. .simple-address__box {
  308. position: relative;
  309. }
  310. .simple-address-view {
  311. position: relative;
  312. bottom: 0;
  313. left: 0;
  314. /* #ifndef APP-NVUE */
  315. width: 100%;
  316. /* #endif */
  317. /* #ifdef APP-NVUE */
  318. width: 750rpx;
  319. /* #endif */
  320. height: 408rpx;
  321. background-color: rgba(255, 255, 255, 1);
  322. }
  323. .picker-item {
  324. text-align: center;
  325. line-height: 70rpx;
  326. text-overflow: ellipsis;
  327. font-size: 28rpx;
  328. }
  329. </style>