Diy.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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\merchant\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. ['name', ''],
  40. ['version', ''],
  41. ['is_diy',1],
  42. ]);
  43. [$page, $limit] = $this->getPage();
  44. $where['mer_id'] = $this->request->merId();
  45. $where['type'] = 2;
  46. $data = $this->repository->getMerchantList($where, $page, $limit);
  47. return app('json')->success($data);
  48. }
  49. /**
  50. * 平台给的默认模板
  51. * @return \think\response\Json
  52. * @author Qinii
  53. * @day 2023/9/4
  54. */
  55. public function defaultLst()
  56. {
  57. [$page, $limit] = $this->getPage();
  58. $where['default_ids'] = $this->repository->withMerSearch([
  59. 'mer_id' => $this->request->merId(),
  60. 'type_id'=> $this->request->merchant()->type_id,
  61. 'category_id'=> $this->request->merchant()->category_id,
  62. 'is_trader'=> $this->request->merchant()->is_trader,
  63. ]);
  64. $data = $this->repository->getMerchantDefaultList($where, $page, $limit);
  65. return app('json')->success($data);
  66. }
  67. /**
  68. * 保存资源
  69. * @param int $id
  70. * @return mixed
  71. */
  72. public function saveData(int $id = 0)
  73. {
  74. // 0.全部店铺使用、1. 指定店铺使用、2. 指定商户分类使用、3. 指定店铺类型使用、4. 指定商户类别使用
  75. $data = $this->request->params([
  76. ['name', ''],
  77. ['title', ''],
  78. ['value', ''],
  79. ['type', '2'],
  80. ['cover_image', ''],
  81. ['is_show', 0],
  82. ['is_bg_color', 0],
  83. ['is_bg_pic', 0],
  84. ['bg_tab_val', 0],
  85. ['color_picker', ''],
  86. ['bg_pic', ''],
  87. ['is_diy',1],
  88. ]);
  89. $data['mer_id'] = $this->request->merId();
  90. $value = is_string($data['value']) ? json_decode($data['value'], true) : $data['value'];
  91. $infoDiy = $id ? $this->repository->getWhere(['id' => $id, 'mer_id' => $data['mer_id']]) : [];
  92. if ($infoDiy && $infoDiy['is_default'])
  93. return app('json')->fail('默认模板不能修改');
  94. if ($infoDiy) {
  95. foreach ($value as $k => $item) {
  96. if ($item['name'] === 'goodList') {
  97. if (isset($item['selectConfig']['list'])) {
  98. unset($item['selectConfig']['list']);
  99. }
  100. if (isset($item['goodsList']['list']) && is_array($item['goodsList']['list'])) {
  101. $item['goodsList']['ids'] = array_column($item['goodsList']['list'], 'product_id');
  102. unset($item['goodsList']['list']);
  103. }
  104. } elseif ($item['name'] === 'articleList') {
  105. if (isset($item['selectList']['list']) && is_array($item['selectList']['list'])) {
  106. unset($item['selectList']['list']);
  107. }
  108. } elseif ($item['name'] === 'promotionList') {
  109. unset($item['productList']['list']);
  110. }
  111. $value[$k] = $item;
  112. }
  113. $data['value'] = json_encode($value);
  114. } else {
  115. if (isset($value['d_goodList']['selectConfig']['list'])) {
  116. unset($value['d_goodList']['selectConfig']['list']);
  117. } elseif (isset($value['d_goodList']['goodsList']['list'])) {
  118. $limitMax = config('database.page.limitMax', 50);
  119. 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) {
  120. return app('json')->fail('您设置得商品个数超出系统限制,最大限制' . $limitMax . '个商品');
  121. }
  122. $value['d_goodList']['goodsList']['ids'] = array_column($value['d_goodList']['goodsList']['list'], 'product_id');
  123. unset($value['d_goodList']['goodsList']['list']);
  124. } elseif (isset($value['k_newProduct']['goodsList']['list'])) {
  125. $list = [];
  126. foreach ($value['k_newProduct']['goodsList']['list'] as $item) {
  127. $list[] = [
  128. 'image' => $item['image'],
  129. 'store_info' => $item['store_info'],
  130. 'store_name' => $item['store_name'],
  131. 'id' => $item['id'],
  132. 'price' => $item['price'],
  133. 'ot_price' => $item['ot_price'],
  134. ];
  135. }
  136. $value['k_newProduct']['goodsList']['list'] = $list;
  137. } elseif (isset($value['selectList']['list']) && is_array($value['selectList']['list'])) {
  138. unset($value['goodsList']['list']);
  139. }
  140. $data['value'] = json_encode($value, JSON_UNESCAPED_UNICODE);
  141. }
  142. $data['version'] = '1.0';
  143. return app('json')->success($id ? '修改成功' : '保存成功',
  144. ['id' => $this->repository->saveData($id, $data)]
  145. );
  146. }
  147. /**
  148. * 选择方法
  149. *
  150. * @return \Illuminate\Http\JsonResponse
  151. */
  152. public function select()
  153. {
  154. // 设置查询条件
  155. $where = ['is_diy' => 0, 'is_del' => 0];
  156. // 获取数据
  157. $data = $this->repository->getOptions($where);
  158. // 返回成功响应
  159. return app('json')->success($data);
  160. }
  161. /**
  162. * 删除模板
  163. * @param $id
  164. * @return mixed
  165. */
  166. public function del($id)
  167. {
  168. $this->repository->del($id,$this->request->merId());
  169. return app('json')->success('删除成功');
  170. }
  171. /**
  172. * 使用模板
  173. * @param $id
  174. * @return mixed
  175. */
  176. public function setStatus($id)
  177. {
  178. // TODO 使用模板时取消复制
  179. // $res = $this->repository->get($id);
  180. // if ($res['is_default'] == 1) {
  181. // $ret = $this->repository->copy($id,$this->request->merId());
  182. // $id = $ret['id'];
  183. // }
  184. $this->repository->setUsed($id,$this->request->merId());
  185. return app('json')->success('修改成功');
  186. }
  187. /**
  188. * 获取一条数据
  189. * @param int $id
  190. * @return mixed
  191. */
  192. public function getInfo(int $id)
  193. {
  194. if (!$id)return app('json')->fail('参数错误');
  195. $info = $this->repository->getWhere([$this->repository->getPk() => $id, 'mer_id' => $this->request->merId(),'is_default' => 0], '*');
  196. if ($info) {
  197. $info = $info->toArray();
  198. } else {
  199. return app('json')->fail('模板不存在');
  200. }
  201. $info['value'] = json_decode($info['value'], true);
  202. if ($info['value']) {
  203. $articleServices = app()->make(ArticleRepository::class);
  204. if ($info['is_diy']) {
  205. foreach ($info['value'] as &$item) {
  206. if ($item['name'] === 'goodList' && isset($item['goodsList']['ids']) && count($item['goodsList']['ids'])) {
  207. $item['goodsList']['list'] = app()->make(SpuRepository::class)->search(['product_ids' => $item['goodsList']['ids']])->select();
  208. } elseif ($item['name'] === 'articleList') {//文章
  209. $data = [];
  210. if ($item['selectConfig']['activeValue'] ?? 0) {
  211. $data = $articleServices->search(0,['cid' => $item['selectConfig']['activeValue'] ?? 0], 0, $item['numConfig']['val'] ?? 10);
  212. $data = $data['list'];
  213. }
  214. $item['selectList']['list'] = $data['list'] ?? [];
  215. } elseif ($item['name'] === 'promotionList') {//活动模仿
  216. $data = [];
  217. if (isset($item['tabConfig']['tabCur']) && $typeArr = $item['tabConfig']['list'][$item['tabConfig']['tabCur']] ?? []) {
  218. $val = $typeArr['link']['activeVal'] ?? 0;
  219. if ($val) {
  220. $data = $this->get_groom_list($val, (int)($item['numConfig']['val'] ?? 0));
  221. }
  222. }
  223. $item['productList']['list'] = $data;
  224. }
  225. }
  226. } else {
  227. if ($info['value']) {
  228. if (isset($info['value']['d_goodList']['goodsList'])) {
  229. $info['value']['d_goodList']['goodsList']['list'] = [];
  230. }
  231. if (isset($info['value']['d_goodList']['goodsList']['ids']) && count($info['value']['d_goodList']['goodsList']['ids'])) {
  232. $info['value']['d_goodList']['goodsList']['list'] = app()->make(SpuRepository::class)->getApiSearch(['product_ids' => $info['value']['d_goodList']['goodsList']['ids']],1,10);
  233. }
  234. }
  235. }
  236. }
  237. return app('json')->success(compact('info'));
  238. }
  239. /**
  240. * 设置模版默认数据
  241. * @param $id
  242. * @return mixed
  243. */
  244. public function setDefaultData($id)
  245. {
  246. if (!$id) return app('json')->fail('参数错误');
  247. $info = $this->repository->getWhere([$this->repository->getPk() => $id, 'mer_id' => $this->request->merId()]);
  248. if ($info) {
  249. if ($info->is_default) return app('json')->fail('默认模板不能修改');
  250. $info->default_value = $info->value;
  251. $info->update_time = time();
  252. $info->save();
  253. return app('json')->success('设置成功');
  254. } else {
  255. return app('json')->fail('模板不存在');
  256. }
  257. }
  258. /**
  259. * 还原模板数据
  260. * @param $id
  261. * @return mixed
  262. */
  263. public function Recovery($id)
  264. {
  265. if (!$id) return app('json')->fail('参数错误');
  266. $info = $this->repository->getWhere([$this->repository->getPk() => $id, 'mer_id' => $this->request->merId()]);
  267. if ($info) {
  268. if ($info->is_default) return app('json')->fail('默认模板不能修改');
  269. $info->value = $info->default_value;
  270. $info->update_time = time();
  271. $info->save();
  272. return app('json')->success('还原成功');
  273. } else {
  274. return app('json')->fail('模板不存在');
  275. }
  276. }
  277. /**
  278. * 实际获取方法
  279. * @param $type
  280. * @return array
  281. */
  282. protected function get_groom_list($type, int $num = 0)
  283. {
  284. $services = app()->make(SpuRepository::class);
  285. $info = [];
  286. [$page, $limit] = $this->getPage();
  287. $where['is_gift_bag'] = 0;
  288. $where['order'] = 'star';
  289. $where['product_type'] = 0;
  290. if ($type == 1) {//TODO 精品推荐
  291. $where['hot_type'] = 'best';
  292. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 精品推荐个数
  293. } else if ($type == 2) {//TODO 热门榜单
  294. $where['hot_type'] = 'hot';
  295. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 热门榜单 猜你喜欢
  296. } else if ($type == 3) {//TODO 首发新品
  297. $where['hot_type'] = 'new';
  298. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 首发新品
  299. } else if ($type == 4) {//TODO 促销单品
  300. $where['hot_type'] = 'good';
  301. $info = $services->getApiSearch($where, $page, $limit, null);//TODO 促销单品
  302. }
  303. return $info;
  304. }
  305. /**
  306. * 获取商品列表
  307. *
  308. * @return \think\response\Json
  309. */
  310. public function productLst()
  311. {
  312. // 获取分页参数
  313. [$page, $limit] = $this->getPage();
  314. // 获取查询条件
  315. $where = $this->request->params([
  316. ['store_name', ''], // 店铺名称
  317. ['order', 'star'], // 排序方式
  318. ['cate_pid', 0], // 分类父ID
  319. ['star', ''], // 星级
  320. ['product_type', 0], // 商品类型
  321. ['mer_cate_id', ''], // 商家分类ID
  322. ['cate_id', ''], // 分类ID
  323. 'product_ids', // 商品ID
  324. ]);
  325. $where['is_gift_bag'] = 0; // 不是礼包
  326. $where['keyword'] = $where['store_name']; // 关键字为店铺名称
  327. // 如果是商家管理员,则添加商家ID查询条件
  328. if ($this->request->merId()) $where['mer_id'] = $this->request->merId();
  329. // 调用 SpuRepository 类的 getApiSearch 方法获取商品列表
  330. $data = app()->make(SpuRepository::class)->getApiSearch($where, $page, $limit, null);
  331. // 返回 JSON 格式的商品列表数据
  332. return app('json')->success($data);
  333. }
  334. /**
  335. * 复制模板
  336. *
  337. * @param int $id 模板ID
  338. * @return \think\response\Json 返回JSON格式的数据
  339. */
  340. public function copy($id)
  341. {
  342. $data = $this->repository->copy($id, $this->request->merId());
  343. return app('json')->success('复制模板成功', $data);
  344. }
  345. /**
  346. * 审核模板
  347. *
  348. * @param int $id 模板ID
  349. * @return \think\response\Json 返回JSON格式的数据
  350. */
  351. public function review($id)
  352. {
  353. // 调用repository层的get方法,获取指定ID的模板数据
  354. $rest = $this->repository->get($id);
  355. if (!$rest) return app('json')->fail('数据不存在');
  356. $image = $this->repository->review($id, $this->request->merId());
  357. // 返回JSON格式的数据,状态码为200,提示信息为'审核成功',数据为$image
  358. return app('json')->success(compact('image'));
  359. }
  360. }