Server.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace App\Api\Service;
  3. use Illuminate\Support\Facades\Request;
  4. use Illuminate\Support\Facades\Validator;
  5. /**
  6. * API服务端总入口
  7. * @author Flc <2016-7-31 11:27:09>
  8. */
  9. class Server
  10. {
  11. /**
  12. * 请求参数
  13. * @var array
  14. */
  15. protected $params = [];
  16. /**
  17. * API请求Method名
  18. * @var string
  19. */
  20. protected $method;
  21. /**
  22. * app_id
  23. * @var string
  24. */
  25. protected $app_id;
  26. /**
  27. * app_secret
  28. * @var string
  29. */
  30. protected $app_secret;
  31. /**
  32. * 回调数据格式
  33. * @var string
  34. */
  35. protected $format = 'json';
  36. /**
  37. * 签名方法
  38. * @var string
  39. */
  40. protected $sign_method = 'md5';
  41. /**
  42. * 是否输出错误码
  43. * @var boolean
  44. */
  45. protected $error_code_show = false;
  46. /**
  47. * 初始化
  48. * @param Error $error Error对象
  49. */
  50. public function __construct(Error $error)
  51. {
  52. $this->params = Request::all();
  53. $this->error = $error;
  54. }
  55. /**
  56. * api服务入口执行
  57. * @param Request $request 请求参数
  58. * @return [type] [description]
  59. */
  60. public function run($request)
  61. {
  62. // A.1 初步校验
  63. $rules = [
  64. 'app_id' => 'required',
  65. 'method' => 'required',
  66. 'format' => 'in:,json',
  67. 'sign_method' => 'in:,md5',
  68. 'nonce' => 'required|string|min:1|max:32|',
  69. 'sign' => 'required',
  70. ];
  71. $messages = [
  72. 'app_id.required' => '1001',
  73. 'method.required' => '1003',
  74. 'format.in' => '1004',
  75. 'sign_method.in' => '1005',
  76. 'nonce.required' => '1010',
  77. 'nonce.string' => '1011',
  78. 'nonce.min' => '1012',
  79. 'nonce.max' => '1012',
  80. 'sign.required' => '1006'
  81. ];
  82. $v = Validator::make($this->params, $rules, $messages);
  83. if ($v->fails()) {
  84. return $this->response(['status' => false, 'code' => $v->messages()->first()]);
  85. }
  86. // A.2 赋值对象
  87. $this->format = !empty($this->params['format']) ? $this->params['format'] : $this->format;
  88. $this->sign_method = !empty($this->params['sign_method']) ? $this->params['sign_method'] : $this->sign_method;
  89. $this->app_id = $this->params['app_id'];
  90. $this->method = $this->params['method'];
  91. // B. appid校验
  92. $app = App::getInstance($this->app_id)->info();
  93. if (! $app)
  94. return $this->response(['status' => false, 'code' => '1002']);
  95. $this->app_secret = $app->app_secret;
  96. // C. 校验签名
  97. $signRes = config('app.debug')?['status'=>true]:$this->checkSign($this->params);
  98. if (! $signRes || ! $signRes['status']) {
  99. return $this->response(['status' => false, 'code' => $signRes['code']]);
  100. }
  101. // D. 校验接口名
  102. // D.1 通过方法名获取类名
  103. $this->method=str_replace('.','\\',$this->method);
  104. $className = self::getClassName($this->method);
  105. // D.2 判断类名是否存在
  106. $classPath = __NAMESPACE__ . '\\Response\\' . $className;
  107. if (!$className || !class_exists($classPath)) {
  108. return $this->response(['status' => false, 'code' => '1008']);
  109. }
  110. // D.3 判断方法是否存在
  111. if (! method_exists($classPath, 'run')) {
  112. return $this->response(['status' => false, 'code' => '1009']);
  113. }
  114. $this->classname = $classPath;
  115. // X 如果有uid 要验证
  116. if(!empty($this->params['uid'])&&!empty($this->params['token'])&&!$this->checkToken($this->params['uid'],$this->params['token'])){
  117. return $this->response(['status' => false, 'code' => '1014']);
  118. }
  119. // E. api接口分发
  120. $class = new $classPath;
  121. return $this->response((array) $class->run($this->params,$request));
  122. }
  123. /**
  124. * 检查token和uid是否一致
  125. * @param $uid
  126. * @param $token
  127. * @return bool
  128. */
  129. protected function checkToken($uid,$token){
  130. if(md5(sha1($uid))==$token){
  131. return true;
  132. }
  133. return false;
  134. }
  135. /**
  136. * 校验签名
  137. * @param [type] $params [description]
  138. * @return [type] [description]
  139. */
  140. protected function checkSign($params)
  141. {
  142. $sign = array_key_exists('sign', $params) ? $params['sign'] : '';
  143. if (empty($sign))
  144. return array('status' => false, 'code' => '1006');
  145. unset($params['sign']);
  146. if ($sign != $this->generateSign($params)){
  147. info('pay error'.$params['method']);
  148. // return array('status' => false, 'code' => '1007');
  149. }
  150. return array('status' => true, 'code' => '200');
  151. }
  152. /**
  153. * 生成签名
  154. * @param array $params 待校验签名参数
  155. * @return string|false
  156. */
  157. protected function generateSign($params)
  158. {
  159. if ($this->sign_method == 'md5')
  160. return $this->generateMd5Sign($params);
  161. return false;
  162. }
  163. /**
  164. * md5方式签名
  165. * @param array $params 待签名参数
  166. * @return string
  167. */
  168. protected function generateMd5Sign($params)
  169. {
  170. ksort($params);
  171. $tmps = array();
  172. foreach ($params as $k => $v) {
  173. $tmps[] = $k . $v;
  174. }
  175. $string = $this->app_secret . implode('', $tmps) . $this->app_secret;
  176. return strtoupper(md5($string));
  177. }
  178. /**
  179. * 通过方法名转换为对应的类名
  180. * @param string $method 方法名
  181. * @return string|false
  182. */
  183. protected function getClassName($method)
  184. {
  185. $methods = explode('.', $method);
  186. if (!is_array($methods))
  187. return false;
  188. $tmp = array();
  189. foreach ($methods as $value) {
  190. $tmp[] = ucwords($value);
  191. }
  192. $className = implode('', $tmp);
  193. return $className;
  194. }
  195. /**
  196. * 输出结果
  197. * @param array $result 结果
  198. * @return response
  199. */
  200. protected function response(array $result)
  201. {
  202. if (! array_key_exists('msg', $result) && array_key_exists('code', $result)) {
  203. $result['msg'] = $this->getError($result['code']);
  204. }
  205. if ($this->format == 'json') {
  206. return response()->json($result);
  207. }
  208. return false;
  209. }
  210. /**
  211. * 返回错误内容
  212. * @param string $code 错误码
  213. * @return string
  214. */
  215. protected function getError($code)
  216. {
  217. return $this->error->getError($code, $this->error_code_show);
  218. }
  219. }