SignalR Method for Sending Page Jump Notification

  • 2021-09-12 00:53:24
  • OfStack

When WeChat Mall uses Alipay to pay, it needs a transition page to prompt users to open the page with the browser to pay, and then open WeChat after the browser pays (WeChat still displays the transition page at this time), and the transition page needs to jump to the order details page. So how does this transition page know that it needs to jump?

At present, all I can think of is to send a notification with SigbalR to tell the transition page to jump.

Step 1, first add SigbalR related dll.

Step 2, Customize hub


 public class myHub:Hub
 {
  public override Task OnConnected()
  {
   return base.OnConnected();
  }
 }

Step 3, customize UserIdProvider, because we need to send the notification of jump to the specified user


public class CustomerUserIdProvider: IUserIdProvider
 {
  public string GetUserId(IRequest request)
  {
   // Get the current logged-in user 
   var customer = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer;
   if(customer==null)
   {
    return "";
   }else
   {
    // Returns the currently logged-in user id
    return customer.Id.ToString();
   }
  }
 }

Step 4: Register our custom UserIdProvider in startup


//SignlR
 var idProvider = new CustomerUserIdProvider();
GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => idProvider);
app.MapSignalR();

Step 5: Add this line of code to the asynchronous notification of Alipay payment completion to send the notification, and pass in the order number parameter. Here, send the notification to the user of order customerid. order customerid and id of the current login are the same. So we can receive information.


var myHub = GlobalHost.ConnectionManager.GetHubContext<myHub>();
myHub.Clients.User(order.OrderCustomerId.ToString()).redirctOrderDetails(order.OrderNumber); 

Step 6: Accept execution on the transition page


//  Declaration 1 Agents reference the hub , Remember $.connection. The first letter of the following method must be lowercase , That's why I use aliases 
var chat = $.connection.chinookHub;
//  Here is the method called by the registry hub , And 1.0 The difference is the need chat.client Post-registration ,1.0 You do not need 
chat.client.redirctOrderDetails = function (oerderNumber) {
 window.location.href = "/Customer/MyOrderDetails?page=4&orderNumber=" + oerderNumber;
 };
chat.client.redirctMoneyAccountDetail = function () {
 window.location.href = "/Customer/AccountDetail/Money";
 };
 //  Start a connection 
$.connection.hub.start();

In this way, you can perfectly realize the jump page after payment is completed. I can't think of a better way at present.


Related articles: