Diy.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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', ''],
  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 $this->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. return app('json')->success($this->services->createForm());
  275. }
  276. /**
  277. * 保存页面
  278. * @return mixed
  279. */
  280. public function save(){
  281. $data = $this->request->postMore([
  282. ['name', ''],
  283. ['template_name', ''],
  284. ]);
  285. if (!$data['name']) throw new AdminException('请输入页面名称');
  286. if (!$data['template_name']) throw new AdminException('请输入页面类型');
  287. $data['version'] = '1.0';
  288. $data['add_time'] = time();
  289. $data['type'] = 1;
  290. $this->services->save($data);
  291. return app('json')->success('保存成功!');
  292. }
  293. /**
  294. * 设置默认数据
  295. * @param $id
  296. * @return mixed
  297. */
  298. public function setRecovery($id){
  299. if (!$id) throw new AdminException('参数错误');
  300. $info = $this->services->get($id);
  301. if ($info) {
  302. $info->default_value = $info->value;
  303. $info->update_time = time();
  304. $info->save();
  305. return app('json')->success('设置成功');
  306. } else {
  307. throw new AdminException('模板不存在');
  308. }
  309. }
  310. /**
  311. * 获取商品列表
  312. * @return mixed
  313. */
  314. public function getProductList(){
  315. $where = $this->request->getMore([
  316. ['cate_id', ''],
  317. ['store_name', ''],
  318. ['type', 0],
  319. ]);
  320. $where['is_show'] = 1;
  321. $where['is_del'] = 0;
  322. /** @var StoreCategoryServices $storeCategoryServices */
  323. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  324. if ($where['cate_id'] !== '') {
  325. if ($storeCategoryServices->value(['id' => $where['cate_id']], 'pid')) {
  326. $where['sid'] = $where['cate_id'];
  327. } else {
  328. $where['cid'] = $where['cate_id'];
  329. }
  330. }
  331. unset($where['cate_id']);
  332. $list = $this->services->ProductList($where);
  333. return app('json')->success($list);
  334. }
  335. }