欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SignalR发送页面跳转通知的方法

程序员文章站 2022-07-18 15:06:37
微信商城使用支付宝支付的时候,需要有个过度页面提示用户用浏览器打开页面去支付,等用户在浏览器支付完之后再打开微信(微信此时依旧显示的是过度页面),过度页面需要跳转到订单详情...

微信商城使用支付宝支付的时候,需要有个过度页面提示用户用浏览器打开页面去支付,等用户在浏览器支付完之后再打开微信(微信此时依旧显示的是过度页面),过度页面需要跳转到订单详情页面。那么这个过度页面怎么知道需要跳转呢?

目前能想到的就是用sigbalr发送通知告诉那个过度页面去跳转.

第一步,先添加sigbalr相关dll.

第二步,自定义hub

 public class myhub:hub
 {
  public override task onconnected()
  {
   return base.onconnected();
  }
 }

第三步,自定义useridprovider,因为我们需要给指定用户发送跳转的通知

public class customeruseridprovider: iuseridprovider
 {
  public string getuserid(irequest request)
  {
   //获取当前登录用户
   var customer = enginecontext.current.resolve<iworkcontext>().currentcustomer;
   if(customer==null)
   {
    return "";
   }else
   {
    //返回当前登录用户id
    return customer.id.tostring();
   }
  }
 }

第四步,在startup里注册我们自定义的useridprovider

//signlr
 var idprovider = new customeruseridprovider();
globalhost.dependencyresolver.register(typeof(iuseridprovider), () => idprovider);
app.mapsignalr();

第五步,在支付宝支付完成异步通知加上这行代码发送通知,并且传入订单单号参数,这里给order customerid的用户发送通知,order customerid和当前登录者的id是一样的。所以能接收到信息。

var myhub = globalhost.connectionmanager.gethubcontext<myhub>();
myhub.clients.user(order.ordercustomerid.tostring()).redirctorderdetails(order.ordernumber); 

第六步,在在过度页面接受执行

// 声明一个代理引用该集线器,记得$.connection.后面的方法首字母必须要小写,这也是我为什么使用别名的原因
var chat = $.connection.chinookhub;
// 这里是注册集线器调用的方法,和1.0不同的是需要chat.client后注册,1.0则不需要
chat.client.redirctorderdetails = function (oerdernumber) {
 window.location.href = "/customer/myorderdetails?page=4&ordernumber=" + oerdernumber;
 };
chat.client.redirctmoneyaccountdetail = function () {
 window.location.href = "/customer/accountdetail/money";
 };
 // 启动连接
$.connection.hub.start();

这样,就可以完美的实现支付完成后跳转页面了。目前没想到更好的办法了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。