DiyServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. declare (strict_types=1);
  12. namespace app\services\diy;
  13. use app\services\BaseServices;
  14. use app\dao\diy\DiyDao;
  15. use app\services\other\QrcodeServices;
  16. use app\services\system\config\SystemGroupDataServices;
  17. use app\services\system\config\SystemGroupServices;
  18. use crmeb\exceptions\AdminException;
  19. use crmeb\services\SystemConfigService;
  20. use think\exception\ValidateException;
  21. /**
  22. * 首页diy
  23. * Class DiyServices
  24. * @package app\services\diy
  25. * @mixin DiyDao
  26. */
  27. class DiyServices extends BaseServices
  28. {
  29. /**
  30. /**
  31. * @var string
  32. */
  33. protected $cacheKey = 'diy_cache';
  34. /**
  35. * 数据类型
  36. * @var array
  37. */
  38. public $type = [
  39. 1 => 'DIY数据',
  40. 3 => [
  41. 'member' => '个人中心',
  42. 'category' => '分类样式',
  43. 'color_change' => '主题风格',
  44. ],
  45. 4 => '系统表单数据'
  46. ];
  47. /**
  48. * 商品详情diy默认数据
  49. * @var array
  50. */
  51. protected $productDetail = [
  52. 'price_type' => [1],//展示1:svip价2:会员价
  53. 'is_share' => true,//分享
  54. 'is_name' => true, //商品名称
  55. 'is_brand' => true,//品牌
  56. 'is_ot_price' => false,//原价
  57. 'is_sales' => true,//销量
  58. 'is_stock' => true,//库存
  59. 'is_coupon' => true,//优惠券
  60. 'is_activity' => true,//活动
  61. 'is_promotions' => true,//优惠活动
  62. 'is_specs' => true,//参数
  63. 'is_ensure' => true,//保障服务
  64. 'is_sku' => true,//规格选择
  65. 'sku_style' => 1,//规格样式
  66. 'is_store' => true,//门店
  67. 'is_reply' => true,//是否站评论
  68. 'reply_num' => 1,//评论数量
  69. 'is_discounts' => true,//是否展示搭配购
  70. 'discounts_num' => 3,//搭配狗数量
  71. 'is_recommend' => true,//是否展示优品推荐
  72. 'recommend_num' => 6,//优惠推荐数量
  73. 'is_description' => true//是否展示商品详情
  74. ];
  75. /**
  76. * DiyServices constructor.
  77. * @param DiyDao $dao
  78. */
  79. public function __construct(DiyDao $dao)
  80. {
  81. $this->dao = $dao;
  82. }
  83. /**
  84. * 获取DIY列表
  85. * @param array $where
  86. * @return array
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public function getDiyList(array $where)
  92. {
  93. [$page, $limit] = $this->getPageValue();
  94. $list = $this->dao->getDiyList($where, $page, $limit, ['is_diy', 'template_name', 'id', 'title', 'name', 'type', 'add_time', 'update_time', 'status', 'cover_image']);
  95. $count = $this->dao->count($where);
  96. return compact('list', 'count');
  97. }
  98. /**
  99. * 保存资源
  100. * @param int $id
  101. * @param array $data
  102. * @return int
  103. */
  104. public function saveData(int $id = 0, array $data = [])
  105. {
  106. if ($id) {
  107. if ($data['type'] === '') {
  108. unset($data['type']);
  109. }
  110. $data['update_time'] = time();
  111. $res = $this->dao->update($id, $data);
  112. if (!$res) throw new AdminException('修改失败');
  113. } else {
  114. $data['add_time'] = time();
  115. $data['update_time'] = time();
  116. $data['is_diy'] = 1;
  117. $res = $this->dao->save($data);
  118. if (!$res) throw new AdminException('保存失败');
  119. $id = $res->id;
  120. }
  121. $this->dao->cacheTag()->clear();
  122. $this->dao->cacheTag()->set('index_diy_' . $id, $data['version']);
  123. $this->updateCacheDiyVersion();
  124. event('diy.update');
  125. return $id;
  126. }
  127. /**
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\DbException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. * @author 等风来
  132. * @email 136327134@qq.com
  133. * @date 2022/11/9
  134. */
  135. public function updateCacheDiyVersion()
  136. {
  137. $diyInfo = $this->dao->get(['status' => 1, 'type' => 1, 'is_diy' => 1], ['id', 'version']);
  138. if (!$diyInfo) {
  139. $this->dao->cacheHander()->delete('index_diy_default');
  140. } else {
  141. $this->dao->cacheTag()->set('index_diy_default', $diyInfo['version']);
  142. }
  143. }
  144. /**
  145. * @return mixed
  146. * @throws \Throwable
  147. * @author 等风来
  148. * @email 136327134@qq.com
  149. * @date 2022/11/9
  150. */
  151. public function getCacheDiyVersion()
  152. {
  153. return $this->dao->cacheTag()->remember('index_diy_default', function () {
  154. return $this->dao->value(['status' => 1, 'type' => 1, 'is_diy' => 1], 'version');
  155. });
  156. }
  157. /**
  158. * 删除DIY模板
  159. * @param int $id
  160. */
  161. public function del(int $id)
  162. {
  163. if ($id == 1) throw new AdminException('默认模板不能删除');
  164. $count = $this->dao->getCount(['id' => $id, 'status' => 1]);
  165. if ($count) throw new AdminException('该模板使用中,无法删除');
  166. $res = $this->dao->update($id, ['is_del' => 1]);
  167. if (!$res) throw new AdminException('删除失败,请稍后再试');
  168. $this->dao->cacheTag()->clear();
  169. $this->updateCacheDiyVersion();
  170. $this->dao->cacheHander()->delete('index_diy_' . $id);
  171. event('diy.update');
  172. }
  173. /**
  174. * 设置模板使用
  175. * @param int $id
  176. */
  177. public function setStatus(int $id)
  178. {
  179. $info = $this->dao->get($id);
  180. if (!$info) {
  181. throw new AdminException('默认不存在');
  182. }
  183. if (!$info['is_show']) {
  184. throw new AdminException('请编辑开启页面状态后,在设置为首页');
  185. }
  186. $this->dao->update($info['type'], ['status' => 0], 'type');
  187. $this->dao->update($id, ['status' => 1, 'update_time' => time()]);
  188. $this->dao->cacheTag()->clear();
  189. $this->updateCacheDiyVersion();
  190. event('diy.update');
  191. return ['status' => 1, 'msg' => '设置成功'];
  192. }
  193. /**
  194. * 获取页面数据
  195. * @return array|\think\Model|null
  196. * @throws \think\db\exception\DataNotFoundException
  197. * @throws \think\db\exception\DbException
  198. * @throws \think\db\exception\ModelNotFoundException
  199. */
  200. public function getDiy(string $name)
  201. {
  202. $data = [];
  203. if ($name == '') {
  204. $info = $this->dao->getOne(['status' => 1, 'type' => 1]);
  205. } else {
  206. $info = $this->dao->getOne(['template_name' => $name]);
  207. }
  208. if ($info) {
  209. $info = $info->toArray();
  210. $data = json_decode($info['value'], true);
  211. }
  212. return $data;
  213. }
  214. /**
  215. * 返回当前diy版本
  216. * @param int $id
  217. * @return mixed
  218. * @throws \Throwable
  219. * @author 等风来
  220. * @email 136327134@qq.com
  221. * @date 2022/11/9
  222. */
  223. public function getDiyVersion(int $id)
  224. {
  225. if ($id) {
  226. return $this->dao->cacheTag()->remember('index_diy_' . $id, function () use ($id) {
  227. return $this->dao->value(['id' => $id], 'version');
  228. });
  229. } else {
  230. return $this->getCacheDiyVersion();
  231. }
  232. }
  233. /**
  234. * 获取diy详细数据
  235. * @param int $id
  236. * @return array|object
  237. * @throws \think\db\exception\DataNotFoundException
  238. * @throws \think\db\exception\DbException
  239. * @throws \think\db\exception\ModelNotFoundException
  240. */
  241. public function getDiyInfo(int $id)
  242. {
  243. return $this->dao->cacheTag()->remember('diy_info_' . $id, function () use ($id) {
  244. $field = ['title', 'value', 'is_show', 'is_bg_color', 'color_picker', 'bg_pic', 'bg_tab_val', 'is_bg_pic', 'order_status'];
  245. $diyInfo = $this->dao->get($id ?: ['status' => 1, 'type' => 1, 'is_diy' => 1], $field);
  246. if (!$diyInfo) {
  247. $diyInfo = $this->dao->get(['template_name' => 'default'], $field);
  248. }
  249. if ($diyInfo) {
  250. $diyInfo = $diyInfo->toArray();
  251. $diyInfo['value'] = json_decode($diyInfo['value'], true);
  252. $value = [];
  253. foreach ($diyInfo['value'] as $key => $item) {
  254. if ($item['name'] == 'customerService') {
  255. $item['routine_contact_type'] = SystemConfigService::get('routine_contact_type', 0);
  256. }
  257. if ($item['name'] == 'promotionList') {
  258. unset($item['titleShow']['title'],
  259. $item['opriceShow']['title'],
  260. $item['priceShow']['title'],
  261. $item['couponShow']['title']);
  262. }
  263. if ($item['name'] == 'activeParty') {
  264. unset($item['titleConfig']['place'],
  265. $item['titleConfig']['max'],
  266. $item['desConfig']['place'],
  267. $item['desConfig']['max']
  268. );
  269. if (isset($item['menuConfig']['list']['info'])) {
  270. foreach ($item['menuConfig']['list']['info'] as $k => $v) {
  271. unset($v['tips'], $v['max']);
  272. $item['menuConfig']['list']['info'][$k] = $v;
  273. }
  274. }
  275. }
  276. if ($item['name'] == 'pageFoot' && !$id) {
  277. continue;
  278. }
  279. $value[$key] = $item;
  280. }
  281. $diyInfo['value'] = $value;
  282. return $diyInfo;
  283. } else {
  284. return [];
  285. }
  286. });
  287. }
  288. /**
  289. * 获取底部导航
  290. * @param string $template_name
  291. * @return array|mixed
  292. */
  293. public function getNavigation(string $template_name)
  294. {
  295. return $this->dao->cacheTag()->remember('navigation_' . $template_name, function () use ($template_name) {
  296. $value = $this->dao->value($template_name ? ['template_name' => $template_name] : ['status' => 1, 'type' => 1], 'value');
  297. if (!$value) {
  298. $value = $this->dao->value(['template_name' => 'default'], 'value');
  299. }
  300. $navigation = [];
  301. if ($value) {
  302. $value = json_decode($value, true);
  303. foreach ($value as $item) {
  304. if (isset($item['name']) && strtolower($item['name']) === 'pagefoot') {
  305. $navigation = $item;
  306. break;
  307. }
  308. }
  309. }
  310. return $navigation;
  311. });
  312. }
  313. /**
  314. * 获取换色/分类数据
  315. * @throws \think\db\exception\DataNotFoundException
  316. * @throws \think\db\exception\DbException
  317. * @throws \think\db\exception\ModelNotFoundException
  318. */
  319. public function getColorChange(string $name)
  320. {
  321. $key = 'color_change_' . $name . '_3';
  322. return $this->dao->cacheStrRemember($key, function () use ($name) {
  323. return $this->dao->value(['template_name' => $name, 'type' => 3], 'value');
  324. });
  325. }
  326. /**
  327. * 取单个diy小程序预览二维码
  328. * @param int $id
  329. * @return string
  330. * @throws \think\db\exception\DataNotFoundException
  331. * @throws \think\db\exception\DbException
  332. * @throws \think\db\exception\ModelNotFoundException
  333. */
  334. public function getRoutineCode(int $id)
  335. {
  336. $diy = $this->dao->getOne(['id' => $id, 'is_del' => 0]);
  337. if (!$diy) {
  338. throw new AdminException('数据不存在');
  339. }
  340. $type = $diy['status'] ? 8 : 6;
  341. /** @var QrcodeServices $QrcodeService */
  342. $QrcodeService = app()->make(QrcodeServices::class);
  343. $image = $QrcodeService->getRoutineQrcodePath($id, 0, $type, [], true);
  344. return $image;
  345. }
  346. /**
  347. * 获取个人中心数据
  348. * @return array
  349. * @throws \think\db\exception\DataNotFoundException
  350. * @throws \think\db\exception\DbException
  351. * @throws \think\db\exception\ModelNotFoundException
  352. */
  353. public function getMemberData()
  354. {
  355. $key = 'diy_data_member_3';
  356. $info = $this->dao->cacheRemember($key, function () {
  357. $info = $this->dao->get(['template_name' => 'member', 'type' => 3]);
  358. if ($info) {
  359. return $info->toArray();
  360. } else {
  361. return null;
  362. }
  363. });
  364. if (!$info) {
  365. throw new ValidateException('数据不存在');
  366. }
  367. $value = json_decode($info['value'], true);
  368. $data = [];
  369. $data['status'] = (int)($value['status'] ?? 1);
  370. $data['vip_type'] = (int)($value['vip_type'] ?? 1);
  371. $data['newcomer_status'] = (int)($value['newcomer_status'] ?? 1);
  372. $data['newcomer_style'] = (int)($value['newcomer_style'] ?? 1);
  373. $data['order_status'] = $info['order_status'] ? (int)$info['order_status'] : 1;
  374. $data['menu_status'] = $info['menu_status'] ? (int)$info['menu_status'] : 1;
  375. $data['service_status'] = $info['service_status'] ? (int)$info['service_status'] : 1;
  376. $data['my_banner_status'] = !!((int)$info['my_banner_status'] ?? 1);
  377. $data['color_change'] = (int)$this->getColorChange('color_change');
  378. /** @var SystemGroupDataServices $systemGroupDataServices */
  379. $systemGroupDataServices = app()->make(SystemGroupDataServices::class);
  380. /** @var SystemGroupServices $systemGroupServices */
  381. $systemGroupServices = app()->make(SystemGroupServices::class);
  382. $menus_gid = $systemGroupServices->value(['config_name' => 'routine_my_menus'], 'id');
  383. $banner_gid = $systemGroupServices->value(['config_name' => 'routine_my_banner'], 'id');
  384. $routine_my_menus = $systemGroupDataServices->getGroupDataList(['gid' => $menus_gid]);
  385. $routine_my_menus = $routine_my_menus['list'] ?? [];
  386. $url = ['/kefu/mobile_list', '/pages/store_spread/index', '/pages/admin/order_cancellation/index', '/pages/admin/order/index'];
  387. foreach ($routine_my_menus as &$item) {
  388. if (!isset($item['type']) || !$item['type']) {
  389. $item['type'] = in_array($item['url'], $url) ? 2 : 1;
  390. }
  391. }
  392. $data['routine_my_menus'] = $routine_my_menus;
  393. $routine_my_banner = $systemGroupDataServices->getGroupDataList(['gid' => $banner_gid]);
  394. $routine_my_banner = $routine_my_banner['list'] ?? [];
  395. $data['routine_my_banner'] = $routine_my_banner;
  396. return $data;
  397. }
  398. /**
  399. * 保存个人中心数据配置
  400. * @param array $data
  401. * @return bool
  402. * @throws \think\db\exception\DataNotFoundException
  403. * @throws \think\db\exception\DbException
  404. * @throws \think\db\exception\ModelNotFoundException
  405. */
  406. public function memberSaveData(array $data)
  407. {
  408. $key = 'diy_data_member_3';
  409. /** @var SystemGroupDataServices $systemGroupDataServices */
  410. $systemGroupDataServices = app()->make(SystemGroupDataServices::class);
  411. if (!isset($data['status']) || !$data['status']) throw new AdminException('参数错误');
  412. $info = $this->dao->get(['template_name' => 'member', 'type' => 3]);
  413. $routine_my_banner = $data['routine_my_banner'] ?? [];
  414. $routine_my_menus = $data['routine_my_menus'] ?? [];
  415. $value = ['status' => $data['status'], 'vip_type' => $data['vip_type'] ?? 1, 'newcomer_status' => $data['newcomer_status'] ?? 1, 'newcomer_style' => $data['newcomer_style'] ?? 1];
  416. $data['value'] = json_encode($value);
  417. $data['my_banner_status'] = $data['my_banner_status'] && $data['routine_my_banner'] ? 1 : 0;
  418. unset($data['status'], $data['routine_my_banner'], $data['routine_my_menus']);
  419. if ($info) {
  420. $data['update_time'] = time();
  421. if (!$this->dao->update($info['id'], $data)) {
  422. throw new AdminException('编辑保存失败');
  423. }
  424. $data = array_merge($info->toArray(), $data);
  425. $this->dao->cacheUpdate($data, $key);
  426. } else {
  427. throw new AdminException('个人中心模板不存在');
  428. }
  429. $systemGroupDataServices->saveAllData($routine_my_banner, 'routine_my_banner');
  430. $systemGroupDataServices->saveAllData($routine_my_menus, 'routine_my_menus');
  431. return true;
  432. }
  433. /**
  434. * 获取商品diy数据
  435. * @return array
  436. * @throws \think\db\exception\DataNotFoundException
  437. * @throws \think\db\exception\DbException
  438. * @throws \think\db\exception\ModelNotFoundException
  439. */
  440. public function getProductDetailDiy()
  441. {
  442. $name = 'product_detail';
  443. $key = $this->cacheKey . '_' . $name;
  444. $default = $this->productDetail;
  445. return $this->dao->cacheTag()->remember($key, function () use ($name, $default) {
  446. $info = $this->dao->get(['template_name' => $name, 'type' => 3]);
  447. if ($info) {
  448. $result = json_decode($info['value'], true);
  449. $result = array_merge($default, array_intersect_key($result, $default));
  450. } else {
  451. $result = $default;
  452. }
  453. return $result;
  454. });
  455. }
  456. /**
  457. * 保存商品diy数据
  458. * @param array $content
  459. * @return bool
  460. * @throws \think\db\exception\DataNotFoundException
  461. * @throws \think\db\exception\DbException
  462. * @throws \think\db\exception\ModelNotFoundException
  463. */
  464. public function saveProductDetailDiy(array $content)
  465. {
  466. $name = 'product_detail';
  467. $key = $this->cacheKey . '_' . $name;
  468. $info = $this->dao->get(['template_name' => $name, 'type' => 3]);
  469. $data = [];
  470. $data['value'] = json_encode($content);
  471. $time = time();
  472. if ($info) {
  473. $data['update_time'] = $time;
  474. if (!$this->dao->update($info['id'], $data)) {
  475. throw new AdminException('编辑保存失败');
  476. }
  477. } else {
  478. $data['template_name'] = 'product_detail';
  479. $data['type'] = 3;
  480. $data['add_time'] = $time;
  481. $this->dao->save($data);
  482. }
  483. $this->dao->cacheTag()->clear();
  484. return true;
  485. }
  486. }