chat.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. <template>
  2. <view class="content">
  3. <view class="content-box" @touchstart="touchstart" id="content-box" :class="{'content-showfn':showFunBtn}">
  4. <!-- 背景图- 定位方式 -->
  5. <image class="content-box-bg" :src="_user_info.chatBgImg" :style="{ height: imgHeight }"></image>
  6. <view class="content-box-loading" v-if="loading"><u-loading mode="flower"></u-loading></view>
  7. <view class="history-font" @tap="linkToHistory()">历史记录</view>
  8. <view class="message" v-for="(item, index) in messageList" :key="index" :id="`msg-${item.hasBeenSentId}`"
  9. :class="index === 0 ? 'marginTop50' : ' '">
  10. <view class="message-item " :class="item.isItMe ? 'right' : 'left'">
  11. <image class="img" :src="item.fromUserHeadImg" mode=""></image>
  12. <!-- contentType = 1 文本 -->
  13. <view class="content" v-if="item.contentType == 1">{{ item.content }}
  14. <button class="pay-btn-class" @tap="goPayPage" v-show="index == 2">前往缴费</button>
  15. </view>
  16. <!-- contentType = 2 语音 -->
  17. <view class="content contentType2" :class="[{ 'content-type-right': item.isItMe }]"
  18. v-if="item.contentType == 2" @tap="handleAudio(item)" hover-class="contentType2-hover-class"
  19. :style="{width:`${200+(item.contentDuration*2)}rpx`}" style=" justify-content: space-between;">
  20. <image src="/static/comment.png" mode="aspectFit" style="width: 30rpx;height:40rpx"
  21. @click.stop="translateVoice(item)"></image>
  22. <view class="">{{ item.contentDuration }}''</view>
  23. <view class="voice_icon" :class="[
  24. { voice_icon_right: item.isItMe },
  25. { voice_icon_left: !item.isItMe },
  26. { voice_icon_right_an: item.anmitionPlay && item.isItMe },
  27. { voice_icon_left_an: item.anmitionPlay && !item.isItMe }
  28. ]"></view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. <!-- 底部聊天输入框 -->
  34. <view class="input-box" :class="{ 'input-box-mpInputMargin': mpInputMargin }">
  35. <view class="input-box-flex">
  36. <!-- #ifndef H5 -->
  37. <!-- <image v-if="chatType === 'voice'" class="icon_img" :src="require('@/static/voice.png')" @click="switchChatType('keyboard')"></image>
  38. <image v-if="chatType === 'keyboard'" class="icon_img" :src="require('@/static/keyboard.png')" @click="switchChatType('voice')"></image> -->
  39. <!-- #endif -->
  40. <view class="input-box-flex-grow">
  41. <!-- <input
  42. v-if="chatType === 'voice'"
  43. type="text"
  44. class="content"
  45. id="input"
  46. v-model="formData.content"
  47. :hold-keyboard="true"
  48. :confirm-type="'send'"
  49. :confirm-hold="true"
  50. placeholder-style="color:#DDDDDD;"
  51. :cursor-spacing="10"
  52. @confirm="sendMsg(null)"
  53. /> -->
  54. <view class="voice_title" @touchstart.stop.prevent="startVoice" @touchmove.stop.prevent="moveVoice"
  55. @touchend.stop="endVoice" @touchcancel.stop="cancelVoice">
  56. {{ voiceTitle }}
  57. </view>
  58. </view>
  59. <!-- 功能性按钮 -->
  60. <!-- <image class=" icon_btn_add" :src="require('@/static/add.png')" @tap="switchFun"></image> -->
  61. <!-- #ifdef H5 -->
  62. <!-- <button class="btn" type="primary" size="mini" @touchend.prevent="sendMsg(null)">发送</button> -->
  63. <!-- #endif -->
  64. </view>
  65. <view class="fun-box" :class="{'show-fun-box':showFunBtn}">
  66. <u-grid :col="4" hover-class="contentType2-hover-class" :border="false" @click="clickGrid">
  67. <u-grid-item v-for="(item, index) in funList" :index="index" :key="index" bg-color="#eaeaea">
  68. <u-icon :name="item.icon" :size="52"></u-icon>
  69. <view class="grid-text">{{ item.title }}</view>
  70. </u-grid-item>
  71. </u-grid>
  72. </view>
  73. </view>
  74. <!-- //语音动画 -->
  75. <view class="voice_an" v-if="recording">
  76. <view class="voice_an_icon">
  77. <view id="one" class="wave"></view>
  78. <view id="two" class="wave"></view>
  79. <view id="three" class="wave"></view>
  80. <view id="four" class="wave"></view>
  81. <view id="five" class="wave"></view>
  82. <view id="six" class="wave"></view>
  83. <view id="seven" class="wave"></view>
  84. </view>
  85. <view class="text">{{voiceIconText}}</view>
  86. </view>
  87. <uni-popup ref="popup" type="center" :mask-click="false">
  88. <view class="uni-tip">
  89. <view class="pop-title">
  90. <text>文本信息</text>
  91. <image src="/static//close-pop.svg" @click="closePop"></image>
  92. </view>
  93. <view class="pop-content">我叫陈斌,今天21岁,毕业于西安电子科技大学</view>
  94. </view>
  95. </uni-popup>
  96. </view>
  97. </template>
  98. <script>
  99. import {
  100. mapState
  101. } from "vuex";
  102. export default {
  103. data() {
  104. return {
  105. fromUserInfo: {},
  106. formData: {
  107. content: '',
  108. limit: 15,
  109. index: 1
  110. },
  111. messageList: [],
  112. loading: true, //标识是否正在获取数据
  113. imgHeight: '1000px',
  114. mpInputMargin: false, //适配微信小程序 底部输入框高度被顶起的问题
  115. chatType: "voice", // 图标类型 'voice'语音 'keyboard'键盘
  116. voiceTitle: '点击开始面试',
  117. Recorder: uni.getRecorderManager(),
  118. Audio: uni.createInnerAudioContext(),
  119. recording: false, //标识是否正在录音
  120. isStopVoice: false, //加锁 防止点击过快引起的当录音正在准备(还没有开始录音)的时候,却调用了stop方法但并不能阻止录音的问题
  121. voiceInterval: null,
  122. interviewid: "",
  123. voiceTime: 0, //总共录音时长
  124. canSend: true, //是否可以发送
  125. PointY: 0, //坐标位置
  126. voiceIconText: "正在录音...",
  127. showFunBtn: false, //是否展示功能型按钮
  128. AudioExam: null, //正在播放音频的实例
  129. funList: [{
  130. icon: "photo-fill",
  131. title: "照片",
  132. uploadType: ["album"]
  133. },
  134. {
  135. icon: "camera-fill",
  136. title: "拍摄",
  137. uploadType: ["camera"]
  138. },
  139. ],
  140. };
  141. },
  142. computed: {
  143. ...mapState(["_token", "_user_info"])
  144. },
  145. methods: {
  146. //拼接消息 处理滚动
  147. async joinData() {
  148. if (!this.loading) {
  149. //如果没有获取数据 即loading为false时,return 避免用户重复上拉触发加载
  150. return;
  151. }
  152. this.loading = false;
  153. const data = await this.getMessageData();
  154. //获取节点信息
  155. const {
  156. index
  157. } = this.formData;
  158. const sel =
  159. `#msg-${index > 1 ? this.messageList[0].hasBeenSentId : data[data.length - 1].hasBeenSentId}`;
  160. this.messageList = [...data, ...this.messageList];
  161. //填充数据后,视图会自动滚动到最上面一层然后瞬间再跳回bindScroll的指定位置 ---体验不是很好,后期优化
  162. // this.$nextTick(() => {
  163. // this.bindScroll(sel);
  164. // //如果还有数据
  165. // if (this.formData.limit >= data.length) {
  166. // this.formData.index++;
  167. // setTimeout(() => {
  168. // this.loading = true;
  169. // }, 200);
  170. // }
  171. // });
  172. },
  173. //处理滚动
  174. // bindScroll(sel, duration = 0) {
  175. // const query = uni.createSelectorQuery().in(this);
  176. // query
  177. // .select(sel)
  178. // .boundingClientRect(data => {
  179. // uni.pageScrollTo({
  180. // scrollTop: data && data.top - 40,
  181. // duration
  182. // });
  183. // })
  184. // .exec();
  185. // },
  186. //获取消息
  187. getMessageData() {
  188. let arr = [];
  189. if (!this.$data.interviewid) {
  190. arr.push({
  191. hasBeenSentId: 0, //已发送过去消息的id
  192. content: `欢迎使用chart智能面试系统`,
  193. fromUserHeadImg: this.fromUserInfo.fromUserHeadImg, //用户头像
  194. fromUserId: "",
  195. isItMe: false, //true此条信息是我发送的 false别人发送的
  196. createTime: Date.now(),
  197. contentType: 1, // 1文字文本 2语音
  198. anmitionPlay: false //标识音频是否在播放
  199. })
  200. arr.push({
  201. hasBeenSentId: 0, //已发送过去消息的id
  202. content: `如需面试请点击下方按钮`,
  203. fromUserHeadImg: this.fromUserInfo.fromUserHeadImg, //用户头像
  204. fromUserId: "",
  205. isItMe: false, //true此条信息是我发送的 false别人发送的
  206. createTime: Date.now(),
  207. contentType: 1, // 1文字文本 2语音
  208. anmitionPlay: false //标识音频是否在播放
  209. })
  210. arr.push({
  211. hasBeenSentId: 0, //已发送过去消息的id
  212. content: `请提交岗位信息并进行缴费`,
  213. fromUserHeadImg: this.fromUserInfo.fromUserHeadImg, //用户头像
  214. fromUserId: "",
  215. isItMe: false, //true此条信息是我发送的 false别人发送的
  216. createTime: Date.now(),
  217. contentType: 1, // 1文字文本 2语音
  218. anmitionPlay: false //标识音频是否在播放
  219. })
  220. } else {
  221. arr.push({
  222. hasBeenSentId: 0, //已发送过去消息的id
  223. content: `下面我们将开始正式面试,所有问题您直接回复语音即可,因为每条语音最长时长为1分钟,如需回复多条语音,间隔时间不能超过15秒,请问准备好了吗?`,
  224. fromUserHeadImg: this.fromUserInfo.fromUserHeadImg, //用户头像
  225. fromUserId: "",
  226. isItMe: false, //true此条信息是我发送的 false别人发送的
  227. createTime: Date.now(),
  228. contentType: 1, // 1文字文本 2语音
  229. anmitionPlay: false //标识音频是否在播放
  230. })
  231. }
  232. return arr
  233. // let getData = () => {
  234. // let arr = [];
  235. // let startIndex = (this.formData.index - 1) * this.formData.limit;
  236. // let endIndex = startIndex + this.formData.limit;
  237. // for (let i = startIndex; i < endIndex; i++) {
  238. // const isItMe = Math.random() > 0.5 ? true : false;
  239. // arr.unshift({
  240. // hasBeenSentId: i, //已发送过去消息的id
  241. // content: `很高兴认识你,这是第${i + 1}条消息。`,
  242. // fromUserHeadImg: isItMe ? this._user_info.headImg : this.fromUserInfo
  243. // .fromUserHeadImg, //用户头像
  244. // fromUserId: isItMe ? this._user_info.id : this.fromUserInfo.fromUserId,
  245. // isItMe, //true此条信息是我发送的 false别人发送的
  246. // createTime: Date.now(),
  247. // contentType: 1, // 1文字文本 2语音
  248. // anmitionPlay: false //标识音频是否在播放
  249. // });
  250. // }
  251. // return arr;
  252. // };
  253. // return new Promise((resolve, reject) => {
  254. // const data = getData();
  255. // setTimeout(() => {
  256. // resolve(data);
  257. // }, 500);
  258. // });
  259. },
  260. //发送消息
  261. sendMsg(data) {
  262. let that = this;
  263. if (!that.$data.interviewid) {
  264. uni.showToast({
  265. title: "请进入付款操作,填写相应信息,进行面试!",
  266. icon: "none"
  267. });
  268. return
  269. }
  270. const params = {
  271. hasBeenSentId: Date.now(), //已发送过去消息的id
  272. content: this.formData.content,
  273. fromUserHeadImg: this._user_info.headImg, //用户头像
  274. fromUserId: this._user_info.id,
  275. isItMe: true, //true此条信息是我发送的 false别人发送的
  276. createTime: Date.now(),
  277. contentType: 1
  278. };
  279. if (data) {
  280. if (data.contentType == 2) {
  281. //说明是发送语音
  282. params.content = data.content;
  283. params.contentType = data.contentType;
  284. params.contentDuration = data.contentDuration;
  285. params.anmitionPlay = false;
  286. } else if (data.contentType == 3) {
  287. //发送图片
  288. params.content = data.content;
  289. params.contentType = data.contentType;
  290. }
  291. uni.uploadFile({
  292. url: getApp().globalData.services.chatInterviewUrl, //需要设置为全局
  293. filePath: params.content,
  294. name:"upfile",
  295. formData: {
  296. accesstoken: that.$store.state._token,
  297. jwtcode: that.$store.state._jwtcode,
  298. interviewid: that.$data.interviewid,
  299. },
  300. success: ({
  301. data: {
  302. errcode,
  303. errmsg
  304. }
  305. }) => {
  306. if (errcode === "0") {
  307. that.$data.messageList.push({
  308. hasBeenSentId: 0, //已发送过去消息的id
  309. content: errmsg,
  310. fromUserHeadImg: this.fromUserInfo.fromUserHeadImg, //用户头像
  311. fromUserId: "",
  312. isItMe: false, //true此条信息是我发送的 false别人发送的
  313. createTime: Date.now(),
  314. contentType: 1, // 1文字文本 2语音
  315. anmitionPlay: false //标识音频是否在播放
  316. })
  317. } else {
  318. uni.showToast({
  319. title: errmsg,
  320. icon: "none"
  321. });
  322. }
  323. },
  324. fail(e) {
  325. debugger;
  326. }
  327. });
  328. } else if (!this.$u.trim(this.formData.content)) {
  329. //验证输入框书否为空字符传
  330. return;
  331. }
  332. this.messageList.push(params);
  333. this.$nextTick(() => {
  334. this.formData.content = '';
  335. // #ifdef MP-WEIXIN
  336. if (params.contentType == 1) {
  337. uni.pageScrollTo({
  338. scrollTop: 99999,
  339. duration: 0, //小程序如果有滚动效果 input的焦点也会随着页面滚动...
  340. });
  341. } else {
  342. setTimeout(() => {
  343. uni.pageScrollTo({
  344. scrollTop: 99999,
  345. duration: 0, //小程序如果有滚动效果 input的焦点也会随着页面滚动...
  346. });
  347. }, 150)
  348. }
  349. // #endif
  350. // #ifndef MP-WEIXIN
  351. uni.pageScrollTo({
  352. scrollTop: 99999,
  353. duration: 100
  354. });
  355. // #endif
  356. if (this.showFunBtn) {
  357. this.showFunBtn = false;
  358. }
  359. // #ifdef MP-WEIXIN
  360. if (params.contentType == 1) {
  361. this.mpInputMargin = true;
  362. }
  363. // #endif
  364. //h5浏览器并没有很好的办法控制键盘一直处于唤起状态 而且会有样式性的问题
  365. // #ifdef H5
  366. uni.hideKeyboard();
  367. // #endif
  368. });
  369. },
  370. //用户触摸屏幕的时候隐藏键盘
  371. touchstart() {
  372. uni.hideKeyboard();
  373. },
  374. // userid 用户id
  375. linkToBusinessCard(userId) {
  376. this.$u.route({
  377. url: 'pages/businessCard/businessCard',
  378. params: {
  379. userId
  380. }
  381. });
  382. },
  383. linkToHistory() {
  384. uni.navigateTo({
  385. url: '../history/index'
  386. })
  387. },
  388. goPayPage() {
  389. uni.navigateTo({
  390. url: '../payPage/index'
  391. })
  392. },
  393. translateVoice(params) {
  394. this.$refs.popup.open()
  395. },
  396. closePop() {
  397. this.$refs.popup.close()
  398. },
  399. //准备开始录音
  400. startVoice(e) {
  401. if (!this.Audio.paused) {
  402. //如果音频正在播放 先暂停。
  403. this.stopAudio(this.AudioExam)
  404. }
  405. this.recording = true;
  406. this.isStopVoice = false;
  407. this.canSend = true;
  408. this.voiceIconText = "正在录音..."
  409. this.PointY = e.touches[0].clientY;
  410. this.Recorder.start({
  411. format: 'mp3'
  412. });
  413. },
  414. //录音已经开始
  415. beginVoice() {
  416. if (this.isStopVoice) {
  417. this.Recorder.stop();
  418. return;
  419. }
  420. this.voiceTitle = '松开 结束'
  421. this.voiceInterval = setInterval(() => {
  422. this.voiceTime++;
  423. }, 1000)
  424. },
  425. //move 正在录音中
  426. moveVoice(e) {
  427. const PointY = e.touches[0].clientY
  428. const slideY = this.PointY - PointY;
  429. if (slideY > uni.upx2px(120)) {
  430. this.canSend = false;
  431. this.voiceIconText = '松开手指 取消发送 '
  432. } else if (slideY > uni.upx2px(60)) {
  433. this.canSend = true;
  434. this.voiceIconText = '手指上滑 取消发送 '
  435. } else {
  436. this.voiceIconText = '正在录音... '
  437. }
  438. },
  439. //结束录音
  440. endVoice() {
  441. this.isStopVoice = true; //加锁 确保已经结束录音并不会录制
  442. this.Recorder.stop();
  443. this.voiceTitle = '点击开始面试'
  444. },
  445. //录音被打断
  446. cancelVoice(e) {
  447. this.voiceTime = 0;
  448. this.voiceTitle = '点击开始面试';
  449. this.canSend = false;
  450. this.Recorder.stop();
  451. },
  452. //处理录音文件
  453. handleRecorder({
  454. tempFilePath,
  455. duration
  456. }) {
  457. let contentDuration;
  458. // #ifdef MP-WEIXIN
  459. this.voiceTime = 0;
  460. if (duration < 600) {
  461. this.voiceIconText = "说话时间过短";
  462. setTimeout(() => {
  463. this.recording = false;
  464. }, 200)
  465. return;
  466. }
  467. contentDuration = duration / 1000;
  468. // #endif
  469. // #ifdef APP-PLUS
  470. contentDuration = this.voiceTime + 1;
  471. this.voiceTime = 0;
  472. if (contentDuration <= 0) {
  473. this.voiceIconText = "说话时间过短";
  474. setTimeout(() => {
  475. this.recording = false;
  476. }, 200)
  477. return;
  478. };
  479. // #endif
  480. this.recording = false;
  481. const params = {
  482. contentType: 2,
  483. content: tempFilePath,
  484. contentDuration: Math.ceil(contentDuration)
  485. };
  486. this.canSend && this.sendMsg(params);
  487. },
  488. //控制播放还是暂停音频文件
  489. handleAudio(item) {
  490. this.AudioExam = item;
  491. this.Audio.paused ? this.playAudio(item) : this.stopAudio(item);
  492. },
  493. //播放音频
  494. playAudio(item) {
  495. this.Audio.src = item.content;
  496. this.Audio.hasBeenSentId = item.hasBeenSentId;
  497. this.Audio.play();
  498. item.anmitionPlay = true;
  499. },
  500. //停止音频
  501. stopAudio(item) {
  502. item.anmitionPlay = false;
  503. this.Audio.src = '';
  504. this.Audio.stop();
  505. },
  506. //关闭动画
  507. closeAnmition() {
  508. const hasBeenSentId = this.Audio.hasBeenSentId;
  509. const item = this.messageList.find(it => it.hasBeenSentId == hasBeenSentId);
  510. item.anmitionPlay = false;
  511. },
  512. //点击宫格时触发
  513. clickGrid(index) {
  514. if (index == 0) {
  515. this.chooseImage(['album'])
  516. } else if (index == 1) {
  517. this.chooseImage(['camera'])
  518. }
  519. },
  520. //发送图片
  521. chooseImage(sourceType) {
  522. uni.chooseImage({
  523. sourceType,
  524. sizeType: ['compressed'],
  525. success: res => {
  526. this.showFunBtn = false;
  527. for (let i = 0; i < res.tempFilePaths.length; i++) {
  528. const params = {
  529. contentType: 3,
  530. content: res.tempFilePaths[i],
  531. };
  532. this.sendMsg(params)
  533. }
  534. }
  535. })
  536. },
  537. //查看大图
  538. viewImg(imgList) {
  539. uni.previewImage({
  540. urls: imgList,
  541. // #ifndef MP-WEIXIN
  542. indicator: 'number'
  543. // #endif
  544. });
  545. },
  546. login(accesstoken, code) {
  547. let that = this;
  548. uni.request({
  549. url: getApp().globalData.services.loginUrl, //需要设置为全局
  550. method: 'POST',
  551. header: {
  552. 'content-Type': 'application/x-www-form-urlencoded'
  553. },
  554. data: {
  555. accesstoken,
  556. code
  557. },
  558. success: ({
  559. errMsg,
  560. data: {
  561. jwtcode
  562. }
  563. }) => {
  564. if (errMsg === "request:ok") {
  565. that.$store.commit("setJwtcode", jwtcode)
  566. }
  567. }
  568. });
  569. },
  570. },
  571. onPageScroll(e) {
  572. if (e.scrollTop < 50) {
  573. this.joinData();
  574. }
  575. },
  576. onNavigationBarButtonTap({
  577. index
  578. }) {
  579. if (index == 0) {
  580. //用户详情 设置
  581. } else if (index == 1) {
  582. //返回按钮
  583. this.$u.route({
  584. type: 'switchTab',
  585. url: 'pages/home/home'
  586. });
  587. }
  588. },
  589. //返回按钮事件
  590. onBackPress(e) {
  591. //以下内容对h5不生效
  592. //--所以如果用浏览器自带的返回按钮进行返回的时候页面不会重定向 正在寻找合适的解决方案
  593. this.$u.route({
  594. type: 'switchTab',
  595. url: 'pages/home/home'
  596. });
  597. return true;
  598. },
  599. onLoad(info) {
  600. // { messageId,fromUserName,fromUserHeadImg } = info
  601. // const userInfo = this.firendList.filter(item => item.userId == info.fromUserId)[0];
  602. // console.log('userInfo')
  603. this.fromUserInfo = {
  604. fromUserName: 'ai-test',
  605. fromUserHeadImg: require('@/static/ai-bg.png'),
  606. fromUserId: 1,
  607. messageId: 666
  608. };
  609. //录音开始事件
  610. this.Recorder.onStart(e => {
  611. this.beginVoice();
  612. });
  613. //录音结束事件
  614. this.Recorder.onStop(res => {
  615. clearInterval(this.voiceInterval);
  616. this.handleRecorder(res);
  617. });
  618. //音频停止事件
  619. this.Audio.onStop(e => {
  620. this.closeAnmition();
  621. });
  622. //音频播放结束事件
  623. this.Audio.onEnded(e => {
  624. this.closeAnmition();
  625. });
  626. let pages = getCurrentPages(); //获取当前页面栈
  627. let currentPage = pages[pages.length - 1]; //获取当前页面对象
  628. let interviewid = currentPage.options.interviewid;
  629. this.$data.interviewid = interviewid || "";
  630. },
  631. onReady() {
  632. //自定义返回按钮 因为原生的返回按钮不可阻止默认事件
  633. // #ifdef H5
  634. const icon = document.getElementsByClassName('uni-page-head-btn')[0];
  635. icon.style.display = 'none';
  636. // #endif
  637. uni.setNavigationBarTitle({
  638. title: this.fromUserInfo.fromUserName
  639. });
  640. this.joinData();
  641. uni.getSystemInfo({
  642. success: res => {
  643. this.imgHeight = res.windowHeight + 'px';
  644. }
  645. });
  646. uni.onKeyboardHeightChange(res => {
  647. if (res.height == 0) {
  648. // #ifdef MP-WEIXIN
  649. this.mpInputMargin = false;
  650. // #endif
  651. } else {
  652. this.showFunBtn = false;
  653. }
  654. });
  655. let that = this;
  656. let {
  657. _token
  658. } = this.$data;
  659. let {
  660. services,
  661. appid,
  662. appsecret: secret
  663. } = getApp().globalData;
  664. //获取access tokens
  665. if (!_token) {
  666. uni.request({
  667. url: services.accessTokenUrl, //需要设置为全局
  668. method: 'POST',
  669. header: {
  670. 'content-Type': 'application/x-www-form-urlencoded'
  671. },
  672. data: {
  673. appid,
  674. secret
  675. },
  676. success: ({
  677. errMsg,
  678. data: {
  679. accesstoken
  680. }
  681. }) => {
  682. if (errMsg === "request:ok") {
  683. that.$store.commit("setToken", accesstoken)
  684. //获取code
  685. uni.login({
  686. provider: 'weixin', //使用微信登录
  687. success: function({
  688. code,
  689. errMsg
  690. }) {
  691. if (errMsg === "login:ok") {
  692. that.login(accesstoken, code)
  693. }
  694. }
  695. })
  696. }
  697. }
  698. });
  699. }
  700. //获取用户信息
  701. if (!this._user_info) {
  702. uni.showModal({
  703. title: '授权',
  704. content: "需要您的授权",
  705. success: () => {
  706. uni.getUserProfile({
  707. desc: "获取您的昵称、头像、地区及性别",
  708. success: (res) => {
  709. if (res.errMsg === "getUserProfile:ok") {
  710. let data = {
  711. headImg: res.userInfo.avatarUrl,
  712. id: res.iv, //id
  713. userName: res.userInfo.nickName, //昵称
  714. wechatNumber: res.userInfo.nickName, //微信号
  715. signature: res.signature, //个性签名
  716. phone: '', //手机号
  717. pictureBanner: require(
  718. '@/static/image/address.png'), //相册背景图
  719. chatBgImg: require('@/static/ai-bg.png'), //聊天背景图
  720. address: res.userInfo.city
  721. }
  722. that.$store.commit("setUserInfo", data)
  723. }
  724. },
  725. fail(e) {}
  726. })
  727. }
  728. })
  729. }
  730. },
  731. };
  732. </script>
  733. <style lang="scss" scoped>
  734. @import './index.scss'
  735. </style>