Diy.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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\StoreBargainServices;
  14. use app\services\activity\StoreCombinationServices;
  15. use app\services\activity\StoreSeckillServices;
  16. use app\services\diy\DiyServices;
  17. use app\services\other\CacheServices;
  18. use app\services\product\product\StoreCategoryServices;
  19. use app\services\product\product\StoreProductServices;
  20. use crmeb\exceptions\AdminException;
  21. use crmeb\services\FileService;
  22. use think\facade\App;
  23. class Diy extends AuthController
  24. {
  25. protected $services;
  26. public function __construct(App $app, DiyServices $services)
  27. {
  28. parent::__construct($app);
  29. $this->services = $services;
  30. }
  31. /**
  32. * DIY列表
  33. * @return mixed
  34. * @throws \think\db\exception\DataNotFoundException
  35. * @throws \think\db\exception\DbException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. */
  38. public function getList()
  39. {
  40. $where = $this->request->getMore([
  41. ['status', ''],
  42. ['type', 0],
  43. ['name', ''],
  44. ['version', ''],
  45. ]);
  46. $data = $this->services->getDiyList($where);
  47. return app('json')->success($data);
  48. }
  49. /**
  50. * 保存资源
  51. * @param int $id
  52. * @return mixed
  53. */
  54. public function saveData(int $id = 0)
  55. {
  56. $data = $this->request->postMore([
  57. ['value', ''],
  58. ]);
  59. $value_config = ['seckill', 'bargain', 'combination', 'goodList'];
  60. $value = is_string($data['value']) ? json_decode($data['value'], true) : $data['value'];
  61. foreach ($value as $key => &$val) {
  62. if (in_array($key, $value_config) && is_array($val)) {
  63. foreach ($val as $k => &$v) {
  64. if (isset($v['selectConfig']['list']) && $v['selectConfig']['list']) {
  65. $v['selectConfig']['list'] = [];
  66. }
  67. if (isset($v['goodsList']['list']) && $v['goodsList']['list'] && $v['tabConfig']['tabVal'] == 1) {
  68. $limitMax = config('database.page.limitMax', 50);
  69. if (count($v['goodsList']['list']) > $limitMax) {
  70. return app('json')->fail('您设置得商品个数超出系统限制,最大限制' . $limitMax . '个商品');
  71. }
  72. $v['ids'] = array_column($v['goodsList']['list'], 'id');
  73. $v['goodsList']['list'] = [];
  74. }
  75. }
  76. }
  77. }
  78. $data['value'] = json_encode($value);
  79. $this->services->saveData($id, $data);
  80. return app('json')->success('保存成功');
  81. }
  82. /**
  83. * 删除模板
  84. * @param $id
  85. * @return mixed
  86. */
  87. public function del($id)
  88. {
  89. $this->services->del($id);
  90. return app('json')->success('删除成功');
  91. }
  92. /**
  93. * 使用模板
  94. * @param $id
  95. * @return mixed
  96. */
  97. public function setStatus($id)
  98. {
  99. $name = $this->services->value(['id' => $id], 'template_name');
  100. if (!is_file(public_path('template') . $name . '.zip')) {
  101. throw new AdminException('请上传模板压缩包');
  102. }
  103. FileService::delDir(runtime_path('wap'));
  104. FileService::delDir(public_path('pages'));
  105. FileService::delDir(public_path('static'));
  106. @unlink(public_path() . 'index.html');
  107. $this->services->setStatus($id);
  108. FileService::zipOpen(public_path('template') . $name . '.zip', public_path());
  109. return app('json')->success('设置成功');
  110. }
  111. /**
  112. * 获取一条数据
  113. * @param int $id
  114. * @return mixed
  115. */
  116. public function getInfo(int $id, StoreProductServices $services, StoreSeckillServices $seckillServices, StoreCombinationServices $combinationServices, StoreBargainServices $bargainServices)
  117. {
  118. if (!$id) throw new AdminException('参数错误');
  119. $info = $this->services->get($id);
  120. if ($info) {
  121. $info = $info->toArray();
  122. } else {
  123. throw new AdminException('模板不存在');
  124. }
  125. if (!$info['value']) return app('json')->success(compact('info'));
  126. $info['value'] = json_decode($info['value'], true);
  127. $value_config = ['seckill', 'bargain', 'combination', 'goodList'];
  128. foreach ($info['value'] as $key => &$val) {
  129. if (in_array($key, $value_config) && is_array($val)) {
  130. if ($key == 'goodList') {
  131. foreach ($val as $k => &$v) {
  132. if (isset($v['ids']) && $v['ids'] && $v['tabConfig']['tabVal'] == 1) {
  133. $v['goodsList']['list'] = $services->getSearchList(['ids' => $v['ids']]);
  134. }
  135. }
  136. }
  137. if ($key == "seckill") {
  138. foreach ($val as $k => &$v) {
  139. if (isset($v['ids']) && $v['ids'] && $v['tabConfig']['tabVal'] == 1) {
  140. $v['goodsList']['list'] = $seckillServices->getDiySeckillList(['ids' => $v['ids']])['list'];
  141. }
  142. }
  143. }
  144. if ($key == "bargain") {
  145. foreach ($val as $k => &$v) {
  146. if (isset($v['ids']) && $v['ids'] && $v['tabConfig']['tabVal'] == 1) {
  147. $v['goodsList']['list'] = $bargainServices->getHomeList(['ids' => $v['ids']])['list'];
  148. }
  149. }
  150. }
  151. if ($key == "combination") {
  152. foreach ($val as $k => &$v) {
  153. if (isset($v['ids']) && $v['ids'] && $v['tabConfig']['tabVal'] == 1) {
  154. $v['goodsList']['list'] = $combinationServices->getHomeList(['ids' => $v['ids']])['list'];
  155. }
  156. }
  157. }
  158. }
  159. }
  160. return app('json')->success(compact('info'));
  161. }
  162. /**
  163. * 获取uni-app路径
  164. * @return mixed
  165. */
  166. public function getUrl()
  167. {
  168. $url = sys_data('uni_app_link');
  169. if ($url) {
  170. foreach ($url as &$link) {
  171. $link['url'] = $link['link'];
  172. $link['parameter'] = trim($link['param']);
  173. }
  174. } else {
  175. /** @var CacheServices $cache */
  176. $cache = app()->make(CacheServices::class);
  177. $url = $cache->getDbCache('uni_app_url', null);
  178. }
  179. return app('json')->success(compact('url'));
  180. }
  181. /**
  182. * 获取商品分类
  183. * @return mixed
  184. * @throws \think\db\exception\DataNotFoundException
  185. * @throws \think\db\exception\DbException
  186. * @throws \think\db\exception\ModelNotFoundException
  187. */
  188. public function getCategory()
  189. {
  190. /** @var StoreCategoryServices $categoryService */
  191. $categoryService = app()->make(StoreCategoryServices::class);
  192. $list = $categoryService->getTierList(1, 1);
  193. $data = [];
  194. foreach ($list as $value) {
  195. $data[] = [
  196. 'id' => $value['id'],
  197. 'title' => $value['html'] . $value['cate_name']
  198. ];
  199. }
  200. return app('json')->success($data);
  201. }
  202. /**
  203. * 获取商品
  204. * @return mixed
  205. */
  206. public function getProduct()
  207. {
  208. $where = $this->request->getMore([
  209. ['id', 0],
  210. ['salesOrder', ''],
  211. ['priceOrder', ''],
  212. ]);
  213. $id = $where['id'];
  214. unset($where['id']);
  215. /** @var StoreCategoryServices $storeCategoryServices */
  216. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  217. if ($storeCategoryServices->value(['id' => $id], 'pid')) {
  218. $where['sid'] = $id;
  219. } else {
  220. $where['cid'] = $id;
  221. }
  222. [$page, $limit] = $this->services->getPageValue();
  223. /** @var StoreProductServices $productService */
  224. $productService = app()->make(StoreProductServices::class);
  225. $list = $productService->getSearchList($where, $page, $limit);
  226. return app('json')->success($list);
  227. }
  228. /**
  229. * 获取提货点自提开启状态
  230. * @return mixed
  231. */
  232. public function getStoreStatus()
  233. {
  234. $data['store_status'] = sys_config('store_self_mention', 0);
  235. return app('json')->success($data);
  236. }
  237. /**
  238. * 还原模板数据
  239. * @param $id
  240. * @return mixed
  241. */
  242. public function Recovery($id)
  243. {
  244. if (!$id) throw new AdminException('参数错误');
  245. $info = $this->services->get($id);
  246. if ($info) {
  247. $info->value = $info->default_value;
  248. $info->update_time = time();
  249. $info->save();
  250. return app('json')->success('还原成功');
  251. } else {
  252. throw new AdminException('模板不存在');
  253. }
  254. }
  255. /**
  256. * 获取二级分类
  257. * @return mixed
  258. */
  259. public function getByCategory()
  260. {
  261. $where = $this->request->getMore([
  262. ['pid', -1],
  263. ['name', '']
  264. ]);
  265. /** @var StoreCategoryServices $categoryServices */
  266. $categoryServices = app()->make(StoreCategoryServices::class);
  267. return app('json')->success($categoryServices->getALlByIndex($where));
  268. }
  269. /**
  270. * 添加页面
  271. * @return mixed
  272. */
  273. public function create()
  274. {
  275. return app('json')->success($this->services->createForm());
  276. }
  277. /**
  278. * 保存页面
  279. * @return mixed
  280. */
  281. public function save()
  282. {
  283. $data = $this->request->postMore([
  284. ['name', ''],
  285. ['template_name', ''],
  286. ]);
  287. if (!$data['name']) throw new AdminException('请输入页面名称');
  288. if (!$data['template_name']) throw new AdminException('请输入页面类型');
  289. $data['version'] = '1.0';
  290. $data['add_time'] = time();
  291. $data['type'] = 2;
  292. $this->services->save($data);
  293. return app('json')->success('保存成功!');
  294. }
  295. /**
  296. * 设置默认数据
  297. * @param $id
  298. * @return mixed
  299. */
  300. public function setRecovery($id)
  301. {
  302. if (!$id) throw new AdminException('参数错误');
  303. $info = $this->services->get($id);
  304. if ($info) {
  305. $info->default_value = $info->value;
  306. $info->update_time = time();
  307. $info->save();
  308. return app('json')->success('设置成功');
  309. } else {
  310. throw new AdminException('模板不存在');
  311. }
  312. }
  313. /**
  314. * 获取商品列表
  315. * @return mixed
  316. */
  317. public function getProductList()
  318. {
  319. $where = $this->request->getMore([
  320. ['cate_id', ''],
  321. ['store_name', ''],
  322. ['type', 0],
  323. ]);
  324. $where['is_show'] = 1;
  325. $where['is_del'] = 0;
  326. /** @var StoreCategoryServices $storeCategoryServices */
  327. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  328. if ($where['cate_id'] !== '') {
  329. if ($storeCategoryServices->value(['id' => $where['cate_id']], 'pid')) {
  330. $where['sid'] = $where['cate_id'];
  331. } else {
  332. $where['cid'] = $where['cate_id'];
  333. }
  334. }
  335. unset($where['cate_id']);
  336. $list = $this->services->ProductList($where);
  337. return app('json')->success($list);
  338. }
  339. /**
  340. * 分类、个人中心、一键换色
  341. * @param $type
  342. * @return mixed
  343. */
  344. public function getColorChange($type)
  345. {
  346. $status = (int)$this->services->getColorChange((string)$type);
  347. return app('json')->success(compact('status'));
  348. }
  349. /**
  350. * 保存分类、个人中心、一键换色
  351. * @param $status
  352. * @param $type
  353. * @return mixed
  354. */
  355. public function colorChange($status, $type)
  356. {
  357. if (!$status) throw new AdminException('参数错误');
  358. $info = $this->services->get(['template_name' => $type, 'type' => 1]);
  359. if ($info) {
  360. $info->value = $status;
  361. $info->update_time = time();
  362. $info->save();
  363. return app('json')->success('设置成功');
  364. } else {
  365. throw new AdminException('模板不存在');
  366. }
  367. }
  368. /**
  369. * 获取个人中心数据
  370. * @return mixed
  371. */
  372. public function getMember()
  373. {
  374. $data = $this->services->getMemberData();
  375. return app('json')->success($data);
  376. }
  377. /**
  378. * 保存个人中心数据
  379. * @return mixed
  380. */
  381. public function memberSaveData()
  382. {
  383. $data = $this->request->postMore([
  384. ['status', 0],
  385. ['order_status', 0],
  386. ['my_banner_status', 0],
  387. ['routine_my_banner', []],
  388. ['routine_my_menus', []]
  389. ]);
  390. $this->services->memberSaveData($data);
  391. return app('json')->success('保存成功');
  392. }
  393. }