【全栈】SprintBoot+vue3迷你商城(9)

【全栈】SprintBoot+vue3迷你商城(9)

往期的文章都在这里啦,大家有兴趣可以看一下

后端部分:

【全栈】SprintBoot+vue3迷你商城(1)
【全栈】SprintBoot+vue3迷你商城(2)
【全栈】SprintBoot+vue3迷你商城-扩展:利用python爬虫爬取商品数据
【全栈】SprintBoot+vue3迷你商城(3)
【全栈】SprintBoot+vue3迷你商城(4)
【全栈】SprintBoot+vue3迷你商城(5)
【全栈】SprintBoot+vue3迷你商城(6)

前端部分:

【全栈】SprintBoot+vue3迷你商城-扩展:vue的基本用法

【全栈】SprintBoot+vue3迷你商城-扩展:vue3项目创建及目录介绍

【全栈】SprintBoot+vue3迷你商城(7)

【全栈】SprintBoot+vue3迷你商城(8)

本期我们来写用户基本信息显示以及购物车显示的页面

文章目录

1.用户基本信息显示页面的开发

1.1.搭建页面,绑定数据与函数

/src/views/user/UserInfo.vue

vue 复制代码
<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'

import { useUserInfoStore } from '@/stores/userInfo';
const userInfoStore = useUserInfoStore();
const userInfoModel= ref(userInfoStore.info)

const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}


const activeIndex = ref('1')
const handleSelect = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}

import { useRouter } from 'vue-router'; 
const router=useRouter();



import { Avatar,  SwitchButton } from '@element-plus/icons-vue';


import { useTokenStore } from '@/stores/token'; 
const tokenStore=useTokenStore();
const showLogoutDialog = ref(false);
const logout = () => {
  ElMessage.success('成功退出登录');
  tokenStore.removeToken();
  router.push('/login'); 
  showLogoutDialog.value = false; 
};

const handleClose = (done) => {
  ElMessage({
    message: '对话框已关闭',
    type: 'warning'
  });
  done();
};

</script>


<template>
  <div class="common-layout">
    <el-container class="bg">
      <el-header>
        <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" :ellipsis="false"
          @select="handleSelect">
          <el-menu-item index="0" @click="router.push('/')">
            <el-icon><ShoppingTrolley /></el-icon>
            <img style="width: 100px" src="@/assets/logo2.jpg" alt="logo" />
          </el-menu-item>
          <span style="font-size: medium;">
              <el-avatar style="margin-top:5px" :src="userInfoModel.userPic"  />
            
            {{ userInfoModel.userType }}:{{ userInfoModel.username }}
          </span>
          <el-menu-item index="1" @click="router.push('/user/info')"><el-icon>
              <Avatar />
            </el-icon>我的</el-menu-item>
          <el-sub-menu index="2">
            <template #title>工具栏</template>
            <el-menu-item v-if="userInfoModel.userType==='商家'" index="2-1" @click="router.push('/user/goods')">我的商品</el-menu-item>
            <el-menu-item index="2-2" @click="router.push('/user/cart')">购物车</el-menu-item>
            <el-menu-item index="2-3" @click="showLogoutDialog = true;">
                            <el-icon>
                                <SwitchButton />
                            </el-icon>
                            退出登录
                        </el-menu-item>
          </el-sub-menu>
        </el-menu>
        <el-dialog title="确认退出登录" v-model="showLogoutDialog" width="30%" :before-close="handleClose">
              <span>确定要退出登录吗?</span>
              <template #footer>
                <span class="dialog-footer">
                  <el-button @click="showLogoutDialog = false">取消</el-button>
                  <el-button type="primary" @click="logout">确定</el-button>
                </span>
              </template>
            </el-dialog> 
      </el-header>
      <el-main>
        <el-card class="page-container" style="background-color: coral;">
          <template #header>
            <div class="header">
              <span>基本资料</span>
            </div>
          </template>
          <p>用户头像:</p>
          <el-upload class="avatar-uploader" action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
            :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
            <img v-if="userInfoModel.userPic" :src="userInfoModel.userPic" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon">
              <Plus />
            </el-icon>
          </el-upload>
          <el-row>
            <el-col :span="12">
              <el-form label-width="100px" size="large">
                <el-form-item label="用户名">
                  <el-input disabled :value="userInfoModel.username"></el-input>
                </el-form-item>
                <el-form-item label="用户类型">
                  <el-input :value="userInfoModel.userType"></el-input>
                </el-form-item>
                <el-form-item label="用户手机号">
                  <el-input :value="userInfoModel.phoneNumber"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary">提交修改</el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
        </el-card>
      </el-main>
    </el-container>
  </div>
  <hr>
  <div style="width: 300px;height: 200px;margin-left: auto;margin-right: auto;">
    <div style="margin-top: 100px;">
      <p style="text-align: center;margin-top: 10px;filter: opacity(50%);font-size: small;">迷你商城,本网站为练习网站。
        本站商品全在淘宝:"https://www.taobao.com"中获取,若对站内商品感兴趣,请到淘宝搜索相关商品并购买</p>
    </div>
  </div>
