Diy.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\diy;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\activity\bargain\StoreBargainServices;
  14. use app\services\activity\combination\StoreCombinationServices;
  15. use app\services\activity\seckill\StoreSeckillServices;
  16. use app\services\article\ArticleServices;
  17. use app\services\diy\DiyServices;
  18. use app\services\other\CacheServices;
  19. use app\services\product\product\StoreCategoryServices;
  20. use app\services\product\product\StoreProductServices;
  21. use crmeb\exceptions\AdminException;
  22. use think\facade\App;
  23. /**
  24. *
  25. * Class Diy
  26. * @package app\controller\admin\v1\diy
  27. */
  28. class Diy extends AuthController
  29. {
  30. /**
  31. * @param App $app
  32. * @param DiyServices $services
  33. */
  34. public function __construct(App $app, DiyServices $services)
  35. {
  36. parent::__construct($app);
  37. $this->services = $services;
  38. }
  39. /**
  40. * DIY列表
  41. * @return mixed
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function getList()
  47. {
  48. $where = $this->request->getMore([
  49. ['status', ''],
  50. ['type', 0],
  51. ['name', ''],
  52. ['version', ''],
  53. ]);
  54. $where['type'] = -1;
  55. $data = $this->services->getDiyList($where);
  56. return app('json')->success($data);
  57. }
  58. /**
  59. * 保存可视化编辑资源
  60. * @param int $id
  61. * @return mixed
  62. */
  63. public function saveData(int $id = 0)
  64. {
  65. $data = $this->request->postMore([
  66. ['value', ''],
  67. ]);
  68. $value_config = ['seckill', 'bargain', 'combination', 'goodList'];
  69. $value = is_string($data['value']) ? json_decode($data['value'], true) : $data['value'];
  70. foreach ($value as $key => &$val) {
  71. if (in_array($key, $value_config) && is_array($val)) {
  72. foreach ($val as $k => &$v) {
  73. if (isset($v['selectConfig']['list']) && $v['selectConfig']['list']) {
  74. $v['selectConfig']['list'] = [];
  75. }
  76. if (isset($v['goodsList']['list']) && $v['goodsList']['list'] && $v['tabConfig']['tabVal'] == 1) {
  77. $limitMax = config('database.page.limitMax', 50);
  78. if (count($v['goodsList']['list']) > $limitMax) {
  79. return app('json')->fail(400350);
  80. }
  81. $v['ids'] = array_column($v['goodsList']['list'], 'id');
  82. $v['goodsList']['list'] = [];
  83. }
  84. }
  85. }
  86. }
  87. $data['value'] = json_encode($value);
  88. $data['version'] = uniqid();
  89. $this->services->saveData($id, $data);
  90. return app('json')->success(100000);
  91. }
  92. /**
  93. * 保存Diy资源
  94. * @param int $id
  95. * @return mixed
  96. */
  97. public function saveDiyData(int $id = 0)
  98. {
  99. $data = $this->request->postMore([
  100. ['name', ''],
  101. ['title', ''],
  102. ['value', ''],
  103. ['type', ''],
  104. ['cover_image', ''],
  105. ['is_show', 0],
  106. ['is_bg_color', 0],
  107. ['is_bg_pic', 0],
  108. ['bg_tab_val', 0],
  109. ['color_picker', ''],
  110. ['bg_pic', ''],
  111. ]);
  112. $value = is_string($data['value']) ? json_decode($data['value'], true) : $data['value'];
  113. $infoDiy = $id ? $this->services->get($id, ['is_diy']) : [];
  114. if ($infoDiy && $infoDiy['is_diy']) {
  115. foreach ($value as $key => &$item) {
  116. if ($item['name'] === 'goodList') {
  117. if (isset($item['selectConfig']['list'])) {
  118. unset($item['selectConfig']['list']);
  119. }
  120. if (isset($item['goodsList']['list']) && is_array($item['goodsList']['list'])) {
  121. $limitMax = config('database.page.limitMax', 50);
  122. if (isset($item['numConfig']['val']) && isset($item['tabConfig']['tabVal']) && $item['tabConfig']['tabVal'] == 0 && $item['numConfig']['val'] > $limitMax) {
  123. return app('json')->fail(400350);
  124. }
  125. $item['goodsList']['ids'] = array_column($item['goodsList']['list'], 'id');
  126. unset($item['goodsList']['list']);
  127. }
  128. } elseif ($item['name'] === 'articleList') {
  129. if (isset($item['selectList']['list']) && is_array($item['selectList']['list'])) {
  130. unset($item['selectList']['list']);
  131. }
  132. } elseif ($item['name'] === 'promotionList') {
  133. unset($item['productList']['list']);
  134. }
  135. }
  136. $data['value'] = json_encode($value);
  137. } else {
  138. if (isset($value['d_goodList']['selectConfig']['list'])) {
  139. unset($value['d_goodList']['selectConfig']['list']);
  140. } elseif (isset($value['d_goodList']['goodsList']['list'])) {
  141. $limitMax = config('database.page.limitMax', 50);
  142. if (isset($value['d_goodList']['numConfig']['val']) && isset($value['d_goodList']['tabConfig']['tabVal']) && $value['d_goodList']['tabConfig']['tabVal'] == 0 && $value['d_goodList']['numConfig']['val'] > $limitMax) {
  143. return app('json')->fail(400350);
  144. }
  145. $value['d_goodList']['goodsList']['ids'] = array_column($value['d_goodList']['goodsList']['list'], 'id');
  146. unset($value['d_goodList']['goodsList']['list']);
  147. } elseif (isset($value['k_newProduct']['goodsList']['list'])) {
  148. $list = [];
  149. foreach ($value['k_newProduct']['goodsList']['list'] as $item) {
  150. $list[] = [
  151. 'image' => $item['image'],
  152. 'store_info' => $item['store_info'],
  153. 'store_name' => $item['store_name'],
  154. 'id' => $item['id'],
  155. 'price' => $item['price'],
  156. 'ot_price' => $item['ot_price'],
  157. ];
  158. }
  159. $value['k_newProduct']['goodsList']['list'] = $list;
  160. } elseif (isset($value['selectList']['list']) && is_array($value['selectList']['list'])) {
  161. unset($value['goodsList']['list']);
  162. }
  163. $data['value'] = json_encode($value);
  164. }
  165. $data['version'] = '1.0';
  166. $data['type'] = 2;
  167. $data['is_diy'] = 1;
  168. $data['version'] = uniqid();
  169. return app('json')->success($id ? 100001 : 100000, ['id' => $this->services->saveData($id, $data)]);
  170. }
  171. /**
  172. * 删除模板
  173. * @param $id
  174. * @return mixed
  175. */
  176. public function del($id)
  177. {
  178. $this->services->del($id);
  179. return app('json')->success(100002);
  180. }
  181. /**
  182. * 使用模板
  183. * @param $id
  184. * @return mixed
  185. */
  186. public function setStatus($id)
  187. {
  188. $this->services->setStatus($id);
  189. return app('json')->success(100014);
  190. }
  191. /**
  192. * 获取一条数据
  193. * @param int $id
  194. * @param StoreProductServices $services
  195. * @param StoreSeckillServices $seckillServices
  196. * @param StoreCombinationServices $combinationServices
  197. * @param StoreBargainServices $bargainServices
  198. * @return mixed
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\DbException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. */
  203. public function getInfo(int $id, StoreProductServices $services, StoreSeckillServices $seckillServices, StoreCombinationServices $combinationServices, StoreBargainServices $bargainServices)
  204. {
  205. if (!$id) throw new AdminException(100100);
  206. $info = $this->services->get($id);
  207. if ($info) {
  208. $info = $info->toArray();
  209. } else {
  210. throw new AdminException(400351);
  211. }
  212. if (!$info['value']) return app('json')->success(compact('info'));
  213. $info['value'] = json_decode($info['value'], true);
  214. $value_config = ['seckill', 'bargain', 'combination', 'goodList'];
  215. foreach ($info['value'] as $key => &$val) {
  216. if (in_array($key, $value_config) && is_array($val)) {
  217. if ($key == 'goodList') {
  218. foreach ($val as $k => &$v) {
  219. if (isset($v['ids']) && $v['ids'] && $v['tabConfig']['tabVal'] == 1) {
  220. $v['goodsList']['list'] = $services->getSearchList(['ids' => $v['ids']]);
  221. }
  222. }
  223. }
  224. if ($key == "seckill") {
  225. foreach ($val as $k => &$v) {
  226. if (isset($v['ids']) && $v['ids'] && $v['tabConfig']['tabVal'] == 1) {
  227. $v['goodsList']['list'] = $seckillServices->getDiySeckillList(['ids' => $v['ids']])['list'];
  228. }
  229. }
  230. }
  231. if ($key == "bargain") {
  232. foreach ($val as $k => &$v) {
  233. if (isset($v['ids']) && $v['ids'] && $v['tabConfig']['tabVal'] == 1) {
  234. $v['goodsList']['list'] = $bargainServices->getHomeList(['ids' => $v['ids']])['list'];
  235. }
  236. }
  237. }
  238. if ($key == "combination") {
  239. foreach ($val as $k => &$v) {
  240. if (isset($v['ids']) && $v['ids'] && $v['tabConfig']['tabVal'] == 1) {
  241. $v['goodsList']['list'] = $combinationServices->getHomeList(['ids' => $v['ids']])['list'];
  242. }
  243. }
  244. }
  245. }
  246. }
  247. return app('json')->success(compact('info'));
  248. }
  249. /**
  250. * 获取diy数据
  251. * @param $id
  252. * @param StoreProductServices $services
  253. * @return mixed
  254. * @throws \think\db\exception\DataNotFoundException
  255. * @throws \think\db\exception\DbException
  256. * @throws \think\db\exception\ModelNotFoundException
  257. */
  258. public function getDiyInfo($id, StoreProductServices $services)
  259. {
  260. if (!$id) throw new AdminException(100100);
  261. $info = $this->services->get($id);
  262. if ($info) {
  263. $info = $info->toArray();
  264. } else {
  265. throw new AdminException(400351);
  266. }
  267. $info['value'] = json_decode($info['value'], true);
  268. if ($info['value']) {
  269. /** @var ArticleServices $articleServices */
  270. $articleServices = app()->make(ArticleServices::class);
  271. if ($info['is_diy']) {
  272. foreach ($info['value'] as &$item) {
  273. if ($item['name'] === 'goodList') {
  274. if (isset($item['goodsList']['ids']) && count($item['goodsList']['ids'])) {
  275. $item['goodsList']['list'] = $services->getSearchList(['ids' => $item['goodsList']['ids']]);
  276. } else {
  277. $item['goodsList']['list'] = [];
  278. }
  279. } elseif ($item['name'] === 'articleList') {//文章
  280. $data = [];
  281. if ($item['selectConfig']['activeValue'] ?? 0) {
  282. $data = $articleServices->getList(['cid' => $item['selectConfig']['activeValue'] ?? 0], 0, $item['numConfig']['val'] ?? 0);
  283. }
  284. $item['selectList']['list'] = $data['list'] ?? [];
  285. } elseif ($item['name'] === 'promotionList') {//活动模仿
  286. $data = [];
  287. if (isset($item['tabConfig']['tabCur']) && $typeArr = $item['tabConfig']['list'][$item['tabConfig']['tabCur']] ?? []) {
  288. $val = $typeArr['link']['activeVal'] ?? 0;
  289. if ($val) {
  290. $data = $this->get_groom_list($val, (int)($item['numConfig']['val'] ?? 0));
  291. }
  292. }
  293. $item['productList']['list'] = $data;
  294. }
  295. }
  296. } else {
  297. if (isset($info['value']['d_goodList']['goodsList'])) {
  298. $info['value']['d_goodList']['goodsList']['list'] = [];
  299. }
  300. if (isset($info['value']['d_goodList']['goodsList']['ids']) && count($info['value']['d_goodList']['goodsList']['ids'])) {
  301. $info['value']['d_goodList']['goodsList']['list'] = $services->getSearchList(['ids' => $info['value']['d_goodList']['goodsList']['ids']]);
  302. }
  303. }
  304. }
  305. return app('json')->success(compact('info'));
  306. }
  307. /**
  308. * 获取推荐商品
  309. * @param $type
  310. * @param int $num
  311. * @return array|array[]
  312. * @throws \think\db\exception\DataNotFoundException
  313. * @throws \think\db\exception\DbException
  314. * @throws \think\db\exception\ModelNotFoundException
  315. */
  316. protected function get_groom_list($type, int $num = 0)
  317. {
  318. /** @var StoreProductServices $services */
  319. $services = app()->make(StoreProductServices::class);
  320. $info = [];
  321. if ($type == 1) {// 精品推荐
  322. $info = $services->getRecommendProduct(0, 'is_best', $num);// 精品推荐个数
  323. } else if ($type == 2) {// 热门榜单
  324. $info = $services->getRecommendProduct(0, 'is_hot', $num);// 热门榜单 猜你喜欢
  325. } else if ($type == 3) {// 首发新品
  326. $info = $services->getRecommendProduct(0, 'is_new', $num);// 首发新品
  327. } else if ($type == 4) {// 促销单品
  328. $info = $services->getRecommendProduct(0, 'is_benefit', $num);// 促销单品
  329. } else if ($type == 5) {// 会员商品
  330. $whereVip = [
  331. ['vip_price', '>', 0],
  332. ['is_vip', '=', 1],
  333. ];
  334. $info = $services->getRecommendProduct(0, $whereVip, $num);// 会员商品
  335. }
  336. return $info;
  337. }
  338. /**
  339. * 获取uni-app路径
  340. * @return mixed
  341. */
  342. public function getUrl()
  343. {
  344. $url = sys_data('uni_app_link');
  345. if ($url) {
  346. foreach ($url as &$link) {
  347. $link['url'] = $link['link'];
  348. $link['parameter'] = trim($link['param']);
  349. }
  350. } else {
  351. /** @var CacheServices $cache */
  352. $cache = app()->make(CacheServices::class);
  353. $url = $cache->getDbCache('uni_app_url', null);
  354. }
  355. return app('json')->success(compact('url'));
  356. }
  357. /**
  358. * 获取商品分类
  359. * @return mixed
  360. * @throws \think\db\exception\DataNotFoundException
  361. * @throws \think\db\exception\DbException
  362. * @throws \think\db\exception\ModelNotFoundException
  363. */
  364. public function getCategory()
  365. {
  366. /** @var StoreCategoryServices $categoryService */
  367. $categoryService = app()->make(StoreCategoryServices::class);
  368. $list = $categoryService->cascaderList(1, 1);
  369. return app('json')->success($list);
  370. }
  371. /**
  372. * 获取商品
  373. * @return mixed
  374. */
  375. public function getProduct()
  376. {
  377. $where = $this->request->getMore([
  378. ['id', 0],
  379. ['salesOrder', ''],
  380. ['priceOrder', ''],
  381. ]);
  382. $id = $where['id'];
  383. unset($where['id']);
  384. $where['is_show'] = 1;
  385. /** @var StoreCategoryServices $storeCategoryServices */
  386. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  387. if ($storeCategoryServices->value(['id' => $id], 'pid')) {
  388. $where['sid'] = $id;
  389. } else {
  390. $where['cid'] = $id;
  391. }
  392. [$page, $limit] = $this->services->getPageValue();
  393. /** @var StoreProductServices $productService */
  394. $productService = app()->make(StoreProductServices::class);
  395. $list = $productService->getSearchList($where, $page, $limit);
  396. return app('json')->success($list);
  397. }
  398. /**
  399. * 获取提货点自提开启状态
  400. * @return mixed
  401. */
  402. public function getStoreStatus()
  403. {
  404. $data['store_status'] = sys_config('store_self_mention', 0);
  405. return app('json')->success($data);
  406. }
  407. /**
  408. * 还原模板数据
  409. * @param $id
  410. * @return mixed
  411. */
  412. public function Recovery($id)
  413. {
  414. if (!$id) throw new AdminException(100100);
  415. $info = $this->services->get($id);
  416. if ($info) {
  417. $info->value = $info->default_value;
  418. $info->update_time = time();
  419. $info->save();
  420. return app('json')->success(100014);
  421. } else {
  422. throw new AdminException(400351);
  423. }
  424. }
  425. /**
  426. * 获取二级分类
  427. * @return mixed
  428. */
  429. public function getByCategory()
  430. {
  431. $where = $this->request->getMore([
  432. ['pid', -1],
  433. ['name', '']
  434. ]);
  435. /** @var StoreCategoryServices $categoryServices */
  436. $categoryServices = app()->make(StoreCategoryServices::class);
  437. return app('json')->success($categoryServices->getALlByIndex($where));
  438. }
  439. /**
  440. * 添加页面
  441. * @return mixed
  442. * @throws \FormBuilder\Exception\FormBuilderException
  443. */
  444. public function create()
  445. {
  446. return app('json')->success($this->services->createForm());
  447. }
  448. /**
  449. * 保存页面
  450. * @return mixed
  451. */
  452. public function save()
  453. {
  454. $data = $this->request->postMore([
  455. ['name', ''],
  456. ]);
  457. if (!$data['name']) app('json')->fail(400352);
  458. $data['version'] = '1.0';
  459. $data['add_time'] = time();
  460. $data['type'] = 0;
  461. $data['is_diy'] = 1;
  462. $this->services->save($data);
  463. return app('json')->success(100000);
  464. }
  465. /**
  466. * 设置默认数据
  467. * @param $id
  468. * @return mixed
  469. */
  470. public function setRecovery($id)
  471. {
  472. if (!$id) throw new AdminException(100100);
  473. $info = $this->services->get($id);
  474. if ($info) {
  475. $info->default_value = $info->value;
  476. $info->update_time = time();
  477. $info->save();
  478. return app('json')->success(100014);
  479. } else {
  480. throw new AdminException(100026);
  481. }
  482. }
  483. /**
  484. * 获取商品列表
  485. * @return mixed
  486. * @throws \think\db\exception\DataNotFoundException
  487. * @throws \think\db\exception\DbException
  488. * @throws \think\db\exception\ModelNotFoundException
  489. */
  490. public function getProductList()
  491. {
  492. $where = $this->request->getMore([
  493. ['cate_id', ''],
  494. ['store_name', ''],
  495. ['type', 0],
  496. ]);
  497. $where['is_show'] = 1;
  498. $where['is_del'] = 0;
  499. /** @var StoreCategoryServices $storeCategoryServices */
  500. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  501. if ($where['cate_id'] !== '') {
  502. if ($storeCategoryServices->value(['id' => $where['cate_id']], 'pid')) {
  503. $where['sid'] = $where['cate_id'];
  504. } else {
  505. $where['cid'] = $where['cate_id'];
  506. }
  507. }
  508. unset($where['cate_id']);
  509. $list = $this->services->ProductList($where);
  510. return app('json')->success($list);
  511. }
  512. /**
  513. * 分类、个人中心、一键换色
  514. * @param $type
  515. * @return mixed
  516. */
  517. public function getColorChange($type)
  518. {
  519. $status = (int)$this->services->getColorChange((string)$type);
  520. return app('json')->success(compact('status'));
  521. }
  522. /**
  523. * 保存分类、个人中心、一键换色
  524. * @param $status
  525. * @param $type
  526. * @return mixed
  527. */
  528. public function colorChange($status, $type)
  529. {
  530. if (!$status) throw new AdminException(100100);
  531. $info = $this->services->get(['template_name' => $type, 'type' => 1]);
  532. if ($info) {
  533. $info->value = $status;
  534. $info->update_time = time();
  535. $info->save();
  536. return app('json')->success(100014);
  537. } else {
  538. throw new AdminException(100026);
  539. }
  540. }
  541. /**
  542. * 获取个人中心数据
  543. * @return mixed
  544. */
  545. public function getMember()
  546. {
  547. $data = $this->services->getMemberData();
  548. return app('json')->success($data);
  549. }
  550. /**
  551. * 保存个人中心数据
  552. * @return mixed
  553. * @throws \think\db\exception\DataNotFoundException
  554. * @throws \think\db\exception\DbException
  555. * @throws \think\db\exception\ModelNotFoundException
  556. */
  557. public function memberSaveData()
  558. {
  559. $data = $this->request->postMore([
  560. ['status', 0],
  561. ['order_status', 0],
  562. ['my_banner_status', 0],
  563. ['routine_my_banner', []],
  564. ['routine_my_menus', []]
  565. ]);
  566. $this->services->memberSaveData($data);
  567. return app('json')->success(100000);
  568. }
  569. /**
  570. * 获取开屏广告
  571. * @return mixed
  572. */
  573. public function getOpenAdv()
  574. {
  575. /** @var CacheServices $cacheServices */
  576. $cacheServices = app()->make(CacheServices::class);
  577. $data = $cacheServices->getDbCache('open_adv', '');
  578. if ($data == '') {
  579. $data = [
  580. 'status' => 0,
  581. 'time' => '',
  582. 'type' => 'pic',
  583. 'value' => [],
  584. 'video_link' => '',
  585. ];
  586. }
  587. return app('json')->success($data);
  588. }
  589. /**
  590. * 保存开屏广告
  591. * @return mixed
  592. */
  593. public function openAdvAdd()
  594. {
  595. $data = $this->request->postMore([
  596. ['status', 0],
  597. ['time', 0],
  598. ['type', ''],
  599. ['value', []],
  600. ['video_link', '']
  601. ]);
  602. if ($data['type'] == '') $data['type'] = 'pic';
  603. /** @var CacheServices $cacheServices */
  604. $cacheServices = app()->make(CacheServices::class);
  605. $cacheServices->setDbCache('open_adv', $data);
  606. return app('json')->success(100000);
  607. }
  608. /**
  609. * 获取单个diy小程序预览二维码
  610. * @param $id
  611. * @return mixed
  612. */
  613. public function getRoutineCode($id)
  614. {
  615. $image = $this->services->getRoutineCode((int)$id);
  616. return app('json')->success(compact('image'));
  617. }
  618. }