use-address.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <template>
  2. <view class="use-address" v-if="showPopup" @touchmove.stop.prevent="clear">
  3. <!-- 遮罩层 -->
  4. <view
  5. class="use-address-mask"
  6. @touchmove.stop.prevent="clear"
  7. v-if="maskClick"
  8. :class="[ani + '-mask', animation ? 'mask-ani' : '']"
  9. :style="{
  10. 'background-color': maskBgColor
  11. }"
  12. @tap="hideMask(true)"
  13. ></view>
  14. <view class="use-address-content use-address--fixed" :class="[type, ani + '-content', animation ? 'content-ani' : '']">
  15. <view class="use-address__header">
  16. <view class="use-address__header-btn-box" @click="pickerCancel">
  17. <text class="use-address__header-text" :style="{ color: cancelColor, fontSize: btnFontSize }">取消</text>
  18. </view>
  19. <view class="use-address__header-btn-box" @click="pickerConfirm">
  20. <text class="use-address__header-text" :style="{ color: confirmColor || themeColor, fontSize: btnFontSize }">确定</text>
  21. </view>
  22. </view>
  23. <view class="use-address__box">
  24. <picker-view indicator-style="height: 70rpx;" class="use-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.label }}</view>
  28. <!-- #endif -->
  29. <!-- #ifdef APP-NVUE -->
  30. <text class="picker-item" :style="{ fontSize: fontSize }" v-for="(item, index) in provinceDataList" :key="index">{{ item.label }}</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.label }}</view>
  36. <!-- #endif -->
  37. <!-- #ifdef APP-NVUE -->
  38. <text class="picker-item" :style="{ fontSize: fontSize }" v-for="(item, index) in cityDataList" :key="index">{{ item.label }}</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.label }}</view>
  44. <!-- #endif -->
  45. <!-- #ifdef APP-NVUE -->
  46. <text class="picker-item" :style="{ fontSize: fontSize }" v-for="(item, index) in areaDataList" :key="index">{{ item.label }}</text>
  47. <!-- #endif -->
  48. </picker-view-column>
  49. </picker-view>
  50. </view>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. /**
  56. * Simple-addres 地址联动组件
  57. * @description 三级地址联动,支持(app)nvue、小程序、H5
  58. * @tutorial https://ext.dcloud.net.cn/plugin?id=1084
  59. * @property {String} animation 是否开启动画
  60. * @property {String} type = [bottom] 弹出层类型,暂时只支持底部弹出
  61. * @property {Boolean} maskClick = [true | false] 是否允许点击遮罩层关闭
  62. * @property {Boolean} show = [true | false] 显示或隐藏地址组件
  63. * @property {String} maskBgColor 遮罩层背景颜色
  64. * @property {String} cancelColor 取消按钮颜色,默认为:#1aad19
  65. * @property {String} confirmColor 确认按钮颜色,默认为:themeColor
  66. * @property {String} themeColor 主题颜色,后续会废弃该配置,建议使用`cancelColor`或`confirmColor`
  67. * @property {String} btnFontSize 取消、确认按钮字体大小,默认为`uni.scss里的 $uni-font-size-base `
  68. * @property {String} fontSize picker-item字体大小,默认为:28rpx
  69. * @property {Array} pickerValueDefault 默认值,可以通过function queryIndex 获取
  70. * @property {Function} queryIndex 根据自定义信息返回对应的index
  71. * @property {Function} open 打开
  72. * @example <use-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" themeColor='#007AFF'></use-address>
  73. */
  74. import provinceData from './city-data/province.js';
  75. import cityData from './city-data/city.js';
  76. import areaData from './city-data/area.js';
  77. export default {
  78. name: 'simpleAddress',
  79. props: {
  80. mode: {
  81. // 地址类型
  82. // default 则代表老版本根据index索引获取数据
  83. //
  84. type: String,
  85. default: 'default'
  86. },
  87. // 开启动画
  88. animation: {
  89. type: Boolean,
  90. default: true
  91. },
  92. /* 弹出层类型,可选值;
  93. bottom:底部弹出层
  94. */
  95. type: {
  96. type: String,
  97. default: 'bottom'
  98. },
  99. // maskClick
  100. maskClick: {
  101. type: Boolean,
  102. default: true
  103. },
  104. show: {
  105. type: Boolean,
  106. default: true
  107. },
  108. maskBgColor: {
  109. type: String,
  110. default: 'rgba(0, 0, 0, 0.4)' //背景颜色 rgba(0, 0, 0, 0.4) 为空则调用 uni.scss
  111. },
  112. themeColor: {
  113. type: String,
  114. default: '' // 确认按钮颜色(向下兼容)
  115. },
  116. cancelColor: {
  117. type: String,
  118. default: '' // 取消按钮颜色
  119. },
  120. confirmColor: {
  121. type: String,
  122. default: '' // 确认按钮颜色
  123. },
  124. fontSize: {
  125. type: String,
  126. default: '28rpx' // picker-item字体大小
  127. },
  128. btnFontSize: {
  129. type: String,
  130. default: '' // 按钮的字体大小
  131. },
  132. /* 默认值 */
  133. pickerValueDefault: {
  134. type: Array,
  135. default() {
  136. return [0, 0, 0];
  137. }
  138. }
  139. },
  140. data() {
  141. return {
  142. ani: '',
  143. showPopup: false,
  144. pickerValue: [0, 0, 0],
  145. provinceDataList: [],
  146. cityDataList: [],
  147. areaDataList: []
  148. };
  149. },
  150. watch: {
  151. show(newValue) {
  152. if (newValue) {
  153. this.open();
  154. } else {
  155. this.close();
  156. }
  157. },
  158. pickerValueDefault() {
  159. this.init();
  160. }
  161. },
  162. created() {
  163. this.init();
  164. },
  165. methods: {
  166. init() {
  167. this.handPickValueDefault(); // 对 pickerValueDefault 做兼容处理
  168. this.provinceDataList = provinceData;
  169. this.cityDataList = cityData[this.pickerValueDefault[0]];
  170. this.areaDataList = areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]];
  171. this.pickerValue = this.pickerValueDefault;
  172. },
  173. handPickValueDefault() {
  174. if (this.pickerValueDefault !== [0, 0, 0]) {
  175. if (this.pickerValueDefault[0] > provinceData.length - 1) {
  176. this.pickerValueDefault[0] = provinceData.length - 1;
  177. }
  178. if (this.pickerValueDefault[1] > cityData[this.pickerValueDefault[0]].length - 1) {
  179. this.pickerValueDefault[1] = cityData[this.pickerValueDefault[0]].length - 1;
  180. }
  181. if (this.pickerValueDefault[2] > areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1) {
  182. this.pickerValueDefault[2] = areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1;
  183. }
  184. }
  185. },
  186. pickerChange(e) {
  187. let changePickerValue = e.detail.value;
  188. if (this.pickerValue[0] !== changePickerValue[0]) {
  189. // 第一级发生滚动
  190. this.cityDataList = cityData[changePickerValue[0]];
  191. this.areaDataList = areaData[changePickerValue[0]][0];
  192. changePickerValue[1] = 0;
  193. changePickerValue[2] = 0;
  194. } else if (this.pickerValue[1] !== changePickerValue[1]) {
  195. // 第二级滚动
  196. this.areaDataList = areaData[changePickerValue[0]][changePickerValue[1]];
  197. changePickerValue[2] = 0;
  198. }
  199. this.pickerValue = changePickerValue;
  200. this._$emit('onChange');
  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. let pcikerLabel =
  215. this.provinceDataList[this.pickerValue[0]].label + '-' + this.cityDataList[this.pickerValue[1]].label + '-' + this.areaDataList[this.pickerValue[2]].label;
  216. return pcikerLabel;
  217. },
  218. _getCityCode() {
  219. return this.cityDataList[this.pickerValue[1]].value;
  220. },
  221. _getProvinceCode() {
  222. return this.provinceDataList[this.pickerValue[0]].value;
  223. },
  224. _getAreaCode() {
  225. return this.areaDataList[this.pickerValue[2]].value;
  226. },
  227. queryIndex(params = [], type = 'value') {
  228. // params = [ 11 ,1101,110101 ];
  229. // 1.获取省份的index
  230. let provinceIndex = provinceData.findIndex(res => res[type] == params[0]);
  231. let cityIndex = cityData[provinceIndex].findIndex(res => res[type] == params[1]);
  232. let areaIndex = areaData[provinceIndex][cityIndex].findIndex(res => res[type] == params[2]);
  233. return {
  234. index: [provinceIndex, cityIndex, areaIndex],
  235. data: {
  236. province: provinceData[provinceIndex],
  237. city: cityData[provinceIndex][cityIndex],
  238. area: areaData[provinceIndex][cityIndex][areaIndex]
  239. }
  240. };
  241. },
  242. clear() {},
  243. hideMask() {
  244. this._$emit('onCancel');
  245. this.close();
  246. },
  247. pickerCancel() {
  248. this._$emit('onCancel');
  249. this.close();
  250. },
  251. pickerConfirm() {
  252. this._$emit('onConfirm');
  253. this.close();
  254. },
  255. open() {
  256. this.showPopup = true;
  257. this.$nextTick(() => {
  258. setTimeout(() => {
  259. this.ani = 'simple-' + this.type;
  260. }, 100);
  261. });
  262. },
  263. close(type) {
  264. if (!this.maskClick && type) return;
  265. this.ani = '';
  266. this.$nextTick(() => {
  267. setTimeout(() => {
  268. this.showPopup = false;
  269. }, 300);
  270. });
  271. }
  272. }
  273. };
  274. </script>
  275. <style lang="scss" scoped>
  276. .use-address {
  277. /* #ifndef APP-NVUE */
  278. display: flex;
  279. /* #endif */
  280. flex-direction: column;
  281. }
  282. .use-address-mask {
  283. position: fixed;
  284. bottom: 0;
  285. top: 0;
  286. left: 0;
  287. right: 0;
  288. transition-property: opacity;
  289. transition-duration: 0.3s;
  290. opacity: 0;
  291. /* #ifndef APP-NVUE */
  292. z-index: 99;
  293. /* #endif */
  294. }
  295. .mask-ani {
  296. transition-property: opacity;
  297. transition-duration: 0.2s;
  298. }
  299. .simple-bottom-mask {
  300. opacity: 1;
  301. }
  302. .simple-center-mask {
  303. opacity: 1;
  304. }
  305. .use-address--fixed {
  306. position: fixed;
  307. bottom: 0;
  308. left: 0;
  309. right: 0;
  310. transition-property: transform;
  311. transition-duration: 0.3s;
  312. transform: translateY(460rpx);
  313. /* #ifndef APP-NVUE */
  314. z-index: 99;
  315. /* #endif */
  316. }
  317. .use-address-content {
  318. background-color: #ffffff;
  319. }
  320. .simple-content-bottom {
  321. bottom: 0;
  322. left: 0;
  323. right: 0;
  324. transform: translateY(500rpx);
  325. }
  326. .content-ani {
  327. transition-property: transform, opacity;
  328. transition-duration: 0.2s;
  329. }
  330. .simple-bottom-content {
  331. transform: translateY(0);
  332. }
  333. .simple-center-content {
  334. transform: scale(1);
  335. opacity: 1;
  336. }
  337. .use-address__header {
  338. position: relative;
  339. /* #ifndef APP-NVUE */
  340. display: flex;
  341. /* #endif */
  342. flex-direction: row;
  343. flex-wrap: nowrap;
  344. justify-content: space-between;
  345. border-bottom-color: #f2f2f2;
  346. border-bottom-style: solid;
  347. border-bottom-width: 1rpx;
  348. }
  349. .use-address--fixed-top {
  350. /* #ifndef APP-NVUE */
  351. display: flex;
  352. /* #endif */
  353. flex-direction: row;
  354. justify-content: space-between;
  355. border-top-color: $uni-border-color;
  356. border-top-style: solid;
  357. border-top-width: 1rpx;
  358. }
  359. .use-address__header-btn-box {
  360. /* #ifndef APP-NVUE */
  361. display: flex;
  362. /* #endif */
  363. flex-direction: row;
  364. align-items: center;
  365. justify-content: center;
  366. height: 70rpx;
  367. }
  368. .use-address__header-text {
  369. text-align: center;
  370. font-size: $uni-font-size-base;
  371. color: #1aad19;
  372. line-height: 70rpx;
  373. padding-left: 40rpx;
  374. padding-right: 40rpx;
  375. }
  376. .use-address__box {
  377. position: relative;
  378. }
  379. .use-address-view {
  380. position: relative;
  381. bottom: 0;
  382. left: 0;
  383. /* #ifndef APP-NVUE */
  384. width: 100%;
  385. /* #endif */
  386. /* #ifdef APP-NVUE */
  387. width: 750rpx;
  388. /* #endif */
  389. height: 408rpx;
  390. background-color: rgba(255, 255, 255, 1);
  391. }
  392. .picker-item {
  393. text-align: center;
  394. line-height: 70rpx;
  395. text-overflow: ellipsis;
  396. font-size: 28rpx;
  397. }
  398. </style>