report.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <template>
  2. <view class="content">
  3. <view class="topbar" v-show="isIphone || isWeiXin">
  4. </view>
  5. <!-- #ifdef MP-WEIXIN -->
  6. <view class="topbar">
  7. </view>
  8. <!-- #endif -->
  9. <view class="status_bar">
  10. <view class="left" @click="goList">上报记录</view>
  11. <span style="font-size: 36rpx;">隐患上报</span>
  12. <view class="right"></view>
  13. </view>
  14. <view class="mainCont">
  15. <u-cell-group style="margin-bottom: 10px">
  16. <u-cell-item :arrow="false" :value-style="{textAlign: 'left',paddingLeft: '10px'}">
  17. <view slot="title"><span class="red"></span>所在位置:</view>
  18. <u-input placeholder="请填写详细位置" v-model="form.local" type="text" />
  19. </u-cell-item>
  20. </u-cell-group>
  21. <view class="card" style="width: 100%;border-radius: none;">
  22. <view class="top">
  23. <span style="color: #5976BA" @click="type = 'audio'" v-show="type == 'text'">
  24. <u-icon name="mic"></u-icon>切换语音录入
  25. </span>
  26. <span style="color: #5976BA" @click="type = 'text'" v-show="type == 'audio'">
  27. <u-icon name="edit-pen"></u-icon>切换文字输入
  28. </span>
  29. </view>
  30. <view class="bottom">
  31. <u-input v-show="type == 'text'" v-model="mstext" placeholder="请输入详情描述..." :height="300"
  32. type="textarea" />
  33. <view class="imgupload">
  34. <u-upload :show-progress="false" ref="upload" @on-remove="uploadRemove" @on-success="uploadSuccess"
  35. :header="uploadHeader" max-count="3" width="80px" height="80px" :custom-btn="true"
  36. :action="action" :file-list="fileList">
  37. <view slot="addBtn" class="slot-btn">
  38. <image style="width: 80px;height: 80px" src="/static/tjzp.png" alt="">
  39. </view>
  40. </u-upload>
  41. </view>
  42. <view class="audioBox" v-show="type == 'audio'">
  43. <view class="left">
  44. <view class="imgBox">
  45. <image style="width: 36px;height: 36px" src="/static/sj.png"
  46. v-show="audioType == '3' && !playIng" alt="" @click="playAudio">
  47. <image style="width: 36px;height: 36px" src="/static/bf.gif"
  48. v-show="audioType == '3' && playIng" alt="">
  49. <p v-show="audioType == '3'">我的录音</p>
  50. </view>
  51. </view>
  52. <view class="middle">
  53. <view class="outBox" v-show="audioType == '2'">
  54. <image style="width: 100%;height: 80px" src="/static/lyzb.gif" alt="">
  55. <p style="color: #CCCCCC;text-align: center;margin-top: -20px">录音中,松开结束录音</p>
  56. </view>
  57. <view class="imgBox" @click="submitAudio" @touchstart="toAudio" @touchend="audioEnd">
  58. <image style="margin-top: 15px;width: 33px;height: 45px" src="/static/ht.png" alt=""
  59. v-show="audioType == '1'">
  60. <image style="width: 80px;height: 80px" src="/static/lyz.gif" alt=""
  61. v-show="audioType == '2'">
  62. <image style="margin-top: 25px;width: 44px;height: 30px" src="/static/dh.png" alt=""
  63. v-show="audioType == '3'">
  64. </view>
  65. <p v-show="audioType == '1'">长按录音</p>
  66. <p v-show="audioType == '2'">录音中</p>
  67. <p v-show="audioType == '3'">提交</p>
  68. </view>
  69. <view class="right">
  70. <view class="imgBox">
  71. <image style="width: 24px;height: 33px" @click="reShow = true" src="/static/ht2.png"
  72. v-show="audioType == '3'" alt="">
  73. <p v-show="audioType == '3'">重新录音</p>
  74. </view>
  75. </view>
  76. </view>
  77. </view>
  78. </view>
  79. </view>
  80. <view class="bottomButton" @click="submitData" v-show="type == 'text'">
  81. <view>提交</view>
  82. </view>
  83. <u-modal v-model="reShow" @confirm="audioType = '1'" :show-cancel-button="true" content="请确认是否重录"></u-modal>
  84. <u-toast ref="uToast" />
  85. </view>
  86. </template>
  87. <script>
  88. const recorderManager = uni.getRecorderManager();
  89. // const UPL = require('@/utils/upload')
  90. const NET = require('@/utils/request')
  91. const API = require('@/config/api')
  92. export default {
  93. data() {
  94. return {
  95. isIphone: false,
  96. isWeiXin: false,
  97. audioType: '1',
  98. type: 'text',
  99. mstext: "",
  100. reShow: false,
  101. form: {
  102. local: "",
  103. },
  104. action: API.uploadFile,
  105. fileList: [],
  106. imgList: [],
  107. uploadHeader: {
  108. Authorization: 'Bearer ' + uni.getStorageSync('token')
  109. },
  110. // recorderManager: null,
  111. innerAudioContext: null,
  112. voicePath: '',
  113. soundPath: '',
  114. soundFile: null,
  115. playIng: false,
  116. recordTime: 0,
  117. recordDo: null
  118. }
  119. },
  120. onLoad(options) {
  121. // if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  122. // this.isIphone = true;
  123. // }
  124. uni.getSystemInfo({
  125. success: (res) => {
  126. console.log(res)
  127. if (res.model == 'iPhone') {
  128. this.isIphone = true;
  129. }
  130. },
  131. fail: (err) => {
  132. console.log(err)
  133. }
  134. })
  135. let self = this;
  136. this.innerAudioContext = uni.createInnerAudioContext();
  137. // this.recorderManager = uni.getRecorderManager();
  138. recorderManager.onStop(function(res) {
  139. console.log(res)
  140. self.voicePath = res.tempFilePath;
  141. });
  142. },
  143. onReady() {
  144. if (this.isIphone) {
  145. document.getElementsByClassName('mainCont')[0].style.height = document.getElementsByClassName('mainCont')[
  146. 0].clientHeight - 25 + 'px'
  147. }
  148. },
  149. methods: {
  150. goList() {
  151. uni.navigateTo({
  152. url: "/report/list"
  153. })
  154. },
  155. test() {
  156. uni.navigateTo({
  157. url: "/wasteCheck/list"
  158. })
  159. },
  160. uploadSuccess(data, index) {
  161. console.log(data, index)
  162. if (data.code == 200) {
  163. data.data[0].fileType = 2
  164. this.imgList.push(data.data[0])
  165. this.$refs.uToast.show({
  166. title: '上传成功',
  167. type: 'success',
  168. })
  169. } else {
  170. this.$refs.upload.remove(index)
  171. this.$refs.uToast.show({
  172. title: data.msg,
  173. type: 'warning',
  174. })
  175. }
  176. },
  177. uploadRemove(index) {
  178. console.log(index);
  179. this.imgList.splice(index, 1)
  180. },
  181. toAudio() {
  182. if (this.recordDo) {
  183. clearInterval(this.recordDo)
  184. this.recordDo = false
  185. this.recordTime = 0
  186. }
  187. this.recordDo = setInterval(() => {
  188. this.recordTime = this.recordTime + 1
  189. }, 1000)
  190. if (this.audioType == '1') {
  191. try {
  192. EbeiPlugins.startAudioRecord((res) => {
  193. // alert(res)
  194. // alert('成功')
  195. this.audioType = '2'
  196. }, (err) => {
  197. // alert('失败')
  198. // alert(err)
  199. this.$refs.uToast.show({
  200. title: '调取录音功能失败,请开启对应权限',
  201. type: 'warning',
  202. })
  203. })
  204. } catch {
  205. this.audioType = '2'
  206. recorderManager.start({
  207. format: 'mp3'
  208. });
  209. }
  210. // recorderManager.start();
  211. } else {}
  212. },
  213. audioEnd() {
  214. if (this.recordDo) {
  215. clearInterval(this.recordDo)
  216. this.recordDo = null
  217. console.log(this.recordTime)
  218. if (this.recordTime < 3) {
  219. this.$refs.uToast.show({
  220. title: '录音时间过短,请重新录制',
  221. type: 'warning',
  222. })
  223. this.audioType = '1'
  224. return false
  225. } else {
  226. this.recordTime = 0
  227. }
  228. }
  229. if (this.audioType == '1' || this.audioType == '3') {
  230. return false
  231. }
  232. try {
  233. EbeiPlugins.stopAudioRecord((res) => {
  234. // alert(res)
  235. this.voicePath = res.file
  236. this.audioType = '3'
  237. console.log('成功')
  238. // this.soundPath = res.file
  239. }, (err) => {
  240. console.log('失败')
  241. this.$refs.uToast.show({
  242. title: '录音失败,请打开录音权限再试',
  243. type: 'warning',
  244. })
  245. // this.audioType = '1'
  246. })
  247. // recorderManager.stop()
  248. } catch {
  249. recorderManager.stop()
  250. this.audioType = '3'
  251. // console.log('调取录音失败')
  252. }
  253. },
  254. playAudio() {
  255. if (this.isIphone) {
  256. this.playIng = true
  257. EbeiPlugins.playAudioRecord(this.voicePath, 1, (res) => {
  258. this.playIng = false
  259. }, (err) => {
  260. })
  261. } else {
  262. let innerAudioContext = uni.createInnerAudioContext();
  263. innerAudioContext.src = this.voicePath;
  264. innerAudioContext.play();
  265. this.playIng = true;
  266. innerAudioContext.onPlay(() => {
  267. console.log('开始播放');
  268. });
  269. innerAudioContext.onError((res) => {
  270. console.log('播放失败');
  271. console.log(res);
  272. console.log(res.errMsg);
  273. console.log(res.errCode);
  274. });
  275. innerAudioContext.onEnded(() => {
  276. this.playIng = false;
  277. });
  278. }
  279. },
  280. submitAudio() {
  281. if (this.audioType == '3') {
  282. // alert(this.voicePath)
  283. try {
  284. EbeiPlugins.uploadAudioRecord(this.voicePath, (re) => {
  285. NET.request(API.getFilePath, {
  286. fileId: re.fileId
  287. }).then(res => {
  288. this.soundFile = {
  289. fileId: re.fileId,
  290. filePath: res.msg,
  291. fileType: 1
  292. }
  293. this.$refs.uToast.show({
  294. title: '录音保存成功',
  295. type: 'success',
  296. })
  297. }).catch(error => {
  298. this.$refs.uToast.show({
  299. title: error.msg,
  300. type: 'warning',
  301. })
  302. })
  303. })
  304. } catch {
  305. console.log(uni.getStorageSync('token'))
  306. uni.uploadFile({
  307. url: API.uploadFile,
  308. filePath: this.voicePath,
  309. name: 'file',
  310. header: {
  311. Authorization: 'Bearer ' + uni.getStorageSync('token')
  312. },
  313. success: res => {
  314. console.log(JSON.parse(res.data))
  315. let resData = JSON.parse(res.data).data[0]
  316. this.soundFile = {
  317. fileId: resData.fileId,
  318. filePath: resData.filePath,
  319. fileType: 1
  320. }
  321. this.submitData()
  322. this.$refs.uToast.show({
  323. title: '录音保存成功',
  324. type: 'success',
  325. })
  326. },
  327. fail: err => {
  328. this.$refs.uToast.show({
  329. title: '录音上传失败',
  330. type: 'warning',
  331. })
  332. },
  333. })
  334. }
  335. }
  336. },
  337. submitData() {
  338. // if (!this.form.local) {
  339. // this.$refs.uToast.show({
  340. // title: '请填写位置',
  341. // type: 'warning',
  342. // })
  343. // return false
  344. // } else
  345. if (!this.mstext && this.type == 'text') {
  346. this.$refs.uToast.show({
  347. title: '请填写详情描述',
  348. type: 'warning',
  349. })
  350. return false
  351. } else if (!this.soundFile && this.type == 'audio') {
  352. this.$refs.uToast.show({
  353. title: '未发现录音文件,请录音',
  354. type: 'warning',
  355. })
  356. return false
  357. }
  358. let postData = {
  359. description: this.mstext,
  360. fileList: this.imgList,
  361. location: this.form.local,
  362. sourceType: 1,
  363. troubleType: this.type == 'text' ? 1 : 2,
  364. userId: uni.getStorageSync('userId')
  365. }
  366. if (this.type == 'audio') {
  367. postData.fileList.push(this.soundFile)
  368. }
  369. NET.request(API.htroubleReport, postData, 'POST').then(res => {
  370. this.$refs.uToast.show({
  371. title: res.msg,
  372. type: 'success',
  373. })
  374. uni.navigateTo({
  375. url: "/report/list"
  376. })
  377. }).catch(error => {
  378. this.$refs.uToast.show({
  379. title: error.msg,
  380. type: 'warning',
  381. })
  382. })
  383. }
  384. }
  385. }
  386. </script>
  387. <style lang="scss">
  388. .mainCont {
  389. height: calc(100% - 80rpx);
  390. padding: 0px 0 60px 0;
  391. box-sizing: border-box;
  392. overflow-y: auto;
  393. }
  394. /deep/ .u-cell__value {
  395. color: #1c1c1c;
  396. }
  397. .imgupload {
  398. margin-bottom: 10px;
  399. padding: 10px;
  400. background: #fff;
  401. .imgbox {
  402. img {
  403. width: 80px;
  404. height: 80px;
  405. margin: 10px 10px 10px 0;
  406. }
  407. }
  408. }
  409. .audioBox {
  410. margin-top: 200px;
  411. margin-bottom: 50px;
  412. padding: 0 20px;
  413. display: flex;
  414. justify-content: space-between;
  415. align-items: flex-end;
  416. .left,
  417. .right {
  418. .imgBox {
  419. width: 65px;
  420. height: 71px;
  421. text-align: center;
  422. p {
  423. margin-top: 5px;
  424. text-align: center;
  425. }
  426. }
  427. }
  428. .middle {
  429. position: relative;
  430. .outBox {
  431. position: absolute;
  432. top: -140px;
  433. left: calc(-35vw + 40px);
  434. width: 70vw;
  435. height: 80px;
  436. }
  437. .imgBox {
  438. width: 80px;
  439. height: 80px;
  440. border-radius: 80px;
  441. text-align: center;
  442. background-color: #5976ba;
  443. }
  444. p {
  445. margin-top: 5px;
  446. text-align: center;
  447. }
  448. }
  449. }
  450. </style>