simple-address.vue 8.3 KB

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