123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <template>
- <view class="root">
- <view class="content-box">
- <u-cell-group>
- <u-cell title="头像" @click="handlerUploadImg" isLink>
- <template slot="value">
- <u-avatar shape="square" size="60" :src="userInfo.avatar" @click.stop="viewAvatar(userInfo.avatar)"></u-avatar>
- </template>
- </u-cell>
- <u-cell title="昵称" isLink>
- <template slot="value" >
- <u-input inputAlign="right" :border="false" v-model="userInfo.nickname" />
- </template>
- </u-cell>
- <u-cell title="性别" isLink>
- <template slot="value">
- <u-radio-group v-model="userInfo.gender">
- <u-radio v-for="item in sexList" :key="item.type" :label="item.text" :name="item.type" />
- </u-radio-group>
- </template>
- </u-cell>
- </u-cell-group>
- </view>
- <view class="btn-box">
- <u-button type="primary" shape="circle" @click="handlerSubmitBtn">确认</u-button>
- </view>
- </view>
- </template>
- <script>
- import { uploadFile } from "@/utils/upload"
- export default {
- data() {
- return {
- userInfo:{
- userId:'',
- avatar:'',
- gender:'',
- nickname:''
- },
- time_value: '',
- timeShow: false,
- sexList: [{
- id: 0,
- text: '男',
- type:'MALE'
- },
- {
- id: 1,
- text: '女',
- type:'FEMALE'
- }
- ],
- current: 0,
- }
- },
- mounted(){
- this.userInfo.userId = this.$store.getters.userId
- this.userInfo.avatar = this.$store.getters.avatar
- this.userInfo.gender = this.$store.getters.gender
- this.userInfo.nickname = this.$store.getters.nickname
- },
- methods: {
- viewAvatar(url){
- uni.previewImage({
- urls:[url]
- })
- },
- // 选择性别
- handlerSelectGender(item) {
- this.current = item.id
- },
- // 上传头像
- async handlerUploadImg() {
- const res = await uni.showActionSheet({
- itemList: ['拍照', '从相册选择'],
- });
- if (res.tapIndex === 0) {
- // 用户选择拍照
- this.takePhoto();
- } else if (res.tapIndex === 1) {
- // 用户选择从相册选择
- this.chooseImage();
- }
- },
- // 拍照
- takePhoto() {
- uni.chooseImage({
- sourceType: ['camera'],
- count: 1,
- success: res => {
- const tempFilePaths = res.tempFilePaths;
- this.uploadAvatar(tempFilePaths[0]);
- },
- fail: error => {
- console.error(error);
- },
- });
- },
- //从相册中选择
- chooseImage() {
- uni.chooseImage({
- sourceType: ['album'],
- count: 1,
- success: res => {
- const tempFilePaths = res.tempFilePaths;
- this.uploadAvatar(tempFilePaths[0]);
- },
- fail: error => {
- console.error(error);
- },
- });
- },
- // 上传头像
- async uploadAvatar(filePath) {
- // 在这里实现上传头像的逻辑,将filePath作为参数传入
- const res = await uploadFile(filePath)
- if(res.statusCode === 200 && res.data){
- const resp = JSON.parse(res.data)
- this.userInfo.avatar = resp.data.url
- }
- },
- // 点击提交按钮
- handlerSubmitBtn() {
- this.$store.dispatch('UpdateUserInfo',this.userInfo).then(res=>{
- setTimeout(()=>{
- uni.navigateBack(-1)
- },1500)
- })
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .root{
- background-color: #f3f3f3;
- height: calc(100vh - env(safe-area-inset-top));
- position: relative;
- .content-box{
- background-color: #fff;
- }
- .btn-box{
- position: absolute;
- bottom: 0;
- padding: 32rpx 16rpx;
- box-sizing: border-box;
- width: 100%;
- }
- }
- </style>
|