OptionRepository.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace app\common\repositories;
  3. use app\common\helpers\Cache;
  4. use app\common\models\Option;
  5. use app\common\models\UniAccount;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Database\QueryException;
  9. class OptionRepository extends Repository
  10. {
  11. public $group = [];
  12. /**
  13. * Create a new option repository.
  14. *
  15. * @return void
  16. */
  17. public function __construct()
  18. {
  19. try {
  20. if ($load_option = Cache::get('load_option')) {
  21. $this->group = $load_option['group'];
  22. $this->items = $load_option['items'];
  23. return;
  24. }
  25. if (\YunShop::app()->uniacid) {
  26. $options = Option::whereIn('uniacid', [0, \YunShop::app()->uniacid])->get();
  27. } else {
  28. $options = \app\common\modules\option\OptionRepository::all();
  29. }
  30. // 至少有一个公众号开启的插件列表
  31. foreach ($options as $option) {
  32. if ($this->items[$option['option_name']]['enabled'] == 1) {
  33. continue;
  34. }
  35. $this->items[$option['option_name']] = $option;
  36. }
  37. // 每个公众号开启的插件列表
  38. foreach ($options as $option) {
  39. $this->group[$option['uniacid']][$option['option_name']] = $option;
  40. }
  41. $this->group[0] = $this->group[0] ?: [];
  42. foreach ($this->group as $uniacid => &$uniacItems) {
  43. if (!empty($uniacid)) {
  44. // 合并全局和单公众号设置,单公众号设置优先
  45. $uniacItems = array_merge($this->group[0], $uniacItems);
  46. }
  47. }
  48. Cache::put('load_option',['group'=>$this->group,'items'=>$this->items],60);
  49. } catch (QueryException $e) {
  50. $this->items = [];
  51. }
  52. }
  53. public function uniacid($uniacid)
  54. {
  55. if (empty($uniacid)) {
  56. return $this->all();
  57. }
  58. return $this->group[$uniacid] ?: $this->group[0];
  59. }
  60. /**
  61. * Set a given option value.
  62. *
  63. * @param array|string $key
  64. * @param mixed $value
  65. * @return void
  66. */
  67. public function set($key, $value = null)
  68. {
  69. if (is_array($key)) {
  70. // If given key is an array
  71. foreach ($key as $innerKey => $innerValue) {
  72. Arr::set($this->items, $innerKey, $innerValue);
  73. $this->doSetOption($innerKey, $innerValue);
  74. }
  75. } else {
  76. Arr::set($this->items, $key, $value);
  77. $this->doSetOption($key, $value);
  78. }
  79. }
  80. /**
  81. * Do really save modified options to database.
  82. *
  83. * @return void
  84. */
  85. protected function doSetOption($key, $value)
  86. {
  87. try {
  88. if (!DB::table('yz_options')->where('option_name', $key)->first()) {
  89. $uniAccount = UniAccount::get();
  90. $pluginData = [];
  91. foreach ($uniAccount as $u) {
  92. $pluginData[] = [
  93. 'uniacid' => $u->uniacid,
  94. 'option_name' => $key,
  95. 'option_value' => $value
  96. ];
  97. }
  98. DB::table('yz_options')
  99. ->insert($pluginData);
  100. } else {
  101. DB::table('yz_options')
  102. ->where('option_name', $key)
  103. ->update(['option_value' => $value]);
  104. }
  105. \app\common\modules\option\OptionRepository::flush();
  106. } catch (QueryException $e) {
  107. return;
  108. }
  109. }
  110. /**
  111. * Do really save modified options to database.
  112. *
  113. * @return void
  114. * @deprecated
  115. */
  116. public function save()
  117. {
  118. $this->itemsModified = array_unique($this->itemsModified);
  119. try {
  120. foreach ($this->itemsModified as $key) {
  121. if (!DB::table('yz_options')->where('option_name', $key)->first()) {
  122. DB::table('yz_options')
  123. ->insert(['option_name' => $key, 'option_value' => $this[$key]]);
  124. } else {
  125. DB::table('yz_options')
  126. ->where('option_name', $key)
  127. ->update(['option_value' => $this[$key]]);
  128. }
  129. \app\common\modules\option\OptionRepository::flush();
  130. }
  131. // clear the list
  132. $this->itemsModified = [];
  133. } catch (QueryException $e) {
  134. return;
  135. }
  136. }
  137. /**
  138. * Prepend a value onto an array option value.
  139. *
  140. * @param string $key
  141. * @param mixed $value
  142. * @return void
  143. */
  144. public function prepend($key, $value)
  145. {
  146. $array = $this->get($key);
  147. array_unshift($array, $value);
  148. $this->set($key, $array);
  149. }
  150. /**
  151. * Return the options with key in the given array.
  152. *
  153. * @param array $array
  154. * @return array
  155. */
  156. public function only(array $array)
  157. {
  158. $result = [];
  159. foreach ($this->items as $key => $value) {
  160. if (in_array($key, $array)) {
  161. $result[$key] = $value;
  162. }
  163. }
  164. return $result;
  165. }
  166. /**
  167. * Save all modified options into database
  168. */
  169. public function __destruct()
  170. {
  171. $this->save();
  172. }
  173. public function editDisable($id)
  174. {
  175. $result = DB::table('yz_options')->where('id', $id)->delete();
  176. \app\common\modules\option\OptionRepository::flush();
  177. app('supervisor')->restart();
  178. return $result;
  179. }
  180. public function editEnabledById($id, $enabled)
  181. {
  182. $result = DB::table('yz_options')->where('id', $id)->update(['enabled' => $enabled]);
  183. \app\common\modules\option\OptionRepository::flush();
  184. return $result;
  185. }
  186. public function editTopShowById($id, $enabled)
  187. {
  188. $result = DB::table('yz_options')->where('uniacid', \YunShop::app()->uniacid)->where('id', $id)->update(['top_show' => $enabled]);
  189. \app\common\modules\option\OptionRepository::flush();
  190. return $result;
  191. }
  192. public function insertPlugin($pluginData)
  193. {
  194. $result = DB::table('yz_options')->insert($pluginData);
  195. \app\common\modules\option\OptionRepository::flush();
  196. app('supervisor')->restart();
  197. return $result;
  198. }
  199. }