</template>




<style>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  margin-bottom: 100px;
  margin-left: 200px;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}

.el-menu--horizontal>.el-menu-item:nth-child(1) {
  margin-right: auto;
}

.bg {
  background-color: pink;
}

.el-menu-demo {
  background-color: coral;
}
</style>

1.2.制定与后端接口交互的函数

/src/api/user.js

javascript 复制代码
export const userInfoService=()=>{
    return request.get('/user/userInfo');
}

1.3.效果展示

2.购物车显示的页面开发

2.1.搭建页面,绑定数据与函数

/src/views/user/UserCart.vue

vue 复制代码
<script lang="ts" setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router';
const router=useRouter();
const activeIndex = ref('2-2')
const handleSelect = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
import { useUserInfoStore } from '@/stores/userInfo';
const userInfoStore = useUserInfoStore();
const userInfoModel= ref(userInfoStore.info)
import {userCartService} from '@/api/user'
const userCartList=ref([])
const userCart=async()=>{
  let result = await userCartService();
  userCartList.value=result.data;

  }
userCart();

import { ElMessage } from 'element-plus';
import { Avatar,  SwitchButton } from '@element-plus/icons-vue';


import { useTokenStore } from '@/stores/token'; 
const tokenStore=useTokenStore();
const showLogoutDialog = ref(false);
const logout = () => {
  ElMessage.success('成功退出登录');
  tokenStore.removeToken();
  router.push('/login'); 
  showLogoutDialog.value = false; 
};

const handleClose = (done) => {
  ElMessage({
    message: '对话框已关闭',
    type: 'warning'
  });
  done();
};
</script>


