index.vue 10 KB

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