Diy.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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\controller\admin\system\diy;
  12. use app\common\repositories\article\ArticleRepository;
  13. use app\common\repositories\store\product\ProductRepository;
  14. use app\common\repositories\store\product\SpuRepository;
  15. use app\common\repositories\store\StoreCategoryRepository;
  16. use app\common\repositories\system\diy\DiyRepository;
  17. use crmeb\basic\BaseController;
  18. use think\App;
  19. use think\exception\ValidateException;
  20. class Diy extends BaseController
  21. {
  22. protected $repository;
  23. public function __construct(App $app, DiyRepository $repository)
  24. {
  25. parent::__construct($app);
  26. $this->repository = $repository;
  27. }
  28. /**
  29. * DIY列表
  30. * @return mixed
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function lst()
  36. {
  37. $where = $this->request->params([
  38. ['status', ''],
  39. ['type', ''],
  40. ['name', ''],
  41. ['version', ''],
  42. ['is_diy',1]
  43. ]);
  44. $where['mer_id'] = $this->request->merId();
  45. [$page, $limit] = $this->getPage();
  46. $data = $this->repository->getSysList($where,$page, $limit);
  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->params([
  57. ['name', ''],
  58. ['title', ''],
  59. ['value', ''],
  60. ['type', '1'],
  61. ['cover_image', ''],
  62. ['is_show', 0],
  63. ['is_bg_color', 0],
  64. ['is_bg_pic', 0],
  65. ['bg_tab_val', 0],
  66. ['color_picker', ''],
  67. ['bg_pic', ''],
  68. ['is_diy',1],
  69. ]);
  70. $data['mer_id'] = $this->request->merId();
  71. $value = is_string($data['value']) ? json_decode($data['value'], true) : $data['value'];
  72. $infoDiy = $id ? $this->repository->getWhere(['id' => $id, 'mer_id' => $data['mer_id']]) : [];
  73. if ($infoDiy && $infoDiy['is_default'])
  74. return app('json')->fail('默认模板不能修改');
  75. if ($infoDiy && $infoDiy['is_diy']) {
  76. foreach ($value as $key => &$item) {
  77. if ($item['name'] === 'goodList') {
  78. if (isset($item['selectConfig']['list'])) {
  79. unset($item['selectConfig']['list']);
  80. }
  81. if (isset($item['goodsList']['list']) && is_array($item['goodsList']['list'])) {
  82. $limitMax = config('database.page.limitMax', 50);
  83. if (isset($item['numConfig']['val']) && isset($item['tabConfig']['tabVal']) && $item['tabConfig']['tabVal'] == 0 && $item['numConfig']['val'] > $limitMax) {
  84. return app('json')->fail('您设置得商品个数超出系统限制,最大限制' . $limitMax . '个商品');
  85. }
  86. $item['goodsList']['ids'] = array_column($item['goodsList']['list'], 'product_id');
  87. unset($item['goodsList']['list']);
  88. }
  89. } elseif ($item['name'] === 'articleList') {
  90. if (isset($item['selectList']['list']) && is_array($item['selectList']['list'])) {
  91. unset($item['selectList']['list']);
  92. }
  93. } elseif ($item['name'] === 'promotionList') {
  94. unset($item['productList']['list']);
  95. }
  96. }
  97. $data['value'] = json_encode($value);
  98. } else {
  99. if (isset($value['d_goodList']['selectConfig']['list'])) {
  100. unset($value['d_goodList']['selectConfig']['list']);
  101. } elseif (isset($value['d_goodList']['goodsList']['list'])) {
  102. $limitMax = config('database.page.limitMax', 50);
  103. 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) {
  104. return app('json')->fail('您设置得商品个数超出系统限制,最大限制' . $limitMax . '个商品');
  105. }
  106. $value['d_goodList']['goodsList']['ids'] = array_column($value['d_goodList']['goodsList']['list'], 'product_id');
  107. unset($value['d_goodList']['goodsList']['list']);
  108. } elseif (isset($value['k_newProduct']['goodsList']['list'])) {
  109. $list = [];
  110. foreach ($value['k_newProduct']['goodsList']['list'] as $item) {
  111. $list[] = [
  112. 'image' => $item['image'],
  113. 'store_info' => $item['store_info'],
  114. 'store_name' => $item['store_name'],
  115. 'id' => $item['id'],
  116. 'price' => $item['price'],
  117. 'ot_price' => $item['ot_price'],
  118. ];
  119. }
  120. $value['k_newProduct']['goodsList']['list'] = $list;
  121. } elseif (isset($value['selectList']['list']) && is_array($value['selectList']['list'])) {
  122. unset($value['goodsList']['list']);
  123. }
  124. $data['value'] = json_encode($value,JSON_UNESCAPED_UNICODE);
  125. }
  126. $data['version'] = '1.0';
  127. return app('json')->success($id ? '修改成功' : '保存成功',
  128. ['id' => $this->repository->saveData($id, $data)]
  129. );
  130. }
  131. public function select()
  132. {
  133. $where = ['is_diy' => 0, 'is_del' => 0];
  134. $data = $this->repository->getOptions($where);
  135. return app('json')->success($data);
  136. }
  137. /**
  138. * 删除模板
  139. * @param $id
  140. * @return mixed
  141. */
  142. public function del($id)
  143. {
  144. $this->repository->del($id,$this->request->merId());
  145. return app('json')->success('删除成功');
  146. }
  147. public function getDiyInfo()
  148. {
  149. return app('json')->success($this->repository->getDiyInfo(0,$this->request->merId()));
  150. }
  151. /**
  152. * 使用模板
  153. * @param $id
  154. * @return mixed
  155. */
  156. public function setStatus($id)
  157. {
  158. $this->repository->setUsed($id,$this->request->merId());
  159. return app('json')->success('修改成功');
  160. }
  161. /**
  162. * 获取一条数据
  163. * @param int $id
  164. * @return mixed
  165. */
  166. public function getInfo(int $id)
  167. {
  168. if (!$id) throw new ValidateException('参数错误');
  169. $info = $this->repository->getWhere([$this->repository->getPk() => $id, 'mer_id' => $this->request->merId()]);
  170. if ($info) {
  171. $info = $info->toArray();
  172. } else {
  173. throw new ValidateException('模板不存在');
  174. }
  175. $info['value'] = json_decode($info['value'], true);
  176. if ($info['value']) {
  177. $articleServices = app()->make(ArticleRepository::class);
  178. if ($info['is_diy']) {
  179. foreach ($info['value'] as &$item) {
  180. if ($item['name'] === 'goodList' && isset($item['goodsList']['ids']) && count($item['goodsList']['ids'])) {
  181. $item['goodsList']['list'] = app()->make(SpuRepository::class)->search(['product_ids' => $item['goodsList']['ids']])->select();
  182. } elseif ($item['name'] === 'articleList') {//文章
  183. $data = [];
  184. if ($item['selectConfig']['activeValue'] ?? 0) {
  185. $data = $articleServices->search(0,['cid' => $item['selectConfig']['activeValue'] ?? 0], 0, $item['numConfig']['val'] ?? 10);
  186. $data = $data['list'];
  187. }
  188. $item['selectList']['list'] = $data['list'] ?? [];
  189. } elseif ($item['name'] === 'promotionList') {//活动模仿
  190. $data = [];
  191. if (isset($item['tabConfig']['tabCur']) && $typeArr = $item['tabConfig']['list'][$item['tabConfig']['tabCur']] ?? []) {
  192. $val = $typeArr['link']['activeVal'] ?? 0;
  193. if ($val) {
  194. $data = $this->get_groom_list($val, (int)($item['numConfig']['val'] ?? 0));
  195. }
  196. }
  197. $item['productList']['list'] = $data;
  198. }
  199. }
  200. } else {
  201. if ($info['value']) {
  202. if (isset($info['value']['d_goodList']['goodsList'])) {
  203. $info['value']['d_goodList']['goodsList']['list'] = [];
  204. }
  205. if (isset($info['value']['d_goodList']['goodsList']['ids']) && count($info['value']['d_goodList']['goodsList']['ids'])) {
  206. $info['value']['d_goodList']['goodsList']['list'] = app()->make(SpuRepository::class)->getApiSearch(['product_ids' => $info['value']['d_goodList']['goodsList']['ids']],1,10);
  207. }
  208. }
  209. }
  210. }
  211. return app('json')->success(compact('info'));
  212. }
  213. /**
  214. * 设置模版默认数据
  215. * @param $id
  216. * @return mixed
  217. */
  218. public function setDefaultData($id)
  219. {
  220. if (!$id) return app('json')->fail('参数错误');
  221. $info = $this->repository->getWhere([$this->repository->getPk() => $id, 'mer_id' => $this->request->merId()]);
  222. if ($info) {
  223. if ($info->is_default) return app('json')->fail('默认模板不能修改');
  224. $info->default_value = $info->value;
  225. $info->update_time = time();
  226. $info->save();
  227. return app('json')->success('设置成功');
  228. } else {
  229. return app('json')->fail('模板不存在');
  230. }
  231. }
  232. /**
  233. * 还原模板数据
  234. * @param $id
  235. * @return mixed
  236. */
  237. public function Recovery($id)
  238. {
  239. if (!$id) return app('json')->fail('参数错误');
  240. $info = $this->repository->getWhere([$this->repository->getPk() => $id, 'mer_id' => $this->request->merId()]);
  241. if ($info) {
  242. if ($info->is_default) return app('json')->fail('默认模板不能修改');
  243. $info->value = $info->default_value;
  244. $info->update_time = time();
  245. $info->save();
  246. return app('json')->success('还原成功');
  247. } else {
  248. return app('json')->fail('模板不存在');
  249. }
  250. }
  251. /**
  252. * 实际获取方法
  253. * @param $type
  254. * @return array
  255. */
  256. protected function get_groom_list($type, int $num = 0)
  257. {
  258. $services = app()->make(SpuRepository::class);
  259. $info = [];
  260. [$page, $limit] = $this->getPage();
  261. $where['is_gift_bag'] = 0;
  262. $where['order'] = 'star';
  263. $where['product_type'] = 0;
  264. if ($type == 1) {//TODO 精品推荐
  265. $where['hot_type'] = 'best';
  266. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 精品推荐个数
  267. } else if ($type == 2) {//TODO 热门榜单
  268. $where['hot_type'] = 'hot';
  269. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 热门榜单 猜你喜欢
  270. } else if ($type == 3) {//TODO 首发新品
  271. $where['hot_type'] = 'new';
  272. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 首发新品
  273. } else if ($type == 4) {//TODO 促销单品
  274. $where['hot_type'] = 'good';
  275. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 促销单品
  276. }
  277. return $info;
  278. }
  279. public function productLst()
  280. {
  281. [$page, $limit] = $this->getPage();
  282. $where = $this->request->params([
  283. ['store_name',''],
  284. ['order', 'star'],
  285. ['cate_pid',0],
  286. ['star',''],
  287. 'product_type',
  288. 'mer_cate_id'
  289. ]);
  290. $where['is_gift_bag'] = 0;
  291. $where['keyword'] = $where['store_name'];
  292. if ($this->request->merId()) $where['mer_id'] = $this->request->merId();
  293. $data = app()->make(SpuRepository::class)->getApiSearch($where, $page, $limit, null);
  294. return app('json')->success($data);
  295. }
  296. public function copy($id)
  297. {
  298. $data = $this->repository->copy($id,$this->request->merId());
  299. return app('json')->success($data);
  300. }
  301. }