Newsletter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\customer;
  10. use fecshop\services\Service;
  11. use Yii;
  12. /**
  13. * Newsletter sub service of customer.
  14. * @method subscribe($email, $isRegister = true) see [[\fecshop\services\customer\Newsletter::actionSubscribe()]]
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class Newsletter extends Service
  19. {
  20. /**
  21. * $storagePrex , $storage , $storagePath 为找到当前的storage而设置的配置参数
  22. * 可以在配置中更改,更改后,就会通过容器注入的方式修改相应的配置值
  23. */
  24. public $storage; // = 'NewsletterMysqldb'; // NewsletterMysqldb | NewsletterMongodb 当前的storage,如果在config中配置,那么在初始化的时候会被注入修改
  25. /**
  26. * 设置storage的path路径,
  27. * 如果不设置,则系统使用默认路径
  28. * 如果设置了路径,则使用自定义的路径
  29. */
  30. public $storagePath = '';
  31. protected $_newsletter;
  32. public function init()
  33. {
  34. parent::init();
  35. // 从数据库配置中得到值, 设置成当前service存储,是Mysqldb 还是 Mongodb
  36. $config = Yii::$app->store->get('service_db', 'newsletter');
  37. $this->storage = 'NewsletterMysqldb';
  38. if ($config == Yii::$app->store->serviceMongodbName) {
  39. $this->storage = 'NewsletterMongodb';
  40. }
  41. $currentService = $this->getStorageService($this);
  42. $this->_newsletter = new $currentService();
  43. }
  44. // 动态更改为mongodb model
  45. public function changeToMongoStorage()
  46. {
  47. $this->storage = 'ReviewMongodb';
  48. $currentService = $this->getStorageService($this);
  49. $this->_newsletter = new $currentService();
  50. }
  51. // 动态更改为mongodb model
  52. public function changeToMysqlStorage()
  53. {
  54. $this->storage = 'ReviewMysqldb';
  55. $currentService = $this->getStorageService($this);
  56. $this->_newsletter = new $currentService();
  57. }
  58. public function getPrimaryKey()
  59. {
  60. return $this->_newsletter->getPrimaryKey();
  61. }
  62. public function getByPrimaryKey($primaryKey)
  63. {
  64. return $this->_newsletter->getByPrimaryKey($primaryKey);
  65. }
  66. /*
  67. * example filter:
  68. * [
  69. * 'numPerPage' => 20,
  70. * 'pageNum' => 1,
  71. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  72. 'where' => [
  73. ['>','price',1],
  74. ['<=','price',10]
  75. * ['sku' => 'uk10001'],
  76. * ],
  77. * 'asArray' => true,
  78. * ]
  79. */
  80. public function coll($filter = '')
  81. {
  82. return $this->_newsletter->coll($filter);
  83. }
  84. /**
  85. * @param $emailAddress | String
  86. * @return bool
  87. * 检查邮件是否之前被订阅过
  88. */
  89. public function emailIsExist($emailAddress)
  90. {
  91. return $this->_newsletter->emailIsExist($emailAddress);
  92. }
  93. /**
  94. * @param $emailAddress | String
  95. * @return bool
  96. * 订阅邮件
  97. */
  98. public function subscribe($emailAddress, $isRegister = false)
  99. {
  100. return $this->_newsletter->subscribe($emailAddress, $isRegister);
  101. }
  102. }