BaseGoodsWidget.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/9/8
  8. * Time: 14:13
  9. */
  10. namespace app\backend\modules\goods\widget;
  11. use app\common\helpers\Url;
  12. use app\common\models\Goods;
  13. use app\framework\Http\Request;
  14. use Illuminate\Contracts\Support\Arrayable;
  15. /**
  16. * Class BaseGoodsWidget
  17. * @package app\backend\modules\goods\widget
  18. * @property Goods goods
  19. */
  20. abstract class BaseGoodsWidget implements Arrayable
  21. {
  22. // private $module = [
  23. // 'base' => '商品信息',
  24. // 'tool' => '商品工具',
  25. // 'marketing' => '营销设置',
  26. // 'profit' => '分润设置',
  27. // 'industry' => '行业设置'
  28. // ];
  29. private $route; //路由
  30. protected $goods;
  31. protected $whiteList = []; //挂件白名单
  32. protected $blackList = []; //挂件黑名单
  33. public $title; //挂件名称
  34. public $group; //挂件所属分类
  35. public $code; //挂件唯一标识需与组件文件名称保持一致
  36. public $widget_key = ''; //挂件数据集合键名
  37. protected $request;
  38. public function __construct($goods)
  39. {
  40. $this->goods = $goods;
  41. }
  42. public function setTitle($title)
  43. {
  44. $this->title = $title;
  45. }
  46. public function getTitle()
  47. {
  48. return $this->title;
  49. }
  50. protected function setRoute($route)
  51. {
  52. $this->route = $route;
  53. }
  54. public function getRoute()
  55. {
  56. return $this->route;
  57. }
  58. public function getGroup()
  59. {
  60. return $this->group;
  61. }
  62. public function getWidgetKey()
  63. {
  64. return $this->widget_key;
  65. }
  66. //filter
  67. public function insideAuthorization($route)
  68. {
  69. $this->setRoute($route);
  70. //plugin white list filter
  71. if ($this->isAllow($route)) {
  72. return true;
  73. }
  74. //plugin black list filter
  75. if ($this->isBarred($route)) {
  76. return false;
  77. }
  78. return $this->usable();
  79. }
  80. /**
  81. * @param $route string
  82. * @return bool
  83. */
  84. protected function isAllow($route)
  85. {
  86. return $this->whiteList() && in_array($route, $this->whiteList());
  87. }
  88. /**
  89. * @param $route string
  90. * @return bool
  91. */
  92. protected function isBarred($route)
  93. {
  94. return in_array($route, $this->blackList()) || $this->blackList() == ['*'];
  95. }
  96. protected function whiteList()
  97. {
  98. return $this->whiteList;
  99. }
  100. protected function blackList()
  101. {
  102. return $this->blackList;
  103. }
  104. protected function getCode()
  105. {
  106. return $this->code;
  107. }
  108. /**
  109. * Convert the model instance to an array.
  110. *
  111. * @return array
  112. */
  113. public function toArray()
  114. {
  115. return [
  116. 'group' =>$this->getGroup(), //模板分组
  117. 'title' =>$this->getTitle(), //模板名称
  118. 'attr_hide' => $this->attrHide(),
  119. 'template_code' =>$this->getCode(), //模板名称
  120. 'page_path' => $this->pagePath(), //模板引入路径
  121. 'widget_key' => $this->getWidgetKey(), //模板数据提交键名
  122. 'data' => $this->getWidgetData(), //模板数据
  123. ];
  124. }
  125. public function attrHide()
  126. {
  127. return [];
  128. }
  129. public function getWidgetData()
  130. {
  131. if ($this->defaultValuePlugin()) {
  132. $config = \Yunshop\GoodsDefaultValue\common\WidgetConfig::appointWidget($this->getWidgetKey());
  133. if ($config) {
  134. /**
  135. * @var BaseGoodsWidget $widgetClass
  136. */
  137. $widgetClass = new $config['class']($this->goods);
  138. $widgetClass->setRoute($this->getRoute());
  139. $widgetClass->setTitle($config['title']);
  140. $widgetClass->setRequest($this->getRequest());
  141. if ($widgetClass->usable()) {
  142. return $widgetClass->getData();
  143. }
  144. }
  145. }
  146. return $this->getData();
  147. }
  148. protected function defaultValuePlugin()
  149. {
  150. //是否新添加商品
  151. if ($this->goods || !app('plugins')->isEnabled('goods-default-value')) {
  152. return false;
  153. }
  154. return \Yunshop\GoodsDefaultValue\common\EditPageWidget::isOpen($this->getRoute(), $this->getWidgetKey());
  155. }
  156. protected function getPath($path)
  157. {
  158. return Url::shopUrl($path);
  159. }
  160. /**
  161. * @param $request
  162. */
  163. public function setRequest($request)
  164. {
  165. if (is_null($request)) {
  166. $this->request = request();
  167. }
  168. $this->request = $request;
  169. }
  170. /**
  171. * 获取request对象
  172. * @return Request
  173. */
  174. protected function getRequest($key = null)
  175. {
  176. if (!isset($this->request)) {
  177. $this->request = request();
  178. }
  179. if ($key) {
  180. return $this->request->input($key, '');
  181. }
  182. return $this->request;
  183. }
  184. /**
  185. * 权限判断
  186. * @return boolean
  187. */
  188. public function usable()
  189. {
  190. return true;
  191. }
  192. abstract public function getData();
  193. //挂件页面路径给前端引入
  194. abstract public function pagePath();
  195. //插件文件名称
  196. abstract public function pluginFileName();
  197. }