Diy.php 23 KB

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