search.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. const api = require('../../../utils/api.js');
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. mask: false,
  8. animationStatus: false,
  9. recodeText:'长按录音',
  10. setting:true
  11. },
  12. // 打开遮罩层
  13. mask: function () {
  14. let that = this;
  15. that.setData({
  16. mask: true
  17. })
  18. },
  19. // 关闭遮罩层
  20. clear: function () {
  21. let that = this;
  22. that.setData({
  23. mask: false
  24. })
  25. },
  26. recodeEnd: function (e) {
  27. let that = this
  28. that.setData({
  29. animationStatus: false
  30. })
  31. // that.data.recorderManager = wx.getRecorderManager();
  32. that.data.recorderManager.stop();
  33. console.log(e)
  34. wx.showLoading({
  35. title: '正在搜索',
  36. })
  37. that.data.recorderManager.onStop((res) => {
  38. this.tempFilePath = res.tempFilePath;
  39. console.log(res.tempFilePath)
  40. that.data.bdaudio = res.tempFilePath
  41. wx.uploadFile({
  42. url: api.UploadAudio,
  43. filePath: res.tempFilePath,
  44. header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8', },
  45. name: 'voiceFile',
  46. success(res){
  47. wx.hideLoading()
  48. res.data = JSON.parse(res.data)
  49. if (res.data.errno==-1){
  50. wx.showToast({
  51. title: '没有数据',
  52. icon:'none'
  53. })
  54. }else{
  55. that.setData({
  56. data: res.data.data,
  57. mask: false
  58. })
  59. }
  60. }
  61. })
  62. const {
  63. tempFilePath
  64. } = res
  65. })
  66. },
  67. inText:function(e){
  68. let that=this
  69. if(e.detail.value==''){
  70. that.setData({
  71. data: '',
  72. search:wx.getStorageSync('search')
  73. })
  74. }
  75. },
  76. // 清除历史
  77. clearData:function(){
  78. wx.setStorageSync('search',[])
  79. wx.showToast({
  80. title: '清除成功',
  81. icon:'none'
  82. })
  83. this.setData({
  84. search:''
  85. })
  86. },
  87. //表单搜索
  88. blur:function(e){
  89. let that=this
  90. if (String(e.detail.value).trim()==''){
  91. that.setData({
  92. data:''
  93. })
  94. return false;
  95. }
  96. wx.showLoading({
  97. title: '正在搜索',
  98. mask: true
  99. })
  100. if(e.currentTarget.dataset.keys){
  101. e.detail.value = e.currentTarget.dataset.keys
  102. }
  103. wx.request({
  104. url: api.UploadAudio,
  105. data: { serviceName: String(e.detail.value).trim()},
  106. success(res){
  107. wx.hideLoading()
  108. var search=[]
  109. if (!wx.getStorageSync('search')){
  110. wx.setStorageSync('search', [])
  111. }
  112. search = wx.getStorageSync('search')
  113. search.push(String(e.detail.value).trim())
  114. var arr = new Set(search)
  115. wx.setStorageSync('search', [...arr])
  116. that.setData({
  117. data:res.data.data
  118. })
  119. }
  120. })
  121. },
  122. //跳转
  123. nav:function(e){
  124. wx.navigateTo({
  125. url: '/pages/handle/serverDetails/serverDetails?unid='+e.currentTarget.dataset.unid+'&search=1',
  126. })
  127. },
  128. recodeClick: function () {
  129. let that=this
  130. this.setData({
  131. animationStatus: true,
  132. recodeText:'录音中'
  133. })
  134. if (that.data.recorderManager){
  135. that.data.recorderManager.stop()
  136. }
  137. that.data.recorderManager = wx.getRecorderManager();
  138. const options = {
  139. duration: 10000, //指定录音的时长,单位 ms
  140. sampleRate: 16000, //采样率
  141. numberOfChannels: 1, //录音通道数
  142. encodeBitRate: 96000, //编码码率
  143. format: 'mp3', //音频格式,有效值 aac/mp3
  144. frameSize: 50, //指定帧大小,单位 KB
  145. }
  146. //开始录音
  147. that.data.recorderManager.start(options);
  148. that.data.recorderManager.onStart(() => {
  149. console.log('recorder start')
  150. });
  151. //错误回调
  152. that.data.recorderManager.onError((res) => {
  153. console.log(res);
  154. })
  155. },
  156. openSetting:function(e){
  157. let that = this
  158. if(wx.getStorageSync('settings')){
  159. wx.openSetting({
  160. success(res) {
  161. console.log(res.authSetting)
  162. if (res.authSetting['scope.record']) {
  163. wx.setStorageSync('setting', false)
  164. that.setData({
  165. setting: false
  166. })
  167. }
  168. }
  169. })
  170. }else{
  171. wx.authorize({
  172. scope: 'scope.record',
  173. success(res) {
  174. that.setData({
  175. setting: false
  176. })
  177. }, fail(res) {
  178. wx.showToast({
  179. title: '拒绝录音授权,可能会影响功能使用',
  180. icon:'none'
  181. })
  182. wx.setStorageSync('setting', true)
  183. wx.setStorageSync('settings', true)
  184. that.setData({
  185. setting: true
  186. })
  187. return false;
  188. }
  189. })
  190. }
  191. },
  192. /**
  193. * 生命周期函数--监听页面加载
  194. */
  195. onLoad: function (options) {
  196. wx.getSetting({
  197. success:res=>{
  198. if(!res.authSetting['scope.record']){
  199. wx.setStorageSync('settings', '')
  200. }
  201. }
  202. })
  203. this.setData({
  204. search: wx.getStorageSync('search')
  205. })
  206. },
  207. /**
  208. * 生命周期函数--监听页面初次渲染完成
  209. */
  210. onReady: function () {
  211. let that=this
  212. wx.request({
  213. url: api.HotService,
  214. success(res){
  215. that.setData({
  216. hotService:res.data.data
  217. })
  218. }
  219. })
  220. },
  221. /**
  222. * 生命周期函数--监听页面显示
  223. */
  224. onShow: function () {
  225. },
  226. /**
  227. * 生命周期函数--监听页面隐藏
  228. */
  229. onHide: function () {
  230. },
  231. /**
  232. * 生命周期函数--监听页面卸载
  233. */
  234. onUnload: function () {
  235. },
  236. /**
  237. * 页面相关事件处理函数--监听用户下拉动作
  238. */
  239. onPullDownRefresh: function () {
  240. },
  241. /**
  242. * 页面上拉触底事件的处理函数
  243. */
  244. onReachBottom: function () {
  245. },
  246. /**
  247. * 用户点击右上角分享
  248. */
  249. onShareAppMessage: function () {
  250. }
  251. })