PopularizePageShowController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2018/11/28
  6. * Time: 13:56
  7. */
  8. namespace app\backend\modules\member\controllers;
  9. use app\backend\modules\income\Income;
  10. use app\common\components\BaseController;
  11. use app\common\exceptions\ShopException;
  12. use app\common\facades\Setting;
  13. use app\common\helpers\Url;
  14. use app\common\models\Menu;
  15. use app\common\services\popularize\PopularizePageShowFactory;
  16. class PopularizePageShowController extends BaseController
  17. {
  18. /**
  19. * 加载模板
  20. * @return string
  21. * @throws \Throwable
  22. */
  23. public function index()
  24. {
  25. return view('member.popularize.index', [])->render();
  26. }
  27. //微信
  28. public function wechatSet()
  29. {
  30. $info = Setting::get("popularize.wechat");
  31. $set = request()->input('set');
  32. if (!empty($set)) {
  33. foreach ($set['plugin'] as $key => $value) {
  34. $set[$key] = $value;
  35. }
  36. unset($set['plugin']);
  37. if (Setting::set("popularize.wechat", $set)) {
  38. return $this->successJson('保存成功', ['data' => true]);
  39. } else {
  40. throw new ShopException('保存失败');
  41. }
  42. }
  43. return $this->successJson('ok', $this->getResult($info));
  44. }
  45. //微信小程序
  46. public function miniSet()
  47. {
  48. $info = Setting::get("popularize.mini");
  49. $set = request()->input('set');
  50. if (!empty($set)) {
  51. foreach ($set['plugin'] as $key => $value) {
  52. $set[$key] = $value;
  53. }
  54. unset($set['plugin']);
  55. if (Setting::set("popularize.mini", $set)) {
  56. return $this->successJson('保存成功', ['data' => true]);
  57. } else {
  58. throw new ShopException('保存失败');
  59. }
  60. }
  61. $info['min-app'] = 1;
  62. return $this->successJson('ok', $this->getResult($info, 1));
  63. }
  64. //手机浏览器 pc
  65. public function wapSet()
  66. {
  67. $info = Setting::get("popularize.wap");
  68. $set = request()->input('set');
  69. if (!empty($set)) {
  70. foreach ($set['plugin'] as $key => $value) {
  71. $set[$key] = $value;
  72. }
  73. unset($set['plugin']);
  74. if (Setting::set("popularize.wap", $set)) {
  75. return $this->successJson('保存成功', ['data' => true]);
  76. } else {
  77. throw new ShopException('保存失败');
  78. }
  79. }
  80. return $this->successJson('ok', $this->getResult($info));
  81. }
  82. //app
  83. public function appSet()
  84. {
  85. $info = Setting::get("popularize.app");
  86. $set = request()->input('set');
  87. if (!empty($set)) {
  88. foreach ($set['plugin'] as $key => $value) {
  89. $set[$key] = $value;
  90. }
  91. unset($set['plugin']);
  92. if (Setting::set("popularize.app", $set)) {
  93. return $this->successJson('保存成功', ['data' => true]);
  94. } else {
  95. throw new ShopException('保存失败');
  96. }
  97. }
  98. return $this->successJson('ok', $this->getResult($info));
  99. }
  100. //支付宝
  101. public function alipaySet()
  102. {
  103. $info = Setting::get("popularize.alipay");
  104. $set = request()->input('set');
  105. if (!empty($set)) {
  106. foreach ($set['plugin'] as $key => $value) {
  107. $set[$key] = $value;
  108. }
  109. unset($set['plugin']);
  110. if (Setting::set("popularize.alipay", $set)) {
  111. return $this->successJson('保存成功', ['data' => true]);
  112. } else {
  113. throw new ShopException('保存失败');
  114. }
  115. }
  116. return $this->successJson('ok', $this->getResult($info));
  117. }
  118. /**
  119. * 获取商城开启的插件
  120. * @return array 开启的插件页面路由与名称
  121. */
  122. protected function getData()
  123. {
  124. $lang_set = $this->getLangSet();
  125. $config = $this->getIncomePageConfig();
  126. $plugins = $this->getPlugins();
  127. foreach ($config as $key => $item) {
  128. $incomeFactory = new PopularizePageShowFactory(new $item['class'], $lang_set);
  129. if ($plugins[$incomeFactory->getMark()]) {
  130. $array[] = [
  131. 'url' => $plugins[$incomeFactory->getMark()],
  132. 'title' => $incomeFactory->getTitle(),
  133. 'mark' => $incomeFactory->getMark(),
  134. 'status' => is_array($plugins[$incomeFactory->getMark()]) ? 1 : 0,
  135. 'type' => $item['type']
  136. ];
  137. } else {
  138. $array[] = [
  139. 'url' => $incomeFactory->getAppUrl(),
  140. 'title' => $incomeFactory->getTitle(),
  141. 'mark' => $incomeFactory->getMark(),
  142. 'status' => 0,
  143. 'type' => $item['type']
  144. ];
  145. }
  146. }
  147. return $array;
  148. }
  149. //当收入页面显示的前端路由有两个时,需要在把俩个路由都添加进来
  150. // \Config::set(['popularize_page_show.收入页面类的getMark()方法返回的值' => 路由数组);
  151. protected function getPlugins()
  152. {
  153. $popularize_page_show = [
  154. 'area_dividend' => 'regionalAgencyCenter',
  155. 'store_cashier' => 'cashier',
  156. 'store_withdraw' => 'storeManage',
  157. 'hotel_cashier' => 'HotelCashier',
  158. 'hotel_withdraw' => 'HotelManage',
  159. 'merchant' => 'enterprise_index',
  160. 'micro' => 'microShop_ShopKeeperCenter',
  161. ];
  162. $plugin = \app\common\modules\shop\ShopConfig::current()->get('popularize_page_show') ?: [];
  163. return array_merge($popularize_page_show, $plugin);
  164. }
  165. /**
  166. * 生成js文件给前端用
  167. */
  168. protected function toJson()
  169. {
  170. //放弃使用这个方法,原因无法匹配多个公众号
  171. return;
  172. $all_set = Setting::getByGroup("popularize");
  173. $data = [
  174. 'wechat' => [
  175. 'vue_route' => !empty($all_set['wechat']['vue_route']) ? $all_set['wechat']['vue_route'] : [],
  176. 'url' => !empty($all_set['wechat']['callback_url']) ? $all_set['wechat']['callback_url'] : '',
  177. ],
  178. 'mini' => [
  179. 'vue_route' => !empty($all_set['mini']['vue_route']) ? $all_set['mini']['vue_route'] : [],
  180. 'url' => !empty($all_set['mini']['callback_url']) ? $all_set['mini']['callback_url'] : '',
  181. ],
  182. 'wap' => [
  183. 'vue_route' => !empty($all_set['wap']['vue_route']) ? $all_set['wap']['vue_route'] : [],
  184. 'url' => !empty($all_set['wap']['callback_url']) ? $all_set['wap']['callback_url'] : '',
  185. ],
  186. 'app' => [
  187. 'vue_route' => !empty($all_set['app']['vue_route']) ? $all_set['app']['vue_route'] : [],
  188. 'url' => !empty($all_set['app']['callback_url']) ? $all_set['app']['callback_url'] : '',
  189. ],
  190. 'alipay' => [
  191. 'vue_route' => !empty($all_set['alipay']['vue_route']) ? $all_set['alipay']['vue_route'] : [],
  192. 'url' => !empty($all_set['alipay']['callback_url']) ? $all_set['alipay']['callback_url'] : '',
  193. ],
  194. ];
  195. $string = json_encode($data);
  196. $sj = date('Y-m-d H:i:s', time());
  197. $json_str = <<<json
  198. //update $sj
  199. let popularize = {$string};
  200. if (typeof define === "function") {
  201. define(popularize)
  202. } else {
  203. window.\$popularize = popularize;
  204. }
  205. json;
  206. $path = 'static' . DIRECTORY_SEPARATOR . 'yunshop' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'popularize' . DIRECTORY_SEPARATOR;
  207. $absolute_file = $path . 'popularize_' . \YunShop::app()->uniacid . '.js';
  208. // 生成目录
  209. if (!is_dir(base_path($path))) {
  210. mkdir(base_path($path), 0777);
  211. }
  212. return file_put_contents(base_path($absolute_file), $json_str);
  213. }
  214. /**
  215. * 收入页面配置 config
  216. *
  217. * @return mixed
  218. */
  219. private function getIncomePageConfig()
  220. {
  221. return Income::current()->getPageItems();
  222. }
  223. /**
  224. * 获取商城中的插件名称自定义设置
  225. *
  226. * @return mixed
  227. */
  228. private function getLangSet()
  229. {
  230. $lang = \Setting::get('shop.lang', ['lang' => 'zh_cn']);
  231. return $lang[$lang['lang']];
  232. }
  233. private function getItems()//获取所有插件
  234. {
  235. $config = \app\backend\modules\income\Income::current()->getPageItems();//获取所有插件
  236. $data = [];
  237. foreach ($config as $key => $item) {
  238. $incomeFactory = new $item['class'];
  239. $data[$key]['value'] = $incomeFactory->getMark();//获取收类型的名称
  240. $data[$key]['name'] = $incomeFactory->getTitle();
  241. }
  242. return $data;
  243. }
  244. public function getPluginType()
  245. {
  246. return [
  247. 'dividend' => '入口类',
  248. 'industry' => '行业类',
  249. 'marketing' => '营销类',
  250. 'business_management' => '企业管理类',
  251. 'tool' => '工具类',
  252. 'recharge' => '生活充值',
  253. 'api' => '接口类',
  254. 'store' => '门店应用类',
  255. 'blockchain' => '区块链类',
  256. ];
  257. }
  258. public function setArray($info)
  259. {
  260. $array = $this->getData();
  261. foreach ($array as $value) {
  262. if ($value['type'] == 'dividend') {
  263. $dividend[] = [
  264. 'url' => $value['url'],
  265. 'name' => $value['title'],
  266. 'type' => $value['type'],
  267. 'status' => in_array($value['url'], $info['vue_route']) ? 1 : 0,
  268. 'mark' => $value['mark']
  269. ];
  270. }
  271. if ($value['type'] == 'industry') {
  272. $industry[] = [
  273. 'url' => $value['url'],
  274. 'name' => $value['title'],
  275. 'type' => $value['type'],
  276. 'status' => in_array($value['url'], $info['vue_route']) ? 1 : 0,
  277. 'mark' => $value['mark']
  278. ];
  279. }
  280. if ($value['type'] == 'marketing') {
  281. $marketing[] = [
  282. 'url' => $value['url'],
  283. 'name' => $value['title'],
  284. 'type' => $value['type'],
  285. 'status' => in_array($value['url'], $info['vue_route']) ? 1 : 0,
  286. 'mark' => $value['mark']
  287. ];
  288. }
  289. if ($value['type'] == 'business_management') {
  290. $business_management[] = [
  291. 'url' => $value['url'],
  292. 'name' => $value['title'],
  293. 'type' => $value['type'],
  294. 'status' => in_array($value['url'], $info['vue_route']) ? 1 : 0,
  295. 'mark' => $value['mark']
  296. ];
  297. }
  298. if ($value['type'] == 'tool') {
  299. $tool[] = [
  300. 'url' => $value['url'],
  301. 'name' => $value['title'],
  302. 'type' => $value['type'],
  303. 'status' => in_array($value['url'], $info['vue_route']) ? 1 : 0,
  304. 'mark' => $value['mark']
  305. ];
  306. }
  307. if ($value['type'] == 'recharge') {
  308. $recharge[] = [
  309. 'url' => $value['url'],
  310. 'name' => $value['title'],
  311. 'type' => $value['type'],
  312. 'status' => in_array($value['url'], $info['vue_route']) ? 1 : 0,
  313. 'mark' => $value['mark']
  314. ];
  315. }
  316. if ($value['type'] == 'api') {
  317. $api[] = [
  318. 'url' => $value['url'],
  319. 'name' => $value['title'],
  320. 'type' => $value['type'],
  321. 'status' => in_array($value['url'], $info['vue_route']) ? 1 : 0,
  322. 'mark' => $value['mark']
  323. ];
  324. }
  325. if ($value['type'] == 'store') {
  326. $store[] = [
  327. 'url' => $value['url'],
  328. 'name' => $value['title'],
  329. 'type' => $value['type'],
  330. 'status' => in_array($value['url'], $info['vue_route']) ? 1 : 0,
  331. 'mark' => $value['mark']
  332. ];
  333. }
  334. if ($value['type'] == 'blockchain') {
  335. $blockchain[] = [
  336. 'url' => $value['url'],
  337. 'name' => $value['title'],
  338. 'type' => $value['type'],
  339. 'status' => in_array($value['url'], $info['vue_route']) ? 1 : 0,
  340. 'mark' => $value['mark']
  341. ];
  342. }
  343. }
  344. $not_plugin = [
  345. 'dividend' => $dividend,
  346. 'industry' => $industry,
  347. 'marketing' => $marketing,
  348. 'business_management' => $business_management,
  349. 'tool' => $tool,
  350. 'recharge' => $recharge,
  351. 'api' => $api,
  352. 'store' => $store,
  353. 'blockchain' => $blockchain
  354. ];
  355. return $not_plugin;
  356. }
  357. public function getResult($info, $item = 0)
  358. {
  359. $not_plugin = $this->setArray($info);
  360. $array1 = $this->getItems();
  361. foreach ($array1 as $v) {
  362. $plugin[] = [
  363. 'status' => !empty($info[$v['value']]) ? $info[$v['value']] : 0,
  364. 'name' => $v['name'],
  365. 'value' => $v['value'],
  366. ];
  367. }
  368. $basics = [
  369. 'background_color' => $info['background_color'],
  370. 'callback_url' => $info['callback_url'],
  371. 'is_show_unable' => $info['is_show_unable'],
  372. 'show_member_id' => $info['show_member_id'],
  373. 'extension' => in_array('extension', $info['vue_route']) ? 1 : 0,
  374. 'not_plugin' => $not_plugin,
  375. 'plugin_type' => $this->getPluginType()
  376. ];
  377. if ($item == 1) {
  378. $basics['small_extension_link'] = $info['small_extension_link'];
  379. }
  380. return [
  381. 'plugin' => $plugin,
  382. 'basics' => $basics,
  383. ];
  384. }
  385. }