address_add.vue 6.42 KB
Newer Older
1 2 3 4
<template>
  <view>
    <dHeader :title="$lang.lang.addressInfo.addInfo"></dHeader>
    <view class="container">
5 6
      <uni-forms ref="form" :model="form"   :rules="rules">
        <uni-forms-item class="custom-label-width"
chenwei's avatar
chenwei committed
7 8 9 10
          :label="$lang.lang.addressInfo.consignee"
          required
          name="name"
        >
11 12 13 14
          <uni-easyinput
            class="input"
            :trim="true"
            :inputBorder="false"
15
            v-model="form.name"
16 17 18
            :placeholder="$lang.lang.notices.consignee"
          />
        </uni-forms-item>
chenwei's avatar
chenwei committed
19 20 21 22 23
        <uni-forms-item
          :label="$lang.lang.addressInfo.phone"
          required
          name="phone"
        >
24
          <view class="phone-box">
25 26 27 28 29 30
            <picker :value="areaIndex" :range="areaName" @change="areaChange">
              <view class="phone-code">
                +{{ areaCode }}
                <uni-icons type="down" class="code-icon" size="10"></uni-icons>
              </view>
            </picker>
31 32 33 34 35 36 37 38 39 40 41 42 43
            <uni-easyinput
              class="input"
              :trim="true"
              :inputBorder="false"
              v-model.trim="form.phone"
              :placeholder="$lang.lang.notices.phone"
            />
          </view>
        </uni-forms-item>
        <uni-forms-item
          class="item"
          :label="$lang.lang.addressInfo.fullAddress"
          required
44
          name="address"
45 46 47 48 49 50 51 52 53
        >
          <view class="textarea-box">
            <uni-easyinput
              class="input"
              type="textarea"
              :trim="true"
              autoHeight
              :inputBorder="false"
              :maxlength="100"
54
              v-model.trim="form.address"
55 56 57
              :placeholder="$lang.lang.notices.fullAddress"
            >
            </uni-easyinput>
chenwei's avatar
chenwei committed
58 59 60
            <view class="placeholder">{{
              $lang.lang.notices.fullAddressLength
            }}</view>
61 62
          </view>
        </uni-forms-item>
chenwei's avatar
chenwei committed
63 64 65
        <view class="btn" @click="saveAddress">{{
          $lang.lang.notices.saveAddress
        }}</view>
66 67 68 69 70 71
      </uni-forms>
    </view>
  </view>
</template>

