index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <template>
  2. <view class="content">
  3. <navigation-custom :config="config" :scrollTop="scrollTopNav" @customConduct="customConduct" :scrollMaxHeight="scrollMaxHeight" />
  4. <scroll-view id="scrollview" class='chat-box' :style="{height: style.contentViewHeight + 'px'}" scroll-y="true"
  5. :scroll-with-animation="false" :scroll-top="scrollTop" @scrolltoupper="lower()">
  6. <view class="tips-box">
  7. {{nowTime}}
  8. </view>
  9. <view class="tips-box tips-title">
  10. 您好,智能投顾很高兴为您服务!
  11. </view>
  12. <view class="talk-box">
  13. <view class="talk-head-box">
  14. <image src="../../static/rabotHead.png" mode="" class="talk-head"></image>
  15. </view>
  16. <view class="talk-content">您好,智能顾投很高兴为您服务!</view>
  17. </view>
  18. <view v-for="chat in talkList" :key="chat.talkType" class="m-item">
  19. <view class="talk-box talk-box-end" v-show="chat.content">
  20. <view class="talk-content">{{chat.content}}</view>
  21. <view class="talk-head-box">
  22. <image :src='userHeadUrl' mode="" class="talk-head"></image>
  23. </view>
  24. </view>
  25. <view class="talk-box">
  26. <view class="talk-head-box">
  27. <image :src='rabotHeadImg' mode="" class="talk-head"></image>
  28. </view>
  29. <view class="talk-content">{{chat.reply}}</view>
  30. </view>
  31. </view>
  32. </scroll-view>
  33. <view class="send-box" :style="{ 'padding-bottom' : isShowKeyBoard + 'px'}">
  34. <input type="text" class="enter-box" v-model="inputInfo" adjust-position='{{true}}' >
  35. <button type="primary" class="submit-message" @click="sendInputInfo()">发送</button>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import navigationCustom from "../../components/struggler-navigationCustom/navigation-custom";
  41. var md5 = require("../../common/md5.js");
  42. var page = 1;
  43. export default {
  44. data() {
  45. return {
  46. title: 'RABOT',
  47. nowTime: '',
  48. nowHeight: '622px',
  49. inputInfo: '',
  50. isShowKeyBoard:0,
  51. rabotHeadImg: "../../static/rabotHead.png",
  52. userHeadUrl: getApp().globalData.user_headUrl || '../../static/userDefault.png',
  53. talkList: [],
  54. config: {
  55. title: "阿拉丁神灯", //title
  56. bgcolor: "#fff", //背景颜色
  57. fontcolor: "black", //文字颜色,默认白色
  58. type: 3, //type 1,3胶囊 2,4无胶囊模式
  59. transparent: false, //是否背景透明 默认白色
  60. linear: true, //是为开启下滑渐变
  61. share: true, //是否将主页按钮显示为分享按钮
  62. menuIcon: "../../static/icon/iconfont-user.png", //当type为3或者4的时候左边的icon文件位置,注意位置与当前页面不一样
  63. // menuText:"返回", 当type为3或4的时候icon右边的文字
  64. },
  65. scrollTopNav: 0, // 当linear为true的时候需要通过onpagescroll传递参数
  66. scrollMaxHeight: 200 ,//滑动的高度限制,超过这个高度即背景全部显示
  67. scrollTop:0,
  68. scrollRequestEnable: true,
  69. pageSize:10,
  70. style: {
  71. pageHeight: 0,
  72. contentViewHeight: 0,
  73. footViewHeight: 90,
  74. mitemHeight: 0
  75. },
  76. }
  77. },
  78. components: {
  79. navigationCustom
  80. },
  81. onPageScroll(e) {
  82. this.scrollTopNav = e.scrollTop;
  83. },
  84. onLoad() {
  85. this.userLogin();
  86. this.getEquipmentHeight();
  87. },
  88. onShow() {
  89. this.getNowTime();
  90. },
  91. methods: {
  92. userLogin(){
  93. uni.login({
  94. success: (res) => {
  95. this.getCodeRabot(res)
  96. }
  97. });
  98. },
  99. getCodeRabot(res){
  100. let that = this;
  101. uni.request({
  102. url:getApp().globalData.shareUrl, //需要设置为全局
  103. method: 'POST',
  104. header: {
  105. 'content-type': 'application/x-www-form-urlencoded'
  106. },
  107. data: {
  108. method: 'login',
  109. timestamp: getApp().globalData.globalTimestamp, //Date.now()
  110. code: res.code,
  111. sign: md5('login' + getApp().globalData.globalTimestamp),
  112. // invited:'invited'
  113. },
  114. success: res => {
  115. // 通过openid发起会员登录
  116. getApp().globalData.user_id = res.data.msg.id;
  117. getApp().globalData.open_id = res.data.msg.openid;
  118. getApp().globalData.isAuth = res.data.msg.isauth === '0';
  119. getApp().globalData.user_name = res.data.msg.name;
  120. getApp().globalData.user_headUrl = res.data.msg.headimg;
  121. getApp().globalData.times = res.data.msg.times;
  122. getApp().globalData.total_times = res.data.msg.total_times;
  123. that.getHistory();
  124. }
  125. });
  126. },
  127. getNowTime() {
  128. let time = new Date();
  129. let y = time.getFullYear();
  130. let m = time.getMonth() + 1;
  131. let d = time.getDate();
  132. let h = time.getHours();
  133. let i = time.getMinutes();
  134. i = i > 10 ? i : '0' + i;
  135. this.nowTime = y + "年" + m + "月" + d + "日" + " " + h + ":" + i
  136. },
  137. getEquipmentHeight() {
  138. const height = uni.getSystemInfoSync().windowHeight;
  139. this.nowHeight = height + 'px';
  140. getApp().globalData.glbalHeight = this.nowHeight;
  141. this.style.pageHeight = height;
  142. this.style.contentViewHeight = height - uni.getSystemInfoSync().screenWidth / 750 * (100) - 70; //像素 因为给出的是像素高度 然后我们用的是upx 所以换算一下
  143. },
  144. scrollToBottom() {
  145. let that = this;
  146. let query = uni.createSelectorQuery();
  147. query.selectAll('.m-item').boundingClientRect();
  148. query.select('#scrollview').boundingClientRect();
  149. query.exec((res) => {
  150. that.style.mitemHeight = 0;
  151. res[0].forEach((rect) => that.style.mitemHeight = that.style.mitemHeight + rect.height + 40) //获取所有内部子元素的高度
  152. // 因为vue的虚拟DOM 每次生成的新消息都是之前的,所以采用异步setTimeout 主要就是添加了这红字
  153. that.scrollTop = that.style.mitemHeight - that.style.contentViewHeight
  154. // setTimeout(() => {
  155. // if (that.style.mitemHeight > (that.style.contentViewHeight - 100)) { //判断子元素高度是否大于显示高度
  156. // that.scrollTop = that.style.mitemHeight - that.style.contentViewHeight //用子元素的高度减去显示的高度就获益获得序言滚动的高度
  157. // }
  158. // }, 100)
  159. })
  160. },
  161. getHistory() {
  162. let that = this;
  163. uni.request({
  164. url: getApp().globalData.shareUrl, //需要设置为全局
  165. method: 'POST',
  166. header: {
  167. 'content-type': 'application/x-www-form-urlencoded'
  168. },
  169. data: {
  170. method: 'question_list',
  171. timestamp: getApp().globalData.globalTimestamp, //Date.now()
  172. uid: getApp().globalData.user_id,
  173. sign: md5('question_list' + getApp().globalData.globalTimestamp),
  174. page: page || 1,
  175. pageSize: 10,
  176. },
  177. success: res => {
  178. that.userHeadUrl = getApp().globalData.user_headUrl;
  179. if(page === 1){
  180. that.scrollRequestEnable = true;
  181. that.talkList = ((res.data.msg).reverse());
  182. console.log(1,that.talkList)
  183. setTimeout(()=>{
  184. that.scrollToBottom()
  185. },100)
  186. }else if (page > 1 && res.data.msg.length === that.pageSize) {
  187. that.scrollRequestEnable = true;
  188. that.talkList.unshift(...((res.data.msg).reverse()));
  189. }else if (page > 1 && (res.data.msg.length > 0) && (res.data.msg.length < that.pageSize)){
  190. that.scrollRequestEnable = true;
  191. that.talkList.unshift(...((res.data.msg).reverse()));
  192. }else if (page > 1 && res.data.msg.length == 0){
  193. that.scrollRequestEnable = false;
  194. }
  195. }
  196. });
  197. },
  198. customConduct() {
  199. uni.navigateTo({
  200. url: '../selfInfo/selfInfo'
  201. })
  202. },
  203. sendMsgRequest(msg) {
  204. let that = this;
  205. uni.request({
  206. url: getApp().globalData.shareUrl, //需要设置为全局
  207. method: 'POST',
  208. header: {
  209. 'content-type': 'application/x-www-form-urlencoded'
  210. },
  211. data: {
  212. method: 'question',
  213. timestamp: getApp().globalData.globalTimestamp, //Date.now()
  214. uid: getApp().globalData.user_id,
  215. sign: md5('question' + getApp().globalData.globalTimestamp),
  216. content: msg
  217. },
  218. success: res => {
  219. this.inputInfo = '';
  220. that.getHistory();
  221. }
  222. });
  223. },
  224. sendInputInfo() {
  225. if (!this.inputInfo) {
  226. uni.showToast({
  227. title: '请输入股票代码',
  228. icon: 'none'
  229. });
  230. } else {
  231. page = 1;
  232. this.sendMsgRequest(this.inputInfo)
  233. }
  234. },
  235. // 滚动至底部触发的事件
  236. lower() {
  237. if (this.scrollRequestEnable) {
  238. // 允许滚动时,触底page加一,重新请求数据
  239. page++;
  240. this.getHistory();
  241. } else {
  242. // 不允许滚动时,只显示"到底了"提示
  243. // this.showBottem = true
  244. console.log('noData')
  245. }
  246. },
  247. // getInputFocus(e){
  248. // this.isShowKeyBoard = e.detail.height
  249. // },
  250. // getInputBlur(){
  251. // this.isShowKeyBoard = 0
  252. // },
  253. },
  254. }
  255. </script>
  256. <style>
  257. .content {
  258. text-align: center;
  259. }
  260. .chat-box {
  261. background: #f4f5f7;
  262. padding-top: 3%;
  263. padding-bottom: 100upx;
  264. }
  265. .tips-box {
  266. background: #D5D5DA;
  267. font-size: 28upx;
  268. color: #fff;
  269. border-radius: 30upx;
  270. width: 50%;
  271. margin: 0 auto;
  272. height: 50rpx;
  273. line-height: 50rpx
  274. }
  275. .tips-title {
  276. width: 70%;
  277. margin-top: 5%;
  278. font-size: 30rpx;
  279. height: 70rpx;
  280. line-height: 70rpx
  281. }
  282. .send-box {
  283. width: 100%;
  284. height: 90rpx;
  285. position: fixed;
  286. bottom: 0;
  287. background: #F4F0ED;
  288. display: flex;
  289. align-items: center;
  290. }
  291. .talk-box {
  292. display: flex;
  293. justify-content: flex-start;
  294. align-items: center;
  295. margin-top: 3%;
  296. }
  297. .talk-box-end {
  298. justify-content: flex-end;
  299. }
  300. .talk-head-box {
  301. width: 80rpx;
  302. height: 80rpx;
  303. border-radius: 30rpx;
  304. margin-left: 2%;
  305. margin-right: 2%;
  306. }
  307. .talk-head {
  308. width: 100%;
  309. height: 100%;
  310. border-radius: 50%;
  311. }
  312. .talk-content {
  313. padding: 2%;
  314. background: #fff;
  315. /* height:50rpx;
  316. line-height: 50rpx; */
  317. font-size: 28rpx;
  318. border-top-right-radius: 20rpx;
  319. border-bottom-left-radius: 20rpx;
  320. border-bottom-right-radius: 20rpx;
  321. text-align: left;
  322. max-width: 70%;
  323. }
  324. .enter-box {
  325. width: 78%;
  326. height: 70rpx;
  327. margin-left: 5px;
  328. background: #fff;
  329. border-radius: 1%;
  330. text-align: left;
  331. }
  332. .submit-message {
  333. width: 18%;
  334. height: 70rpx;
  335. font-size: 28rpx;
  336. line-height: 70rpx;
  337. color: #fff;
  338. background-color: #0A98D5 !important;
  339. }
  340. .logo {
  341. height: 200upx;
  342. width: 200upx;
  343. margin-top: 200upx;
  344. margin-left: auto;
  345. margin-right: auto;
  346. margin-bottom: 50upx;
  347. }
  348. .text-area {
  349. display: flex;
  350. justify-content: center;
  351. }
  352. .title {
  353. font-size: 36upx;
  354. color: #8f8f94;
  355. }
  356. </style>