Helper.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. }