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

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


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

前端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>
相关推荐
一只特立独行的猪6111 小时前
Java面试——集合篇
java·开发语言·面试
讓丄帝愛伱2 小时前
spring boot启动报错:so that it conforms to the canonical names requirements
java·spring boot·后端
weixin_586062022 小时前
Spring Boot 入门指南
java·spring boot·后端
Passion不晚2 小时前
Vue vs React vs Angular 的对比和选择
vue.js·react.js·前端框架·angular.js
Dola_Pan5 小时前
Linux文件IO(二)-文件操作使用详解
java·linux·服务器
wang_book5 小时前
Gitlab学习(007 gitlab项目操作)
java·运维·git·学习·spring·gitlab
蜗牛^^O^6 小时前
Docker和K8S
java·docker·kubernetes
从心归零7 小时前
sshj使用代理连接服务器
java·服务器·sshj
IT毕设梦工厂8 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
Jiaberrr8 小时前
前端实战:使用JS和Canvas实现运算图形验证码(uniapp、微信小程序同样可用)
前端·javascript·vue.js·微信小程序·uni-app