接口规范文档
  • 首页 > php
  • 作者:berlin
  • 2019年7月7日 15:31 星期日
  • 浏览:274 次
  • 字号:    
  • 评论:0 条   编辑
  • 时间:2019-7-7 15:31   浏览:274  


    一.我们系统的一般的接口设计和跨域处理

    1.接口示例


    1.前后端token验证
    1. /*
    2. * 初始化操作
    3. */
    4. public function initialize() {
    5. // api接口测试的seesion_id,用param参数传递,cookie不能自动获取了!
    6. if (I('PHPSESSION')) {
    7. session_id(I('PHPSESSION'));
    8. }
    9. Session::start();
    10. parent::initialize();
    11. Header('Access-Control-Allow-Origin: *');
    12. //Header('Access-Control-Allow-Headers: *');
    13. Header('Access-Control-Allow-Headers: IsApi,Token');
    14. header("Cache-control: private"); // history.back返回后输入框值丢失问题 参考文章 http://www.tp-shop.cn/article_id_1465.html http://blog.csdn.net/qinchaoguang123456/article/details/29852881
    15. header('Content-type: application/json;charset=utf-8'); //设置文档格式为json
    16. $this->session_id = session_id(); // 当前的 session_id
    17. define('SESSION_ID',$this->session_id); //将当前的session_id保存为常量,供其它方法调用
    18. $this->page_size=10;
    19. // 判断当前用户是否手机
    20. if(isMobile())
    21. cookie('is_mobile','1',3600);
    22. else
    23. cookie('is_mobile','0',3600);
    24. if (!function_exists('getallheaders'))
    25. {
    26. function getallheaders()
    27. {
    28. foreach ($_SERVER as $name => $value)
    29. {
    30. if (substr($name, 0, 5) == 'HTTP_')
    31. {
    32. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
    33. }
    34. }
    35. return $headers;
    36. }
    37. }
    38. //微信浏览器
    39. //$_SESSION['openid']='';
    40. $headers = getallheaders();
    41. Log::record('2019050801000'.$headers['isapi']);
    42. if(!session('?user')){
    43. $headers = getallheaders();
    44. $headers = array_change_key_case($headers);
    45. if($headers['token']){
    46. $logic = new UsersLogic();
    47. $data = $logic->app_checklogin($headers['token']);
    48. $this->loginhandel($data);
    49. }elseif(strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger') && !$headers['isapi']){
    50. //$wx_type = tpCache('basic.wx_type');
    51. //接入的微信类型
    52. $this->weixin_config = M('wx_user')->find(); //获取微信配置
    53. Log::record('20190508跳转链接01');
    54. if(is_array($this->weixin_config) && $this->weixin_config['wait_access'] == 1){
    55. Log::record('20190508跳转链接022222');
    56. $wxuser = $this->GetOpenid(); //授权获取openid以及微信用户信息
    57. session('subscribe', $wxuser['subscribe']);// 当前这个用户是否关注了微信公众号
    58. //微信自动登录
    59. //$logic = new UsersLogic();
    60. //$data = $logic->thirdLogin($wxuser);
    61. $userLogic = new UsersLogicApp();
    62. $is_bind_account = tpCache('basic.is_bind_account');
    63. if ($is_bind_account == 1) {
    64. $data = $userLogic->thirdLogin_new($wxuser);
    65. if ($data['status'] != 1) {
    66. if ($data['result'] === '100') {
    67. session("third_oauth" , $wxuser);
    68. }
    69. //$this->ajaxReturn($data);
    70. }
    71. } else {
    72. $data = $userLogic->thirdLogin($wxuser);
    73. }
    74. $this->loginhandel($data);
    75. }
    76. }
    77. }
    78. if ((ACTION_NAME != 'do_login' && ACTION_NAME != 'do_register') && CONTROLLER_NAME !='Index' && CONTROLLER_NAME !='Salesdate'&& CONTROLLER_NAME !='Workshop') {
    79. if (!session('user.user_id')) {
    80. $this->ajaxReturn(array('status' => -99, 'msg' => '登陆已过期,请重新登陆'));
    81. exit;
    82. }
    83. }
    84. }

    token返回的方法

    1. public function app_checklogin($token = ''){
    2. $result = array();
    3. if (empty($token)){
    4. $result = array('status'=>-100, 'msg'=>'必须传递token', 'result'=>'');
    5. } else {
    6. $user = M('users')->where("token", $token)->find();
    7. if (empty($user)) {
    8. $result = array('status'=>-101, 'msg'=>'token错误', 'result'=>'');
    9. } else {
    10. $result = array('status'=>1,'msg'=>'登陆成功','result'=>$user);
    11. }
    12. }
    13. return $result;
    14. }

    前端登陆

    1. doLogin() {
    2. this.account.token = this.Msgcode.token;
    3. this.user.login(this.account).subscribe((resp:any) => {
    4. //this.navCtrl.push(MainPage);
    5. if (resp.status > 0) {
    6. this.loginback.status = 1;
    7. this.loginback.res = resp;
    8. console.log(this.loginback.res);
    9. if(this.loginback.res.status>0){
    10. this.Native.showToastTips(resp.msg);
    11. this.modalCtrl.dismiss(this.loginback);
    12. this.navCtrl.navigateRoot(['/']);
    13. }
    14. } else {
    15. this.Native.showToastTips(resp.msg);
    16. }
    17. }, (err) => {
    18. });
    19. }

    拦截器

    1. intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    2. let authReq;
    3. // console.log('http:',req.url);
    4. console.log('http:',req.url);
    5. if (req.url.indexOf('/assets') > -1) {
    6. authReq = req.clone({
    7. url: (req.url)
    8. });
    9. }else if (req.url.indexOf('/ross') > -1) {
    10. authReq = req.clone({
    11. url: (req.url)
    12. });
    13. } else if (req.url == 'http://59.41.187.99:6790/ross/post/cloud/operation') {
    14. authReq = req.clone({
    15. url: (req.url)
    16. });
    17. } else if (req.url == 'http://cloudpf.weunit.cn/cloudpf/Printing/print') {
    18. authReq = req.clone({
    19. url: (req.url)
    20. });
    21. } else if (req.url == 'http://cloudpf.weunit.cn/cloudpf/Printing/print') {
    22. authReq = req.clone({
    23. url: (req.url)
    24. });
    25. } else if (req.url.indexOf('/ross/mes/cloud/api') > -1) {
    26. authReq = req.clone({
    27. url: (req.url)
    28. });
    29. } else {
    30. authReq = req.clone({
    31. url: (req.url),
    32. setHeaders: {
    33. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    34. IsApi: 'true',
    35. Token: this.globalData.token ? this.globalData.token : ''
    36. }
    37. });
    38. }
    39. if (authReq.method != 'GET') {
    40. this.showLoading();
    41. }
    42. return <any>next.handle(authReq).pipe(mergeMap((event: any) => {
    43. if (event instanceof HttpResponse && event.status !== 200) {
    44. return throwError(event);
    45. }
    46. // console.log(event);
    47. return Observable.create(observer => {
    48. if (event.body) {
    49. // console.log(event.body);
    50. // 统一判断全局状态返回
    51. if (event.body.status == -99) {
    52. this.nativeService.showToastTips(event.body.msg);
    53. //延时判断是否微信浏览器 是否登陆 是否正在授权中
    54. if (this.wconf.isWeiXin()) {
    55. //满足条件触发 微信授权提示框并获取微信配置
    56. this.Api.get('WechatOauth/getwc_config').subscribe((res: any) => { }, (err) => { });
    57. } else {
    58. window['epInstance']['emit']('toLogin', 0);
    59. }
    60. } else if (event.body.status == -100) {
    61. //弹出微信授权窗口
    62. window['epInstance']['emit']('wechatLogin', event.body);
    63. }
    64. observer.next(event);
    65. } else {
    66. observer.next(event);
    67. }
    68. if (event.type > 0) {
    69. this.hideLoading();
    70. }
    71. }); // 请求成功返回响应
    72. }),
    73. catchError((res: HttpResponse<any>) => {
    74. // 请求失败处理
    75. this.hideLoading();
    76. if (!this.nativeService.isConnecting()) {
    77. this.nativeService.showToastTips('请连接网络');
    78. } else if (res instanceof TimeoutError) {
    79. this.nativeService.showToastTips('请求超时,请稍后再试!');
    80. } else {
    81. let msg = '请求发生异常';
    82. switch (res.status) {
    83. case 0:
    84. //console.log('未知的Api');
    85. msg = '未知的Api';
    86. break;
    87. case 401:
    88. break;
    89. case 404:
    90. //console.log('Api请求地址不存在');
    91. msg = 'Api请求地址不存在';
    92. //this.events.publish('toLogin');
    93. break;
    94. case 403:
    95. //console.log('业务错误');
    96. break;
    97. case 500:
    98. //console.log('服务器出错');
    99. msg = 'Api请求地址不存在';
    100. break;
    101. }
    102. // this.nativeService.showToastTips(msg);
    103. }
    104. return throwError(event);
    105. }));
    106. }

    export class InterceptorService implements HttpInterceptor {

    2.跨域处理

    base.php处理

    前端拦截器处理

    二.接口设计规范

    1.接口规范
    https://blog.csdn.net/qq_38935667/article/details/79280295
    2.安全性设计
    https://www.jianshu.com/p/c6518a8f4040

    三.restful接口设计

    https://www.jianshu.com/p/b531519fe813
    jwt
    https://www.cnblogs.com/xiaohuochai/p/8440335.html

     您阅读这篇文章共花了: 
    微信分享提示:①点击右上角的【∶】,②再点击【发送给朋友】或【分享到朋友圈】,让更多精彩传递!
  •  本文无需标签!
      昵称   邮箱   主页
    返回顶部 返回首页 关于我们 自定义链接 自定义链接2 手机版本后花园 版权所有:IT技术分享 站长: berlin主题:水墨书香免费版 v1.4 程序:emlog
  • 文章:24 篇
  • 评论:1 条
  • 微语:1 条
  • 友链:1 个
  • 分类:8 个
  • 标签:0 个
  • 作者:1 人
  • 建站日期:2014-04-24