请选择 进入手机版 | 继续访问电脑版
帖子
帖子
用户
博客
课程
显示全部楼层
85
帖子
6
勋章
5746
Y币

AVM框架的模块使用───以客服功能为示例

[复制链接]
发表于 2023-1-4 17:46:59
以开发客服功能为例,基于AVM框架编写,结合iflyRecognition 模块(封装了 科大讯飞 的SDK 的语音听写、语音在线合成功能),轻松简单地实现了语音转文字客服功能。
stml代码:
  1. <template name='tpl'>
  2.         <view class="page">
  3.                 <safe-area class="header">
  4.                         <text class="header__title">客服</text>
  5.                 </safe-area>
  6.                 <scroll-view class="main" id="box">
  7.                         <text class="time">{nowtime}</text>
  8.                         <view class="servicebox">
  9.                                 <img class="avatarimg" src="../../image/kf@3x.png" alt="">
  10.                                 <view class="questionbox">
  11.                                         <view class="question">
  12.                                                 <text class="questiontxt">猜你想问</text>
  13.                                         </view>
  14.                                         <view class="question" v-for="(item,index) in listdata" @click="trigger(item)">
  15.                                                 <text class="questioncon">{item.titleName}</text>
  16.                                                 <img class="rightimg" src="../../image/right.png" alt="">
  17.                                         </view>
  18.                                 </view>
  19.                         </view>
  20.                         <view class="answerbox">
  21.                                 <view class="answercon" v-for="(item,index) in showdata">
  22.                                         <text class="time">{item.time}</text>
  23.                                         <view class="userbox" v-if="item.user==1">
  24.                                                 <view class="contxtbox">
  25.                                                         <text class="answercontxt">{item.con}</text>
  26.                                                 </view>
  27.                                                 <img class="avatarimg2" src="../../image/wd_tx.png" alt="">
  28.                                         </view>
  29.                                         <view class="serbox" v-if="item.user==2">
  30.                                                 <img class="avatarimg" src="../../image/kf@3x.png" alt="">
  31.                                                 <view class="contxtbox">
  32.                                                         <text class="answercontxt">{item.con}</text>
  33.                                                 </view>
  34.                                         </view>
  35.                                 </view>
  36.                         </view>
  37.                 </scroll-view>
  38.                 <view class="footer">
  39.                         <img @click="phonebtn()" class="phoneimg" src="../../image/phone.png" alt="">
  40.                         <input class="foottxt" v-model="sendword" type="text">
  41.                         <button class="footbtn" @click="send()">发送</button>
  42.                 </view>
  43.                 <view class="showbox" v-if="isshow">
  44.                         <view class="wordsbox" v-if="words">
  45.                                 <text class="wordstxt">{words}</text>
  46.                         </view>
  47.                         <view class="mainbox" @click="stop()">
  48.                                 <img class="ypgif" src="../../image/yp.gif" alt="">
  49.                                 <img class="stopimg" src="../../image/xslx_lzz@2x.png" alt="">
  50.                         </view>
  51.                 </view>
  52.         </view>
  53. </template>
  54. <script>
  55. export default {
  56.         name: "tpl",
  57.         apiready() {
  58.                 this.data.nowtime = this.getTimeData()//当前时间
  59.                 this.init()//模块初始化
  60.         },
  61.         data() {
  62.                 return {
  63.                         nowtime: '',//当前时间
  64.                         listdata: [//想问的问题
  65.                                 {
  66.                                         titleName: "公告",
  67.                                         titleContent: "2021年12月1日至2022年1月31日,APP开展深圳地区公测,欢迎各位家长及同学们参与公测并给我们提供宝贵的意见、建议。公测期间前100位成功注册的用户将免费获得VIP课程。",
  68.                                 },
  69.                                 {
  70.                                         titleName: "账号与登录",
  71.                                         titleContent: "输入手机号码,在获取验证码后可以快速完成账户注册。在这个过程中您需要选择并确认您的身份(学生、老师)。选择学生身份:请同学您按指引要求填写相关信息后点击“提交”完成账户注册。选择老师身份:请老师您按指引要求填写相关信息后点击“提交”完成老师身份的账户注册。",
  72.                                 },
  73.                                 {
  74.                                         titleName: "选择课程",
  75.                                         titleContent: "在【学习】栏目内,您可以选择您想要的课程内容,包括可以免费使用的“听、背、读”部分的内容,以及收费的“诵、讲”部分的VIP内容。完成充值后即可使用VIP内容。",
  76.                                 },
  77.                                 {
  78.                                         titleName: "充值与消费",
  79.                                         titleContent: "目前我们设立有“月卡VIP会员”、“季卡VIP会员”、“半年VIP会员”、“年卡VIP会员”四个档次,您可以根据自己的需求来购买会员。(公测期间,前100位注册用户可免费获得终生VIP用户权限)。",
  80.                                 },
  81.                                 {
  82.                                         titleName: "操作问题",
  83.                                         titleContent: "请您简单描述一下您所遇到的操作问题,现在由于咨询的人数较多,我们将尽快予以答复,请您耐心等待。",
  84.                                 },
  85.                                 {
  86.                                         titleName: "反馈bug",
  87.                                         titleContent: "请您简单描述一下您所遇到的bug,我们会将您所提供的线索及时反馈给技术部门,感谢给予我们的帮助。",
  88.                                 },
  89.                                 {
  90.                                         titleName: "建议",
  91.                                         titleContent: "我们非常乐于倾听您的声音,感谢您宝贵的建议!",
  92.                                 },
  93.                                 {
  94.                                         titleName: "投诉",
  95.                                         titleContent: "请您简单描述一下您要投诉的内容,现在由于咨询的人数较多,我们将尽快予以答复,请您耐心等待。",
  96.                                 }
  97.                         ],
  98.                         isshow: false,//显示话筒输入
  99.                         words: '',//文本
  100.                         showdata: [],//显示的数据
  101.                         sendword: "",//发送文字
  102.                 };
  103.         },
  104.         computed: {

  105.         },
  106.         methods: {
  107.                 //获取当前时间时分
  108.                 getTimeData() {
  109.                         var data = new Date()
  110.                         var h = data.getHours() < 10 ? '0' + data.getHours() : data.getHours()
  111.                         var m = data.getMinutes() < 10 ? '0' + data.getMinutes() : data.getMinutes()
  112.                         var now = h + ':' + m
  113.                         return now
  114.                 },
  115.                 //模块初始化
  116.                 init() {
  117.                         console.log("初始化");
  118.                         var iflyRecognition = api.require('iflyRecognition');
  119.                         iflyRecognition.createUtility({
  120.                                 ios_appid: '******',
  121.                                 android_appid: '*******'
  122.                         }, function (ret, err) {
  123.                                 if (ret.status) {
  124.                                         console.log('创建成功');
  125.                                 } else {
  126.                                         console.log('创建失败');
  127.                                 }
  128.                         });
  129.                 },
  130.                 //话筒输入
  131.                 phonebtn() {
  132.                         this.data.isshow = true
  133.                         this.start()//开始识别
  134.                 },
  135.                 //开始识别
  136.                 start() {
  137.                         console.log("开始");
  138.                         var that =this
  139.                         var iflyRecognition = api.require('iflyRecognition');
  140.                         iflyRecognition.record({
  141.                                 vadbos: 10000,
  142.                                 vadeos: 10000,
  143.                                 rate: 16000,
  144.                                 asrptt: 1,
  145.                         }, function (ret, err) {
  146.                                 if (ret.status) {
  147.                                         that.data.words = ret.wordStr
  148.                                         console.log(JSON.stringify(ret));
  149.                                 } else {
  150.                                         console.log(JSON.stringify(err));
  151.                                 }
  152.                         });
  153.                 },
  154.                 //停止
  155.                 stop() {
  156.                         this.data.isshow = false
  157.                         console.log("停止");
  158.                         var iflyRecognition = api.require('iflyRecognition');
  159.                         iflyRecognition.stopRecord();
  160.                         this.data.sendword =this.data.words
  161.                 },
  162.                 //点击问题
  163.                 trigger(item) {
  164.                         var obj = {
  165.                                 time: this.getTimeData(),
  166.                                 user: 1,//用户
  167.                                 con: item.titleName//用户点击的问题内容
  168.                         }
  169.                         this.data.showdata.push(obj) //用户点击的数据
  170.                         document.getElementById('box').scrollTo({ position: 'lower' });//滚动到底部
  171.                         //一秒后客服回答
  172.                         setTimeout(() => {
  173.                                 var obj2 = {
  174.                                         time: this.getTimeData(),
  175.                                         user: 2,//客服
  176.                                         con: item.titleContent //回答的内容
  177.                                 }
  178.                                 this.data.showdata.push(obj2) //客服回答
  179.                         }, 1000)
  180.                         setTimeout(() => {
  181.                                 document.getElementById('box').scrollTo({ position: 'lower' });//滚动到底部
  182.                         }, 1100)
  183.                 },
  184.                 //发送问题
  185.                 send() {
  186.                         var obj = {
  187.                                 time: this.getTimeData(),
  188.                                 user: 1,
  189.                                 con: this.data.sendword//用户输入的内容
  190.                         }
  191.                         this.data.showdata.push(obj) //用户发送的问题内容
  192.                         document.getElementById('box').scrollTo({ position: 'lower' });//滚动到底部
  193.                         //一秒后客服回答
  194.                         setTimeout(() => {
  195.                                 var obj2 = {
  196.                                         time: this.getTimeData(),
  197.                                         user: 2,//客服
  198.                                         con: "反馈成功,24小时以内会联系您" //回答的内容
  199.                                 }
  200.                                 this.data.showdata.push(obj2) //客服回答
  201.                         }, 1000)
  202.                         setTimeout(() => {
  203.                                 document.getElementById('box').scrollTo({ position: 'lower' });//滚动到底部
  204.                         }, 1100)
  205.                         this.data.sendword = ""
  206.                 },
  207.         }
  208. };
  209. </script>
  210. <style>
  211. .page {
  212.         height: 100%;
  213.         background-color: #fafafa;
  214. }
  215. .header {
  216.         background: #81a9c3;
  217.         justify-content: center;
  218.         align-items: center;
  219. }
  220. .header__title {
  221.         color: #fff;
  222.         font-size: 18px;
  223.         font-weight: bold;
  224.         height: 50px;
  225.         line-height: 50px;
  226. }

  227. .main {
  228.         flex: 1;
  229.         padding: 15px;
  230. }
  231. .time {
  232.         text-align: center;
  233.         line-height: 24px;
  234.         color: rgb(78, 78, 78);
  235. }
  236. .servicebox {
  237.         flex-direction: row;
  238. }
  239. .avatarimg {
  240.         width: 40px;
  241.         height: 40px;
  242.         margin-right: 10px;
  243.         border-radius: 50%;
  244. }
  245. .avatarimg2 {
  246.         width: 40px;
  247.         height: 40px;
  248.         margin-left: 10px;
  249.         border-radius: 50%;
  250. }
  251. .questionbox {
  252.         width: 200px;
  253.         background-color: #ffffff;
  254.         padding: 10px;
  255. }
  256. .question {
  257.         width: 100%;
  258.         padding: 5px;
  259.         flex-direction: row;
  260.         justify-content: space-between;
  261.         align-items: center;
  262.         border-bottom: 1px solid rgb(219, 219, 219);
  263. }
  264. .questiontxt {
  265.         color: rgb(0, 0, 0);
  266.         font-size: 16px;
  267. }
  268. .questioncon {
  269.         color: rgb(107, 107, 107);
  270.         font-size: 14px;
  271. }
  272. .rightimg {
  273.         width: 7px;
  274.         height: 12px;
  275. }

  276. .answercon {
  277.         padding: 10px 0px;
  278. }
  279. .userbox {
  280.         width: 100%;
  281.         flex-direction: row;
  282.         justify-content: flex-end;
  283. }
  284. .serbox {
  285.         width: 100%;
  286.         flex-direction: row;
  287.         justify-content: left;
  288. }
  289. .contxtbox {
  290.         background: #fff;
  291.         padding: 10px;
  292.         border-radius: 8px;
  293.         align-items: center;
  294. }
  295. .answercontxt {
  296.         font-size: 14px;
  297.         color: #333;
  298. }
  299. .footer {
  300.         width: 100%;
  301.         height: 60px;
  302.         background: #ffffff;
  303.         flex-direction: row;
  304.         justify-content: center;
  305.         align-items: center;
  306.         padding: 20px;
  307. }
  308. .phoneimg {
  309.         width: 30px;
  310.         height: 30px;
  311.         margin-right: 10px;
  312. }
  313. .foottxt {
  314.         width: 75%;
  315.         height: 30px;
  316.         padding: 0 5px;
  317. }
  318. .footbtn {
  319.         margin-left: 10px;
  320.         width: 80px;
  321.         height: 30px;
  322.         background-color: rgba(251, 131, 78, 0.8);
  323.         color: #fff;
  324. }
  325. .showbox {
  326.         width: 100%;
  327.         height: 100%;
  328.         background: rgba(33, 6, 0, 0.4);
  329.         position: absolute;
  330.         z-index: 9;
  331.         top: 0px;
  332.         left: 0px;
  333.         justify-content: center;
  334.         align-items: center;
  335. }
  336. .wordstxt {
  337.         background: #ffffff;
  338.         padding: 10px;
  339. }
  340. .mainbox {
  341.         width: 150px;
  342.         height: 80px;
  343.         justify-content: center;
  344.         align-items: center;
  345. }
  346. .ypgif {
  347.         width: 150px;
  348.         position: absolute;
  349.         top: 0;
  350.         left: 0;
  351.         z-index: -1;
  352. }
  353. .stopimg {
  354.         width: 70px;
  355. }
  356. </style>
复制代码
效果图:


本帖子中包含更多资源,您需要 登录 才可以下载或查看,没有帐号?立即注册

X
您需要登录后才可以回帖 登录

本版积分规则