找回密码
 立即注册

微信小程序实战,用vue3实现每日浪漫情话推荐~

匿名  发表于 2022-4-22 10:55:53 阅读模式 打印 上一主题 下一主题
1、前言
之前做了个恋爱话术微信小法式,满足了平常聊天的需要,实现高情商的恋爱聊天。比来也完成了话术库的优化更新,新增30万条话术数据,同时利用了分词技术,婚配更完善。
但比来忽然发现,天天早上给女朋友发一段优美情话可以让她高兴一成天,但无法自己的说话水平确切有限,不能随手拈来,实在让人有点不爽。
不外法子总比困难多,作为高情商的法式猿,来历于平常生活的需求常常是我们最大的动力,必须盘他,所以想着在之前的恋爱话术小法式上在加一个逐日情话保举的功用。
希望这个法式对其他单身大概恋爱中的兄弟们有所帮助,也希望兄弟们关注加三连支持一波哈~~~
2、保举接口简介

==辣鸡办事器,兄弟们轻点调哈==
浪漫保举情话开放接口

接口地址:https://yin-feng.top/open/getRecommendLove
请求方式:POST
请求数据范例:application/json
响应数据范例:*/*
请求参数:
  1. {}
复制代码
响应参数:

微信小法式实战,用vue3实现逐日浪漫情话保举~-1.jpg
响应示例:
  1. [
  2.     {
  3.         "content": "",
  4.         "id": 0,
  5.         "score": 0,
  6.         "title": ""
  7.     }
  8. ]
复制代码
3、前端页面开辟
老样子,我们还是利用uniapp框架来开辟,uniapp的先容就不多说了哈。
3.1 设置pages.json文件
我们此次筹算配两个菜单,包括浪漫情话保举和话术搜索。首要在pages.json里面就行设置。
==pages.json== 文件用来对 uni-app 停止全局设置,决议页面文件的途径、窗口款式、原生的导航栏、底部的原生tabbar 等。它类似微信小法式中app.json的页面治理部分。
在 ==pages.json== 中供给 tabBar 设置,不但仅是为了方便快速开辟导航,更重要的是在App和小法式端提升性能。在这两个平台,底层原生引擎在启动时无需期待js引擎初始化,即可间接读取 pages.json 中设置的 tabBar 信息,衬着原生tab。
官方文档参考tabBar
出格留意

  • 当设备 position 为 top 时,将不会显现 icon
  • tabBar 中的 list 是一个数组,只能设置最少2个、最多5个 tab,tab 按数组的顺序排序。
  • tabbar 切换第一次加载时能够衬着不实时,可以在每个tabbar页面的onLoad生命周期里先弹出一个期待雪花(hello uni-app利用了此方式)
  • tabbar 的页面展现过一次后就保存在内存中,再次切换 tabbar 页面,只会触发每个页面的onShow,不会再触发onLoad。
  • 顶部的 tabbar 今朝仅微信小法式上支持。需要用到顶部选项卡的话,倡议不利用 tabbar 的顶部设备,而是自己做顶部选项卡。

新增浪漫情话页面设置
  1. {
  2.             "path": "pages/recommend/recommend",
  3.             "style": {
  4.                 "navigationBarTitleText": "浪漫情话",
  5.                 "backgroundColor": "#eeeeee",
  6.                 "enablePullDownRefresh": false
  7.             }
  8.         }
复制代码
增加两个tab标签
  1. "tabBar": {
  2.         "color": "#7A7E83",
  3.         "selectedColor": "#0055ff",
  4.         "borderStyle": "black",
  5.         "backgroundColor": "#ffffe1",
  6.         "height":"60px",
  7.         "fontSize":"18px",
  8.         "list": [
  9.             {
  10.                 "pagePath": "pages/recommend/recommend",
  11.                 "iconPath": "static/imgs/love1.png",
  12.                 "selectedIconPath": "static/imgs/love2.png",
  13.                 "text": "浪漫情话"
  14.             },
  15.             {
  16.                 "pagePath": "pages/index/index",
  17.                 "iconPath": "static/imgs/爱心1.png",
  18.                 "selectedIconPath": "static/imgs/爱心2.png",
  19.                 "text": "话术搜索"
  20.             }
  21.         ]
  22.     }
复制代码
完整的page.json文件
  1. {
  2.     "pages": [ //pages数组中第一项暗示利用启动页,参考:https://uniapp.dcloud.io/collocation/pages
  3.         {
  4.             "path": "pages/recommend/recommend",
  5.             "style": {
  6.                 "navigationBarTitleText": "浪漫情话",
  7.                 "backgroundColor": "#eeeeee",
  8.                 "enablePullDownRefresh": false
  9.             }
  10.         },
  11.         {
  12.             "path": "pages/index/index",
  13.             "style": {
  14.                 "navigationBarTitleText": "恋爱话术",
  15.                 "backgroundColor": "#eeeeee",
  16.                 "enablePullDownRefresh": false
  17.             }
  18.         }, {
  19.             "path": "component/WebView/WebView",
  20.             "style": {
  21.                 "navigationBarTitleText": "",
  22.                 "enablePullDownRefresh": false
  23.             }
  24.         }
  25.     ],
  26.     "tabBar": {
  27.         "color": "#7A7E83",
  28.         "selectedColor": "#0055ff",
  29.         "borderStyle": "black",
  30.         "backgroundColor": "#ffffe1",
  31.         "height":"60px",
  32.         "fontSize":"18px",
  33.         "list": [
  34.             {
  35.                 "pagePath": "pages/recommend/recommend",
  36.                 "iconPath": "static/imgs/love1.png",
  37.                 "selectedIconPath": "static/imgs/love2.png",
  38.                 "text": "浪漫情话"
  39.             },
  40.             {
  41.                 "pagePath": "pages/index/index",
  42.                 "iconPath": "static/imgs/爱心1.png",
  43.                 "selectedIconPath": "static/imgs/爱心2.png",
  44.                 "text": "话术搜索"
  45.             }
  46.         ]
  47.     },
  48.     "globalStyle": {
  49.         "navigationBarTextStyle": "black",
  50.         "navigationBarTitleText": "恋爱话术",
  51.         "navigationBarBackgroundColor": "#ffffe1",
  52.         "backgroundColor": "#f5ffff"
  53.     }
  54. }
复制代码
微信小法式实战,用vue3实现逐日浪漫情话保举~-2.jpg

在这里插入图片描写

3.2 封装api接口

首要在==http.api.js==里面设置,这个文件之前也包括了我们的话术搜索接口
  1. import service from './http.interceptor.js'
  2. const api = {
  3.     // 话术搜索
  4.     getLoveChat: params => service.post('/open/getLoveChat', params),
  5.     getBanner: () => service.post('/open/getBanner'),
  6.     // 浪漫情话保举
  7.     getRecommendLove: () => service.post('/open/getRecommendLove'),
  8.     loveScore: params => service.post('/open/loveScore', params),
  9. }
  10. export default api
复制代码
3.3 编写浪漫情话页面
我们还是利用vue3加uniapp的语法停止页面开辟,同时为了使页面更优美,封装一个瀑布流组件停止衬着数据。下图就是我们要实现的大致结果
微信小法式实战,用vue3实现逐日浪漫情话保举~-3.jpg

在这里插入图片描写

3.3.1 封装瀑布流组件
  1. <template>
  2.     <view class="u-waterfall">
  3.         <view id="u-left-column" class="u-column">
  4.             <slot name="left" :leftList="leftList"></slot>
  5.         </view>
  6.         <view id="u-right-column" class="u-column">
  7.             <slot name="right" :rightList="rightList"></slot>
  8.         </view>
  9.     </view>
  10. </template>
  11. <script>
  12.     export default {
  13.         name: "waterfall",
  14.         props: {
  15.             value: {
  16.                 // 瀑布流数据
  17.                 type: Array,
  18.                 required: true,
  19.                 default: function() {
  20.                     return [];
  21.                 }
  22.             }
  23.         },
  24.         data() {
  25.             return {
  26.                 leftList: [],
  27.                 rightList: [],
  28.                 tempList: []
  29.             }
  30.         },
  31.         watch: {
  32.             copyFlowList(nVal, oVal) {
  33.                 this.tempList = this.cloneData(this.copyFlowList);
  34.                 this.leftList = []
  35.                 this.rightList = []
  36.                 this.splitData();
  37.             }
  38.         },
  39.         mounted() {
  40.             this.tempList = this.cloneData(this.copyFlowList);
  41.             this.splitData();
  42.         },
  43.         computed: {
  44.             // 破坏flowList变量的援用,否则watch的成果新旧值是一样的
  45.             copyFlowList() {
  46.                 return this.cloneData(this.value);
  47.             }
  48.         },
  49.         methods: {
  50.             async splitData() {
  51.                 if (!this.tempList.length) return;
  52.                 let leftRect = await this.$uGetRect('#u-left-column');
  53.                 let rightRect = await this.$uGetRect('#u-right-column');
  54.                 // 假如左侧小于或即是右侧,就增加到左侧,否则增加到右侧
  55.                 let item = this.tempList[0];
  56.                 // 处理屡次快速上拉后,能够数据会乱的题目,由于经过上面的两个await节点查询阻塞一按时候,加上前面的按时器干扰
  57.                 // 数组能够酿成[],致使此item值能够为undefined
  58.                 if (!item) return;
  59.                 if (leftRect.height < rightRect.height) {
  60.                     this.leftList.push(item);
  61.                 } else if (leftRect.height > rightRect.height) {
  62.                     this.rightList.push(item);
  63.                 } else {
  64.                     // 这里是为了保证第一和第二张增加时,左右都能有内容
  65.                     // 由于增加第一张,现实行列的高度能够还是0,这时需要按照行列元素长度判定下一个该放哪边
  66.                     if (this.leftList.length <= this.rightList.length) {
  67.                         this.leftList.push(item);
  68.                     } else {
  69.                         this.rightList.push(item);
  70.                     }
  71.                 }
  72.                 // 移除姑且列表的第一项
  73.                 this.tempList.splice(0, 1);
  74.                 // 假如姑且数组还稀有据,继续循环
  75.                 if (this.tempList.length) {
  76.                     this.splitData();
  77.                 } else {
  78.                     // 在这里模拟触发 我们界说的全局事务 实现数据通讯的目标
  79.                     let height = (leftRect.height > rightRect.height ? leftRect.height : rightRect.height) + 120
  80.                     uni.$emit('swiperHeightChange', height + 'px')
  81.                 }
  82.             },
  83.             // 复制而不是援用工具和数组
  84.             cloneData(data) {
  85.                 return JSON.parse(JSON.stringify(data));
  86.             },
  87.         }
  88.     }
  89. </script>
  90. <style lang="scss" scoped>
  91.     @mixin vue-flex($direction: row) {
  92.         /* #ifndef APP-NVUE */
  93.         display: flex;
  94.         flex-direction: $direction;
  95.         /* #endif */
  96.     }
  97.     .u-waterfall {
  98.         @include vue-flex;
  99.         flex-direction: row;
  100.         align-items: flex-start;
  101.     }
  102.     .u-column {
  103.         @include vue-flex;
  104.         flex: 1;
  105.         flex-direction: column;
  106.         height: auto;
  107.     }
  108.     .u-image {
  109.         width: 100%;
  110.     }
  111. </style>
