jm-address.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <!-- 本插件只是个框架,适用于地址数据要从后台逐级获取的需求 -->
  2. <!-- 可对照注释根据业务需求更改 -->
  3. <!-- 作者:qq315500033 -->
  4. <template>
  5. <view>
  6. <!-- 选择地址展示 -->
  7. <view @tap="showAddress">
  8. {{coname}} - {{ctname}}<!-- - {{csname}} -->
  9. </view>
  10. <!-- 选择地址模态框 -->
  11. <view class="jm-modal" :class="showFlag==true?'show1':''">
  12. <view class="dialog">
  13. <view class="showBox">
  14. <view class="content">选择地址</view>
  15. <!-- 关闭按钮 -->
  16. <view class="action" @tap="hideAddress">
  17. x
  18. </view>
  19. </view>
  20. <view class="choice">
  21. 已选: {{coname}} - {{ctname}}<!-- - {{csname}} -->
  22. </view>
  23. <!-- 省份列表 -->
  24. <view class="addList">
  25. <view v-for="(item,index) in addressd1" :key='index' :class="{check:item.id==co}" @tap="clickAddress(1,item.id,item.name)">
  26. {{item.name}}
  27. </view>
  28. </view>
  29. <!-- 城市列表 -->
  30. <view class="addList">
  31. <view v-for="(item,index) in addressd2" :key='index' :class="{check:item.id==ct}" @tap="clickAddress(2,item.id,item.name)">
  32. {{item.name}}
  33. </view>
  34. </view>
  35. <!-- 地区列表 -->
  36. <!-- <view class="addList">
  37. <view v-for="(item,index) in addressd3" :key='index' :class="{check:item.id==cs}" @tap="clickAddress(3,item.id,item.name)">
  38. {{item.name}}
  39. </view>
  40. </view> -->
  41. </view>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. import upUrl from '../../common/url.js'
  47. var _self;
  48. export default {
  49. data() {
  50. return {
  51. //模态框状态
  52. showFlag: false,
  53. // 省份列表
  54. addressd1: [],
  55. // 城市列表
  56. addressd2: [],
  57. // 地区列表
  58. addressd3: [],
  59. //省份id 默认为64,可根据想要默认展示的id自行更改
  60. co: 2,
  61. //默认省份名称
  62. coname: '北京市',
  63. //城市id 默认为64,可根据想要默认展示的id自行更改
  64. ct: 36,
  65. //默认城市名称
  66. ctname: '北京城区',
  67. //地区id 默认为575,可根据想要默认展示的id自行更改
  68. //cs: 110101,
  69. //默认地区名称
  70. //csname: '东城区'
  71. };
  72. },
  73. props: {
  74. //载入的标签数据
  75. addressd: Array,
  76. area:{
  77. type:Object,
  78. default () {
  79. return {}
  80. }
  81. }
  82. },
  83. watch:{
  84. area:{
  85. handler(value){
  86. console.log(value)
  87. this.co=value.co;
  88. this.coname=value.coname;
  89. this.ct=value.ct;
  90. this.ctname=value.ctname;
  91. //this.cs=value.cs;
  92. //this.csname=value.csname;
  93. //默认获取省份列表
  94. console.log("area")
  95. this.getadd(1, 0);
  96. //默认获取城市列表
  97. this.getadd(2, this.co);
  98. //默认获取地区列表
  99. //this.getadd(3, this.ct);
  100. // 声明默认地址,并传送给父组件
  101. this.emitData();
  102. },
  103. deep:true
  104. }
  105. },
  106. mounted() {
  107. _self = this;
  108. /* this.getInfo(); */
  109. //默认获取省份列表
  110. this.getadd(1, 0);
  111. //默认获取城市列表
  112. this.getadd(2, this.co);
  113. //默认获取地区列表
  114. //this.getadd(3, this.ct,'mounted');
  115. // 声明默认地址,并传送给父组件
  116. this.emitData();
  117. },
  118. methods: {
  119. //初始化组件地址
  120. getInfo(){
  121. this.co=this.area.co;
  122. this.coname=this.area.coname;
  123. this.ct=this.area.ct;
  124. this.ctname=this.area.ctname;
  125. //this.cs=this.area.cs;
  126. //this.csname=this.area.csname;
  127. },
  128. //呼出模态框
  129. showAddress() {
  130. // 呼出模态框
  131. this.showFlag = true
  132. },
  133. // 关闭模态框
  134. hideAddress() {
  135. // 关闭模态框
  136. this.showFlag = null;
  137. // 声明默认地址,并传送给父组件
  138. this.emitData();
  139. },
  140. // 声明默认地址,并传送给父组件
  141. emitData() {
  142. var data = {
  143. coname: _self.coname,
  144. ctname: _self.ctname,
  145. //csname: _self.csname,
  146. co:_self.co,
  147. ct:_self.ct,
  148. //cs:_self.cs
  149. }
  150. _self.$emit("changes", data);
  151. },
  152. // 模态框地址点击赋值并获取下一级
  153. // --flag--- 1(省份点击);2(城市点击);3(地区点击);
  154. // --id----(点击的地址id);
  155. // --name--(点击的地址名称);
  156. clickAddress(flag, id, name) {
  157. //判断点击的状态
  158. switch (flag) {
  159. case 1:
  160. _self.coname = name;
  161. _self.co = id;
  162. _self.getadd(2, id);
  163. break;
  164. case 2:
  165. _self.ctname = name;
  166. _self.ct = id;
  167. _self.emitData();
  168. _self.hideAddress();
  169. /* _self.getadd(3, id); */
  170. break;
  171. /* case 3:
  172. _self.csname = name;
  173. _self.cs = id;
  174. _self.emitData();
  175. _self.hideAddress();
  176. break; */
  177. default:
  178. return;
  179. }
  180. },
  181. getadd(flag, id) {
  182. uni.request({
  183. url: upUrl.websiteUrl+'common/areaList',
  184. method:'POST',
  185. data: {
  186. parent_id:parseInt(id)
  187. },
  188. success: function(res) {
  189. switch (flag) {
  190. case 1:
  191. _self.addressd1 = res.data.data;
  192. break;
  193. case 2:
  194. _self.addressd2 = res.data.data;
  195. /* if(type!='mounted'){
  196. _self.ctname = res.data.data[0].name;
  197. _self.ct = res.data.data[0].id;
  198. } */
  199. /* _self.getadd(3, _self.ct); */
  200. break;
  201. /* case 3:
  202. _self.addressd3 = res.data.data;
  203. if(type!='mounted'){
  204. setTimeout(()=>{
  205. _self.csname = res.data.data[0].name;
  206. _self.cs = res.data.data[0].id;
  207. },5000)
  208. }
  209. break; */
  210. default:
  211. return;
  212. }
  213. }
  214. });
  215. // 模拟获取数据,真实场景可参考上面demo
  216. /* getadd(flag, id) {
  217. switch (flag) {
  218. case 1:
  219. _self.addressd1 = addObj;
  220. break;
  221. case 2:
  222. var newArr = [];
  223. for (var i = 0; i < addObj.length; i++) {
  224. if (addObj[i].id == id) {
  225. newArr = addObj[i].city
  226. }
  227. }
  228. _self.addressd2 = newArr;
  229. _self.ctname = newArr[0].name;
  230. _self.ct = newArr[0].id;
  231. _self.getadd(3, _self.ct);
  232. break;
  233. case 3:
  234. // 根据业务做相应处理
  235. break;
  236. default:
  237. return;
  238. }
  239. } */
  240. }
  241. }
  242. }
  243. </script>
  244. <style lang="scss" scoped>
  245. .choice {
  246. background-color: #fff;
  247. padding: 20upx;
  248. font-size: 28upx;
  249. }
  250. .addList {
  251. height: 570upx;
  252. box-sizing: border-box;
  253. overflow-y: scroll;
  254. width: 31%;
  255. float: left;
  256. margin-left: 1%;
  257. margin-right: 1%;
  258. font-size: 28upx;
  259. }
  260. .addList view {
  261. height: 70upx;
  262. line-height: 70upx;
  263. overflow: hidden;
  264. }
  265. .check {
  266. color: #fff;
  267. background-color: #FFDC16;
  268. }
  269. .jm-modal {
  270. position: fixed;
  271. top: 0;
  272. right: 0;
  273. bottom: 0;
  274. left: 0;
  275. z-index: 1110;
  276. opacity: 0;
  277. outline: 0;
  278. text-align: center;
  279. -ms-transform: scale(1.185);
  280. transform: scale(1.185);
  281. backface-visibility: hidden;
  282. perspective: 2000upx;
  283. background: rgba(0, 0, 0, 0.6);
  284. transition: all 0.3s ease-in-out 0s;
  285. pointer-events: none;
  286. }
  287. .jm-modal::before {
  288. content: "\200B";
  289. display: inline-block;
  290. height: 100%;
  291. vertical-align: middle;
  292. }
  293. .show1 {
  294. opacity: 1;
  295. transition-duration: 0.3s;
  296. -ms-transform: scale(1);
  297. transform: scale(1);
  298. overflow-x: hidden;
  299. overflow-y: auto;
  300. pointer-events: auto;
  301. }
  302. .dialog {
  303. position: relative;
  304. display: inline-block;
  305. vertical-align: middle;
  306. margin-left: auto;
  307. margin-right: auto;
  308. width: 680upx;
  309. max-width: 100%;
  310. height: 700upx;
  311. background-color: #f8f8f8;
  312. border-radius: 10upx;
  313. overflow: hidden;
  314. }
  315. .action {
  316. position: absolute;
  317. right: 30upx;
  318. }
  319. .content {
  320. position: absolute;
  321. text-align: center;
  322. width: calc(100% - 340upx);
  323. left: 0;
  324. right: 0;
  325. bottom: 0;
  326. top: 0;
  327. margin: auto;
  328. height: 60upx;
  329. font-size: 32upx;
  330. line-height: 60upx;
  331. cursor: none;
  332. pointer-events: none;
  333. text-overflow: ellipsis;
  334. white-space: nowrap;
  335. overflow: hidden;
  336. }
  337. .showBox {
  338. background-color: #fff;
  339. display: flex;
  340. position: relative;
  341. align-items: center;
  342. min-height: 100upx;
  343. justify-content: space-between;
  344. }
  345. </style>