| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- <!-- 本插件只是个框架,适用于地址数据要从后台逐级获取的需求 -->
- <!-- 可对照注释根据业务需求更改 -->
- <!-- 作者:qq315500033 -->
- <template>
- <view>
- <!-- 选择地址展示 -->
- <view @tap="showAddress">
- {{coname}} - {{ctname}}<!-- - {{csname}} -->
- </view>
- <!-- 选择地址模态框 -->
- <view class="jm-modal" :class="showFlag==true?'show1':''">
- <view class="dialog">
- <view class="showBox">
- <view class="content">选择地址</view>
- <!-- 关闭按钮 -->
- <view class="action" @tap="hideAddress">
- x
- </view>
- </view>
- <view class="choice">
- 已选: {{coname}} - {{ctname}}<!-- - {{csname}} -->
- </view>
- <!-- 省份列表 -->
- <view class="addList">
- <view v-for="(item,index) in addressd1" :key='index' :class="{check:item.id==co}" @tap="clickAddress(1,item.id,item.name)">
- {{item.name}}
- </view>
- </view>
- <!-- 城市列表 -->
- <view class="addList">
- <view v-for="(item,index) in addressd2" :key='index' :class="{check:item.id==ct}" @tap="clickAddress(2,item.id,item.name)">
- {{item.name}}
- </view>
- </view>
- <!-- 地区列表 -->
- <!-- <view class="addList">
- <view v-for="(item,index) in addressd3" :key='index' :class="{check:item.id==cs}" @tap="clickAddress(3,item.id,item.name)">
- {{item.name}}
- </view>
- </view> -->
- </view>
- </view>
- </view>
- </template>
- <script>
- import upUrl from '../../common/url.js'
- var _self;
- export default {
- data() {
- return {
- //模态框状态
- showFlag: false,
- // 省份列表
- addressd1: [],
- // 城市列表
- addressd2: [],
- // 地区列表
- addressd3: [],
- //省份id 默认为64,可根据想要默认展示的id自行更改
- co: 2,
- //默认省份名称
- coname: '北京市',
- //城市id 默认为64,可根据想要默认展示的id自行更改
- ct: 36,
- //默认城市名称
- ctname: '北京城区',
- //地区id 默认为575,可根据想要默认展示的id自行更改
- //cs: 110101,
- //默认地区名称
- //csname: '东城区'
- };
- },
- props: {
- //载入的标签数据
- addressd: Array,
- area:{
- type:Object,
- default () {
- return {}
- }
- }
- },
- watch:{
- area:{
- handler(value){
- console.log(value)
- this.co=value.co;
- this.coname=value.coname;
- this.ct=value.ct;
- this.ctname=value.ctname;
- //this.cs=value.cs;
- //this.csname=value.csname;
- //默认获取省份列表
- console.log("area")
- this.getadd(1, 0);
- //默认获取城市列表
- this.getadd(2, this.co);
- //默认获取地区列表
- //this.getadd(3, this.ct);
- // 声明默认地址,并传送给父组件
- this.emitData();
- },
- deep:true
- }
- },
- mounted() {
- _self = this;
- /* this.getInfo(); */
- //默认获取省份列表
- this.getadd(1, 0);
- //默认获取城市列表
- this.getadd(2, this.co);
- //默认获取地区列表
- //this.getadd(3, this.ct,'mounted');
- // 声明默认地址,并传送给父组件
- this.emitData();
- },
- methods: {
- //初始化组件地址
- getInfo(){
- this.co=this.area.co;
- this.coname=this.area.coname;
- this.ct=this.area.ct;
- this.ctname=this.area.ctname;
- //this.cs=this.area.cs;
- //this.csname=this.area.csname;
- },
- //呼出模态框
- showAddress() {
- // 呼出模态框
- this.showFlag = true
- },
- // 关闭模态框
- hideAddress() {
- // 关闭模态框
- this.showFlag = null;
- // 声明默认地址,并传送给父组件
- this.emitData();
- },
- // 声明默认地址,并传送给父组件
- emitData() {
- var data = {
- coname: _self.coname,
- ctname: _self.ctname,
- //csname: _self.csname,
- co:_self.co,
- ct:_self.ct,
- //cs:_self.cs
- }
- _self.$emit("changes", data);
- },
- // 模态框地址点击赋值并获取下一级
- // --flag--- 1(省份点击);2(城市点击);3(地区点击);
- // --id----(点击的地址id);
- // --name--(点击的地址名称);
- clickAddress(flag, id, name) {
- //判断点击的状态
- switch (flag) {
- case 1:
- _self.coname = name;
- _self.co = id;
- _self.getadd(2, id);
- break;
- case 2:
- _self.ctname = name;
- _self.ct = id;
- _self.emitData();
- _self.hideAddress();
- /* _self.getadd(3, id); */
- break;
- /* case 3:
- _self.csname = name;
- _self.cs = id;
- _self.emitData();
- _self.hideAddress();
- break; */
- default:
- return;
- }
- },
- getadd(flag, id) {
- uni.request({
- url: upUrl.websiteUrl+'common/areaList',
- method:'POST',
- data: {
- parent_id:parseInt(id)
- },
- success: function(res) {
- switch (flag) {
- case 1:
- _self.addressd1 = res.data.data;
- break;
- case 2:
- _self.addressd2 = res.data.data;
- /* if(type!='mounted'){
- _self.ctname = res.data.data[0].name;
- _self.ct = res.data.data[0].id;
- } */
- /* _self.getadd(3, _self.ct); */
- break;
- /* case 3:
- _self.addressd3 = res.data.data;
- if(type!='mounted'){
- setTimeout(()=>{
- _self.csname = res.data.data[0].name;
- _self.cs = res.data.data[0].id;
- },5000)
- }
- break; */
- default:
- return;
- }
- }
- });
- // 模拟获取数据,真实场景可参考上面demo
- /* getadd(flag, id) {
- switch (flag) {
- case 1:
- _self.addressd1 = addObj;
- break;
- case 2:
- var newArr = [];
- for (var i = 0; i < addObj.length; i++) {
- if (addObj[i].id == id) {
- newArr = addObj[i].city
- }
- }
- _self.addressd2 = newArr;
- _self.ctname = newArr[0].name;
- _self.ct = newArr[0].id;
- _self.getadd(3, _self.ct);
- break;
- case 3:
- // 根据业务做相应处理
- break;
- default:
- return;
- }
- } */
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .choice {
- background-color: #fff;
- padding: 20upx;
- font-size: 28upx;
- }
- .addList {
- height: 570upx;
- box-sizing: border-box;
- overflow-y: scroll;
- width: 31%;
- float: left;
- margin-left: 1%;
- margin-right: 1%;
- font-size: 28upx;
- }
- .addList view {
- height: 70upx;
- line-height: 70upx;
- overflow: hidden;
- }
- .check {
- color: #fff;
- background-color: #FFDC16;
- }
- .jm-modal {
- position: fixed;
- top: 0;
- right: 0;
- bottom: 0;
- left: 0;
- z-index: 1110;
- opacity: 0;
- outline: 0;
- text-align: center;
- -ms-transform: scale(1.185);
- transform: scale(1.185);
- backface-visibility: hidden;
- perspective: 2000upx;
- background: rgba(0, 0, 0, 0.6);
- transition: all 0.3s ease-in-out 0s;
- pointer-events: none;
- }
- .jm-modal::before {
- content: "\200B";
- display: inline-block;
- height: 100%;
- vertical-align: middle;
- }
- .show1 {
- opacity: 1;
- transition-duration: 0.3s;
- -ms-transform: scale(1);
- transform: scale(1);
- overflow-x: hidden;
- overflow-y: auto;
- pointer-events: auto;
- }
- .dialog {
- position: relative;
- display: inline-block;
- vertical-align: middle;
- margin-left: auto;
- margin-right: auto;
- width: 680upx;
- max-width: 100%;
- height: 700upx;
- background-color: #f8f8f8;
- border-radius: 10upx;
- overflow: hidden;
- }
- .action {
- position: absolute;
- right: 30upx;
- }
- .content {
- position: absolute;
- text-align: center;
- width: calc(100% - 340upx);
- left: 0;
- right: 0;
- bottom: 0;
- top: 0;
- margin: auto;
- height: 60upx;
- font-size: 32upx;
- line-height: 60upx;
- cursor: none;
- pointer-events: none;
- text-overflow: ellipsis;
- white-space: nowrap;
- overflow: hidden;
- }
- .showBox {
- background-color: #fff;
- display: flex;
- position: relative;
- align-items: center;
- min-height: 100upx;
- justify-content: space-between;
- }
- </style>
|