123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- // pages/other/znzx/znzx.js
- const plugin = requirePlugin("WechatSI")
- const manager = plugin.getRecordRecognitionManager()
- import {
- getQaTalkResult,
- getAnswer,
- getFlyj
- } from "../../../apis/smzc"
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- keyBoardHeight: 0,
- scrollInto: "none",
- msg: {
- from: "me",
- content: ""
- },
- msgList: [{
- from: "other",
- msg_type: "type1",
- content: "您好,我是小慧,很高心为您服务,请问有什么可以帮您?"
- }],
- showDetail: false,
- detailInfo: {},
- inType: 1,
- startSpech: false
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- },
- input(e) {
- let that = this
- that.setData({
- 'msg.content': e.detail.value
- })
- },
- sendMsg() {
- let that = this
- let msgList = that.data.msgList
- let msg = that.data.msg
- let newMsg = JSON.parse(JSON.stringify(msg))
- msgList.push(newMsg)
- that.setData({
- msgList: msgList
- })
- that.resetMeMsg()
- that.getReply(newMsg.content)
- that.setScrollInto()
- },
- // 获取回复
- getReply(e) {
- let that = this
- let form = {
- keyWord: e
- }
- getQaTalkResult(form).then(res => {
- if (res.code == 200) {
- let data = res.data
- switch (data.answerReturnType) {
- default:
- break;
- case "1":
- that.msgType2(data)
- break
- case "2":
- case "3":
- that.msgType1(data)
- break
- case "4":
- that.msgType5(data)
- break
- }
- } else {
- wx.showToast({
- title: res.msg || "服务异常",
- duration: 3000,
- icon: "none"
- })
- }
- })
- },
- // 获取答案
- getAnswer(e) {
- let that = this
- let item = e.currentTarget.dataset.item
- let form = {
- answerId: item.answerId || item.intentId,
- answerTitleId: item.answerTitleId || item.intentTitleId,
- logId: item.logId,
- name: "右",
- titleName: item.answerTitle || item.intentTitle
- }
- getAnswer(form).then(res => {
- if (res.code == 200) {
- let data = res.data
- that.setMeMsg(item.answerTitle || item.intentTitle)
- switch (data.showType) {
- default:
- break;
- case "0":
- that.msgType4(data)
- break
- case "1":
- that.msgType3(data)
- break
- }
- } else {
- wx.showToast({
- title: res.msg || "服务异常",
- duration: 3000,
- icon: "none"
- })
- }
- })
- },
- // 富文本点击事件
- handleTap(e) {
- console.log(e)
- },
- // 普通消息
- msgType1(data) {
- let that = this
- var msg = {
- from: "other",
- msg_type: "type1",
- content: data.answerDetail.answerContent
- }
- let msgList = that.data.msgList
- msgList.push(msg)
- that.setData({
- msgList: msgList
- })
- that.setScrollInto()
- },
- // 消息类型 5
- msgType5(data) {
- let that = this
- var msg = {
- from: "other",
- msg_type: "type1",
- content: "抱歉,暂时无法接入人工服务。"
- }
- let msgList = that.data.msgList
- msgList.push(msg)
- that.setData({
- msgList: msgList
- })
- that.setScrollInto()
- },
- // 消息类型 1
- msgType2(data) {
- let that = this
- var msg = {
- from: "other",
- msg_type: "type2",
- title: data.answerListTip,
- linkList: data.answerList.slice(0, 5),
- subTitle: data.intentDetail.intentDes,
- subLinkList: data.intentDetail.intentList
- }
- let msgList = that.data.msgList
- msgList.push(msg)
- that.setData({
- msgList: msgList
- })
- that.setScrollInto()
- },
- // 消息类型 2
- msgType3(data) {
- let that = this
- var msg = {
- from: "other",
- msg_type: "type3",
- title: data.intentDetail.intentDes,
- linkList: data.intentDetail.intentList,
- }
- let msgList = that.data.msgList
- msgList.push(msg)
- that.setData({
- msgList: msgList
- })
- that.setScrollInto()
- },
- // 消息类型 3
- msgType4(data) {
- let that = this
- let regexWidth = /width="50%/g
- let matches = data.answerDetail.answerContent.match(/ src="([^"]*)"/g);
- let srcs = matches ? matches.map(match => match.replace(/.*src="([^"]*)".*/, '$1')) : [];
- var msg = {
- from: "other",
- msg_type: "type4",
- title: data.answerDetail.answerTitle,
- content: data.answerDetail.answerContent.replace(regexWidth, 'width="100%"'),
- imgSrcList: srcs,
- linkList: data.answerDetail.answerBasis,
- }
- let msgList = that.data.msgList
- msgList.push(msg)
- that.setData({
- msgList: msgList
- })
- that.setScrollInto()
- },
- // 富文本点击事件
- richTap(e) {
- let imgList = e.currentTarget.dataset.img
- if (imgList && imgList.length > 0) {
- wx.previewImage({
- urls: imgList,
- })
- }
- },
- // 获取依据详情
- getDetail(e) {
- let that = this
- let item = e.currentTarget.dataset.item
- let form = {
- answerBasisId: item.answerBasisId
- }
- getFlyj(form).then(res => {
- if (res.code == 200) {
- that.setData({
- detailInfo: res.data,
- showDetail: true
- })
- } else {
- wx.showToast({
- title: res.msg || "服务异常",
- duration: 3000,
- icon: "none"
- })
- }
- })
- },
- close() {
- let that = this
- that.setData({
- showDetail: false,
- detailInfo: {}
- })
- },
- setMeMsg(e) {
- let that = this
- let msgList = that.data.msgList
- let msg = {
- from: "me",
- content: e
- }
- msgList.push(msg)
- that.setData({
- msgList: msgList
- })
- that.resetMeMsg()
- },
- resetMeMsg() {
- let that = this
- that.setData({
- 'msg.content': ""
- })
- },
- setScrollInto() {
- let that = this
- that.setData({
- scrollInto: "bottom"
- })
- },
- intype(e) {
- let that = this
- let type = e.currentTarget.dataset.type
- that.setData({
- inType: type
- })
- },
- startVoice() {
- let that = this
- setTimeout(() => {
- manager.start()
- }, 10);
- manager.onError = function (res) {
- console.log(res)
- wx.showToast({
- title: '识别失败',
- icon: "none",
- duration: 3000
- })
- }
- manager.onStart = function (res) {}
- manager.onStop = function (res) {
- if (res.result) {
- that.setMeMsg(res.result)
- that.getReply(res.result)
- }
- }
- that.setData({
- startSpech: true
- })
- },
- endVoice() {
- let that = this
- manager.stop()
- that.setData({
- startSpech: false
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- let that = this
- wx.onKeyboardHeightChange(res => {
- that.setData({
- keyBoardHeight: res.height
- })
- })
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- wx.offKeyboardHeightChange()
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- }
- })
|