index.vue 13.6 KB
Newer Older
李嘉林 committed
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
<template>
  <div
    class="userInfo"
    :style="{
      '--main-color': themeColor['--main-color'],
      '--minor-color': themeColor['--minor-color'],
    }"
  >
    <div class="line"></div>
    <!--头像-->
    <div class="personal-top">
      <div class="updateHeadPic flex flex-cen">
        <div class="van-cell__title">
          <span>头像</span>
        </div>
        <div class="van-cell__value">
          <!--   <div class="van-field__icon" >
          </div>-->
          <div class="userPic">
            <button open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
              <image
                mode="aspectFill"
                v-if="userMsg.headPortraitUrl != ''"
                :src="userMsg.headPortraitUrl"
              />
              <image
                v-else
                src="http://mayi-newshop.oss-cn-shanghai.aliyuncs.com/public/jpg/39ada9cc-aff3-4842-8376-9f49e36c5811.jpg"
                alt
              />
            </button>
          </div>
        </div>
        <van-icon name="arrow" />
      </div>
    </div>
    <div class="line"></div>
    <!--个人信息-->
    <div class="updateInfo">
      <!-- 昵称 -->
      <van-field
        placeholder="请输入昵称"
        :value="userMsg.nickname"
        @change="changeNickName"
        maxlength="20"
        label="昵称"
        type="nickname"
        class="shuru"
        input-align="right"
      />
      <van-field
        placeholder="请输入姓名"
        :value="userMsg.realname"
        @change="changeRealName"
        maxlength="20"
        label="姓名"
        class="shuru"
        input-align="right"
      />

      <!-- 性别 -->
      <van-cell
        title="性别"
        @click="sex_show = true"
        :value="sex_list[userMsg.gender]"
        is-link
李嘉林 committed
67
        :class="{'noSelect': sex_list[userMsg.gender] == '未设置'}"
李嘉林 committed
68 69 70 71 72 73 74 75 76 77
      />
      <!-- <div class="covername sex">{{userMsg.gender}}</div> -->
      <!-- 设置生日 -->
      <div @click="birthShow = true">
        <van-cell
          v-if="userMsg.birth"
          title="生日"
          :value="userMsg.birth"
          is-link
        />
李嘉林 committed
78
        <van-cell class="noSelect" v-else title="生日" value="未设置" is-link />
李嘉林 committed
79 80 81 82 83 84 85 86 87 88 89
      </div>

      <div class="line"></div>
      <!--手机-->
      <div class="tel flex flex-btw">
        <div>手机号</div>
        <div
          class="font15"
          v-if="userMsg.mobilephone"
          @click="toUpdateBindPhone"
        >
李嘉林 committed
90 91
          <!-- {{ correctTel(userMsg.mobilephone) ? userMsg.mobilephone : "未认证" }} -->
          {{mobilephone}}
李嘉林 committed
92
          <van-icon name="arrow" v-if="!xhyxshopFlag" />
李嘉林 committed
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
        </div>
      </div>
    </div>

    <div class="btn-user" @click="submit">保存</div>
    <!-- 性别弹出层 -->
    <div catchtouchmove="ture">
      <van-popup :show="sex_show" position="bottom">
        <van-picker
          :columns="sex_list"
          :show-toolbar="true"
          @confirm="chooseSex"
          @cancel="chooseSexCancel"
        />
      </van-popup>
    </div>
    <!-- 设置生日弹框 -->
    <div catchtouchmove="ture">
      <van-popup :show="birthShow" position="bottom">
        <van-datetime-picker
          :min-date="minDate"
          :max-date="maxDate"
          :value="currentTime"
          type="date"
          :show-toolbar="true"
          @confirm="chooseBirth"
          @cancel="chooseBirthCancel"
        />
      </van-popup>
    </div>
    <!-- <div @click="click">按下试试</div> -->
  </div>
</template>

