DiyServices.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\diy;
  13. use app\services\activity\bargain\StoreBargainServices;
  14. use app\services\activity\combination\StoreCombinationServices;
  15. use app\services\activity\seckill\StoreSeckillServices;
  16. use app\services\BaseServices;
  17. use app\dao\diy\DiyDao;
  18. use app\services\other\QrcodeServices;
  19. use app\services\product\product\StoreProductServices;
  20. use app\services\system\config\SystemGroupDataServices;
  21. use app\services\system\config\SystemGroupServices;
  22. use crmeb\exceptions\AdminException;
  23. use crmeb\services\CacheService;
  24. use crmeb\services\FormBuilder as Form;
  25. use crmeb\services\SystemConfigService;
  26. use think\facade\Route as Url;
  27. /**
  28. *
  29. * Class DiyServices
  30. * @package app\services\diy
  31. */
  32. class DiyServices extends BaseServices
  33. {
  34. /**
  35. * DiyServices constructor.
  36. * @param DiyDao $dao
  37. */
  38. public function __construct(DiyDao $dao)
  39. {
  40. $this->dao = $dao;
  41. }
  42. /**
  43. * 获取DIY列表
  44. * @param array $where
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function getDiyList(array $where)
  51. {
  52. [$page, $limit] = $this->getPageValue();
  53. $where['is_del'] = 0;
  54. $list = $this->dao->getDiyList($where, $page, $limit, ['id', 'name', 'type', 'add_time', 'update_time', 'is_diy', 'status']);
  55. foreach ($list as &$item) {
  56. $item['type_name'] = $item['type'] == 0 ? '可视化' : '专题页';
  57. }
  58. $count = $this->dao->count($where);
  59. return compact('list', 'count');
  60. }
  61. /**
  62. * 保存资源
  63. * @param int $id
  64. * @param array $data
  65. */
  66. public function saveData(int $id = 0, array $data = [])
  67. {
  68. if ($id) {
  69. $data['update_time'] = time();
  70. $res = $this->dao->update($id, $data);
  71. if (!$res) throw new AdminException(100007);
  72. } else {
  73. $data['add_time'] = time();
  74. $data['update_time'] = time();
  75. $res = $this->dao->save($data);
  76. if (!$res) throw new AdminException(100006);
  77. $id = $res->id;
  78. }
  79. CacheService::clear();
  80. CacheService::set('index_diy_' . $id, $data['version']);
  81. $this->updateCacheDiyVersion();
  82. return $id;
  83. }
  84. /**
  85. * 删除DIY模板
  86. * @param int $id
  87. */
  88. public function del(int $id)
  89. {
  90. if ($id == 1) throw new AdminException(400457);
  91. $count = $this->dao->getCount(['id' => $id, 'status' => 1]);
  92. if ($count) throw new AdminException(400458);
  93. $res = $this->dao->update($id, ['is_del' => 1]);
  94. if (!$res) throw new AdminException(100008);
  95. CacheService::clear();
  96. }
  97. /**
  98. * 设置模板使用
  99. * @param int $id
  100. */
  101. public function setStatus(int $id)
  102. {
  103. $this->dao->update(['is_diy' => 1], ['is_show' => 1, 'type' => 2]);
  104. $this->dao->update([['id', '<>', $id]], ['status' => 0]);
  105. $this->dao->update($id, ['status' => 1, 'update_time' => time()]);
  106. CacheService::clear();
  107. $this->updateCacheDiyVersion();
  108. }
  109. /**
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\DbException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @author 等风来
  114. * @email 136327134@qq.com
  115. * @date 2023/2/8
  116. */
  117. public function updateCacheDiyVersion()
  118. {
  119. $diyInfo = $this->dao->get(['status' => 1, 'is_del' => 0], ['id', 'version']);
  120. if (!$diyInfo) {
  121. CacheService::delete('index_diy_default');
  122. } else {
  123. CacheService::set('index_diy_default', $diyInfo['version']);
  124. }
  125. }
  126. /**
  127. * @param int $id
  128. * @return array|mixed|\think\Model|null
  129. * @throws \think\db\exception\DataNotFoundException
  130. * @throws \think\db\exception\DbException
  131. * @throws \think\db\exception\ModelNotFoundException
  132. * @author 吴汐
  133. * @email 442384644@qq.com
  134. * @date 2023/05/08
  135. */
  136. public function getDiyVersion(int $id)
  137. {
  138. if ($id) {
  139. $cacheKey = 'index_diy_' . $id;
  140. $where = ['id' => $id];
  141. } else {
  142. $cacheKey = 'index_diy_default';
  143. $where = ['status' => 1, 'is_del' => 0];
  144. }
  145. $data = CacheService::remember($cacheKey, function () use ($where) {
  146. return $this->dao->getOne($where, 'version,is_diy');
  147. });
  148. if (isset($data['version']) && isset($data['is_diy'])) {
  149. return $data;
  150. } else {
  151. return $this->dao->getOne($where, 'version,is_diy');
  152. }
  153. }
  154. /**
  155. * 获取页面数据
  156. * @param int $id
  157. * @return array|mixed
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. */
  162. public function getDiy($id = 0)
  163. {
  164. $field = 'name,value,is_show,is_bg_color,color_picker,bg_pic,bg_tab_val,is_bg_pic,order_status,is_diy,title';
  165. $info = CacheService::remember('diy_info_' . $id, function () use ($field, $id) {
  166. if ($id) {
  167. $info = $this->dao->getOne(['id' => $id], $field);
  168. } else {
  169. $info = $this->dao->getOne(['status' => 1, 'is_del' => 0], $field);
  170. }
  171. return $info ? $info->toArray() : [];
  172. });
  173. if ($info) {
  174. if ($info['value']) {
  175. $info['value'] = json_decode($info['value'], true);
  176. if ($info['is_diy']) {
  177. foreach ($info['value'] as &$item) {
  178. if ($item['name'] == 'customerService') {
  179. $item['routine_contact_type'] = sys_config('routine_contact_type', 0);
  180. }
  181. }
  182. return $info;
  183. } else {
  184. foreach ($info['value'] as $key => &$item) {
  185. if ($key == 'customerService') {
  186. foreach ($item as $k => &$v) {
  187. $v['routine_contact_type'] = sys_config('routine_contact_type', 0);
  188. }
  189. }
  190. }
  191. return $info['value'];
  192. }
  193. }
  194. }
  195. return [];
  196. }
  197. /**
  198. * 添加表单
  199. * @return array
  200. * @throws \FormBuilder\Exception\FormBuilderException
  201. */
  202. public function createForm()
  203. {
  204. $field = array();
  205. $title = '添加模板';
  206. $field[] = Form::input('name', '页面名称', '')->required();
  207. return create_form($title, $field, Url::buildUrl('/diy/create'), 'POST');
  208. }
  209. /**
  210. * 获取商品数据
  211. * @param array $where
  212. * @return array
  213. * @throws \think\db\exception\DataNotFoundException
  214. * @throws \think\db\exception\DbException
  215. * @throws \think\db\exception\ModelNotFoundException
  216. */
  217. public function ProductList(array $where)
  218. {
  219. /** @var StoreProductServices $StoreProductServices */
  220. $StoreProductServices = app()->make(StoreProductServices::class);
  221. /** @var StoreBargainServices $StoreBargainServices */
  222. $StoreBargainServices = app()->make(StoreBargainServices::class);
  223. /** @var $StoreCombinationServices StoreCombinationServices */
  224. $StoreCombinationServices = app()->make(StoreCombinationServices::class);
  225. /** @var $StoreSeckillServices StoreSeckillServices */
  226. $StoreSeckillServices = app()->make(StoreSeckillServices::class);
  227. $type = $where['type'];
  228. unset($where['type']);
  229. $data = [];
  230. switch ($type) {
  231. case 0:
  232. $data = $StoreProductServices->searchList($where);
  233. break;
  234. //秒杀
  235. case 2:
  236. $data = $StoreSeckillServices->getDiySeckillList($where);
  237. break;
  238. //拼团
  239. case 3:
  240. $data = $StoreCombinationServices->getDiyCombinationList($where);
  241. break;
  242. case 4:
  243. $where['is_hot'] = 1;
  244. $data = $StoreProductServices->searchList($where);
  245. break;
  246. case 5:
  247. $where['is_new'] = 1;
  248. $data = $StoreProductServices->searchList($where);
  249. break;
  250. case 6:
  251. $where['is_benefit'] = 1;
  252. $data = $StoreProductServices->searchList($where);
  253. break;
  254. case 7:
  255. $where['is_best'] = 1;
  256. $data = $StoreProductServices->searchList($where);
  257. break;
  258. //砍价
  259. case 8:
  260. $data = $StoreBargainServices->getDiyBargainList($where);
  261. break;
  262. }
  263. return $data;
  264. }
  265. /**
  266. * 前台获取首页数据接口
  267. * @param array $where
  268. */
  269. public function homeProductList(array $where, int $uid)
  270. {
  271. /** @var StoreProductServices $StoreProductServices */
  272. $StoreProductServices = app()->make(StoreProductServices::class);
  273. /** @var StoreBargainServices $StoreBargainServices */
  274. $StoreBargainServices = app()->make(StoreBargainServices::class);
  275. /** @var $StoreCombinationServices StoreCombinationServices */
  276. $StoreCombinationServices = app()->make(StoreCombinationServices::class);
  277. /** @var $StoreSeckillServices StoreSeckillServices */
  278. $StoreSeckillServices = app()->make(StoreSeckillServices::class);
  279. $type = $where['type'];
  280. $data = [];
  281. switch ($type) {
  282. case 0:
  283. $where['type'] = $where['isType'] ?? 0;
  284. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  285. break;
  286. //秒杀
  287. case 2:
  288. $data = $StoreSeckillServices->getHomeSeckillList($where);
  289. break;
  290. //拼团
  291. case 3:
  292. $data = $StoreCombinationServices->getHomeList($where);
  293. break;
  294. case 4:
  295. $where['is_hot'] = 1;
  296. $where['type'] = $where['isType'] ?? 0;
  297. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  298. break;
  299. case 5:
  300. $where['is_new'] = 1;
  301. $where['type'] = $where['isType'] ?? 0;
  302. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  303. break;
  304. case 6:
  305. $where['is_benefit'] = 1;
  306. $where['type'] = $where['isType'] ?? 0;
  307. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  308. break;
  309. case 7:
  310. $where['is_best'] = 1;
  311. $where['type'] = $where['isType'] ?? 0;
  312. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  313. break;
  314. //砍价
  315. case 8:
  316. $data = $StoreBargainServices->getHomeList($where);
  317. break;
  318. }
  319. foreach ($data['list'] as &$item) {
  320. $item['image'] = set_file_url($item['image'], sys_config('site_url'));
  321. }
  322. return $data;
  323. }
  324. /**
  325. * 分类、个人中心、一键换色
  326. * @param string $name
  327. * @return mixed
  328. */
  329. public function getColorChange(string $name)
  330. {
  331. return $this->dao->value(['template_name' => $name, 'type' => 1], 'value');
  332. }
  333. public function getMemberData()
  334. {
  335. $info = $this->dao->get(['template_name' => 'member', 'type' => 1]);
  336. $status = (int)$info['value'];
  337. $order_status = $info['order_status'] ? (int)$info['order_status'] : 1;
  338. $color_change = (int)$this->getColorChange('color_change');
  339. /** @var SystemGroupDataServices $systemGroupDataServices */
  340. $systemGroupDataServices = app()->make(SystemGroupDataServices::class);
  341. /** @var SystemGroupServices $systemGroupServices */
  342. $systemGroupServices = app()->make(SystemGroupServices::class);
  343. $menus_gid = $systemGroupServices->value(['config_name' => 'routine_my_menus'], 'id');
  344. $banner_gid = $systemGroupServices->value(['config_name' => 'routine_my_banner'], 'id');
  345. $routine_my_menus = $systemGroupDataServices->getGroupDataList(['gid' => $menus_gid]);
  346. $routine_my_menus = $routine_my_menus['list'] ?? [];
  347. $routine_my_banner = $systemGroupDataServices->getGroupDataList(['gid' => $banner_gid]);
  348. $routine_my_banner = $routine_my_banner['list'] ?? [];
  349. $my_banner_status = boolval($info['my_banner_status']);
  350. return compact('status', 'order_status', 'routine_my_menus', 'routine_my_banner', 'color_change', 'my_banner_status');
  351. }
  352. /**
  353. * 保存个人中心数据配置
  354. * @param array $data
  355. * @return bool
  356. * @throws \think\db\exception\DataNotFoundException
  357. * @throws \think\db\exception\DbException
  358. * @throws \think\db\exception\ModelNotFoundException
  359. */
  360. public function memberSaveData(array $data)
  361. {
  362. /** @var SystemGroupDataServices $systemGroupDataServices */
  363. $systemGroupDataServices = app()->make(SystemGroupDataServices::class);
  364. if (!$data['status']) throw new AdminException(100100);
  365. $info = $this->dao->get(['template_name' => 'member', 'type' => 1]);
  366. if ($info) {
  367. $info->my_banner_status = $data['my_banner_status'];
  368. $info->value = $data['status'];
  369. $info->order_status = $data['order_status'];
  370. $info->update_time = time();
  371. $res = $info->save();
  372. } else {
  373. throw new AdminException(400459);
  374. }
  375. $systemGroupDataServices->saveAllData($data['routine_my_banner'], 'routine_my_banner');
  376. $systemGroupDataServices->saveAllData($data['routine_my_menus'], 'routine_my_menus');
  377. return true;
  378. }
  379. /**
  380. * 获取底部导航
  381. * @param string $template_name
  382. * @return array|mixed
  383. */
  384. public function getNavigation(string $template_name)
  385. {
  386. $value = CacheService::remember('navigation', function () {
  387. $value = $this->dao->value(['status' => 1], 'value');
  388. if (!$value) {
  389. $value = $this->dao->value(['template_name' => 'default'], 'value');
  390. }
  391. return $value;
  392. });
  393. $navigation = [];
  394. if ($value) {
  395. $value = json_decode($value, true);
  396. foreach ($value as $item) {
  397. if (isset($item['name']) && strtolower($item['name']) === 'pagefoot') {
  398. $navigation = $item;
  399. break;
  400. }
  401. }
  402. }
  403. return $navigation;
  404. }
  405. /**
  406. * 取单个diy小程序预览二维码
  407. * @param int $id
  408. * @return string
  409. * @throws \think\db\exception\DataNotFoundException
  410. * @throws \think\db\exception\DbException
  411. * @throws \think\db\exception\ModelNotFoundException
  412. */
  413. public function getRoutineCode(int $id)
  414. {
  415. $diy = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  416. if (!$diy) {
  417. throw new AdminException(100026);
  418. }
  419. /** @var QrcodeServices $QrcodeService */
  420. $QrcodeService = app()->make(QrcodeServices::class);
  421. return $QrcodeService->getRoutineQrcodePath($id, 0, 6, [], false);
  422. }
  423. }