index.vue 7.25 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
    if(options.sessionid){
程默 committed
44 45 46
      try {
        wx.setStorageSync('sessionid', options.sessionid)
      } catch (e) { }
程默 committed
47
    }
程默 committed
48 49 50 51 52
    console.log("onload");
    this.init();
  },
  onShow() {
    if (this.openSetting) {
程默 committed
53
      this.openSetting = false;
程默 committed
54 55
      this.init();
    }
程默 committed
56 57
  },
  methods: {
程默 committed
58 59 60 61 62 63
    init() {
      wx.showLoading({
        title: "加载中"
      });

      let that = this;
程默 committed
64 65 66 67 68 69
      wx.chooseAddress({
        success: function(res) {
          console.log(4);
          that.disposeAddress(res);
        },
        fail: function(res) {
李嘉林 committed
70 71
          // chooseAddress:fail auth deny  ---用户拒绝后msg
          // chooseAddress:fail cancel   ---用户点击取消按钮
程默 committed
72
          console.log(res, "-------------------65",that.options);
程默 committed
73 74
          //用户取消
          console.log(5);
程默 committed
75
          if (res.errMsg.indexOf("cancel") != -1) {
李嘉林 committed
76
            wx.navigateBack();
程默 committed
77
          } else {
李嘉林 committed
78 79
            that.getSett();
          }
程默 committed
80 81 82 83 84
        }
      });
    },
    getSett() {
      let that = this;
程默 committed
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
      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
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
    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
186 187


程默 committed
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
        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
213
            } else {
程默 committed
214
              this.userAddressReq.isDefaultShipping = 0;
程默 committed
215
            }
程默 committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
            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
234
            wx.navigateBack();
程默 committed
235 236 237 238 239 240
          }
        })
        .catch(err => {
          wx.showToast({ title: err, icon: "none" });
          wx.navigateBack();
        });
程默 committed
241 242 243 244 245 246
    }
  }
};
</script>

<style>
程默 committed
247 248 249 250
.domain {
  text-align: center;
  padding-top: 50%;
}
程默 committed
251
</style>