simple-address.nvue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <template>
  2. <view class="simple-address" @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" :style="{color:themeColor}">确定</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 provinceDataList" :key="index">{{item.label}}</view>
  22. <!-- #endif -->
  23. <!-- #ifdef APP-NVUE -->
  24. <text class="picker-item" v-for="(item,index) in provinceDataList" :key="index">{{item.label}}</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 cityDataList" :key="index">{{item.label}}</view>
  30. <!-- #endif -->
  31. <!-- #ifdef APP-NVUE -->
  32. <text class="picker-item" v-for="(item,index) in cityDataList" :key="index">{{item.label}}</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.label}}</view>
  38. <!-- #endif -->
  39. <!-- #ifdef APP-NVUE -->
  40. <text class="picker-item" v-for="(item,index) in areaDataList" :key="index">{{item.label}}</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. themeColor: {
  81. type: String,
  82. default: '', // 主题色
  83. },
  84. showPopup:{
  85. type:Boolean,
  86. default:0
  87. },
  88. /* 默认值 */
  89. pickerValueDefault: {
  90. type: Array,
  91. default () {
  92. return [0, 0, 0]
  93. }
  94. },
  95. },
  96. data() {
  97. return {
  98. ani: '',
  99. pickerValue: [0, 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 = provinceData;
  124. this.cityDataList = cityData[this.pickerValueDefault[0]];
  125. this.areaDataList = areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]];
  126. this.pickerValue = this.pickerValueDefault;
  127. },
  128. handPickValueDefault() {
  129. if (this.pickerValueDefault !== [0, 0, 0]) {
  130. if (this.pickerValueDefault[0] > provinceData.length - 1) {
  131. this.pickerValueDefault[0] = provinceData.length - 1;
  132. }
  133. if (this.pickerValueDefault[1] > cityData[this.pickerValueDefault[0]].length - 1) {
  134. this.pickerValueDefault[1] = cityData[this.pickerValueDefault[0]].length - 1;
  135. }
  136. if (this.pickerValueDefault[2] > areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1) {
  137. this.pickerValueDefault[2] = areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1;
  138. }
  139. }
  140. },
  141. pickerChange(e) {
  142. let changePickerValue = e.detail.value;
  143. if (this.pickerValue[0] !== changePickerValue[0]) {
  144. // 第一级发生滚动
  145. this.cityDataList = cityData[changePickerValue[0]];
  146. this.areaDataList = areaData[changePickerValue[0]][0];
  147. changePickerValue[1] = 0;
  148. changePickerValue[2] = 0;
  149. } else if (this.pickerValue[1] !== changePickerValue[1]) {
  150. // 第二级滚动
  151. this.areaDataList =
  152. areaData[changePickerValue[0]][changePickerValue[1]];
  153. changePickerValue[2] = 0;
  154. }
  155. this.pickerValue = changePickerValue;
  156. this._$emit('onChange');
  157. },
  158. _$emit(emitName) {
  159. let pickObj = {
  160. label: this._getLabel(),
  161. value: this.pickerValue,
  162. cityCode: this._getCityCode(),
  163. areaCode: this._getAreaCode(),
  164. provinceCode: this._getProvinceCode()
  165. };
  166. this.$emit(emitName, pickObj);
  167. },
  168. _getLabel() {
  169. let pcikerLabel =
  170. this.provinceDataList[this.pickerValue[0]].label +
  171. '-' +
  172. this.cityDataList[this.pickerValue[1]].label +
  173. '-' +
  174. this.areaDataList[this.pickerValue[2]].label;
  175. return pcikerLabel;
  176. },
  177. _getCityCode() {
  178. return this.cityDataList[this.pickerValue[1]].value;
  179. },
  180. _getProvinceCode() {
  181. return this.provinceDataList[this.pickerValue[0]].value;
  182. },
  183. _getAreaCode() {
  184. return this.areaDataList[this.pickerValue[2]].value;
  185. },
  186. clear() {
  187. },
  188. hideMask() {
  189. this._$emit('onCancel');
  190. this.close();
  191. },
  192. pickerCancel() {
  193. this._$emit('onCancel');
  194. this.close();
  195. },
  196. pickerConfirm() {
  197. this._$emit('onConfirm');
  198. this.close();
  199. },
  200. open() {
  201. this.showPopup = true
  202. this.$nextTick(() => {
  203. setTimeout(() => {
  204. this.ani = 'simple-' + this.type
  205. }, 100)
  206. })
  207. },
  208. close(type) {
  209. if (!this.maskClick && type) return;
  210. this.ani = ''
  211. this.$nextTick(() => {
  212. setTimeout(() => {
  213. this.showPopup = false
  214. }, 300)
  215. })
  216. }
  217. }
  218. }
  219. </script>
  220. <style lang="scss" scoped>
  221. .simple-address {
  222. /* #ifndef APP-NVUE */
  223. display: flex;
  224. /* #endif */
  225. flex-direction: column;
  226. }
  227. .simple-address-mask {
  228. position: fixed;
  229. bottom: 0;
  230. top: 0;
  231. left: 0;
  232. right: 0;
  233. transition-property: opacity;
  234. transition-duration: 0.3s;
  235. opacity: 0;
  236. /* #ifndef APP-NVUE */
  237. z-index: 99;
  238. /* #endif */
  239. }
  240. .mask-ani {
  241. transition-property: opacity;
  242. transition-duration: 0.2s;
  243. }
  244. .simple-bottom-mask {
  245. opacity: 1;
  246. }
  247. .simple-center-mask {
  248. opacity: 1;
  249. }
  250. .simple-address--fixed {
  251. position: fixed;
  252. bottom: 0;
  253. left: 0;
  254. right: 0;
  255. transition-property: transform;
  256. transition-duration: 0.3s;
  257. transform: translateY(460rpx);
  258. /* #ifndef APP-NVUE */
  259. z-index: 99;
  260. /* #endif */
  261. }
  262. .simple-address-content {
  263. background-color: #FFFFFF;
  264. }
  265. .simple-content-bottom {
  266. bottom: 0;
  267. left: 0;
  268. right: 0;
  269. transform: translateY(500rpx);
  270. }
  271. .content-ani {
  272. transition-property: transform, opacity;
  273. transition-duration: 0.2s;
  274. }
  275. .simple-bottom-content {
  276. transform: translateY(0);
  277. }
  278. .simple-center-content {
  279. transform: scale(1);
  280. opacity: 1;
  281. }
  282. .simple-address__header {
  283. position: relative;
  284. /* #ifndef APP-NVUE */
  285. display: flex;
  286. /* #endif */
  287. flex-direction: row;
  288. flex-wrap: nowrap;
  289. justify-content: space-between;
  290. border-bottom-color: #f2f2f2;
  291. border-bottom-style: solid;
  292. border-bottom-width: 1rpx;
  293. }
  294. .simple-address--fixed-top {
  295. /* #ifndef APP-NVUE */
  296. display: flex;
  297. /* #endif */
  298. flex-direction: row;
  299. justify-content: space-between;
  300. border-top-color: $uni-border-color;
  301. border-top-style: solid;
  302. border-top-width: 1rpx;
  303. }
  304. .simple-address__header-btn-box {
  305. /* #ifndef APP-NVUE */
  306. display: flex;
  307. /* #endif */
  308. flex-direction: row;
  309. align-items: center;
  310. justify-content: center;
  311. height: 70rpx;
  312. }
  313. .simple-address__header-text {
  314. text-align: center;
  315. font-size: $uni-font-size-base;
  316. color: #1aad19;
  317. line-height: 70rpx;
  318. padding-left: 40rpx;
  319. padding-right: 40rpx;
  320. }
  321. .simple-address__box {
  322. position: relative;
  323. }
  324. .simple-address-view {
  325. position: relative;
  326. bottom: 0;
  327. left: 0;
  328. /* #ifndef APP-NVUE */
  329. width: 100%;
  330. /* #endif */
  331. /* #ifdef APP-NVUE */
  332. width: 750rpx;
  333. /* #endif */
  334. height: 408rpx;
  335. background-color: rgba(255, 255, 255, 1);
  336. }
  337. .picker-item {
  338. text-align: center;
  339. line-height: 70rpx;
  340. text-overflow: ellipsis;
  341. font-size: 28rpx;
  342. }
  343. </style>