DiyServices.php 16 KB

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