list.vue 12.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
<template>
    <div>
        <div class="cover-back-menu" @click="pageBack"></div>
        <div v-if="showEditAddress" class="edit-address-wrap">
            <div class="tip">为提高配送时效,请您尽量准确填写四级地址。</div>
            <div class="form-block" style="min-height: 578px;">
                <form class="edit-address">
                    <input type="hidden" name="id" value="">
                    <label class="username">
                        收 货 人 :
                        <input type="text" name="consignee" maxlength="21" v-model="editAddressInfo.consignee">
                    </label>
                    <label class="mobile">
                        联系电话:
                        <input type="text" name="mobile" v-model="editAddressInfo.mobile">
                    </label>
                    <label class="area" @click="showAct">
                        所在地区:
                        <input type="hidden" name="area_code" v-bind:value="editAddressInfo.areaCode">
                        <input type="text" name="area" v-bind:value="editAddressInfo.showArea" readonly="">
                        <span class="iconfont"></span>
                    </label>
                    <label class="address">
                        详细地址:
                        <textarea name="address" maxlength="255" v-model="editAddressInfo.address"></textarea>
                    </label>
                </form>

                <div class="submit" @click="saveAddress">确认</div>
            </div>
            <address-act></address-act>
        </div>
        <div v-else class="address-list-wrap">
            <div class="address-list">
                <div class="address-item" v-for="(addr, index) in addressList" @click="choseAddress(addr)" :key="index">
                    <span class="name">{{addr.consignee}}</span>
                    <span class="tel">{{addr.mobile}}</span>
                    <p class="address-info">{{addr.area}} {{addr.address}}</p>
                    <div class="action iconfont">
                        <span class="edit" @click.stop="editAddress(addr)"></span>
                        <span class="del" @click.stop="delAddress(addr.addressId)"></span>
                    </div>
                </div>
            </div>
            <div class="tip">为提高配送时效,请您尽量准确填写四级地址。</div>
            <div class="add-address" @click="addAddress">添加新地址</div>
        </div>
    </div>
</template>

<script>
    import addressAct from 'vues/home/address/address-act.vue';
    const $ = require('yoho-jquery');
    const tip = require('js/plugin/tip');
    const bus = require('js/plugin/vue-bus');
    const modal = require('js/plugin/modal2');

    module.exports = {
        props: ['address'],
        data() {
            return {
                addressList: [],
                showEditAddress: false,
                editAddressInfo: {},
                csrf: $('input[name=_csrf]').val()
            };
        },
        created() {
            bus.$on('modify.addressAct', info => {
                if (info) {
                    this.editAddressInfo.areaCode = info.code;
                    this.editAddressInfo.showArea = info.area;
                }
            });

            this.reflushAddressList();
        },
        methods: {
            htmlDecode(str) {
                str = str || '';
                return str
                    .replace(str ? /&(?!#?\w+;)/g : /&/g, '&amp;')
                    .replace(/&lt;/g, '<')
                    .replace(/&gt;/g, '>')
                    .replace(/&quot;/g, '"')
                    .replace(/&#39;/g, '\'');
            },
            reflushAddressList() {
                $.ajax({
                    url: '/home/address.json'
                }).then(res => {
                    if (res && res.address) {
                        res.address.forEach((item) => {
                            item.consignee = this.htmlDecode(item.consignee);
                            item.address = this.htmlDecode(item.address);
                            return item;
                        });

                        this.addressList = res.address;
                    }
                });
            },
            pageBack() {
                if (this.showEditAddress) {
                    this.showEditAddress = false;
                } else {
                    bus.$emit('modify.exchangeAddress', false);
                }
            },
            choseAddress(addr) {
                bus.$emit('modify.exchangeAddress', addr);
            },
            editAddress(addr) {
                this.editAddressInfo = addr;
                this.showEditAddress = true;
            },
            delAddress(id) {
                let that = this;

                $.ajax({
                    method: 'POST',
                    url: '/home/delAddress',
                    data: {
                        id: id,
                        _csrf: this.csrf
                    }
                }).then(result => {
                    if (result.code === 200) {
                        let lastIndex;

                        that.addressList.forEach((element, index) => {
                            if (element.addressId === id) {
                                lastIndex = index;
                            }
                        });

                        if (typeof lastIndex !== 'undefined') {
                            that.addressList.splice(lastIndex, 1);
                        }
                    } else {
                        modal.alert(result.message);
                    }
                });
            },
            addAddress() {
                if (this.addressList.length >= 5) {
                    return tip.show('您最多添加5个收货地址');
                }

                this.editAddress({
                    consignee: '',
                    mobile: '',
                    areaCode: '',
                    showArea: '',
                    address: ''
                });
            },
            validator() {
                let info = this.editAddressInfo;
                let username = info.consignee.replace(/(^\s+)|(\s+$)/g, '');


                // 简单的表单校验
                if (!username) {
                    tip.show('收件人不能为空');
                    return false;
                }

                if (!/^[\u4E00-\u9FA5A-Za-z0-9*]+$/gi.test(username)) {
                    tip.show('收货人姓名不支持特殊符号');
                    return false;
                }

                if (!info.mobile) {
                    tip.show('手机号不能为空');
                    return false;
                }
                if (!info.areaCode || !info.showArea ||
                    info.areaCode.length < 6) {
                    tip.show('省市区不能为空');
                    return false;
                }
                if (!info.address) {
                    tip.show('地址不能为空');
                    return false;
                }

                return {
                    id: info.addressId || '',
                    consignee: username,
                    mobile: info.mobile,
                    area_code: info.areaCode,
                    area: info.showArea,
                    address: info.address
                };

            },
            saveAddress() {
                let that = this;
                let data = this.validator();

                this.submiting = true;

                if (!data) {
                    return;
                }

                data._csrf = this.csrf;

                $.ajax({
                    method: 'POST',
                    url: '/home/saveAddress',
                    data: data
                }).then(function(res) {
                    if (res && res.code === 200) {
                        that.reflushAddressList();
                        that.showEditAddress = false;
                    } else {
                        tip.show(res.message || '网络出了点问题~');
                    }
                    that.submiting = false;
                });
            },
            showAct() {
                this.$children[0].show = true;
            }
        },
        components: {
            addressAct
        }
    };
</script>

<style lang="scss">
    .cover-back-menu {
        width: 105px;
        height: 105px;
        position: absolute;
        margin-top: -105px;
        z-index: 100;
    }

    .edit-address-wrap {
        > .tip {
            padding: 20px;
            text-align: center;
            color: #aeaeae;
        }

        .form-block {
            width: 100%;
            color: #d0d0d0;
            background: #f0f0f0;
        }

        .edit-address {
            padding: 0 30px;
            background: #fff;
            font-size: 30px;
            line-height: 88px;
            border-bottom: 1px solid #e0e0e0;

            label {
                display: block;
                position: relative;

                &:after {
                    content: "";
                    position: absolute;
                    right: -30px;
                    bottom: 0;
                    width: 610px;
                    height: 0;
                    border-top: 1px solid #e0e0e0;
                }

                &:last-of-type:after {
                    content: none;
                }

                .iconfont {
                    position: absolute;
                    right: 0;
                    top: 0;
                }
            }

            input,
            textarea {
                position: absolute;
                top: 0;
                right: 40px;
                width: 360px;
                height: 88px;
                color: #444;
                padding: 0;
                border: none;
                -webkit-appearance: none;
            }

            p {
                position: absolute;
                top: 0;
                right: 40px;
                width: 360px;
                height: 88px;
                color: #444;
                padding: 0;
                border: none;
            }

            .address {
                height: 176px;
            }

            textarea {
                right: 0;
                width: 400px;
                height: 58px * 2;
                padding: 20px 0;
                resize: none;
            }
        }

        .submit {
            margin: 30px auto 0;
            width: 470px;
            height: 88px;
            color: #fff;
            background: #444;
            text-align: center;
            font-size: 32px;
            line-height: 88px;

            &.highlight {
                background: rgba(0, 0, 0, 0.6);
            }
        }
    }

    .address-list-wrap {
        > .tip {
            padding: 20px;
            text-align: center;
            color: #aeaeae;
        }

        .address-item {
            display: block;
            padding: 20px 30px;
            color: #b0b0b0;
            background: #fff;
            border-bottom: 1px solid #e0e0e0;

            .name,
            .tel {
                font-size: 30px;
                line-height: 56px;
                color: #444;
                font-weight: bold;
            }

            .name {
                display: inline-block;
                max-width: 380px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }

            .tel {
                float: right;
            }

            .address-info {
                font-size: 24px;
                line-height: 38px;
            }

            .action {
                font-size: 32px;
                line-height: 60px;
                text-align: right;

                .edit,
                .del {
                    display: inline-block;
                    text-align: center;
                    width: 60px;
                    height: 60px;
                    color: #999;

                    &:hover {
                        color: #666;
                    }
                }

                .edit {
                    margin-right: 20px;
                }
            }
        }

        .add-address {
            display: block;
            margin-bottom: 30px;
            font-size: 32px;
            line-height: 88px;
            color: #444;
            background: #fff;
            text-align: center;
            font-weight: bold;
            border-top: 1px solid #e0e0e0;
            border-bottom: 1px solid #e0e0e0;
        }
    }

</style>