simple-address.vue 13 KB

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