123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <template>
- <view class="info">
- <custom-nav :title="title" :to-back="toBack"></custom-nav>
- <view class="avatar_box">
- <button class="avatar_btn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
- <image class="avatar" :src="avatarUrl"></image>
- </button>
- </view>
- <view class="name_box">
- <view>昵称</view>
- <input type="nickname" class="weui-input" placeholder="请输入昵称" v-model="nickname" @change="getVal" />
- </view>
- <view class="btn_box" @click="save">
- 保存
- </view>
- </view>
- </template>
- <script>
- const defaultAvatarUrl =
- 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
- import { //引入 api
- SaveUserInfo,
- SaveUserAvatar
- } from '../../api.js'
- export default {
- data() {
- return {
- avatarUrl: defaultAvatarUrl, //用户头像
- nickname: '', //用户昵称
- title: '授权头像昵称'
- }
- },
- onLoad(ops) {
- if (ops.from) {
- this.title = '修改头像昵称'
- }
- },
- methods: {
- onChooseAvatar(e) {
- this.avatarUrl = e.detail.avatarUrl
- },
- getVal(e) {
- this.nickname = e.detail.value
- },
- save() {
- if (!this.avatarUrl) {
- uni.showToast({
- title: '头像不能为空',
- icon: 'none'
- })
- return
- }
- if (!this.nickname) {
- uni.showToast({
- title: '昵称不能为空',
- icon: 'none'
- })
- return
- }
- let _this = this
- uni.uploadFile({
- filePath: this.avatarUrl,
- name: 'file',
- url: `https://api.jiuweiyun.cn/api/user/SaveUserAvatar`, //服务器端接收图片的路径
- success: res => {
- let data = JSON.parse(res.data)
- this.saveSub(data.data.path)
- },
- fail: err => {
- uni.showToast({
- title: '上传头像失败'
- })
- }
- })
- },
- saveSub(avatar) {
- let _this = this;
- _this.$ajax.get(
- `${SaveUserInfo}?nickname=${this.nickname}&avatar=${avatar}`
- ).then(([, {
- data: res
- }]) => {
- if (res.code == 200) {
- uni.navigateTo({
- url: '../index/index'
- })
- uni.setStorageSync('nickName', this.nickname)
- uni.setStorageSync('avatar', this.avatarUrl)
- }
- })
- }
- }
- }
- </script>
- <style lang="scss">
- .info {
- width: 100%;
- height: 100%;
- margin: 0 auto;
- }
- .avatar_box {
- margin: 100rpx 0 40rpx 0;
- button {
- margin: 0;
- padding: 0;
- outline: none;
- border-radius: 0;
- background-color: transparent;
- }
- button::after {
- border: none;
- }
- .avatar_btn {
- .avatar {
- width: 240rpx;
- height: 240rpx;
- border-radius: 16rpx;
- }
- }
- .avatar_btn:active {
- outline: none;
- box-shadow: none;
- }
- }
- .name_box {
- width: 100%;
- display: flex;
- just-content: flex-start;
- align-items: center;
- border: 2rpx solid #efefef;
- height: 120rpx;
- padding: 0 24rpx;
- box-sizing: border-box;
- line-height: 120rpx;
- view {
- font-size: 34rpx;
- font-weight: bold;
- margin-right: 24rpx;
- }
- input {
- width: 80%;
- height: 100%;
- font-size: 34rpx;
- }
- }
- .btn_box {
- width: 560rpx;
- height: 88rpx;
- line-height: 88rpx;
- text-align: center;
- font-size: 34rpx;
- font-weight: bold;
- background: linear-gradient(93deg, #F30000 0%, #FE4815 100%);
- opacity: 1;
- border-radius: 44rpx;
- color: #fff;
- margin: 60rpx auto 0;
- }
- </style>
|