Winglau14-lotusAddress.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <template>
  2. <!--地址picker-->
  3. <view>
  4. <view :status="checkStatus" v-if="lotusAddressData.visible" class="lotus-address-mask" @tap="cancelPicker"></view>
  5. <view v-if="lotusAddressData.visible"
  6. :class="lotusAddressData.visible?'lotus-address-box':'lotus-address-box lotus-address-box-out'">
  7. <view class="lotus-address-action">
  8. <text @tap="cancelPicker" class="lotus-address-action-cancel">取消</text>
  9. <text @tap="chosedVal" class="lotus-address-action-affirm">确认</text>
  10. </view>
  11. <view class="lotus-address-picker-box">
  12. <!--省-->
  13. <scroll-view scroll-y :scroll-into-view="'pid'+pChoseIndex" class="lotus-address-picker-box-item">
  14. <view @tap="clickPicker(0,pIndex,pItem);" :id="'pid'+pIndex"
  15. :class="pIndex === pChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'"
  16. v-for="(pItem,pIndex) in province" :key="pIndex">{{pItem}}</view>
  17. </scroll-view>
  18. <!--市-->
  19. <scroll-view scroll-y :scroll-into-view="'cid'+cChoseIndex" class="lotus-address-picker-box-item">
  20. <view @tap="clickPicker(1,cIndex,cItem);" :id="'cid'+cIndex"
  21. :class="cIndex === cChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'"
  22. v-for="(cItem,cIndex) in city" :key="cIndex">{{cItem}}</view>
  23. </scroll-view>
  24. <!--区-->
  25. <scroll-view scroll-y :scroll-into-view="'tid'+tChoseIndex" class="lotus-address-picker-box-item">
  26. <view @tap="clickPicker(2,tIndex,tItem);" :id="'tid'+tIndex"
  27. :class="tIndex === tChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'"
  28. v-for="(tItem,tIndex) in town" :key="tIndex">{{tItem}}</view>
  29. </scroll-view>
  30. <!--区END-->
  31. </view>
  32. </view>
  33. </view>
  34. <!--地址picker END-->
  35. </template>
  36. <script>
  37. import {
  38. lotusAddressJson
  39. } from "./Winglau14-lotusAddress.js";
  40. export default {
  41. props: ['lotusAddressData'],
  42. data() {
  43. return {
  44. visible: false,
  45. province: [],
  46. city: [],
  47. town: [],
  48. provinceName: '',
  49. cityName: '',
  50. townName: '',
  51. type: 0, //0新增1编辑
  52. pChoseIndex: -1,
  53. cChoseIndex: -1,
  54. tChoseIndex: -1
  55. };
  56. },
  57. methods: {
  58. //取消
  59. cancelPicker() {
  60. this.$emit('close')
  61. },
  62. //获取最后选择的省市区的值
  63. chosedVal() {
  64. this.type = 1;
  65. const provinceCode = this.getTarId(this.provinceName);
  66. const cityCode = this.getTarId(this.cityName);
  67. const townCode = this.getTarId(this.townName);
  68. this.visible = false;
  69. let id = null;
  70. //省市区已选最末一项id
  71. if (this.provinceName && this.cityName && this.townName) {
  72. id = townCode
  73. } else if (this.provinceName && this.cityName && !this.townName) {
  74. id = cityCode
  75. } else if (this.provinceName && !this.cityName && !this.townName) {
  76. id = provinceCode
  77. }
  78. this.$emit("choseVal", {
  79. province: this.provinceName,
  80. provinceCode,
  81. city: this.cityName,
  82. cityCode,
  83. town: this.townName,
  84. townCode,
  85. id,
  86. visible: false
  87. });
  88. },
  89. //获取省市区value
  90. getTarId(name, type) {
  91. let id = 0;
  92. lotusAddressJson.map((item, index) => {
  93. if (item.name === name) {
  94. id = item.value;
  95. }
  96. });
  97. return id;
  98. },
  99. //获取市数据
  100. getCityArr(parentId) {
  101. let city = [];
  102. lotusAddressJson.map((item, index) => {
  103. if (item.parent === parentId) {
  104. city.push(item.name);
  105. }
  106. });
  107. return city;
  108. },
  109. //获取区数据
  110. getTownArr(parentId) {
  111. let town = [];
  112. lotusAddressJson.map((item, index) => {
  113. if (index > 34 && item.parent === parentId) {
  114. town.push(item.name);
  115. }
  116. });
  117. return town;
  118. },
  119. //初始化数据
  120. initFn() {
  121. if (!this.province.length) {
  122. lotusAddressJson.map((item, index) => {
  123. if (item.parent <= 34) {
  124. this.province.push(item.name);
  125. }
  126. });
  127. }
  128. //已选择省市区,高亮显示对应选择省市区
  129. const p = this._props.lotusAddressData.provinceName;
  130. const c = this._props.lotusAddressData.cityName;
  131. const t = this._props.lotusAddressData.townName;
  132. //已选省
  133. if (p) {
  134. this.pChoseIndex = this.getTarIndex(this.province, p);
  135. }
  136. //已选市
  137. if (p && c) {
  138. const pid = this.getTarId(p);
  139. this.city = this.getCityArr(pid);
  140. this.cChoseIndex = this.getTarIndex(this.city, c);
  141. }
  142. //已选区
  143. if (p && c && t) {
  144. const cid = this.getTarId(c);
  145. this.town = this.getTownArr(cid);
  146. this.tChoseIndex = this.getTarIndex(this.town, t);
  147. }
  148. //未选省市区
  149. if (!p && !c && !t) {
  150. this.pChoseIndex = -1;
  151. this.cChoseIndex = -1;
  152. this.tChoseIndex = -1;
  153. this.city = [];
  154. this.town = [];
  155. }
  156. },
  157. //获取已选省市区
  158. getChosedData() {
  159. const pid = this.getTarId(this.provinceName, 'province');
  160. this.city = this.getCityArr(pid);
  161. const cid = this.getTarId(this.cityName, 'city');
  162. this.town = this.getTownArr(cid);
  163. //已选省市区获取对应index
  164. if (this.provinceName) {
  165. this.pChoseIndex = this.getTarIndex(this.province, this.provinceName);
  166. }
  167. if (this.cityName) {
  168. this.cChoseIndex = this.getTarIndex(this.city, this.cityName);
  169. }
  170. if (this.townName) {
  171. this.tChoseIndex = this.getTarIndex(this.town, this.townName);
  172. }
  173. },
  174. //选择省市区交互
  175. clickPicker(type, index, name) {
  176. //省
  177. if (type === 0) {
  178. // this.pChoseIndex = this.pChoseIndex >= 0 ? -1 : index
  179. // this.provinceName = this.pChoseIndex >= 0 ? name : "";
  180. if (this.pChoseIndex === index) {
  181. this.pChoseIndex = -1;
  182. this.provinceName = "";
  183. } else {
  184. this.pChoseIndex = index;
  185. this.provinceName = name;
  186. }
  187. this.cChoseIndex = -1;
  188. this.tChoseIndex = -1;
  189. this.cityName = '';
  190. this.townName = '';
  191. }
  192. //市
  193. if (type === 1) {
  194. if (this.cChoseIndex === index) {
  195. this.cChoseIndex = -1;
  196. this.cityName = "";
  197. } else {
  198. this.cChoseIndex = index;
  199. this.cityName = name;
  200. }
  201. this.tChoseIndex = -1;
  202. this.townName = '';
  203. }
  204. //区
  205. if (type === 2) {
  206. if (this.tChoseIndex === index) {
  207. this.tChoseIndex = -1;
  208. this.townName = "";
  209. } else {
  210. this.tChoseIndex = index;
  211. this.townName = name;
  212. }
  213. }
  214. //获取省市区数据
  215. this.getChosedData();
  216. },
  217. //获取已选省市区index
  218. getTarIndex(arr, tarName) {
  219. let cIndex = 0;
  220. arr.map((item, index) => {
  221. if (item === tarName) {
  222. cIndex = index;
  223. }
  224. });
  225. return cIndex;
  226. }
  227. },
  228. computed: {
  229. checkStatus() {
  230. let t = null;
  231. const _this = this;
  232. if (!_this.visible) {
  233. _this.visible = _this._props.lotusAddressData.visible;
  234. //获取省市区
  235. _this.provinceName = _this._props.lotusAddressData.provinceName;
  236. _this.cityName = _this._props.lotusAddressData.cityName;
  237. _this.townName = _this._props.lotusAddressData.townName;
  238. //生成初始化数据
  239. _this.initFn();
  240. t = _this.visible;
  241. }
  242. return t;
  243. }
  244. }
  245. }
  246. </script>
  247. <style lang="scss" scoped>
  248. .lotus-address-picker {
  249. font-size: 26rpx;
  250. padding-top: 30rpx;
  251. overflow: hidden;
  252. text-overflow: ellipsis;
  253. display: -webkit-box;
  254. -webkit-line-clamp: 1;
  255. -webkit-box-orient: vertical;
  256. line-height: normal;
  257. padding-right: 30rpx;
  258. box-sizing: border-box;
  259. }
  260. .lotus-address-picker-box {
  261. /*display: -webkit-box;
  262. display: -webkit-flex;*/
  263. display: flex;
  264. align-items: center;
  265. justify-content: center;
  266. justify-content: flex-start;
  267. padding-top: 10rpx;
  268. padding-bottom: 10rpx;
  269. }
  270. .lotus-address-picker-box-item {
  271. height: 600upx;
  272. overflow-y: auto;
  273. width: 33.333%;
  274. padding-left: 20rpx;
  275. padding-right: 20rpx;
  276. box-sizing: border-box;
  277. }
  278. .lotus-address-picker2 {
  279. color: #2C6FF3;
  280. position: relative;
  281. }
  282. .lotus-address-picker2:after {
  283. content: '';
  284. position: absolute;
  285. right: 0;
  286. top: 65%;
  287. transform: translateY(-35%) rotate(-45deg);
  288. width: 20rpx;
  289. height: 10rpx;
  290. border-left-width: 4rpx;
  291. border-bottom-width: 4rpx;
  292. border-left-style: solid;
  293. border-bottom-style: solid;
  294. border-left-color: #2C6FF3;
  295. border-bottom-color: #2C6FF3;
  296. }
  297. .lotus-address-mask {
  298. position: fixed;
  299. left: 0;
  300. top: 0;
  301. width: 100%;
  302. height: 100%;
  303. z-index: 999;
  304. background: rgba(0, 0, 0, 0.5);
  305. }
  306. .lotus-address-box {
  307. background: #fff;
  308. position: fixed;
  309. left: 0;
  310. bottom: 0;
  311. width: 100%;
  312. height: auto;
  313. z-index: 10000;
  314. }
  315. .lotus-address-action {
  316. font-size: 30rpx;
  317. /*display: -webkit-box;
  318. display: -webkit-flex;*/
  319. display: flex;
  320. align-items: center;
  321. justify-content: center;
  322. justify-content: space-between;
  323. padding: 25rpx 30rpx;
  324. position: relative;
  325. }
  326. .lotus-address-action:after {
  327. content: " ";
  328. position: absolute;
  329. left: 0;
  330. top: 0;
  331. right: 0;
  332. height: 1px;
  333. border-top: 1px solid #eee;
  334. color: #eee;
  335. transform-origin: 0 0;
  336. transform: scaleY(0.5);
  337. }
  338. .lotus-address-action:before {
  339. content: " ";
  340. position: absolute;
  341. left: 0;
  342. bottom: 0;
  343. right: 0;
  344. height: 1px;
  345. border-bottom: 1px solid #eee;
  346. color: #eee;
  347. transform-origin: 0 100%;
  348. transform: scaleY(0.5);
  349. }
  350. .lotus-address-action-cancel {
  351. color: #969696;
  352. }
  353. .lotus-address-action-affirm {
  354. color: #2C6FF3;
  355. }
  356. </style>