<template>
  <div class="common-layout">
    <el-container class="bg">
      <el-header>
        <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" :ellipsis="false"
          @select="handleSelect">
          <el-menu-item index="0" @click="router.push('/')">
            <el-icon><ShoppingTrolley /></el-icon>
            <img style="width: 100px" src="@/assets/logo2.jpg" alt="logo" />
          </el-menu-item>
          <span style="font-size: medium;">
              <el-avatar style="margin-top:5px" :src="userInfoModel.userPic"  />
            
            {{ userInfoModel.userType }}:{{ userInfoModel.username }}
          </span>
          <el-menu-item index="1" @click="router.push('/user/info')"><el-icon>
              <Avatar />
            </el-icon>我的</el-menu-item>
          <el-sub-menu index="2">
            <template #title>工具栏</template>
            <el-menu-item v-if="userInfoModel.userType==='商家'" index="2-1" @click="router.push('/user/goods')">我的商品</el-menu-item>
            <el-menu-item index="2-2" @click="router.push('/user/cart')">购物车</el-menu-item>
            <el-menu-item index="2-3" @click="showLogoutDialog = true;">
                            <el-icon>
                                <SwitchButton />
                            </el-icon>
                            退出登录
                        </el-menu-item>
          </el-sub-menu>
        </el-menu>
        <el-dialog title="确认退出登录" v-model="showLogoutDialog" width="30%" :before-close="handleClose">
              <span>确定要退出登录吗?</span>
              <template #footer>
                <span class="dialog-footer">
                  <el-button @click="showLogoutDialog = false">取消</el-button>
                  <el-button type="primary" @click="logout">确定</el-button>
                </span>
              </template>
            </el-dialog>
      </el-header>
      <el-main>

        <div style="font-size: 50px;">
          <el-icon><ShoppingTrolley /></el-icon>
          购物车
        
        </div>
        <el-descriptions direction="vertical" border style="margin-top: 20px" v-for="userCartModel in userCartList">
          <el-descriptions-item :rowspan="2" :width="140" label="商品图片" align="center">
            <el-image style="width: 100px; height: 100px"
              :src="userCartModel.goodsImgUrl" />
          </el-descriptions-item>
          
         <el-descriptions-item label="商品名/价格">
          {{ userCartModel.goodsName }}
          <span style="font-size: large;margin-left: 100px;">{{userCartModel.goodsPrice}}¥</span>
        </el-descriptions-item>
          <el-descriptions-item label="商家">{{ userCartModel.merchantName }}</el-descriptions-item>
        </el-descriptions>



      </el-main>
    </el-container>
  </div>
  <hr>
  <div style="width: 300px;height: 200px;margin-left: auto;margin-right: auto;">
    <div style="margin-top: 100px;">
      <p style="text-align: center;margin-top: 10px;filter: opacity(50%);font-size: small;">迷你商城,本网站为练习网站。
        本站商品全在淘宝:"https://www.taobao.com"中获取,若对站内商品感兴趣,请到淘宝搜索相关商品并购买</p>
    </div>
  </div>
</template>




<style>
.el-menu--horizontal>.el-menu-item:nth-child(1) {
  margin-right: auto;
}

.bg {
  background-color: pink;
}

.el-menu-demo {
  background-color: coral;
}
</style>

2.2.制定与后端接口交互的函数

/src/api/user.js

javascript 复制代码
export const userCartService=()=>{
    return request.get('/user/cart');
}

2.3.效果展示

3.总结

本期我们进行了用户基本信息显示与购物车显示的页面的开发,下期我们将搭建其余基本的页面。

相关推荐
customer0836 分钟前
【开源免费】基于SpringBoot+Vue.JS贸易行业crm系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源
追光少年332240 分钟前
Learning Vue 读书笔记 Chapter 2
前端·javascript·vue.js·vue3
前端熊猫40 分钟前
JavaScript 的 Promise 对象和 Promise.all 方法的使用
开发语言·前端·javascript
CPU NULL1 小时前
新版IDEA创建数据库表
java·数据库·spring boot·sql·学习·mysql·intellij-idea
iOS阿玮2 小时前
速领🧧!iOS研究院专属「红包封面」来了,第二弹。
前端
呦呦鹿鸣Rzh2 小时前
HTML-新浪新闻-实现标题-样式1
前端·html
秋月的私语2 小时前
c#启动程序时使用异步读取输出避免假死
java·前端·c#
傻小胖2 小时前
vue3中自定一个组件并且能够用v-model对自定义组件进行数据的双向绑定
前端·javascript·vue.js
桂月二二2 小时前
探索前端的未来:深度使用 SolidJS 构建高性能用户界面
前端·ui
我想学LINUX2 小时前
【2024年华为OD机试】 (C卷,200分)- 机器人走迷宫(JavaScript&Java & Python&C/C++)
java·c语言·javascript·python·华为od·机器人