DiyServices.php 15 KB

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