HomeController.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using EasyTemplate.Tool;
  2. using Microsoft.AspNetCore.Mvc;
  3. using RouteAttribute = Microsoft.AspNetCore.Mvc.RouteAttribute;
  4. namespace EasyTemplate.Blazor.Web.Controller;
  5. /// <summary>
  6. /// 注意,如果需要扫码登录或是支付回调,可以使用该控制器
  7. /// </summary>
  8. [ApiController]
  9. [Route("api/[controller]")]
  10. public class HomeController : ControllerBase
  11. {
  12. public HomeController(IHttpContextAccessor contextAccessor, SqlSugarRepository<SystemUser> user, INotificationService notificationService)
  13. {
  14. ContextAccessor = contextAccessor;
  15. User = user;
  16. NotificationService = notificationService;
  17. }
  18. /// <summary>
  19. /// 这里必须使用IgnoreAntiforgeryToken属性,绕过blazor的防伪验证
  20. /// </summary>
  21. /// <param name="userid"></param>
  22. /// <param name="name"></param>
  23. /// <param name="data"></param>
  24. /// <param name="phone"></param>
  25. /// <returns></returns>
  26. [HttpGet("scan_sign_in_call_back")]
  27. [IgnoreAntiforgeryToken] // <-- 关键特性:忽略该端点的防伪验证
  28. public async Task<IActionResult> ScanSignInCallback(long userid, string name, string data, string phone)
  29. {
  30. var user = await User.AsQueryable().FirstAsync(x => x.Id == userid);
  31. if (user == null)
  32. {
  33. //这里必须使用重定向,它会指向/wwwroot/html/error.html页面,该页面专门用于扫码登录报错
  34. //error.html的query参数包含:
  35. // code:错误代码
  36. // title:提示
  37. // message:错误信息
  38. // image:info,success,warning,error
  39. return Redirect("/html/error.html?code=504&title=提示&message=该账户不存在,请联系管理员&image=info");
  40. }
  41. return Redirect("/");//重定向到首页
  42. }
  43. /// <summary>
  44. ///
  45. /// </summary>
  46. private readonly IHttpContextAccessor ContextAccessor;
  47. /// <summary>
  48. ///
  49. /// </summary>
  50. private readonly SqlSugarRepository<SystemUser> User;
  51. /// <summary>
  52. ///
  53. /// </summary>
  54. private readonly INotificationService NotificationService;
  55. }