addressList.vue 11.6 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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
<template>
  <div class="address-list-page">

    <!-- 导航栏 -->
    <div class="nav">

      <!-- navBar -->
      <nav-bar @back="back"
               :navs="navs">
      </nav-bar>
    </div>

    <div class="pl"></div>

    <!-- 收货地址列表 -->
    <address-list @doSelect="doSelect"
                  @doEdit="doEdit"
                  @doDelete="doDelete"
                  @setDefaultContact="setDefaultContact"
                  :addresss="addresss">
    </address-list>

    <!-- 添加地址按钮 -->
    <address-create-bar v-if="!query.mode || query.mode.toLowerCase() != 'select'"
                        @doCreate="doCreate"></address-create-bar>
  </div>
</template>

<script>

// 引入组件
import navBar from '@/components/navBar/navBar';
import addressList from "@/components/addressList/addressList.vue";
import addressCreateBar from "@/components/addressCreateBar/addressCreateBar.vue";
import Vue from "vue";
import { Toast, Indicator } from "mint-ui";
export default {
  name: 'addressListPage',
  data () {
    return {
      title: "",
      query: {},
      navs: null,							// navBar配置参数
      showToasting: false,				// 显示操作结果
      addresss: null,						// 地址列表数据
      creates: {							// 创建按钮对象
        create: {
          func: this.doCreate
        }
      }
    }
  },
  components: {
    navBar,
    addressList,
    addressCreateBar
  },
  created () {
    // 设置标题
    this.$set(this, 'title', '收货地址');

    // 设置地址栏请求参数
    this.$set(this, "query", this.$route.query);

    this.$set(this, "navs", {
      back: {
        img: "static/img/icon_back.png"
      },
      title: this.title
    });

    // #ifdef H5 || APP-PLUS

    // 更新页面title
    this.$emit("updateHead");

    // #endif

    // 获取地址列表
    this.getMyAddress();

    // 获取地址操作结果,如果有,输出结果
    const value = window.localStorage.getItem('addressOption');

    if (value) {

      // 输出地址操作结果
      Toast(value);

      // showToasting
      this.$set(this, 'showToasting', true);

      // 删除地址操作结果
      window.localStorage.removeItem('addressOption');
    }
  },
  mounted () {


  },
  // #endif
  methods: {

    /**
     * 获取地址列表
     */
    getMyAddress () {

      // 必要参数
      if (this.getStore("uid")) {

        // 显示loading
        Indicator.open('加载中...');

        // 请求接口
        this.http("/Api/" + (this.getApiVersion().index) + "/myAddress", {
          uid: this.getStore("uid"),
        }, this.getMyAddressCallback, {
            method: "POST"
          });
      }
    },

    /**
     * 获取地址列表回调
     * @param {Object} res [服务器返回数据]
     * @param {Object} res [扩展参数]
     */
    getMyAddressCallback (res, ext) {
      Indicator.close();
      // 返回处理
      if (res.code == 200) {
        // 解构数据
        let datas = res.data;
        // 添加加星电话号码
        for (let i = 0; i < datas.length; i++) {
          this.$set(datas[i], "phoneS", this.formatPhoneNumber(datas[i].phone, 3, 4));
        }

        // 设置地址列表对象
        this.addresss = {};
        this.addresss.list = datas;
        this.addresss.options = {
          select: {},
          edit: {},
          delete: {},
          defaultContact: {}
        };
        /**
         * 列表功能模式配置
         * mode:
         * select: 仅可选择地址,不能做其它操作
         * select_edit: 选择地址,可编辑操作
         */

        // 根据参数判断是否要隐藏部分功能
        if (this.query.mode) {

          // 判断来源页面
          switch (this.query.mode) {
            case 'select':

              // 设置地址簿显示隐藏功能按钮
              this.$set(this.addresss.options.defaultContact, 'hide', true);		// 隐藏“设为默认”按钮
              this.$set(this.addresss.options.delete, 'hide', true);				// 隐藏“删除”按钮
              this.$set(this.addresss.options.edit, 'hide', true);				// 隐藏“编辑”按钮
              break;
          }
        }

        // 没有提示内容
        // if (!Toasting) {

        //   // 隐藏loading
        //   uni.hideLoading();
        // } else {

        //   // showToasting
        //   this.$set(this, 'showToasting', false);
        // }
      } else if (res.code == 400) {

        // 清空现有地址列表数据
        this.$set(this, 'addresss', null);

        // 获取地址列表出错
        Toast(res.message);
      } else {

        // 获取地址列表出错
        Toast("获取地址列表出错");
      }
    },

    /**
     * 我的首选地址
     */
    getMyFirstAddress () {

      // 校验必要参数
      if (this.getStore("uid")) {

        // 请求接口
        this.http("/Api/" + (this.getApiVersion().index) + "/myFirstAddress", {
          uid: this.getStore("uid")
        }, this.getMyFirstAddressCallback, {
            method: "POST"
          })
      }
    },

    /**
     * 我的首选地址回调
     * @param {object} res [服务器返回数据]
     * @param {object} ext [扩展参数]
     */
    getMyFirstAddressCallback (res, ext) {

      // 返回处理
      if (res.code == 200) {

        // 解构数据
        let datas = res.data;


        if (datas.uid === this.getStore("uid")) {

          // 同步当前选中的地址信息
          window.localStorage.setItem('currentAddress', JSON.stringify(datas));
        }
      }
    },

    /**
     * 跳转添加收货地址页面
     */
    doCreate () {
      // 跳转添加收货地址页面
      this.$router.push({
        path: "addressCreate",
        uid: this.getStore("uid")
      })
    },

    /**
     * 选择地址
     * @param {Object} item [地址对象]
     */
    doSelect (item) {
      // 判断来源页面
      switch (this.query.mode) {
        case 'select':			// 选择状态,只可选择地址,不能编辑
        case "select_edit":		// 选择状态,可选择地址,可以编辑

          // 验证工具类是否存在
          // 设置选择的地址到Storage(当前选择的地址)
          window.localStorage.setItem('currentAddress', JSON.stringify(item));

          // 后退
          this.back();
          break;
        default:

          // 编辑地址
          this.doEdit(item);
      }
    },

    /**
     * 编辑地址
     * @param {Object} item [地址对象]
     */
    doEdit (item) {
      // 跳转编辑收货地址页面
      this.$router.push({
        path: "addressEdit",
        query: {
          id: item.id,
          uid: this.getStore("uid")
        }
      })
    },

    /**
     * 删除地址操作
     * @param {Object} item [地址信息对象]
     */
    doDelete (item) {
      // 提示用积分值兑换兑换
      let options = {
        title: "",
        message: "确定删除收件人地址吗?",
        confirmButtonText: "确定",
        confirmButtonCallback: this.doDeleteConfirm,
        showCancelButton: true,
        cancelButtonText: "取消",
        params: item
      };
      // 调用showModal层参数
      this.$set(this.$parent, "showOptions", options);
      // 显示通用浮层
      this.$parent.$refs.showModal.showLayer();
    },
    doDeleteConfirm (item) {
      // 用户点击确定
      if (item) {

        // 执行删除操作,校验必选参数
        if (item.uid && item.id) {

          // 显示loading
          Indicator.open('加载中...');
          // 请求接口
          this.http("/Api/" + (this.getApiVersion().index) + "/delAddress", {
            uid: item.uid,
            id: item.id
          }, this.doDeleteCallback, {
              method: "POST",
              uid: item.uid,
              id: item.id
            })
        } else {

          // 参数错误
          Toast("删除收件人参数错误");
        }
      }
    },
    /**
     * 删除地址操作回调
     * @param {Object} res [服务器返回数据]
     * @param {Object} res [扩展参数]
     */
    doDeleteCallback (res, ext) {

      // 返回处理
      if (res.code == 200) {

        /* 判断如果被删除地址是当前设置地址处理 开始 */

        let tempCurrentAddress = JSON.parse(window.localStorage.getItem('currentAddress'));

        if (tempCurrentAddress && tempCurrentAddress.id === ext.id) {

          // 删除刷新Storage
          window.localStorage.removeItem('currentAddress');

          // 设置首选地址
          this.getMyFirstAddress();
        }

        /* 判断如果被删除地址是当前设置地址处理 结束 */

        // 获取地址列表
        this.getMyAddress();

        // 删除成功提醒
        Toast('删除收货地址成功');

        // showToasting
        this.$set(this, 'showToasting', true);
      } else if (res.statusCode === 200 && res.data.code == 400) {

        // 删除收件人失败
        Toast(res.message);
      } else {

        // 删除收件人失败
        Toast("删除收件人失败");
      }
    },

    /**
     * 设置默认联系人(实时调用)
     * @param {Object} item [联系人对象]
     */
    setDefaultContact (item) {

      // 判断是否可设置为默认联系人,如果已经是默认联系人,不能取消
      if (item.state == 2) {

        // 已是默认联系人
        Toast("已是默认联系人,无需设置。");
      } else {

        // 显示loading
        Indicator.open('加载中...');

        // 请求接口
        this.http("/Api/" + (this.getApiVersion().index) + "/editAddress", {
          uid: item.uid,
          id: item.id,
          state: "2"
        }, this.setDefaultContactCallback, {
            method: "POST",
            item
          })
      }
    },

    /**
     * 编辑地址操作回调
     * @param {Object} res [服务器返回数据]
     * @param {Object} res [扩展参数]
     */
    setDefaultContactCallback (res, ext) {

      // 返回处理
      if (res.code == 200) {

        // 获取地址列表
        this.getMyAddress();

        // 输出地址操作结果
        Toast('设置默认地址成功');

        // showToasting
        this.$set(this, 'showToasting', true);
      } else if (res.statusCode === 200 && res.data.code == 400) {

        // 设置默认联系人出错
        Toast(res.message);
      } else {

        // 设置默认联系人出错
        Toast("设置默认联系人出错");
      }
    },

    /**
     * 返回
     */
    back () {
      // 后退
      window.history.go(-1);
    },
    /**
      * 设置start -> end位置内容为 “ * ” 号代替
      * @param {Object} value
      * @param {Object} start
      * @param {Object} end
      */
    formatPhoneNumber (value, start, end) {

      // 返回值
      let result = Array();

      // 判断号码长度,确定处理方式
      if (value.length <= start + end) {
        return value;
      }

      for (let i = 0; i < value.length; i++) {
        if (i >= start && i < value.length - end) {
          result.push("*");
        } else {
          result.push(value[i].toString());
        }
      }

      return result.join("");
    },
  },
  //对象内部的属性监听,也叫深度监听
  head: {
    title () {
      return {
        inner: this.title
      }
    }
  },
  watch: {
    // 设置页面标题
    title (newVal) {
      if (this.$store.state.browser.toLowerCase() == "qq" && this.$store.state.deviceType.toLowerCase() == "iphone") {
        this.$iFrame.insertIFrame(newVal);
      } else {
        this.$emit("updateHead");
      }
    }
  },
}
</script>

<style rel="stylesheet/less" lang="less">
@import './addressList';
</style>