Exemple #1
0
        /// <summary>
        /// 侦测超时
        /// </summary>
        /// <param name="socket"></param>
        /// <returns></returns>
        public async Task ScanningTimeoutDetect(PayQrWebSocket socket, CancellationToken cancellationToken)
        {
            if ((DateTime.Now - socket.StartScanTime).TotalSeconds > this.TimeOutSeconds)
            {
                socket.PayQrStatus = PayQrStatuses.TIMEOUT;
                await NotifyToClient(socket, new { status = PayQrStatuses.TIMEOUT.ToString(), msg = "操作超时,请重新启动付款" }, cancellationToken);

                await _wsconnMgr.RemoveSocket(socket);
            }
        }
        //获取传入的基础信息
        private void _SetStationInfo(PayQrWebSocket socket, HttpContext context)
        {
            var qs         = context.Request.QueryString;
            var stationId  = Convert.ToInt32(context.Request.Query["stationId"]);
            var doctorId   = Convert.ToInt32(context.Request.Query["doctorId"]);
            var stationId2 = Convert.ToInt32(context.User.FindFirst("stationId"));
            var doctorId2  = Convert.ToInt32(context.User.FindFirst("doctorId"));

            socket.StationId  = stationId;
            socket.DoctorId   = doctorId;
            socket.PayOrderId = context.Request.Query["payOrderId"].ToString();
        }
        /// <summary>
        /// 中间件调用
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context)
        {
            //判断是否为websocket请求
            if (context.WebSockets.IsWebSocketRequest)
            {
                //接收客户端
                var cancellationToken = context.RequestAborted;
                var currentWebSocket  = await context.WebSockets.AcceptWebSocketAsync();

                var socket = new PayQrWebSocket {
                    WebSocket = currentWebSocket
                };
                _SetStationInfo(socket, context);//设置客户端的数据
                await _wsconnMgr.AddSocket(socket);

                while (socket.WebSocket.State == WebSocketState.Open)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                    var status = _wsHandler.GetPayQrStatus(socket);
                    switch (status)
                    {
                    case PayQrStatuses.INITIAL:                //初始化
                        await _wsHandler.GetPayQrInfo(socket); //发送获取到的二维码信息给客户端,客户端进行握手处理

                        _wsHandler.StartScaningDetect(socket);
                        break;

                    case PayQrStatuses.MONITORSCANING:                                     //开始监控
                        await _wsHandler.ScanningTimeoutDetect(socket, cancellationToken); //如果超时则会退出

                        Thread.Sleep(300);
                        continue;

                    case PayQrStatuses.ERROR:
                        break;
                    }

                    Thread.Sleep(500);
                }
                await _wsconnMgr.RemoveSocket(socket);
            }
            await this._next(context);
        }
Exemple #4
0
        /// <summary>
        /// 获取支付信息
        /// </summary>
        /// <param name="socket"></param>
        public async Task GetPayQrInfo(PayQrWebSocket socket)
        {
            CHIS.Models.PayQrInfo QrInfo = null;
            try
            {
                string h = await Ass.Net.WebHelper.WebPost(GetUrl("QRPay:HostUrl", "QRPay:GetPayInfo") + $"?payOrderId={socket.PayOrderId}");

                QrInfo = GetFromJsonString <CHIS.Models.PayQrInfo>(h);
                if (QrInfo.status == "ERROR")
                {
                    throw new Exception();
                }
                if (QrInfo.status == "PAYEDSUCCESS")
                {
                    await NotifyToClient(socket, QrInfo);

                    socket.PayQrStatus = PayQrStatuses.PAYEDSUCCESS;
                    await _wsconnMgr.RemoveSocket(socket);//关闭连接
                }
                else
                {
                    QrInfo.union2DCodeUrl = GetUnion2DCodeUrl(QrInfo);//获取统一二维码
                    socket.WxQrUrl        = QrInfo.wx2DCodeUrl;
                    socket.AliQrUrl       = QrInfo.ali2DCodeUrl;
                    socket.PayAmount      = QrInfo.totalAmount;
                    await NotifyToClient(socket, QrInfo);

                    socket.PayQrStatus = PayQrStatuses.GETPAYINFO;
                }
            }
            catch (Exception ex)
            {
                if (QrInfo == null)
                {
                    QrInfo = new CHIS.Models.PayQrInfo()
                    {
                        rlt = false, msg = ex.Message, status = "ERROR"
                    }
                }
                ;
                await NotifyToClient(socket, QrInfo);

                await _wsconnMgr.RemoveSocket(socket);//关闭连接
            }
        }
Exemple #5
0
        public async Task <CHIS.Models.PayWxH5Info> GetWxPayH5Info(PayQrWebSocket socket, string ipAddress)
        {
            CHIS.Models.PayWxH5Info QrInfo = null;
            try
            {
                string h = await Ass.Net.WebHelper.WebPost(GetUrl("QRPay:HostUrl", "QRPay:GetWxH5PayInfo") + $"?payOrderId={socket.PayOrderId}&ipAddress={ipAddress}");

                QrInfo = GetFromJsonString <CHIS.Models.PayWxH5Info>(h);
                if (QrInfo.status == "ERROR")
                {
                    throw new Exception();
                }
                if (QrInfo.status == "PAYEDSUCCESS")
                {
                    await NotifyToClient(socket, QrInfo);

                    socket.PayQrStatus = PayQrStatuses.PAYEDSUCCESS;
                    await _wsconnMgr.RemoveSocket(socket);//关闭连接

                    return(new CHIS.Models.PayWxH5Info()
                    {
                        rlt = false, msg = "已经支付过了", status = "PAYEDSUCCESS"
                    });
                }
                else
                {
                    return(QrInfo);
                }
            }
            catch (Exception ex)
            {
                if (QrInfo == null)
                {
                    QrInfo = new CHIS.Models.PayWxH5Info()
                    {
                        rlt = false, msg = ex.Message, status = "ERROR"
                    }
                }
                ;
                //await NotifyToClient(socket, QrInfo);
                //await _wsconnMgr.RemoveSocket(socket);//关闭连接
                return(QrInfo);
            }
        }
Exemple #6
0
        public int TimeOutSeconds = 60 * 10;//设置10分钟超时

        /// <summary>
        /// 获取支付状态
        /// </summary>
        /// <param name="socket"></param>
        /// <returns></returns>
        public PayQrStatuses GetPayQrStatus(PayQrWebSocket socket)
        {
            return(socket.PayQrStatus);
        }
Exemple #7
0
 /// <summary>
 /// 开始侦测
 /// </summary>
 /// <param name="socket"></param>
 public void StartScaningDetect(PayQrWebSocket socket)
 {
     socket.PayQrStatus   = PayQrStatuses.MONITORSCANING;
     socket.StartScanTime = DateTime.Now;
 }