vue.js客服系统实时聊天项目开发(八)使用axios post请求访客初始化接口

访客的初始化,很多人可能会认为放到链接websocket的时候,通过ws去发送给服务端

但是这样会有一定的问题,因为如果网络不稳定或者ws链接断了,会进行不停的重连,这样会造次多次请求初始化流程

 

所以我会在链接websocket之前,调用一个HTTP的访客初始化接口,这样也方便进行限流,各种账户异常问题判断等逻辑

 在main.js中通过原型把axios 传递进来

import axios from 'axios'

Vue.prototype.$axios= axios

在业务逻辑里面进行调用

复制代码
<script>

    export default {
      name: 'ChatApp',
        data() {
          return {
              visitor:{
                  to_id:"",
                  visitor_id:"",
              },

          }
        },
        methods: {
            visitorLogin(){
                let entId=tools.getQuery("ent_id");
                let toId=tools.getQuery("to_id");
                var _this=this;
                this.$axios.post(this.ApiHost+'/visitor_login', {
                    ent_id:entId,
                    to_id:toId,
                }).then(function (response) {
                    var code=response.data.code;
                    var msg=response.data.msg;
                    if(code!=200){
                        _this.$message({
                          message: msg,
                          type: 'error'
                        });
                        return ;
                    }
                    let result=response.data.result;
                    _this.visitor.to_id=result.to_id;
                    _this.visitor.visitor_id=result.visitor_id;

                }).catch(function (error) {
                    _this.$message({
                      message: error.message,
                      type: 'error'
                    });
                });
            },

        },
        mounted: function () {
            this.visitorLogin();
        }
    }
</script>    
复制代码

这样就完成了发送post请求到访客初始化接口,会生成一个访客ID,前端需要把这个访客ID进行保存一下

唯一在线客服系统

https://gofly.v1kf.com

posted @   唯一客服系统开发笔记  阅读(92)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-01-20 [Golang] 解决 Goland配置GOROOT The selected directory is not a valid home for Go Sdk
2020-01-20 [Python] 关于__init__.py
2018-01-20 [PHP] PHP数组的实现哈希表(HashTable)结构
2018-01-20 [PHP] PHP的脚本执行
点击右上角即可分享
微信分享提示
1
chat with us