Diy.php 23 KB

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