123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * Created by PhpStorm.
- * User: guanxl
- * Date: 2018/7/24
- * Time: 15:22
- */
-
- namespace App\Http\Controllers\Api;
-
- use App\Http\Controllers\Controller;
- use App\Models\Admin;
- use App\Models\Corps;
- use App\Services\AghApiService;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
-
-
- class IndexController extends Controller
- {
- public function sso(Request $request){
-
- //return AghApiService::getInstance()->adminSso();
- $array = [
- "partnerId"=>$request->get("partnerId"),
- "expires"=>$request->get("expires"),
- "nonce"=>$request->get("nonce"),
- "data"=>urlencode($request->get("data")),
- ];
- ksort($array);
- $str ="";
- foreach ($array as $key=>$value){
- $str = $str . $key . '=' . $value . '&';
- }
- $str = substr($str, 0,strlen($str)-1);
- $signature = hash_hmac('SHA256',$str, env("B_API_SECRET_KEY"));
- Log::info(json_encode($array));
- Log::info("signature:".$signature);
-
-
- try{
- if($signature==$request->get("signature")){
- $data = json_decode(base64_decode($request->get("data")),true);
- $adminUserId = $data["admin_user_id"];
- $aghCorpId = $data["corp_id"];
- $name = $data["admin_name"];
-
- $adminUser = Admin::where("admin_user_id",$adminUserId)->first();
- if(empty($adminUser)){
- $corp = Corps::where("source_id",$aghCorpId)->where("source_type","agh")->first();
- if($corp){
- $res = Admin::insert([
- "username" => "agh_".$adminUserId,
- "name" => $name,
- "corp_id" => $corp->corp_id,
- "is_admin" => 0,
- "admin_user_id" => $adminUserId,
- "status" => 1,
- "create_time" => time(),
- "update_time" => time(),
- ]);
- if($res){
- $adminUser = Admin::where("admin_user_id",$adminUserId)->first();
- }
- }else{
- $ret = AghApiService::getInstance()->getCorpInfo($aghCorpId);
- if($ret["errno"]==0){
- $corpSource = $ret["body"];
- $query = [];
- $query['source_id'] = $corpSource['enterpriseId'];
- $query['source_type'] = 'agh';
- $query['corp_name'] = $corpSource['fullName'];
- $query['corp_short_name'] = $corpSource['abbName'];
- $query['contact_name'] = $corpSource['contact'];
- $query['contact_phone'] = $corpSource['cellPhone'];
- $query['contact_email'] = $corpSource['email'];
- $query['contact_address'] = $corpSource['address'];
- $query['status'] = $corpSource['state'] == true ? 1 : 0;
- $query['update_time'] = time();
- $query["create_time"] = time();
-
- Corps::insert($query);
-
- $corp = Corps::where("source_id",$aghCorpId)->where("source_type","agh")->first();
- if($corp){
- $res = Admin::insert([
- "username" => "agh_".$adminUserId,
- "name" => $name,
- "corp_id" => $corp->corp_id,
- "is_admin" => 0,
- "admin_user_id" => $adminUserId,
- "status" => 1,
- "create_time" => time(),
- "update_time" => time(),
- ]);
- if($res){
- $adminUser = Admin::where("admin_user_id",$adminUserId)->first();
- }
- }
-
- }
- }
- }
-
- if($adminUser){
- $corp = Corps::where("corp_id", $adminUser["corp_id"])->first();
-
- DB::table("agh_admin_log")->insert([
- "admin_id" =>$adminUser["id"],
- "name" =>$adminUser["name"],
- "corp_id" =>$corp["corp_id"],
- "agh_corp_id" =>$corp["source_id"],
- "corp_name" =>$corp["corp_name"],
- "first_time" =>$adminUser["create_time"],
- "create_time" => time()
- ]);
-
- $request->session()->put('userInfo', $adminUser);
- $redirect = $request->get("redirect");
- if(empty($redirect)){
- return redirect("/manage");
- }else{
- header("location:".$redirect);
- }
-
- }
-
- return "认证成功,但找不到该企业信息";
- }
- }catch (\Exception $exception){
- Log::info($exception->getTraceAsString());
- }
-
- return "认证不通过";
-
- }
-
- }
|