DiyServices.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 mixed|string|null
  128. * @author 等风来
  129. * @email 136327134@qq.com
  130. * @date 2023/2/8
  131. */
  132. public function getDiyVersion(int $id)
  133. {
  134. if ($id) {
  135. return $this->cacheDriver()->remember('index_diy_' . $id, function () use ($id) {
  136. return $this->dao->getOne(['id' => $id], 'version,is_diy');
  137. });
  138. } else {
  139. return $this->cacheDriver()->remember('index_diy_default', function () {
  140. return $this->dao->getOne(['status' => 1, 'is_del' => 0], 'version,is_diy');
  141. });
  142. }
  143. }
  144. /**
  145. * 获取页面数据
  146. * @param int $id
  147. * @return array|mixed
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\DbException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. */
  152. public function getDiy($id = 0)
  153. {
  154. $field = 'name,value,is_show,is_bg_color,color_picker,bg_pic,bg_tab_val,is_bg_pic,order_status,is_diy,title';
  155. $info = $this->cacheDriver()->remember('diy_info_' . $id, function () use ($field, $id) {
  156. if ($id) {
  157. $info = $this->dao->getOne(['id' => $id], $field);
  158. } else {
  159. $info = $this->dao->getOne(['status' => 1, 'is_del' => 0], $field);
  160. }
  161. return $info ? $info->toArray() : [];
  162. });
  163. if ($info) {
  164. if ($info['value']) {
  165. $info['value'] = json_decode($info['value'], true);
  166. if ($info['is_diy']) {
  167. foreach ($info['value'] as &$item) {
  168. if ($item['name'] == 'customerService') {
  169. $item['routine_contact_type'] = sys_config('routine_contact_type', 0);
  170. }
  171. }
  172. return $info;
  173. } else {
  174. foreach ($info['value'] as $key => &$item) {
  175. if ($key == 'customerService') {
  176. foreach ($item as $k => &$v) {
  177. $v['routine_contact_type'] = sys_config('routine_contact_type', 0);
  178. }
  179. }
  180. }
  181. return $info['value'];
  182. }
  183. }
  184. }
  185. return [];
  186. }
  187. /**
  188. * 添加表单
  189. * @return array
  190. * @throws \FormBuilder\Exception\FormBuilderException
  191. */
  192. public function createForm()
  193. {
  194. $field = array();
  195. $title = '添加模板';
  196. $field[] = Form::input('name', '页面名称', '')->required();
  197. return create_form($title, $field, Url::buildUrl('/diy/create'), 'POST');
  198. }
  199. /**
  200. * 获取商品数据
  201. * @param array $where
  202. * @return array
  203. * @throws \think\db\exception\DataNotFoundException
  204. * @throws \think\db\exception\DbException
  205. * @throws \think\db\exception\ModelNotFoundException
  206. */
  207. public function ProductList(array $where)
  208. {
  209. /** @var StoreProductServices $StoreProductServices */
  210. $StoreProductServices = app()->make(StoreProductServices::class);
  211. /** @var StoreBargainServices $StoreBargainServices */
  212. $StoreBargainServices = app()->make(StoreBargainServices::class);
  213. /** @var $StoreCombinationServices StoreCombinationServices */
  214. $StoreCombinationServices = app()->make(StoreCombinationServices::class);
  215. /** @var $StoreSeckillServices StoreSeckillServices */
  216. $StoreSeckillServices = app()->make(StoreSeckillServices::class);
  217. $type = $where['type'];
  218. unset($where['type']);
  219. $data = [];
  220. switch ($type) {
  221. case 0:
  222. $data = $StoreProductServices->searchList($where);
  223. break;
  224. //秒杀
  225. case 2:
  226. $data = $StoreSeckillServices->getDiySeckillList($where);
  227. break;
  228. //拼团
  229. case 3:
  230. $data = $StoreCombinationServices->getDiyCombinationList($where);
  231. break;
  232. case 4:
  233. $where['is_hot'] = 1;
  234. $data = $StoreProductServices->searchList($where);
  235. break;
  236. case 5:
  237. $where['is_new'] = 1;
  238. $data = $StoreProductServices->searchList($where);
  239. break;
  240. case 6:
  241. $where['is_benefit'] = 1;
  242. $data = $StoreProductServices->searchList($where);
  243. break;
  244. case 7:
  245. $where['is_best'] = 1;
  246. $data = $StoreProductServices->searchList($where);
  247. break;
  248. //砍价
  249. case 8:
  250. $data = $StoreBargainServices->getDiyBargainList($where);
  251. break;
  252. }
  253. return $data;
  254. }
  255. /**
  256. * 前台获取首页数据接口
  257. * @param array $where
  258. */
  259. public function homeProductList(array $where, int $uid)
  260. {
  261. /** @var StoreProductServices $StoreProductServices */
  262. $StoreProductServices = app()->make(StoreProductServices::class);
  263. /** @var StoreBargainServices $StoreBargainServices */
  264. $StoreBargainServices = app()->make(StoreBargainServices::class);
  265. /** @var $StoreCombinationServices StoreCombinationServices */
  266. $StoreCombinationServices = app()->make(StoreCombinationServices::class);
  267. /** @var $StoreSeckillServices StoreSeckillServices */
  268. $StoreSeckillServices = app()->make(StoreSeckillServices::class);
  269. $type = $where['type'];
  270. $data = [];
  271. switch ($type) {
  272. case 0:
  273. $where['type'] = $where['isType'] ?? 0;
  274. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  275. break;
  276. //秒杀
  277. case 2:
  278. $data = $StoreSeckillServices->getHomeSeckillList($where);
  279. break;
  280. //拼团
  281. case 3:
  282. $data = $StoreCombinationServices->getHomeList($where);
  283. break;
  284. case 4:
  285. $where['is_hot'] = 1;
  286. $where['type'] = $where['isType'] ?? 0;
  287. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  288. break;
  289. case 5:
  290. $where['is_new'] = 1;
  291. $where['type'] = $where['isType'] ?? 0;
  292. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  293. break;
  294. case 6:
  295. $where['is_benefit'] = 1;
  296. $where['type'] = $where['isType'] ?? 0;
  297. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  298. break;
  299. case 7:
  300. $where['is_best'] = 1;
  301. $where['type'] = $where['isType'] ?? 0;
  302. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  303. break;
  304. //砍价
  305. case 8:
  306. $data = $StoreBargainServices->getHomeList($where);
  307. break;
  308. }
  309. foreach ($data['list'] as &$item) {
  310. $item['image'] = set_file_url($item['image'], sys_config('site_url'));
  311. }
  312. return $data;
  313. }
  314. /**
  315. * 分类、个人中心、一键换色
  316. * @param string $name
  317. * @return mixed
  318. */
  319. public function getColorChange(string $name)
  320. {
  321. return $this->dao->value(['template_name' => $name, 'type' => 1], 'value');
  322. }
  323. public function getMemberData()
  324. {
  325. $info = $this->dao->get(['template_name' => 'member', 'type' => 1]);
  326. $status = (int)$info['value'];
  327. $order_status = $info['order_status'] ? (int)$info['order_status'] : 1;
  328. $color_change = (int)$this->getColorChange('color_change');
  329. /** @var SystemGroupDataServices $systemGroupDataServices */
  330. $systemGroupDataServices = app()->make(SystemGroupDataServices::class);
  331. /** @var SystemGroupServices $systemGroupServices */
  332. $systemGroupServices = app()->make(SystemGroupServices::class);
  333. $menus_gid = $systemGroupServices->value(['config_name' => 'routine_my_menus'], 'id');
  334. $banner_gid = $systemGroupServices->value(['config_name' => 'routine_my_banner'], 'id');
  335. $routine_my_menus = $systemGroupDataServices->getGroupDataList(['gid' => $menus_gid]);
  336. $routine_my_menus = $routine_my_menus['list'] ?? [];
  337. $routine_my_banner = $systemGroupDataServices->getGroupDataList(['gid' => $banner_gid]);
  338. $routine_my_banner = $routine_my_banner['list'] ?? [];
  339. $my_banner_status = boolval($info['my_banner_status']);
  340. return compact('status', 'order_status', 'routine_my_menus', 'routine_my_banner', 'color_change', 'my_banner_status');
  341. }
  342. /**
  343. * 保存个人中心数据配置
  344. * @param array $data
  345. * @return bool
  346. * @throws \think\db\exception\DataNotFoundException
  347. * @throws \think\db\exception\DbException
  348. * @throws \think\db\exception\ModelNotFoundException
  349. */
  350. public function memberSaveData(array $data)
  351. {
  352. /** @var SystemGroupDataServices $systemGroupDataServices */
  353. $systemGroupDataServices = app()->make(SystemGroupDataServices::class);
  354. if (!$data['status']) throw new AdminException(100100);
  355. $info = $this->dao->get(['template_name' => 'member', 'type' => 1]);
  356. if ($info) {
  357. $info->my_banner_status = $data['my_banner_status'];
  358. $info->value = $data['status'];
  359. $info->order_status = $data['order_status'];
  360. $info->update_time = time();
  361. $res = $info->save();
  362. } else {
  363. throw new AdminException(400459);
  364. }
  365. $systemGroupDataServices->saveAllData($data['routine_my_banner'], 'routine_my_banner');
  366. $systemGroupDataServices->saveAllData($data['routine_my_menus'], 'routine_my_menus');
  367. return true;
  368. }
  369. /**
  370. * 获取底部导航
  371. * @param string $template_name
  372. * @return array|mixed
  373. */
  374. public function getNavigation(string $template_name)
  375. {
  376. $value = $this->cacheDriver()->remember('navigation', function () {
  377. $value = $this->dao->value(['status' => 1], 'value');
  378. if (!$value) {
  379. $value = $this->dao->value(['template_name' => 'default'], 'value');
  380. }
  381. return $value;
  382. });
  383. $navigation = [];
  384. if ($value) {
  385. $value = json_decode($value, true);
  386. foreach ($value as $item) {
  387. if (isset($item['name']) && strtolower($item['name']) === 'pagefoot') {
  388. $navigation = $item;
  389. break;
  390. }
  391. }
  392. }
  393. return $navigation;
  394. }
  395. /**
  396. * 取单个diy小程序预览二维码
  397. * @param int $id
  398. * @return string
  399. */
  400. public function getRoutineCode(int $id)
  401. {
  402. $diy = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  403. if (!$diy) {
  404. throw new AdminException(100026);
  405. }
  406. /** @var QrcodeServices $QrcodeService */
  407. $QrcodeService = app()->make(QrcodeServices::class);
  408. $image = $QrcodeService->getRoutineQrcodePath($id, 0, 6, [], false);
  409. return $image;
  410. }
  411. }