复制代码
3.3.2 template代码编写
这里在引入瀑布流组件以后可间接在html里面利用
  1. <template>
  2.     <view>
  3.         <view class="change" @click="changeContent">
  4.             没找到想要的?赶紧点击这里
  5.             <text class="change-btn">换一批</text>
  6.             <view class="card">
  7.                 点击下方卡片可间接复制
  8.             </view>
  9.         </view>
  10.         <waterfall :value="dataList">
  11.             <template v-slot:left="left">
  12.                 <view v-for="item in left.leftList" :key="item.id" class="left-content" @click="copy(item)">
  13.                     <view class="item">
  14.                         {{item.content}}
  15.                     </view>
  16.                 </view>
  17.             </template>
  18.             <template v-slot:right="right">
  19.                 <view v-for="item in right.rightList" :key="item.id" class="right-content" @click="copy(item)">
  20.                     <view class="item">
  21.                         {{item.content}}
  22.                     </view>
  23.                 </view>
  24.             </template>
  25.         </waterfall>
  26.     </view>
  27. </template>
复制代码
3.3.3 js焦点方式编写
关键代码也不多,主如果一些请求数据的方式和响应式变量的界说。
  1. <script>
  2.     import {
  3.         toRefs,
  4.         reactive,
  5.         onMounted
  6.     } from 'vue'
  7.     import waterfall from '../../component/waterfall/index.vue'
  8.     import api from '../../axios/http.api.js'
  9.     export default {
  10.         name: 'content',
  11.         components: {
  12.             waterfall
  13.         },
  14.         setup() {
  15.             const state = reactive({
  16.                 dataList: [],
  17.                 loading: false,
  18.             })
  19.             const methods = reactive({
  20.                 getRecommendLove: async () => {
  21.                     state.loading = true
  22.                     let res = await api.getRecommendLove()
  23.                     state.loading = false
  24.                     if (res.code) {
  25.                         uni.showToast({
  26.                             title: res.msg,
  27.                             icon: 'none',
  28.                             position: 'top'
  29.                         })
  30.                     }
  31.                     state.dataList = res.data
  32.                 },
  33.                 copy(item) {
  34.                     // 复制话术到剪切板
  35.                     uni.setClipboardData({
  36.                         data: item.content
  37.                     })
  38.                     methods.score(item.id)
  39.                 },
  40.                 // 换一批
  41.                 changeContent() {
  42.                     methods.getRecommendLove()
  43.                 },
  44.                 // 打分
  45.                 async score(id) {
  46.                     let res = await api.loveScore({
  47.                         id
  48.                     })
  49.                     if (res.code) {
  50.                         uni.showToast({
  51.                             title: res.msg,
  52.                             icon: 'none',
  53.                             position: 'top'
  54.                         })
  55.                     }
  56.                 }
  57.             })
  58.             onMounted(() => {
  59.                 methods.getRecommendLove()
  60.                 // 分享菜单
  61.                 wx.showShareMenu({
  62.                     withShareTicket: true,
  63.                     menus: ['shareAppMessage', 'shareTimeline']
  64.                 })
  65.             })
  66.             return {
  67.                 ...toRefs(state),
  68.                 ...toRefs(methods)
  69.             }
  70.         }
  71.     }
  72. </script>
