菜谱项目

SignVerify.php 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use App\User;
  5. use App\Error;
  6. class SignVerify
  7. {
  8. /**
  9. * Handle an incoming request.
  10. *
  11. * @param \Illuminate\Http\Request $request
  12. * @param \Closure $next
  13. * @return mixed
  14. */
  15. public function handle($request, Closure $next)
  16. {
  17. $flag = $this->validSign($request);
  18. if($flag) {
  19. return $next($request);
  20. } else {
  21. return json_encode(['rst'=>['msg'=>'sign is error'], 'errno'=>'401', 'err'=>Error::getError(401),'timestamp'=>time().'']);
  22. }
  23. }
  24. public function validSign($request) {
  25. $params=$request->all();
  26. $sign=$request->get('sign');
  27. if(empty($sign)){
  28. return false;
  29. }
  30. $makesign = $this->getSignature($params, Config('constants.SMS_SECRET_KEY'));
  31. if($makesign == $sign) {
  32. return true;
  33. }
  34. return false;
  35. }
  36. public function getSignature($params, $secret_key) {
  37. // 按数组键名 正序排序
  38. ksort($params);
  39. $tem = array();
  40. foreach ($params as $k => $v) {
  41. if ($k !== 'sign') {
  42. $tem[] = "$k=$v";
  43. }
  44. }
  45. $sk = implode('&', $tem) . $secret_key;
  46. return md5($sk);
  47. }
  48. }