关于【今天吃什么】的思考--随机答案

关于【今天吃什么】的思考


今天吃什么这个问题每天都很困扰,做个随机数的小练习来帮忙回答这个问题吧。

前端vue3

  1. 随机数页面
javascript 复制代码
<template>
  <div>
      <el-button type="primary" @click="randomThat()">点到谁就是谁</el-button>
      <el-divider/>
      <el-button v-for="(item, index) in listdata" :key="index"
       :class="{ 'el-button--success': listPrimary[index],
       'el-button--warning': listInfo[index] }">{{item}}</el-button>
  </div>
</template>

<script>
import { ref } from 'vue';
import axios from 'axios';

const listdata = ref([]);
const listPrimary=ref([]);
const listInfo=ref([]);
export default {
  name:'Choose-Food',
  components: {
  },
  data(){
    return{
      listdata,
      listPrimary,
      listInfo
    }
  },
  mounted(){
    this.getData()
  },
  methods:{
    async getData(){
      await axios.post('/api/foods/choose')
        .then(response => {
          listdata.value = response.data.foods;
          this.clearlist()
        })
        .catch(error => {
          console.error('There was an error!', error);
        });
    },
    doSleep(duration) {
      return new Promise((resolve) => {
        setTimeout(resolve, duration);
      });
    },
    clearlist(){
      listPrimary.value=[]
      listInfo.value=[]
      listdata.value.forEach(() => listPrimary.value.push(false));
      listdata.value.forEach(() => listInfo.value.push(false));
    },
    async randomThat(){
      let lastRam=0;
      for (let i = 0; i < 20; i++) {
          this.clearlist()
          const randomNumber = Math.floor(Math.random() * (listdata.value.length))
          listInfo.value[randomNumber]=true
          lastRam=randomNumber
          await this.doSleep(400);
      }
      this.clearlist()
      listPrimary.value[lastRam]=true
    }
  }
}
</script>
  1. 记录下axios.js
javascript 复制代码
import axios from 'axios'

const service=axios.create({
  //请求超时时间
  timeout:10000,
  baseURL: '/api'
});

export default service;
  1. 记录下main.js
javascript 复制代码
import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import router from './router';

window.__VUE_PROD_DEVTOOLS__ = false;
window.__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ = false;

const app=createApp(App)
app.use(ElementPlus).use(router).mount('#app')
  1. vue.config.js添加如下,代理访问接口处理跨域问题:
javascript 复制代码
module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/'
        }
      }
    }
  }

后端java

虽然前端直接实现即可,不过为了练习下mongodb,还是用下后端吧。

  1. java 接口
java 复制代码
@RestController
@RequestMapping("/foods")
public class FoodsChooose {
    @PostMapping("/choose")
    @ResponseBody
    public Map foodsChoose() {
        return MongoDBUtil.collection("foods");
    }
  1. MongoDBUtil
java 复制代码
public class MongoDBUtil {

    public static MongoClient conn() {
        String url = "mongodb://localhost:27017";
        MongoClient mongoClient = MongoClients.create(url);
        return mongoClient;
    }

    public static Map collection(String queryKey) {
        Map<String,Object> returnValue = new HashMap<>();
        MongoClient mongoClient = conn();
        MongoDatabase database = mongoClient.getDatabase("mongotest");
        MongoCollection<Document> collection = database.getCollection("mongotest");
        FindIterable<Document> result = collection.find();
        for(Document doc : result) {
            if(doc.containsKey(queryKey)){
                returnValue.put(queryKey,doc.get(queryKey));
            }
        }
        closeConn(mongoClient);
        return returnValue;
    }

    public static void closeConn(MongoClient mongoClient) {
        mongoClient.close();
    }
}
  1. pom
xml 复制代码
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-sync</artifactId>
    <version>5.1.2</version>
</dependency>
相关推荐
熊大如如5 分钟前
Java 反射
java·开发语言
猿来入此小猿24 分钟前
基于SSM实现的健身房系统功能实现十六
java·毕业设计·ssm·毕业源码·免费学习·猿来入此·健身平台
goTsHgo1 小时前
Spring Boot 自动装配原理详解
java·spring boot
卑微的Coder1 小时前
JMeter同步定时器 模拟多用户并发访问场景
java·jmeter·压力测试
pjx9871 小时前
微服务的“导航系统”:使用Spring Cloud Eureka实现服务注册与发现
java·spring cloud·微服务·eureka
多多*2 小时前
算法竞赛相关 Java 二分模版
java·开发语言·数据结构·数据库·sql·算法·oracle
爱喝酸奶的桃酥2 小时前
MYSQL数据库集群高可用和数据监控平台
java·数据库·mysql
唐僧洗头爱飘柔95273 小时前
【SSM-SSM整合】将Spring、SpringMVC、Mybatis三者进行整合;本文阐述了几个核心原理知识点,附带对应的源码以及描述解析
java·spring·mybatis·springmvc·动态代理·ioc容器·视图控制器
骑牛小道士3 小时前
Java基础 集合框架 Collection接口和抽象类AbstractCollection
java
alden_ygq3 小时前
当java进程内存使用超过jvm设置大小会发生什么?
java·开发语言·jvm