RemoteService.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 fecshop\services\extension;
  10. //use fecshop\models\mysqldb\cms\StaticBlock;
  11. use Yii;
  12. use fecshop\services\Service;
  13. /**
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class RemoteService extends Service
  18. {
  19. const ADDONS_TOKEN = 'addons_access_token';
  20. public $remoteUrl = 'http://addons.server.fecmall.com';
  21. public $loginUrlKey = '/customer/login/account';
  22. public $getAddonsListUrlKey = '/customer/addons/index';
  23. public $getAddonInfoUrlKey = '/customer/addons/info';
  24. public $getDeveloperInfoUrlKey = '/customer/addons/developer';
  25. // 远程登陆
  26. public function login($param)
  27. {
  28. $url = $this->remoteUrl . $this->loginUrlKey ;
  29. $data = [
  30. 'email' => $param['email'],
  31. 'password' => $param['password'],
  32. ];
  33. list($responseHeader, $result) = $this->getCurlData($url, 'post', [], $data, 30);
  34. if ($result['code'] == 200) {
  35. $access_token = $responseHeader['Access-Token'];
  36. $this->setAccessToken($access_token);
  37. return true;
  38. }
  39. return false;
  40. }
  41. // 得到开发者的信息
  42. public function getDeveloperInfo()
  43. {
  44. $accessToken = $this->getAccessToken();
  45. if (!$accessToken) {
  46. return false;
  47. }
  48. $url = $this->remoteUrl . $this->getDeveloperInfoUrlKey ;
  49. $headerRequest = [
  50. 'access-token: '.$accessToken,
  51. ];
  52. list($responseHeader, $result) = $this->getCurlData($url, 'post', $headerRequest, [], 30);
  53. if ($result['code'] == 200) {
  54. return $result['data'];
  55. }
  56. return false;
  57. }
  58. // 得到远程的addon 信息(我的应用列表)
  59. public function getMyAddonsInfo($pageNum, $numPerPage)
  60. {
  61. $accessToken = $this->getAccessToken();
  62. if (!$accessToken) {
  63. return false;
  64. }
  65. $url = $this->remoteUrl . $this->getAddonsListUrlKey ;
  66. $headerRequest = [
  67. 'access-token: '.$accessToken,
  68. ];
  69. $data = [
  70. 'pageNum' => $pageNum,
  71. 'numPerPage' => $numPerPage,
  72. ];
  73. list($responseHeader, $result) = $this->getCurlData($url, 'post', $headerRequest, $data, 30);
  74. if ($result['code'] == 200) {
  75. return $result['data'];
  76. }
  77. return false;
  78. }
  79. // 得到应用的详细信息。
  80. public function getAddonsInfoByNamespace($namespace)
  81. {
  82. $accessToken = $this->getAccessToken();
  83. if (!$accessToken) {
  84. return false;
  85. }
  86. $url = $this->remoteUrl . $this->getAddonInfoUrlKey ;
  87. $headerRequest = [
  88. 'access-token: '.$accessToken,
  89. ];
  90. $data = [
  91. 'namespace' => $namespace,
  92. ];
  93. list($responseHeader, $result) = $this->getCurlData($url, 'post', $headerRequest, $data, 30);
  94. if ($result['code'] == 200) {
  95. return $result['data'];
  96. }
  97. return false;
  98. }
  99. // 下载应用
  100. public function downloadAddons($namespace, $packageName, $folderName, $addonName)
  101. {
  102. // 得到下载的url
  103. $url = $this->remoteUrl . '/customer/addons/download?namespace='.$namespace;
  104. // 当前应用的package,进行mkdir,然后chomod 777
  105. $packagePath = Yii::getAlias('@addons/'.$packageName);
  106. if (!is_dir($packagePath)){
  107. mkdir($packagePath);
  108. chmod($packagePath, 0777);
  109. }
  110. // 应用文件夹
  111. $packagePath = Yii::getAlias('@addons/'.$packageName.'/'.$folderName);
  112. if (!is_dir($packagePath)){
  113. mkdir($packagePath);
  114. chmod($packagePath, 0777);
  115. }
  116. // 根据文件路径,以及addon的name,得到zip文件存放的文件完整路径
  117. $filePath = Yii::getAlias('@addons/'.$packageName.'/'.$folderName.'/'.$folderName.'.zip');
  118. // 将url中的zip文件,存储到该文件目录。
  119. if ($this->downCurl($url,$filePath)) {
  120. return $filePath;
  121. }
  122. return null;
  123. }
  124. // 远程下载zip包
  125. function downCurl($url, $filePath)
  126. {
  127. $accessToken = $this->getAccessToken();
  128. if (!$accessToken) {
  129. return false;
  130. }
  131. $headerRequest = [
  132. 'access-token: '.$accessToken,
  133. ];
  134. //初始化
  135. $ch = curl_init();
  136. curl_setopt($ch,
  137. CURLOPT_HTTPHEADER,
  138. $headerRequest
  139. );
  140. //echo $filePath;exit;
  141. //设置抓取的url
  142. curl_setopt($ch, CURLOPT_URL, $url);
  143. //打开文件描述符
  144. $fp = fopen ($filePath, 'w+');
  145. curl_setopt($ch, CURLOPT_FILE, $fp);
  146. //这个选项是意思是跳转,如果你访问的页面跳转到另一个页面,也会模拟访问。
  147. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  148. curl_setopt($ch,CURLOPT_TIMEOUT, 5000);
  149. //执行命令
  150. curl_exec($ch);
  151. //关闭URL请求
  152. curl_close($ch);
  153. //关闭文件描述符
  154. fclose($fp);
  155. return true;
  156. }
  157. /*
  158. public function downFile($url,$path){
  159. $arr=parse_url($url);
  160. $fileName=basename($arr['path']);
  161. $file=file_get_contents($url);
  162. file_put_contents($path.$fileName,$file);
  163. }
  164. */
  165. public function isLogin($checkRemote = false)
  166. {
  167. if ($checkRemote) {
  168. // 进行远程检查
  169. }
  170. if ($this->getAccessToken()) {
  171. return true;
  172. }
  173. return false;
  174. }
  175. public function setAccessToken($access_token)
  176. {
  177. if (!$access_token) {
  178. return false;
  179. }
  180. return Yii::$app->session->set(self::ADDONS_TOKEN, $access_token);
  181. }
  182. public function getAccessToken()
  183. {
  184. return Yii::$app->session->get(self::ADDONS_TOKEN);
  185. }
  186. public static function getCurlData($url,$type="get", $headerData, $data=array(),$timeout = 30){
  187. //对空格进行转义
  188. $url = str_replace(' ','+',$url);
  189. if($type == "get"){
  190. if(!empty($data) && is_array($data)){
  191. $arr = [];
  192. foreach($data as $k=>$v){
  193. $arr[] = $k."=".$v;
  194. }
  195. $str = implode("&",$arr);
  196. if(strstr($url,"?")){
  197. $url .= "&".$str;
  198. }else{
  199. $url .= "?".$str;
  200. }
  201. }
  202. }
  203. $data = json_encode($data);
  204. $headerRequest = [
  205. 'Accept: application/json',
  206. 'Content-Type: application/json',
  207. 'Content-Length: ' . strlen($data)
  208. ];
  209. $headerRequest = array_merge($headerRequest, $headerData);
  210. $url = urldecode($url);
  211. //echo $url ;exit;
  212. $ch = curl_init();
  213. //设置选项,包括URL
  214. curl_setopt($ch, CURLOPT_URL, "$url");
  215. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  216. //curl_setopt($ch, CURLOPT_HEADER, 0);
  217. curl_setopt($ch, CURLOPT_HEADER, true);
  218. curl_setopt($ch,CURLOPT_TIMEOUT,$timeout); //定义超时3秒钟
  219. if($type == "post"){
  220. // POST数据
  221. curl_setopt($ch, CURLOPT_POST, 1);
  222. curl_setopt($ch,
  223. CURLOPT_HTTPHEADER,
  224. $headerRequest
  225. );
  226. // 把post的变量加上
  227. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  228. }
  229. //执行并获取url地址的内容
  230. $output = curl_exec($ch);
  231. if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
  232. list($responseHeader, $body) = explode("\r\n\r\n", $output, 2);
  233. $headArr = explode("\r\n", $responseHeader);
  234. $responseHeaderArr = [];
  235. foreach ($headArr as $loop) {
  236. $arr = explode(': ', $loop);
  237. $responseHeaderArr[$arr[0]] = $arr[1];
  238. } //
  239. $reponseBody = json_decode($body, true);
  240. return [$responseHeaderArr, $reponseBody];
  241. }
  242. return ['', ''];
  243. }
  244. }