<script>
import live from "@/api/live";
import index from "@/api/index";
import { DFSImg } from "@/utils/index";
李嘉林 committed
131
import { themeColor } from "@/utils/mayi.js";
李嘉林 committed
132
import { formatTime } from "@/utils/index";
李嘉林 committed
133
const app = getApp()
李嘉林 committed
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
export default {
  name: "userInfo",
  data() {
    return {
      userMsg: {
        headPortraitUrl: "",
        birth: 0,
        realname: "",
        mobilephone: "",
        nickname: "",
      },
      imgArr: [],
      minDate: new Date(1900, 1, 1).getTime(),
      maxDate: new Date().getTime(),
      birthShow: false,
      sex_show: false,
      sex_list: ["女", "男", "未设置"],
      imgNumLimit: 1,
      currentTime: new Date().getTime(),
      currentTime2: new Date().getTime(),
      update_info_data: {},
      maxSize: 3145728,
      themeColor: {
        "--main-color": "",
        "--minor-color": "",
      },
      ossConfig: {},
    };
  },
李嘉林 committed
163 164 165
  computed: {
    mobilephone() {
      return this.correctTel(this.userMsg.mobilephone) ? this.userMsg.mobilephone : "未认证";
李嘉林 committed
166 167 168
    },
    xhyxshopFlag() {
      return this.$store.state.mixid == "xhyxshop"
李嘉林 committed
169 170
    }
  },
李嘉林 committed
171 172 173 174 175 176 177 178 179
  onLoad() {
    console.log("onLoad-----------------");
    if (themeColor["--main-color"] != "#ffffff") {
      this.themeColor = themeColor;
    } else {
      this.mpApp.themeColorCallBack = (res) => {
        this.themeColor = res;
      };
    }
李嘉林 committed
180 181
  },
  onShow() {
李嘉林 committed
182 183 184 185
    this.getUserMsg();
  },
  methods: {
    toUpdateBindPhone() {
李嘉林 committed
186 187
      if (this.xhyxshopFlag) return;
      // 新华优选不可修改手机号
李嘉林 committed
188
      let _this = this;
李嘉林 committed
189
      if (this.correctTel(this.userMsg.mobilephone)) {
李嘉林 committed
190 191 192 193
        app.$themeToLink({
          type: 1,
          link: `/personalCenter/updateBindPhone?mobilephone=${_this.userMsg.mobilephone}`,
        })
李嘉林 committed
194 195
      } else {
        console.log(this.$store.state.userInfo, "-------------------135");
李嘉林 committed
196 197 198 199
        app.$themeToLink({
          type: 1,
          link: `/login/wxRegister?openId=${_this.$store.state.userInfo.wxMiniOpenId}`,
        })
李嘉林 committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
      }
    },
    click() {
      console.log(999999);
    },
    // 返回
    returnClick() {
      // this.$router.back(-1);
      //会员信息组件跳转回原组件
      if (this.$route.query.memberInfo) {
        this.$router.back(-1);
      } else {
        this.$router.push("/personalCenter");
      }
    },
    getUserMsg() {
      live.getUserInfo().then((res) => {
        let result = res.data.data;
李嘉林 committed
218 219 220 221 222 223 224 225 226 227
        this.userMsg = {
          ...result,
          birth: result.birth ? result.birth.split(" ")[0] : "",
          headPortraitUrl: DFSImg(
          result.headPortraitUrl,
            400,
            400,
            1
          )
        };
李嘉林 committed
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
        console.log(this.userMsg, "--------------userMsg");
      });
    },
    changeNickName(val) {
      this.userMsg.nickname = val.mp.detail;
    },
    changeRealName(val) {
      this.userMsg.realname = val.mp.detail;
    },
    submit() {
      // 获取需要提供的数据
      // this.userMsg.headPortraitUrl=this.uploadImg;
      this.update_info_data.userId = this.userMsg.userId;
      this.update_info_data.mobilephone = this.userMsg.mobilephone;
      this.update_info_data.nickname = this.userMsg.nickname;
      this.update_info_data.birth = this.userMsg.birth
        ? `${this.userMsg.birth} 00:00:00`
        : "";
      this.update_info_data.gender = this.userMsg.gender.toString();
      this.update_info_data.headPortraitUrl = this.userMsg.headPortraitUrl;
      this.update_info_data.realname = this.userMsg.realname;

      if (this.userMsg.nickname == "" || this.userMsg.nickname == " ") {
        wx.showToast({
          title: "昵称不得为空",
          icon: "none",
          duration: 2000,
        });
        return;
      }
      if (this.userMsg.nickname.length > 20) {
        wx.showToast({
          title: "昵称不得超过20个字符",
          icon: "none",
          duration: 2000,
        });
        return;
      }
      index.update_info(this.update_info_data).then((res) => {
        if (res.data.code == 200) {
李嘉林 committed
268 269
          let _this = this;
          this.currentTime = new Date(_this.userMsg.birth).getTime();
李嘉林 committed
270 271 272 273 274
           wx.showToast({
            title: "保存成功",
            icon: "success",
            duration: 2000,
          });
李嘉林 committed
275
          wx.setStorageSync("reloadTabbarPage",1)
李嘉林 committed
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
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000,
          });
        }
      });
    },
    // onRead(file) {
    //   console.log(file)
    //   this.uploadImg = file.content;
    //   let uploadImgReq = this.uploadImg
    //   common.getUploadImg({uploadImgReq:uploadImgReq}).then(res => {
    //     console.log(res);
    //   })

    // },
    chooseSex(value) {
      console.log(value, "------------------264");
      let val = value.mp.detail.index;
      this.userMsg.gender = val;
      // if (val == "女") {
      // } else if (val == "男") {
      //   this.userMsg.gender = 1;
      // } else {
      //   this.userMsg.gender = 2;
      // }
      this.sex_show = false;
    },
    chooseSexCancel() {
      this.sex_show = false;
    },
    chooseBirth(val) {
      console.log(
        val,
        formatTime(new Date(val.mp.detail)).split(" ")[0].replace(/\//g, "-"),
        "----------val"
      );
      let brith = formatTime(new Date(val.mp.detail))
        .split(" ")[0]
        .replace(/\//g, "-");
      this.userMsg.birth = brith;
      this.birthShow = false;
    },
    chooseBirthCancel() {
      this.birthShow = false;
    },
    // 校验真实手机号
    correctTel(val) {
      return /^1\d{10}$/.test(val);
    },
    sexTransform(value) {
      console.log(value, "--------------249");
      if (value == 0) {
        return "女";
      } else if (value == 1) {
        return "男";
      } else {
        return "未设置";
      }
    },
    onChooseAvatar(val) {
      let userImg = val.mp.detail.avatarUrl;
      console.log(userImg, "---------------------299");
      let _this = this;
      wx.showLoading({
        title: '上传中...',
      })
      this.get_oss_config(()=>{
        let fileName = `${_this.ossConfig.dir}${userImg.split("/").pop()}`;
        console.log(fileName,'----------fileName')
        wx.uploadFile({
          url: `${_this.ossConfig.host}`,
          filePath: userImg,
          name: "file",
          formData: {
            key: fileName,
            OSSAccessKeyId: _this.ossConfig.accessid,
            policy: _this.ossConfig.policy,
            signature: _this.ossConfig.signature,
            success_action_status: "200",
          },
          success(res) {
            if (res.statusCode == 200) {
              let host =
                _this.ossConfig.host.substr(-1) == "/"
                  ? _this.ossConfig.host
                  : `${_this.ossConfig.host}/`;
              _this.userMsg.headPortraitUrl = `${host}${fileName}`;
              console.log(_this.userMsg.headPortraitUrl,'---_this.userMsg.headPortraitUrl')
              wx.hideLoading();
            }
            //do something
          },
          fail(res) {
            console.log(res, "-----------------fail");
            wx.showToast({
              title: "上传失败",
              icon: "error",
              duration: 1000,
            });
          },
        });
      });
    },
    random_string(len) {
      len = len || 32;
      let chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
      let maxPos = chars.length;
      let pwd = "";
      for (let i = 0; i < len; i++) {
        pwd += chars.charAt(Math.floor(Math.random() * maxPos));
      }
      return pwd;
    },
    get_oss_config(callback=()=>{}) {
      index.get_oss_config().then((res) => {
        console.log(res.data,'-------------------------res375')
        this.ossConfig = res.data;
        console.log(this.ossConfig, "--ossConfig");
        callback();
      });
    },
  },
};
</script>

