Diy.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\SpuRepository;
  14. use app\common\repositories\system\diy\DiyRepository;
  15. use crmeb\basic\BaseController;
  16. use think\App;
  17. use think\exception\ValidateException;
  18. /**
  19. * Diy
  20. */
  21. class Diy extends BaseController
  22. {
  23. protected $repository;
  24. public function __construct(App $app, DiyRepository $repository)
  25. {
  26. parent::__construct($app);
  27. $this->repository = $repository;
  28. }
  29. /**
  30. * DIY列表
  31. * @return mixed
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function lst()
  37. {
  38. $type = $this->request->param('type', 1);
  39. [$page, $limit] = $this->getPage();
  40. $where['is_diy'] = $this->request->param('is_diy', $this->repository::IS_DIY_DIY);
  41. if ($type == 2) {
  42. $data = $this->repository->getMerDefaultList($where, $page, $limit);
  43. } else {
  44. $where['type'] = 1;
  45. $where['mer_id'] = $this->request->merId();
  46. $data = $this->repository->getSysList($where, $page, $limit);
  47. }
  48. return app('json')->success($data);
  49. }
  50. /**
  51. * 保存资源
  52. * @param int $id
  53. * @return mixed
  54. */
  55. public function saveData(int $id = 0)
  56. {
  57. // 0.全部店铺使用、1. 指定店铺使用、2. 指定商户分类使用、3. 指定店铺类型使用、4. 指定商户类别使用
  58. $data = $this->request->params([
  59. ['name', ''],
  60. ['title', ''],
  61. //['value', ''],
  62. ['type', '1'],
  63. ['cover_image', ''],
  64. ['is_show', 0],
  65. ['is_bg_color', 0],
  66. ['is_bg_pic', 0],
  67. ['bg_tab_val', 0],
  68. ['color_picker', ''],
  69. ['bg_pic', ''],
  70. ['is_diy', 1],
  71. ['is_default', 0]
  72. ]);
  73. $data['value'] = $this->request->param('value');
  74. if ($id) unset($data['is_default']);
  75. $data['mer_id'] = $this->request->merId();
  76. $value = is_string($data['value']) ? json_decode($data['value'], true) : $data['value'];
  77. $infoDiy = $id ? $this->repository->getWhere(['id' => $id, 'mer_id' => $data['mer_id']]) : [];
  78. if ($infoDiy) {
  79. foreach ($value as $k => $item) {
  80. if ($item['name'] === 'goodList') {
  81. if (isset($item['selectConfig']['list'])) {
  82. unset($item['selectConfig']['list']);
  83. }
  84. if (isset($item['productList']['list'])) {
  85. unset($item['productList']['list']);
  86. }
  87. if (isset($item['goodsList']['list']) && is_array($item['goodsList']['list'])) {
  88. $item['goodsList']['ids'] = array_column($item['goodsList']['list'], 'product_id');
  89. unset($item['goodsList']['list']);
  90. }
  91. } elseif ($item['name'] === 'articleList') {
  92. if (isset($item['selectList']['list']) && is_array($item['selectList']['list'])) {
  93. unset($item['selectList']['list']);
  94. }
  95. } elseif ($item['name'] === 'promotionList') {
  96. unset($item['productList']['list']);
  97. }
  98. $value[$k] = $item;
  99. }
  100. $data['value'] = json_encode($value);
  101. } else {
  102. if (isset($value['d_goodList']['selectConfig']['list'])) {
  103. unset($value['d_goodList']['selectConfig']['list']);
  104. } elseif (isset($value['d_goodList']['goodsList']['list'])) {
  105. $limitMax = config('database.page.limitMax', 50);
  106. 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) {
  107. return app('json')->fail('您设置得商品个数超出系统限制,最大限制' . $limitMax . '个商品');
  108. }
  109. $value['d_goodList']['goodsList']['ids'] = array_column($value['d_goodList']['goodsList']['list'], 'product_id');
  110. unset($value['d_goodList']['goodsList']['list']);
  111. } elseif (isset($value['k_newProduct']['goodsList']['list'])) {
  112. $list = [];
  113. foreach ($value['k_newProduct']['goodsList']['list'] as $item) {
  114. $list[] = [
  115. 'image' => $item['image'],
  116. 'store_info' => $item['store_info'],
  117. 'store_name' => $item['store_name'],
  118. 'id' => $item['id'],
  119. 'price' => $item['price'],
  120. 'ot_price' => $item['ot_price'],
  121. ];
  122. }
  123. $value['k_newProduct']['goodsList']['list'] = $list;
  124. } elseif (isset($value['selectList']['list']) && is_array($value['selectList']['list'])) {
  125. unset($value['goodsList']['list']);
  126. }
  127. $data['value'] = json_encode($value, JSON_UNESCAPED_UNICODE);
  128. }
  129. $data['version'] = '1.0';
  130. return app('json')->success($id ? '修改成功' : '保存成功',
  131. ['id' => $this->repository->saveData($id, $data)]
  132. );
  133. }
  134. /**
  135. * 获取模板列表
  136. * @return \think\response\Json
  137. * @author Qinii
  138. */
  139. public function select()
  140. {
  141. $where = ['is_diy' => 0, 'is_del' => 0];
  142. $data = $this->repository->getOptions($where);
  143. return app('json')->success($data);
  144. }
  145. /**
  146. * 删除模板
  147. * @param $id
  148. * @return mixed
  149. */
  150. public function del($id)
  151. {
  152. $this->repository->del($id, $this->request->merId());
  153. return app('json')->success('删除成功');
  154. }
  155. /**
  156. * 使用模板
  157. * @param $id
  158. * @return mixed
  159. */
  160. public function setStatus($id)
  161. {
  162. $this->repository->setUsed($id, $this->request->merId());
  163. return app('json')->success('修改成功');
  164. }
  165. /**
  166. * 获取一条数据
  167. * @param int $id
  168. * @return mixed
  169. */
  170. public function getInfo(int $id)
  171. {
  172. if (!$id) throw new ValidateException('参数错误');
  173. $info = $this->repository->getWhere([$this->repository->getPk() => $id]);
  174. if (!$info || (!$info['is_default'] && $info['mer_id']))
  175. return app('json')->fail('模板不存在');
  176. $info = $info->toArray();
  177. $info['value'] = json_decode($info['value'], true);
  178. if ($info['value']) {
  179. $articleServices = app()->make(ArticleRepository::class);
  180. if ($info['is_diy']) {
  181. foreach ($info['value'] as &$item) {
  182. if ($item['name'] === 'goodList' && isset($item['goodsList']['ids']) && count($item['goodsList']['ids'])) {
  183. $item['goodsList']['list'] = app()->make(SpuRepository::class)->search(['product_ids' => $item['goodsList']['ids']])->select();
  184. } elseif ($item['name'] === 'articleList') {//文章
  185. $data = [];
  186. if ($item['selectConfig']['activeValue'] ?? 0) {
  187. $data = $articleServices->search(0, ['cid' => $item['selectConfig']['activeValue'] ?? 0], 0, $item['numConfig']['val'] ?? 10);
  188. $data = $data['list'];
  189. }
  190. $item['selectList']['list'] = $data['list'] ?? [];
  191. } elseif ($item['name'] === 'promotionList') {//活动模仿
  192. $data = [];
  193. if (isset($item['tabConfig']['tabCur']) && $typeArr = $item['tabConfig']['list'][$item['tabConfig']['tabCur']] ?? []) {
  194. $val = $typeArr['link']['activeVal'] ?? 0;
  195. if ($val) {
  196. $data = $this->get_groom_list($val, (int)($item['numConfig']['val'] ?? 0));
  197. }
  198. }
  199. $item['productList']['list'] = $data;
  200. }
  201. }
  202. } else {
  203. if ($info['value']) {
  204. if (isset($info['value']['d_goodList']['goodsList'])) {
  205. $info['value']['d_goodList']['goodsList']['list'] = [];
  206. }
  207. if (isset($info['value']['d_goodList']['goodsList']['ids']) && count($info['value']['d_goodList']['goodsList']['ids'])) {
  208. $info['value']['d_goodList']['goodsList']['list'] = app()->make(SpuRepository::class)->getApiSearch(['product_ids' => $info['value']['d_goodList']['goodsList']['ids']], 1, 10);
  209. }
  210. }
  211. }
  212. }
  213. return app('json')->success(compact('info'));
  214. }
  215. /**
  216. * 设置模版默认数据
  217. * @param $id
  218. * @return mixed
  219. */
  220. public function setDefaultData($id)
  221. {
  222. if (!$id) return app('json')->fail('参数错误');
  223. $info = $this->repository->getWhere([$this->repository->getPk() => $id, 'mer_id' => $this->request->merId()]);
  224. if ($info) {
  225. if ($info->is_default) return app('json')->fail('默认模板不能修改');
  226. $info->default_value = $info->value;
  227. $info->update_time = time();
  228. $info->save();
  229. return app('json')->success('设置成功');
  230. } else {
  231. return app('json')->fail('模板不存在');
  232. }
  233. }
  234. /**
  235. * 还原模板数据
  236. * @param $id
  237. * @return mixed
  238. */
  239. public function Recovery($id)
  240. {
  241. if (!$id) return app('json')->fail('参数错误');
  242. $info = $this->repository->getWhere([$this->repository->getPk() => $id, 'mer_id' => $this->request->merId()]);
  243. if ($info) {
  244. if ($info->is_default) return app('json')->fail('默认模板不能修改');
  245. $info->value = $info->default_value;
  246. $info->update_time = time();
  247. $info->save();
  248. return app('json')->success('还原成功');
  249. } else {
  250. return app('json')->fail('模板不存在');
  251. }
  252. }
  253. /**
  254. * 实际获取方法
  255. * @param $type
  256. * @return array
  257. */
  258. protected function get_groom_list($type, int $num = 0)
  259. {
  260. $services = app()->make(SpuRepository::class);
  261. $info = [];
  262. [$page, $limit] = $this->getPage();
  263. $where['is_gift_bag'] = 0;
  264. $where['order'] = 'star';
  265. $where['product_type'] = 0;
  266. if ($type == 1) {//TODO 精品推荐
  267. $where['hot_type'] = 'best';
  268. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 精品推荐个数
  269. } else if ($type == 2) {//TODO 热门榜单
  270. $where['hot_type'] = 'hot';
  271. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 热门榜单 猜你喜欢
  272. } else if ($type == 3) {//TODO 首发新品
  273. $where['hot_type'] = 'new';
  274. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 首发新品
  275. } else if ($type == 4) {//TODO 促销单品
  276. $where['hot_type'] = 'good';
  277. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 促销单品
  278. }
  279. return $info;
  280. }
  281. /**
  282. * 商品列表
  283. * @return \think\response\Json
  284. * @author Qinii
  285. */
  286. public function productLst()
  287. {
  288. [$page, $limit] = $this->getPage();
  289. $where = $this->request->params([
  290. ['store_name', ''],
  291. ['order', 'star'],
  292. ['cate_pid', 0],
  293. ['star', ''],
  294. ['cate_id', ''],
  295. 'product_type',
  296. 'mer_cate_id',
  297. 'product_ids'
  298. ]);
  299. $where['is_gift_bag'] = 0;
  300. $where['keyword'] = $where['store_name'];
  301. if ($where['product_ids']) $limit = count(explode(',',$where['product_ids']));
  302. if ($this->request->merId()) $where['mer_id'] = $this->request->merId();
  303. //$data = app()->make(SpuRepository::class)->search($where, $page, $limit, null);
  304. $query = app()->make(SpuRepository::class)->search($where);
  305. $data['count'] = $query->count();
  306. $data['list'] = $query->page((int)$page, (int)$limit)->select();
  307. return app('json')->success($data);
  308. }
  309. /**
  310. * 复制模板
  311. * @param $id
  312. * @return \think\response\Json
  313. * @author Qinii
  314. */
  315. public function copy($id)
  316. {
  317. $data = $this->repository->copy($id, $this->request->merId());
  318. return app('json')->success('复制模板成功', $data);
  319. }
  320. /**
  321. * 获取模板可用范围
  322. * @param $id
  323. * @return \think\response\Json
  324. * @author Qinii
  325. */
  326. public function getScope($id)
  327. {
  328. $data = $this->repository->getScope($id);
  329. return app('json')->success($data);
  330. }
  331. /**
  332. * 设置模板可用范围
  333. * @param $id
  334. * @return \think\response\Json
  335. * @author Qinii
  336. */
  337. public function setScope($id)
  338. {
  339. $data = $this->request->params(['scope_type', 'scope_value']);
  340. $this->repository->setScope($id, $data);
  341. return app('json')->success('编辑成功');
  342. }
  343. public function get_product_detail()
  344. {
  345. $data = $this->repository->getProductDetail();
  346. return app('json')->success($data);
  347. }
  348. public function save_product_detail()
  349. {
  350. $res = $this->request->params([
  351. ['name', ''],
  352. ['title', ''],
  353. ['product_detail_diy',[]],
  354. ['type', '1'],
  355. ['cover_image', ''],
  356. ['is_show', 0],
  357. ['is_bg_color', 0],
  358. ['is_bg_pic', 0],
  359. ['bg_tab_val', 0],
  360. ['color_picker', ''],
  361. ['bg_pic', ''],
  362. ['is_diy', 1],
  363. ['is_default', 0]
  364. ]);
  365. $id = $this->request->post('id');
  366. $data = $this->repository->saveProductDetail($res);
  367. return app('json')->success('保存成功');
  368. }
  369. }