接口规范文档

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

 

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

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

拦截器

 
  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.  
  40. if (authReq.method != 'GET') {
  41. this.showLoading();
  42. }
  43. return <any>next.handle(authReq).pipe(mergeMap((event: any) => {
  44. if (event instanceof HttpResponse && event.status !== 200) {
  45. return throwError(event);
  46. }
  47. // console.log(event);
  48. return Observable.create(observer => {
  49. if (event.body) {
  50. // console.log(event.body);
  51. // 统一判断全局状态返回
  52. if (event.body.status == -99) {
  53. this.nativeService.showToastTips(event.body.msg);
  54. //延时判断是否微信浏览器 是否登陆 是否正在授权中
  55. if (this.wconf.isWeiXin()) {
  56. //满足条件触发 微信授权提示框并获取微信配置
  57. this.Api.get('WechatOauth/getwc_config').subscribe((res: any) => { }, (err) => { });
  58. } else {
  59. window['epInstance']['emit']('toLogin', 0);
  60. }
  61. } else if (event.body.status == -100) {
  62. //弹出微信授权窗口
  63. window['epInstance']['emit']('wechatLogin', event.body);
  64. }
  65. observer.next(event);
  66. } else {
  67. observer.next(event);
  68. }
  69. if (event.type > 0) {
  70. this.hideLoading();
  71. }
  72. }); // 请求成功返回响应
  73. }),
  74. catchError((res: HttpResponse<any>) => {
  75. // 请求失败处理
  76. this.hideLoading();
  77. if (!this.nativeService.isConnecting()) {
  78. this.nativeService.showToastTips('请连接网络');
  79. } else if (res instanceof TimeoutError) {
  80. this.nativeService.showToastTips('请求超时,请稍后再试!');
  81. } else {
  82. let msg = '请求发生异常';
  83. switch (res.status) {
  84. case 0:
  85. //console.log('未知的Api');
  86. msg = '未知的Api';
  87. break;
  88. case 401:
  89. break;
  90. case 404:
  91. //console.log('Api请求地址不存在');
  92. msg = 'Api请求地址不存在';
  93. //this.events.publish('toLogin');
  94. break;
  95. case 403:
  96. //console.log('业务错误');
  97. break;
  98. case 500:
  99. //console.log('服务器出错');
  100. msg = 'Api请求地址不存在';
  101. break;
  102. }
  103. // this.nativeService.showToastTips(msg);
  104. }
  105.  
  106. return throwError(event);
  107. }));
  108. }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值