<style scoped lang="scss">
.line {
  height: 10px;
  background-color: #f5f5f5;
}
.r {
  margin-right: 18px;
}
i {
  font-size: 18px;
}
.font15 {
李嘉林 committed
416 417
  font-size: 14px;
  color: #333;
李嘉林 committed
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
}
.userInfo {
  .personal-top {
    width: 100%;
    height: 80px;
    position: relative;
    .updateHeadPic {
      height: 80px;
      padding: 0 17px 0 15px;

      .userPic {
        width: 58px;
        height: 58px;
        border-radius: 50%;
        overflow: hidden;
        border: 1px solid #acacac;
        margin-left: 80px;
        button {
          width: 100%;
          height: 100%;
          padding: 0;
        }
        image {
          width: 100%;
          height: 100%;
        }
      }
      .uploader {
        width: 100%;
        height: 100%;
        position: absolute;
        z-index: 10;
        right: 0;
      }
      .van-cell__title {
        flex: 1;
李嘉林 committed
454
        font-size: 14px;
李嘉林 committed
455
        font-weight: bold;
李嘉林 committed
456
        color: #333;
李嘉林 committed
457 458
      }
      .van-cell__value {
李嘉林 committed
459 460
        font-size: 14px;
        color: #333;
李嘉林 committed
461 462 463 464 465 466 467 468 469 470
      }
      .van-cell:not(:last-child)::after {
        right: 15px;
        border-color: #dbdbdb;
      }
    }
  }
  .updateInfo {
    position: relative;
    width: 100%;
李嘉林 committed
471
    /deep/.van-cell {
李嘉林 committed
472 473 474 475 476 477
      width: 100%;
      height: 55px;
      // border-bottom: 1px solid #f0f2f5;
      display: flex;
      justify-content: space-between;
      align-items: center;
李嘉林 committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
      .van-cell__title,.van-cell__value{
        font-size: 14px;
        color: #333;
      }
    }
    /deep/.noSelect{
      .van-cell__value{
        font-size: 14px;
        color: #999;
      }
    }
    .shuru{
      /deep/.van-field__label{
        font-size: 14px;
        color: #333;
李嘉林 committed
493
      }
李嘉林 committed
494 495 496 497 498 499 500
    }
    .covername {
      width: 45px;
      height: 15px;
      position: absolute;
      right: 18px;
      top: 20px;
李嘉林 committed
501
      font-size: 14px;
李嘉林 committed
502 503
      font-family: MicrosoftYaHei;
      font-weight: 400;
李嘉林 committed
504
      color: #333;
李嘉林 committed
505 506 507 508 509 510 511 512 513 514 515 516
    }
    .sex {
      top: 70px;
    }
    .rightIco {
      padding-right: 14px;
      color: #929292;
    }
  }
  .tel {
    height: 55px;
    padding: 0 15px 0 15px;
李嘉林 committed
517
    font-size: 14px;
李嘉林 committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    i {
      vertical-align: text-top;
    }
  }
  .btn-user {
    position: fixed;
    bottom: 20px;
    width: 350px;
    height: 44px;
    line-height: 44px;
    background: var(--main-color);
    border-radius: 4px;
    left: 50%;
    margin-left: -175px;
    font-size: 17px;
    color: #ffffff;
    text-align: center;
  }
}
</style>