index.vue 7.13 KB
Newer Older
程默 committed
1
<template>
程默 committed
2
  <div class="domain">获取微信地址</div>
程默 committed
3
  <!-- {{address}}----{{openid}} --{{options}}-->
程默 committed
4 5 6 7 8 9 10 11
</template>

<script>
import area from "@/utils/app.area.js";
import addres from "@/api/userAddress";
export default {
  data() {
    return {
程默 committed
12 13 14
      address: wx.getStorageSync("sessionid"),
      openid: wx.getStorageSync("openid"),

程默 committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
      options: "",
      weChatInfo: "",
      userAddressReq: {
        provinceCode: "",
        cityCode: "", //需传入城市code
        districtCode: "",
        zipcode: "",
        consignee: "",
        address: "",
        mobilephone: "",
        isDefaultShipping: 0
      },
      areaResult: {
        provinceName: "",
        cityName: "",
        areaName: ""
      },
      areaList: area,
      province_list: area.province_list,
      city_list: area.city_list,
      county_list: area.county_list,
程默 committed
36 37
      provinceTwoKey: 0,
      openSetting: false
程默 committed
38 39 40 41 42
    };
  },
  onLoad(options) {
    Object.assign(this.$data, this.$options.data());
    this.options = options;
程默 committed
43 44 45 46 47
    console.log("onload");
    this.init();
  },
  onShow() {
    if (this.openSetting) {
程默 committed
48
      this.openSetting = false;
程默 committed
49 50
      this.init();
    }
程默 committed
51 52
  },
  methods: {
程默 committed
53 54 55 56 57 58
    init() {
      wx.showLoading({
        title: "加载中"
      });

      let that = this;
程默 committed
59 60 61 62 63 64
      wx.chooseAddress({
        success: function(res) {
          console.log(4);
          that.disposeAddress(res);
        },
        fail: function(res) {
李嘉林 committed
65 66
          // chooseAddress:fail auth deny  ---用户拒绝后msg
          // chooseAddress:fail cancel   ---用户点击取消按钮
程默 committed
67
          console.log(res, "-------------------65",that.options);
程默 committed
68 69
          //用户取消
          console.log(5);
程默 committed
70
          if (res.errMsg.indexOf("cancel") != -1) {
李嘉林 committed
71
            wx.navigateBack();
程默 committed
72
          } else {
李嘉林 committed
73 74
            that.getSett();
          }
程默 committed
75 76 77 78 79
        }
      });
    },
    getSett() {
      let that = this;
程默 committed
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
      wx.getSetting({
        success(res) {
          if (!res.authSetting["scope.address"]) {
            that.openConfirm();
          } else {
            console.log(2);
            //打开选择地址
            wx.chooseAddress({
              success: function(res) {
                console.log(4);
                that.disposeAddress(res);
              },
              fail: function(res) {
                //用户取消
                console.log(5);
                wx.navigateBack();
              }
            });
          }
        },
        fail(res) {
          console.log("调用失败");
        }
      });
    },
    openConfirm() {
      wx.hideLoading();

      let that = this;
      wx.showModal({
        content: "检测到您没打开通讯地址的权限,是否去设置打开?",
        confirmText: "确认",
        cancelText: "取消",
        success: function(res) {
          console.log(res);
          //点击“确认”时打开设置页面
          if (res.confirm) {
            console.log("用户点击确认");
            wx.openSetting({
              success: res => {
                that.openSetting = true;
              }
            });
          } else {
            console.log("用户点击取消");
            wx.navigateBack();
          }
        },
        fail: function(res) {
          wx.navigateBack();
        }
      });
    },
程默 committed
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
    disposeAddress(val) {
      if (val) {
        this.weChatInfo = val;
        this.userAddressReq.address = this.weChatInfo.detailInfo;
        this.userAddressReq.zipcode = this.weChatInfo.postalCode;
        this.userAddressReq.consignee = this.weChatInfo.userName;
        this.userAddressReq.mobilephone = this.weChatInfo.telNumber;
        this.areaResult.provinceName = this.weChatInfo.provinceName;
        this.areaResult.cityName = this.weChatInfo.cityName;
        this.areaResult.areaName = this.weChatInfo.countyName;
        // 省code
        if (this.areaResult.provinceName == "内蒙古自治区") {
          this.areaResult.provinceName = "内蒙古";
        }
        if (this.areaResult.provinceName == "广西壮族自治区") {
          this.areaResult.provinceName = "广西";
        }
        if (this.areaResult.provinceName == "西藏自治区") {
          this.areaResult.provinceName = "西藏";
        }
        if (this.areaResult.provinceName == "宁夏回族自治区") {
          this.areaResult.provinceName = "宁夏";
        }
        if (this.areaResult.provinceName == "新疆维吾尔自治区") {
          this.areaResult.provinceName = "新疆";
        }
        Object.keys(this.province_list).forEach(key => {
          if (this.areaResult.provinceName == this.province_list[key]) {
            this.userAddressReq.provinceCode = key;
            this.provinceTwoKey = key.slice(0, 2);
          }
        });
        // 市code
        Object.keys(this.city_list).forEach(key => {
          if (this.areaResult.cityName == this.city_list[key]) {
            if (this.provinceTwoKey == key.slice(0, 2)) {
              this.userAddressReq.cityCode = key;
            }
          }
        });
        // 区code
        Object.keys(this.county_list).forEach(key => {
          if (this.areaResult.areaName == this.county_list[key]) {
            if (this.provinceTwoKey == key.slice(0, 2)) {
              this.userAddressReq.districtCode = key;
            }
          }
        });
程默 committed
181 182


程默 committed
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
        console.log(this.areaResult,this.userAddressReq,'=====')
        //是否直接返回地址信息
        if (this.options.refresh) {
          // this.options.back+=`&wxAddress=${encodeURIComponent(JSON.stringify(this.userAddressReq))}`
          let url=this.options.back;
          url+=encodeURIComponent(`&wxAddress=${JSON.stringify(this.userAddressReq)}`);
          console.log('refresh',url)
          setTimeout(() => {
            wx.reLaunch({
              url: `../index/main?from=address&backpath=${url}`
            });
          }, 200);
        } else {
          this.addAdressRequest();
        }
      }
    },
    addAdressRequest() {
      addres
        .getUserAddressList()
        .then(res => {
          if (res.data.code == 200) {
            let list = res.data.data.list;
            if (!list.length > 0) {
              this.userAddressReq.isDefaultShipping = 1;
程默 committed
208
            } else {
程默 committed
209
              this.userAddressReq.isDefaultShipping = 0;
程默 committed
210
            }
程默 committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
            addres
              .addOrEditAddress(this.userAddressReq)
              .then(res => {
                if (res.data.code == 200) {
                  console.log("!!!!!!!!!!!!!", this.options.back);
                  console.log("微信地址", this.userAddressReq);
                } else {
                }

                wx.showToast({ title: res.data.msg, icon: "none" });
                wx.navigateBack();
              })
              .catch(err => {
                wx.showToast({ title: err, icon: "none" });
                wx.navigateBack();
              });
          } else {
            wx.showToast({ title: res.data.msg, icon: "none" });
程默 committed
229
            wx.navigateBack();
程默 committed
230 231 232 233 234 235
          }
        })
        .catch(err => {
          wx.showToast({ title: err, icon: "none" });
          wx.navigateBack();
        });
程默 committed
236 237 238 239 240 241
    }
  }
};
</script>

<style>
程默 committed
242 243 244 245
.domain {
  text-align: center;
  padding-top: 50%;
}
程默 committed
246
</style>