ExternalSignCheck.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hardyx
  5. * Date: 2019/8/29
  6. * Time: 10:52
  7. */
  8. namespace App\Http\Middleware;
  9. use App\Libs\Utils;
  10. use Closure;
  11. use Illuminate\Support\Facades\DB;
  12. class ExternalSignCheck
  13. {
  14. /**
  15. * 外部通用 判断签名是否正确.
  16. */
  17. public function handle($request, Closure $next)
  18. {
  19. $data = $request->all();
  20. \Log::info('对外API入参: '.json_encode($data, 256));
  21. $appid = getProp($data, 'appid', '');
  22. $company = DB::table('companies')->where('appid', $appid)->where('is_enable', 1)->first();
  23. if (!$company) {
  24. Utils::throwError('1001: 您的appid不合法!');
  25. }
  26. $timestamp = getProp($data, 'timestamp', time());
  27. $sign = getProp($data, 'sign', '');
  28. $app_token = getProp($company, 'app_token');
  29. $params = [
  30. 'appid' => $appid,
  31. 'timestamp' => $timestamp,
  32. ];
  33. ksort($params);
  34. $str = strtolower(http_build_query($params));
  35. $checkSign = md5($str.'&key='.$app_token);
  36. \Log::info('ExternalSignCheck: str: '.$str.'; checkSign: '. $checkSign .'; paramSign: '.$sign);
  37. if ($checkSign != $sign) {
  38. Utils::throwError('1002: 您的签名不正确!');
  39. }
  40. $company_id = getProp($company, 'id');
  41. $distribution_channel_ids = DB::table('channel_users as cu')->leftJoin('distribution_channels as dc', 'dc.channel_user_id', 'cu.id')
  42. ->where('cu.company_id', $company_id)->where('cu.is_enabled', 1)->select('dc.id')->get()->pluck('id')->toArray();
  43. // 将数据绑定到全局
  44. $site = app('siteData');
  45. $site->appid = $appid;
  46. $site->app_token = getProp($company, 'app_token');
  47. $site->company_name = getProp($company, 'name');
  48. $site->company_id = $company_id;
  49. $site->channel_id = $distribution_channel_ids;
  50. return $next($request);
  51. }
  52. }