Helper.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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;
  10. use Yii;
  11. /**
  12. * Helper service.
  13. *
  14. * @property \fecshop\services\helper\Appapi $appapi
  15. * @property \fecshop\services\helper\Appserver $appserver appserver sub-service of helper service
  16. * @property \fecshop\services\helper\AR $ar
  17. * @property \fecshop\services\helper\Captcha $captcha
  18. * @property \fecshop\services\helper\Country $country
  19. * @property \fecshop\services\helper\Echart $echart
  20. * @property \fecshop\services\helper\ErrorHandler $errorHandler
  21. * @property \fecshop\services\helper\Errors $errors errors sub-service of helper service
  22. * @property \fecshop\services\helper\Format $format
  23. * @property \fecshop\services\helper\MobileDetect $mobileDetect
  24. * @author Terry Zhao <2358269014@qq.com>
  25. * @since 1.0
  26. */
  27. class Helper extends Service
  28. {
  29. protected $_app_name;
  30. protected $_param;
  31. /**
  32. * 得到当前的app入口的名字,譬如 appfront apphtml5 appserver等.
  33. */
  34. public function getAppName()
  35. {
  36. return Yii::$app->params['appName'];
  37. }
  38. /**
  39. * @param $var | String Or Array 需要进行Html::encode()操作的变量。
  40. * @return $var | String Or Array 去除xss攻击字符后的变量
  41. */
  42. public function htmlEncode($var)
  43. {
  44. if (is_array($var) && !empty($var)) {
  45. foreach ($var as $k=>$v) {
  46. if (is_array($v) && !empty($v)) {
  47. $var[$k] = $this->htmlEncode($v);
  48. } elseif (empty($v)) {
  49. $var[$k] = $v;
  50. } else {
  51. if (is_string($v)) {
  52. $var[$k] = \yii\helpers\Html::encode($v);
  53. }
  54. }
  55. }
  56. } elseif (empty($var)) {
  57. } else {
  58. if (is_string($var)) {
  59. $var = \yii\helpers\Html::encode($var);
  60. }
  61. }
  62. return $var;
  63. }
  64. /**
  65. * @param $domain | String vue类型的appserver传递的domain
  66. * 这个是appservice发送邮件,在邮件里面的url链接地址,在这里保存
  67. */
  68. public function setAppServiceDomain($domain)
  69. {
  70. $this->_param['appServiceDomain'] = $domain;
  71. return true;
  72. }
  73. public function getAppServiceDomain()
  74. {
  75. return isset($this->_param['appServiceDomain']) ? $this->_param['appServiceDomain'] : false;
  76. }
  77. /**
  78. * 该端口是否是Api入口,譬如appserver appapi等,都是属于api的入口
  79. * api入口都会将 Yii::$app->user->enableSession 关闭,因此通过该值判断, 是否是Api App
  80. *
  81. */
  82. public function isApiApp()
  83. {
  84. if (\Yii::$service->store->isApiStore() == true) {
  85. return true;
  86. } else {
  87. return false;
  88. }
  89. }
  90. public function getCustomerIp()
  91. {
  92. return Yii::$app->request->userIP;
  93. }
  94. function createNoncestr( $length = 32 ){
  95. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  96. $str ="";
  97. for ( $i = 0; $i < $length; $i++ ) {
  98. $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
  99. }
  100. return $str;
  101. }
  102. // 递归删除文件夹以及里面的所有的子文件夹和子文件
  103. public function deleteDir($path) {
  104. if (is_dir($path)) {
  105. //扫描一个目录内的所有目录和文件并返回数组
  106. $dirs = scandir($path);
  107. foreach ($dirs as $dir) {
  108. //排除目录中的当前目录(.)和上一级目录(..)
  109. if ($dir != '.' && $dir != '..') {
  110. //如果是目录则递归子目录,继续操作
  111. $sonDir = $path.'/'.$dir;
  112. if (is_dir($sonDir)) {
  113. //递归删除
  114. $this->deleteDir($sonDir);
  115. //目录内的子目录和文件删除后删除空目录
  116. @rmdir($sonDir);
  117. } else {
  118. //如果是文件直接删除
  119. @unlink($sonDir);
  120. }
  121. }
  122. }
  123. @rmdir($path);
  124. }
  125. return true;
  126. }
  127. /**
  128. * 图片文件复制,注意,如果某个文件不是图片类型,则不会被复制(仅仅复制图片)
  129. * 文件夹图片文件拷贝, 如果文件存在,则会被强制覆盖。
  130. * @param string $sourcePath 来源文件夹
  131. * @param string $targetPath 目的地文件夹
  132. * @param boolean $isForce 是否强制复制
  133. * @return bool
  134. */
  135. public function copyDirImage($sourcePath, $targetPath, $isForce = true)
  136. {
  137. if (empty($sourcePath) || empty($targetPath))
  138. {
  139. return false;
  140. }
  141. $dir = opendir($sourcePath);
  142. $this->dir_mkdir($targetPath);
  143. while (false !== ($file = readdir($dir)))
  144. {
  145. if (($file != '.') && ($file != '..'))
  146. {
  147. $sourcePathFile = $sourcePath . '/' . $file;
  148. $targetPathFile = $targetPath . '/' . $file;
  149. if (is_dir($sourcePathFile)){
  150. $this->copyDirImage($sourcePathFile, $targetPathFile);
  151. } else if (Yii::$service->image->isAllowImgType($sourcePathFile, $file)){
  152. if ($isForce) {
  153. copy($sourcePathFile, $targetPathFile);
  154. } else if (!file_exists($targetPathFile)) {
  155. copy($sourcePathFile, $targetPathFile);
  156. } else {
  157. Yii::$service->helper->errors->add('target path:' . $targetPathFile . ' is exist.');
  158. }
  159. } else {
  160. Yii::$service->helper->errors->add('file is not image:' . $sourcePathFile);
  161. }
  162. }
  163. }
  164. closedir($dir);
  165. return true;
  166. }
  167. /**
  168. * 文件夹文件拷贝
  169. *
  170. * @param string $sourcePath 来源文件夹
  171. * @param string $targetPath 目的地文件夹
  172. * @param boolean $isForce 是否强制复制
  173. * @return bool
  174. */
  175. public function copyDir($sourcePath, $targetPath, $isForce = true)
  176. {
  177. if (empty($sourcePath) || empty($targetPath))
  178. {
  179. return false;
  180. }
  181. $dir = opendir($sourcePath);
  182. $this->dir_mkdir($targetPath);
  183. while (false !== ($file = readdir($dir)))
  184. {
  185. if (($file != '.') && ($file != '..')) {
  186. $sourcePathFile = $sourcePath . '/' . $file;
  187. $targetPathFile = $targetPath . '/' . $file;
  188. if (is_dir( $sourcePathFile)) {
  189. $this->copyDir( $sourcePathFile, $targetPathFile);
  190. } else {
  191. //copy($sourcePath . '/' . $file, $targetPath . '/' . $file);
  192. if ($isForce) {
  193. copy($sourcePathFile, $targetPathFile);
  194. } else if (!file_exists($targetPathFile)) {
  195. copy($sourcePathFile, $targetPathFile);
  196. } else {
  197. Yii::$service->helper->errors->add('target path:' . $targetPathFile . ' is exist.');
  198. }
  199. }
  200. }
  201. }
  202. closedir($dir);
  203. return true;
  204. }
  205. /**
  206. * 创建文件夹
  207. *
  208. * @param string $path 文件夹路径
  209. * @param int $mode 访问权限
  210. * @param bool $recursive 是否递归创建
  211. * @return bool
  212. */
  213. public function dir_mkdir($path = '', $mode = 0777, $recursive = true)
  214. {
  215. clearstatcache();
  216. if (!is_dir($path))
  217. {
  218. mkdir($path, $mode, $recursive);
  219. return chmod($path, $mode);
  220. }
  221. return true;
  222. }
  223. public function scanAllDirSubFile($dir, $subDir='/')
  224. {
  225. if(is_dir($dir)){
  226. $files = array();
  227. $child_dirs = scandir($dir);
  228. foreach ($child_dirs as $child_dir){
  229. //'.'和'..'是Linux系统中的当前目录和上一级目录,必须排除掉,
  230. //否则会进入死循环,报segmentation falt 错误
  231. if($child_dir != '.' && $child_dir != '..'){
  232. if(is_dir($dir.'/'.$child_dir)){
  233. //$files[$child_dir] = my_scandir($dir.'/'.$child_dir);
  234. $files = array_merge($files, $this->scanAllDirSubFile($dir.'/'.$child_dir, $subDir.$child_dir.'/'));
  235. }else{
  236. $files[] = $subDir.$child_dir;
  237. }
  238. }
  239. }
  240. return $files;
  241. }else{
  242. return $subDir.$dir;
  243. }
  244. }
  245. }