simple-address.vue 8.1 KB

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