ModulesapiController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fec\controllers;
  10. use Yii;
  11. use yii\web\Controller;
  12. ##################################################################################################################################
  13. # 1.在执行前,先进行module token验证,通过方可继续执行
  14. # 2.通过controller action 自动找到对应的 o文件下的文件,文件路径和方法保持对应
  15. # 3.函数执行完毕,返回的是json格式的数据
  16. # 4.在返回数据之前,进行session的提交,保证修改的session提交到redis。
  17. ##################################################################################################################################
  18. /**
  19. * @author Terry Zhao <2358269014@qq.com>
  20. * @since 1.0
  21. */
  22. class ModulesapiController extends Controller
  23. {
  24. # 通过 controller action 自动找到 模块 ./o文件夹下面的文件,通过对应关系匹配
  25. public function actions()
  26. {
  27. $return = [];
  28. $module_token = Yii::$app->request->post('module_token');
  29. $this_module_token = \fec\helpers\CModule::getToken();
  30. if($module_token != $this_module_token){
  31. $return['ack'] = false;
  32. $return['ack_description'] = "modules token is not right";
  33. $return['content'] = $module_token.'#'.$this_module_token;
  34. echo json_encode($return);
  35. exit;
  36. }
  37. $r = $this->getControllerAndAction();
  38. $t_controller = $r['controller'];
  39. $t_action = $r['action'];
  40. $current_remote_function_param_array = Yii::$app->request->post('current_remote_function_param_array');
  41. $current_remote_function_param_array = unserialize($current_remote_function_param_array);
  42. $param = (!empty($current_remote_function_param_array) && is_array($current_remote_function_param_array) ) ? $current_remote_function_param_array : [] ;
  43. $current_namespace = $this->_name_space;
  44. $module_o_dir = str_replace("\\controllers","\\o",$current_namespace);
  45. $function_exec = $module_o_dir."\\".$t_controller."::".$t_action;
  46. $data = \call_user_func_array($function_exec, $param);
  47. $data = json_decode($data);
  48. $return['ack'] = true;
  49. $return['ack_description'] = "success";
  50. $return['content'] = $data;
  51. # 把模块更新的session更新到redis上面,以供其他模块使用
  52. session_commit();
  53. echo json_encode($return);
  54. exit;
  55. }
  56. # 得到当前的controller 和action
  57. public function getControllerAndAction(){
  58. $path_info = Yii::$app->request->getPathInfo();
  59. $path_info = trim($path_info,"/");
  60. $controller_str = substr($path_info,strpos($path_info,"/")+1);
  61. $str = strrev($controller_str);
  62. $action = strrev(substr($str,0,strpos($str,"/")));
  63. $controllerstr = strrev(substr($str,strpos($str,"/")+1));
  64. $controllerstr = explode("/",$controllerstr);
  65. $arr = [];
  66. $count = count($controllerstr);
  67. $i = 0;
  68. foreach($controllerstr as $v){
  69. $i++;
  70. if($count == $i){
  71. $arr[] = ucfirst($v);
  72. }else{
  73. $arr[] = strtolower($v);
  74. }
  75. }
  76. return [
  77. 'controller' => implode("/",$arr),
  78. 'action' => $action,
  79. ];
  80. }
  81. }