IndexController.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: guanxl
  5. * Date: 2018/7/24
  6. * Time: 15:22
  7. */
  8. namespace App\Http\Controllers\Api;
  9. use App\Http\Controllers\Controller;
  10. use App\Models\Admin;
  11. use App\Models\Corps;
  12. use App\Services\AghApiService;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. class IndexController extends Controller
  17. {
  18. public function sso(Request $request){
  19. //return AghApiService::getInstance()->adminSso();
  20. $array = [
  21. "partnerId"=>$request->get("partnerId"),
  22. "expires"=>$request->get("expires"),
  23. "nonce"=>$request->get("nonce"),
  24. "data"=>urlencode($request->get("data")),
  25. ];
  26. ksort($array);
  27. $str ="";
  28. foreach ($array as $key=>$value){
  29. $str = $str . $key . '=' . $value . '&';
  30. }
  31. $str = substr($str, 0,strlen($str)-1);
  32. $signature = hash_hmac('SHA256',$str, env("B_API_SECRET_KEY"));
  33. Log::info(json_encode($array));
  34. Log::info("signature:".$signature);
  35. try{
  36. if($signature==$request->get("signature")){
  37. $data = json_decode(base64_decode($request->get("data")),true);
  38. $adminUserId = $data["admin_user_id"];
  39. $aghCorpId = $data["corp_id"];
  40. $name = $data["admin_name"];
  41. $adminUser = Admin::where("admin_user_id",$adminUserId)->first();
  42. if(empty($adminUser)){
  43. $corp = Corps::where("source_id",$aghCorpId)->where("source_type","agh")->first();
  44. if($corp){
  45. $res = Admin::insert([
  46. "username" => "agh_".$adminUserId,
  47. "name" => $name,
  48. "corp_id" => $corp->corp_id,
  49. "is_admin" => 0,
  50. "admin_user_id" => $adminUserId,
  51. "status" => 1,
  52. "create_time" => time(),
  53. "update_time" => time(),
  54. ]);
  55. if($res){
  56. $adminUser = Admin::where("admin_user_id",$adminUserId)->first();
  57. }
  58. }else{
  59. $ret = AghApiService::getInstance()->getCorpInfo($aghCorpId);
  60. if($ret["errno"]==0){
  61. $corpSource = $ret["body"];
  62. $query = [];
  63. $query['source_id'] = $corpSource['enterpriseId'];
  64. $query['source_type'] = 'agh';
  65. $query['corp_name'] = $corpSource['fullName'];
  66. $query['corp_short_name'] = $corpSource['abbName'];
  67. $query['contact_name'] = $corpSource['contact'];
  68. $query['contact_phone'] = $corpSource['cellPhone'];
  69. $query['contact_email'] = $corpSource['email'];
  70. $query['contact_address'] = $corpSource['address'];
  71. $query['status'] = $corpSource['state'] == true ? 1 : 0;
  72. $query['update_time'] = time();
  73. $query["create_time"] = time();
  74. Corps::insert($query);
  75. $corp = Corps::where("source_id",$aghCorpId)->where("source_type","agh")->first();
  76. if($corp){
  77. $res = Admin::insert([
  78. "username" => "agh_".$adminUserId,
  79. "name" => $name,
  80. "corp_id" => $corp->corp_id,
  81. "is_admin" => 0,
  82. "admin_user_id" => $adminUserId,
  83. "status" => 1,
  84. "create_time" => time(),
  85. "update_time" => time(),
  86. ]);
  87. if($res){
  88. $adminUser = Admin::where("admin_user_id",$adminUserId)->first();
  89. }
  90. }
  91. }
  92. }
  93. }
  94. if($adminUser){
  95. $corp = Corps::where("corp_id", $adminUser["corp_id"])->first();
  96. DB::table("agh_admin_log")->insert([
  97. "admin_id" =>$adminUser["id"],
  98. "name" =>$adminUser["name"],
  99. "corp_id" =>$corp["corp_id"],
  100. "agh_corp_id" =>$corp["source_id"],
  101. "corp_name" =>$corp["corp_name"],
  102. "first_time" =>$adminUser["create_time"],
  103. "create_time" => time()
  104. ]);
  105. $request->session()->put('userInfo', $adminUser);
  106. $redirect = $request->get("redirect");
  107. if(empty($redirect)){
  108. return redirect("/manage");
  109. }else{
  110. header("location:".$redirect);
  111. }
  112. }
  113. return "认证成功,但找不到该企业信息";
  114. }
  115. }catch (\Exception $exception){
  116. Log::info($exception->getTraceAsString());
  117. }
  118. return "认证不通过";
  119. }
  120. }