<script>
chenwei's avatar
chenwei committed
72
import dHeader from "../../components/dHeader/index.vue";
73 74
export default {
  components: {
chenwei's avatar
chenwei committed
75
    dHeader,
76 77 78 79
  },
  data() {
    return {
      form: {
chenwei's avatar
chenwei committed
80 81 82
        name: "",
        phone: "",
        address: "",
83 84 85
      },
      areaCode: this.$store.getters.areaCode,
      areaIndex: 0,
chenwei's avatar
chenwei committed
86 87
      areaName: [],
    };
88 89 90 91
  },
  computed: {
    rules() {
      return {
92
        name: {
chenwei's avatar
chenwei committed
93 94 95
          rules: [
            { required: true, errorMessage: this.$lang.lang.notices.consignee },
          ],
96 97
        },
        phone: {
98 99 100 101
          rules: [
            { required: true, errorMessage: this.$lang.lang.notices.phone },
            {
              validateFunction: (rule, value) => /^\d+$/.test(value),
chenwei's avatar
chenwei committed
102 103 104
              errorMessage: this.$lang.lang.notices.nophone,
            },
          ],
105
        },
106
        address: {
107
          rules: [
chenwei's avatar
chenwei committed
108 109 110 111 112 113 114 115 116 117 118 119
            {
              required: true,
              errorMessage: this.$lang.lang.notices.fullAddress,
            },
            {
              maxLength: 100,
              errorMessage: this.$lang.lang.notices.fullAddressLength,
            },
          ],
        },
      };
    },
120
  },
121
  created() {
chenwei's avatar
chenwei committed
122
    this.getCountry();
123
  },
124 125 126
  mounted() {
    this.updateLabels();
  },
127
  methods: {
128 129 130 131 132 133 134 135 136 137 138 139
    updateLabels() {
      this.$nextTick(() => {
        const labels = this.$el.querySelectorAll('.uni-forms-item__label');
        labels.forEach(label => {
          // 移除 width 样式
          label.style.removeProperty('width');
          // 设置 min-width 样式
          label.style.minWidth = '70px';
        });
        console.log(labels);
      });
    },
140
    saveAddress() {
141 142 143 144 145 146
      this.$refs.form.validate().then(async (valid) => {
        if (valid) {
          try {
            const params = {
              ...this.form,
              areaCode: this.areaCode,
chenwei's avatar
chenwei committed
147 148 149 150
              memberId: this.$store.getters.id,
            };
            const { code, data, msg } = await this.$request.post(
              "/app-api/member/user-address/create",
151
              params
chenwei's avatar
chenwei committed
152
            );
153
            if (code == 0) {
chenwei's avatar
chenwei committed
154 155 156 157 158 159 160
              uni.$emit("refreshPreviousPage", "show");
              uni.navigateBack();
            } else {
              uni.showToast({
                title: msg,
                duration: 2000,
              });
161 162 163
            }
          } catch (error) {}
        }
chenwei's avatar
chenwei committed
164
      });
165 166 167 168
    },
    // 获取国家区号
    async getCountry() {
      try {
chenwei's avatar
chenwei committed
169 170 171
        const { code, data } = await this.$request.get(
          "/app-api/ecw/country/list-all"
        );
172 173 174
        if (code == 0 && data.length > 0) {
          // 查询用户区号下标
          const i = data.findIndex((item) => {
chenwei's avatar
chenwei committed
175 176 177
            return item.tel == this.areaCode;
          });
          if (i >= 0) this.areaIndex = i;
178
          data.forEach((item) => {
179
            const str = item.tel + " " + item[this.$lang.name];
chenwei's avatar
chenwei committed
180 181
              this.areaName.push(str);
          });
182 183 184 185 186
        }
      } catch (error) {}
    },
    // 区号选择切换
    areaChange(e) {
chenwei's avatar
chenwei committed
187 188 189 190 191
      this.areaIndex = e.detail.value;
      this.areaCode = this.areaName[this.areaIndex].split(" ")[0];
    },
  },
};
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
</script>

<style>
page {
  padding-top: 190upx;
}
.uni-forms {
  padding: 0 48upx;
  box-sizing: border-box;
}
.uni-forms-item {
  padding: 0 24upx;
  margin-bottom: 44upx;
  width: 100%;
  align-items: center;
  min-height: 80upx;
  box-sizing: border-box;
  border-radius: 12upx;
  background-color: #fff;
}
.item {
  align-items: start;
}
.input {
  font-size: 24upx;
}
.textarea-box {
  padding: 0 0 30upx;
}
.placeholder {
  position: absolute;
  bottom: 8upx;
  right: 0;
  font-size: 24upx;
  color: #b3b3b3;
}
.phone-box {
  display: flex;
  align-items: center;
}
.phone-code {
233 234
  display: flex;
  align-items: center;
235 236 237 238 239 240
  margin-right: 10upx;
  padding-right: 10upx;
  border-right: 1px solid #e6e6e6;
  font-size: 24upx;
  color: #000;
}
241 242 243
.code-icon {
  margin-left: 6upx;
}
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
.btn {
  width: 100%;
  height: 80upx;
  line-height: 80upx;
  text-align: center;
  background-color: #3d6ef6;
  color: #fff;
  border-radius: 80upx;
  margin-top: 40upx;
}

::v-deep .uni-forms-item__label {
  font-size: 28upx !important;
  font-weight: 500 !important;
  color: #000;
}
::v-deep .uni-easyinput__content-input {
  padding-left: 0 !important;
}

::v-deep .uni-easyinput__content-textarea {
  height: 100upx;
  min-height: 100upx;
  /* #ifndef APP-NVUE */
  min-height: 100upx;
  width: auto;
  /* #endif */
}
</style>