SmmAddAccount.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Distributor\Actions;
  3. use App\Distributor\Forms\ImportProduct;
  4. use App\Services\SmmService;
  5. use Dcat\Admin\Grid\RowAction;
  6. use Dcat\Admin\Grid\Tools\AbstractTool;
  7. use Dcat\Admin\Widgets\Modal;
  8. use App\Distributor\Forms\SmmAddAccount as SmmAddAccountForm;
  9. use Illuminate\Http\Request;
  10. class SmmAddAccount extends RowAction
  11. {
  12. protected $title = null;
  13. //媒体名称
  14. protected $mediaName = null;
  15. /**
  16. * 返回字段标题
  17. *
  18. * @return string
  19. */
  20. public function title()
  21. {
  22. if ($this->title) {
  23. return $this->title;
  24. }
  25. return admin_trans_label('add_platform_account');
  26. }
  27. public function setData($title,$mediaName)
  28. {
  29. $this->title = $title;
  30. $this->mediaName = $mediaName;
  31. }
  32. public function confirm()
  33. {
  34. $click_privacy = "<a href='/privacy_policy.html' target='_blank'>".admin_trans_label('click_privacy')."</a>";
  35. return [
  36. // 确认弹窗 title
  37. admin_trans_label('read_privacy'),
  38. // 确认弹窗 content
  39. $click_privacy
  40. ];
  41. }
  42. /**
  43. * 处理请求
  44. *
  45. */
  46. public function handle(Request $request)
  47. {
  48. // 获取 parameters 方法传递的参数
  49. $name = $request->get('name');
  50. if ($name) {
  51. //跳转到媒体受权页面
  52. $ssmService = new SmmService($name);
  53. $result = $ssmService->login();
  54. if ($result['status']) {
  55. // 返回 JS 代码触发新窗口打开
  56. $url = $result['data']['url'];
  57. return $this->response()->script(
  58. "window.open('{$url}', '_blank')"
  59. );
  60. }
  61. return $this
  62. ->response()
  63. ->error('获取授权失败,请检查媒体名称是否正确!');
  64. } else {
  65. return $this
  66. ->response()
  67. ->error('媒体名称不能为空');
  68. }
  69. }
  70. public function parameters()
  71. {
  72. if ($this->mediaName) {
  73. return [
  74. 'name' => $this->mediaName,
  75. ];
  76. } else {
  77. return [
  78. // 发送当前行 username 字段数据到接口
  79. 'name' => $this->row->name,
  80. ];
  81. }
  82. }
  83. }