znzx.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // pages/other/znzx/znzx.js
  2. const plugin = requirePlugin("WechatSI")
  3. const manager = plugin.getRecordRecognitionManager()
  4. import {
  5. getQaTalkResult,
  6. getAnswer,
  7. getFlyj
  8. } from "../../../apis/smzc"
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. keyBoardHeight: 0,
  15. scrollInto: "none",
  16. msg: {
  17. from: "me",
  18. content: ""
  19. },
  20. msgList: [{
  21. from: "other",
  22. msg_type: "type1",
  23. content: "您好,我是小慧,很高心为您服务,请问有什么可以帮您?"
  24. }],
  25. showDetail: false,
  26. detailInfo: {},
  27. inType: 1,
  28. startSpech: false
  29. },
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onLoad(options) {
  34. },
  35. input(e) {
  36. let that = this
  37. that.setData({
  38. 'msg.content': e.detail.value
  39. })
  40. },
  41. sendMsg() {
  42. let that = this
  43. let msgList = that.data.msgList
  44. let msg = that.data.msg
  45. let newMsg = JSON.parse(JSON.stringify(msg))
  46. msgList.push(newMsg)
  47. that.setData({
  48. msgList: msgList
  49. })
  50. that.resetMeMsg()
  51. that.getReply(newMsg.content)
  52. that.setScrollInto()
  53. },
  54. // 获取回复
  55. getReply(e) {
  56. let that = this
  57. let form = {
  58. keyWord: e
  59. }
  60. getQaTalkResult(form).then(res => {
  61. if (res.code == 200) {
  62. let data = res.data
  63. switch (data.answerReturnType) {
  64. default:
  65. break;
  66. case "1":
  67. that.msgType2(data)
  68. break
  69. case "2":
  70. case "3":
  71. that.msgType1(data)
  72. break
  73. case "4":
  74. that.msgType5(data)
  75. break
  76. }
  77. } else {
  78. wx.showToast({
  79. title: res.msg || "服务异常",
  80. duration: 3000,
  81. icon: "none"
  82. })
  83. }
  84. })
  85. },
  86. // 获取答案
  87. getAnswer(e) {
  88. let that = this
  89. let item = e.currentTarget.dataset.item
  90. let form = {
  91. answerId: item.answerId || item.intentId,
  92. answerTitleId: item.answerTitleId || item.intentTitleId,
  93. logId: item.logId,
  94. name: "右",
  95. titleName: item.answerTitle || item.intentTitle
  96. }
  97. getAnswer(form).then(res => {
  98. if (res.code == 200) {
  99. let data = res.data
  100. that.setMeMsg(item.answerTitle || item.intentTitle)
  101. switch (data.showType) {
  102. default:
  103. break;
  104. case "0":
  105. that.msgType4(data)
  106. break
  107. case "1":
  108. that.msgType3(data)
  109. break
  110. }
  111. } else {
  112. wx.showToast({
  113. title: res.msg || "服务异常",
  114. duration: 3000,
  115. icon: "none"
  116. })
  117. }
  118. })
  119. },
  120. // 富文本点击事件
  121. handleTap(e) {
  122. console.log(e)
  123. },
  124. // 普通消息
  125. msgType1(data) {
  126. let that = this
  127. var msg = {
  128. from: "other",
  129. msg_type: "type1",
  130. content: data.answerDetail.answerContent
  131. }
  132. let msgList = that.data.msgList
  133. msgList.push(msg)
  134. that.setData({
  135. msgList: msgList
  136. })
  137. that.setScrollInto()
  138. },
  139. // 消息类型 5
  140. msgType5(data) {
  141. let that = this
  142. var msg = {
  143. from: "other",
  144. msg_type: "type1",
  145. content: "抱歉,暂时无法接入人工服务。"
  146. }
  147. let msgList = that.data.msgList
  148. msgList.push(msg)
  149. that.setData({
  150. msgList: msgList
  151. })
  152. that.setScrollInto()
  153. },
  154. // 消息类型 1
  155. msgType2(data) {
  156. let that = this
  157. var msg = {
  158. from: "other",
  159. msg_type: "type2",
  160. title: data.answerListTip,
  161. linkList: data.answerList.slice(0, 5),
  162. subTitle: data.intentDetail.intentDes,
  163. subLinkList: data.intentDetail.intentList
  164. }
  165. let msgList = that.data.msgList
  166. msgList.push(msg)
  167. that.setData({
  168. msgList: msgList
  169. })
  170. that.setScrollInto()
  171. },
  172. // 消息类型 2
  173. msgType3(data) {
  174. let that = this
  175. var msg = {
  176. from: "other",
  177. msg_type: "type3",
  178. title: data.intentDetail.intentDes,
  179. linkList: data.intentDetail.intentList,
  180. }
  181. let msgList = that.data.msgList
  182. msgList.push(msg)
  183. that.setData({
  184. msgList: msgList
  185. })
  186. that.setScrollInto()
  187. },
  188. // 消息类型 3
  189. msgType4(data) {
  190. let that = this
  191. let regexWidth = /width="50%/g
  192. let matches = data.answerDetail.answerContent.match(/ src="([^"]*)"/g);
  193. let srcs = matches ? matches.map(match => match.replace(/.*src="([^"]*)".*/, '$1')) : [];
  194. var msg = {
  195. from: "other",
  196. msg_type: "type4",
  197. title: data.answerDetail.answerTitle,
  198. content: data.answerDetail.answerContent.replace(regexWidth, 'width="100%"'),
  199. imgSrcList: srcs,
  200. linkList: data.answerDetail.answerBasis,
  201. }
  202. let msgList = that.data.msgList
  203. msgList.push(msg)
  204. that.setData({
  205. msgList: msgList
  206. })
  207. that.setScrollInto()
  208. },
  209. // 富文本点击事件
  210. richTap(e) {
  211. let imgList = e.currentTarget.dataset.img
  212. if (imgList && imgList.length > 0) {
  213. wx.previewImage({
  214. urls: imgList,
  215. })
  216. }
  217. },
  218. // 获取依据详情
  219. getDetail(e) {
  220. let that = this
  221. let item = e.currentTarget.dataset.item
  222. let form = {
  223. answerBasisId: item.answerBasisId
  224. }
  225. getFlyj(form).then(res => {
  226. if (res.code == 200) {
  227. that.setData({
  228. detailInfo: res.data,
  229. showDetail: true
  230. })
  231. } else {
  232. wx.showToast({
  233. title: res.msg || "服务异常",
  234. duration: 3000,
  235. icon: "none"
  236. })
  237. }
  238. })
  239. },
  240. close() {
  241. let that = this
  242. that.setData({
  243. showDetail: false,
  244. detailInfo: {}
  245. })
  246. },
  247. setMeMsg(e) {
  248. let that = this
  249. let msgList = that.data.msgList
  250. let msg = {
  251. from: "me",
  252. content: e
  253. }
  254. msgList.push(msg)
  255. that.setData({
  256. msgList: msgList
  257. })
  258. that.resetMeMsg()
  259. },
  260. resetMeMsg() {
  261. let that = this
  262. that.setData({
  263. 'msg.content': ""
  264. })
  265. },
  266. setScrollInto() {
  267. let that = this
  268. that.setData({
  269. scrollInto: "bottom"
  270. })
  271. },
  272. intype(e) {
  273. let that = this
  274. let type = e.currentTarget.dataset.type
  275. that.setData({
  276. inType: type
  277. })
  278. },
  279. startVoice() {
  280. let that = this
  281. setTimeout(() => {
  282. manager.start()
  283. }, 10);
  284. manager.onError = function (res) {
  285. console.log(res)
  286. wx.showToast({
  287. title: '识别失败',
  288. icon: "none",
  289. duration: 3000
  290. })
  291. }
  292. manager.onStart = function (res) {}
  293. manager.onStop = function (res) {
  294. if (res.result) {
  295. that.setMeMsg(res.result)
  296. that.getReply(res.result)
  297. }
  298. }
  299. that.setData({
  300. startSpech: true
  301. })
  302. },
  303. endVoice() {
  304. let that = this
  305. manager.stop()
  306. that.setData({
  307. startSpech: false
  308. })
  309. },
  310. /**
  311. * 生命周期函数--监听页面初次渲染完成
  312. */
  313. onReady() {
  314. },
  315. /**
  316. * 生命周期函数--监听页面显示
  317. */
  318. onShow() {
  319. let that = this
  320. wx.onKeyboardHeightChange(res => {
  321. that.setData({
  322. keyBoardHeight: res.height
  323. })
  324. })
  325. },
  326. /**
  327. * 生命周期函数--监听页面隐藏
  328. */
  329. onHide() {
  330. wx.offKeyboardHeightChange()
  331. },
  332. /**
  333. * 生命周期函数--监听页面卸载
  334. */
  335. onUnload() {
  336. },
  337. /**
  338. * 页面相关事件处理函数--监听用户下拉动作
  339. */
  340. onPullDownRefresh() {
  341. },
  342. /**
  343. * 页面上拉触底事件的处理函数
  344. */
  345. onReachBottom() {
  346. },
  347. /**
  348. * 用户点击右上角分享
  349. */
  350. onShareAppMessage() {
  351. }
  352. })