复制代码
3.3.4 css款式代码编写
我们究竟不是专业的前端,所以款式就随意对付一下就行,兄弟们不要介意哈
  1. <style lang="less" scoped>
  2.     .change {
  3.         width: 100%;
  4.         height: 120px;
  5.         border-bottom: 2px #aaaaff solid;
  6.         border-radius: 0 0 50px 50px;
  7.         background-size: 100% 100%;
  8.         opacity: 0.8;
  9.         padding-top: 45px;
  10.         text-align: center;
  11.         font-size: 20px;
  12.         color: #000000;
  13.         background-image: url(../../static/imgs/img4.png);
  14.         .change-btn {
  15.             display: inline-block;
  16.             padding: 2px;
  17.             color: red;
  18.         }
  19.         .card {
  20.             padding-top: 12px;
  21.             font-size: 13px;
  22.         }
  23.     }
  24.     .left-content {
  25.         margin: 15px 5px 15px 10px;
  26.         padding: 8px;
  27.         border-radius: 6px;
  28.         background: #aaffff linear-gradient(44deg, #aaffff 0%, #F4F8FF 100%);
  29.         font-size: 16px;
  30.         letter-spacing: 5px;
  31.         .item {
  32.             margin-bottom: 10px;
  33.         }
  34.     }
  35.     .right-content {
  36.         margin: 15px 10px 15px 5px;
  37.         padding: 8px;
  38.         border-radius: 6px;
  39.         background: #aaffff linear-gradient(44deg, #aaffff 0%, #F4F8FF 100%);
  40.         font-size: 16px;
  41.         letter-spacing: 5px;
  42.         .item {
  43.             margin-bottom: 10px;
  44.         }
  45.     }
  46. </style>
复制代码
微信小法式实战,用vue3实现逐日浪漫情话保举~-4.jpg

在这里插入图片描写

4、公布微信小法式
公布流程参考这篇博客恋爱话术微信小法式
微信小法式实战,用vue3实现逐日浪漫情话保举~-5.jpg

在这里插入图片描写

兄弟们可以扫描下面的太阳码停止体验哈

微信小法式实战,用vue3实现逐日浪漫情话保举~-6.jpg

在这里插入图片描写

5、总结

源码地址在这哈,会连结一向开源,希望兄弟们多多支持,==下了源码的老铁麻烦点个star哈==
  1. // 下了源码的老铁麻烦点个star哈
  2. https://gitee.com/yinfeng-code/love-chat-wx.git
复制代码
肝文不易,希望对其他单身大概恋爱中的兄弟们有所帮助,也希望兄弟们==关注加三连==支持一波哈
回复

使用道具

说点什么

您需要登录后才可以回帖 登录 | 立即注册
HOT • 推荐

神回复

站长姓名:王殿武 杭州共生网络科技 创始人 云裂变新零售系统 创始人 飞商人脉对接平台 创始人 同城交友聚会平台 创始人 生活经验分享社区 创始人 合作微信:15924191378(注明来意)