DiyServices.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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\activity\StoreBargainServices;
  14. use app\services\activity\StoreCombinationServices;
  15. use app\services\activity\StoreSeckillServices;
  16. use app\services\BaseServices;
  17. use app\dao\diy\DiyDao;
  18. use app\services\product\product\StoreProductServices;
  19. use app\services\system\config\SystemGroupDataServices;
  20. use app\services\system\config\SystemGroupServices;
  21. use crmeb\exceptions\AdminException;
  22. use crmeb\services\FormBuilder as Form;
  23. use think\facade\Route as Url;
  24. use think\Log;
  25. /**
  26. *
  27. * Class DiyServices
  28. * @package app\services\diy
  29. */
  30. class DiyServices extends BaseServices
  31. {
  32. /**
  33. * DiyServices constructor.
  34. * @param DiyDao $dao
  35. */
  36. public function __construct(DiyDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * 获取DIY列表
  42. * @param array $where
  43. * @return array
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public function getDiyList(array $where)
  49. {
  50. [$page, $limit] = $this->getPageValue();
  51. $where['type'] = -1;
  52. $list = $this->dao->getDiyList($where, $page, $limit);
  53. $count = $this->dao->count($where);
  54. return compact('list', 'count');
  55. }
  56. /**
  57. * 保存资源
  58. * @param int $id
  59. * @param array $data
  60. */
  61. public function saveData(int $id = 0, array $data)
  62. {
  63. if ($id) {
  64. $data['update_time'] = time();
  65. $res = $this->dao->update($id, $data);
  66. } else {
  67. $data['add_time'] = time();
  68. $data['update_time'] = time();
  69. $res = $this->dao->save($data);
  70. }
  71. if (!$res) throw new AdminException('保存失败');
  72. }
  73. /**
  74. * 删除DIY模板
  75. * @param int $id
  76. */
  77. public function del(int $id)
  78. {
  79. if ($id == 1) throw new AdminException('默认模板不能删除');
  80. $count = $this->dao->getCount(['id' => $id, 'status' => 1]);
  81. if ($count) throw new AdminException('该模板使用中,无法删除');
  82. $res = $this->dao->update($id, ['is_del' => 1]);
  83. if (!$res) throw new AdminException('删除失败,请稍后再试');
  84. }
  85. /**
  86. * 设置模板使用
  87. * @param int $id
  88. */
  89. public function setStatus(int $id)
  90. {
  91. $this->dao->update([['id', '<>', $id]], ['status' => 0]);
  92. $this->dao->update($id, ['status' => 1, 'type' => 0, 'update_time' => time()]);
  93. }
  94. /**
  95. * 获取页面数据
  96. * @return array|\think\Model|null
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. public function getDiy($name)
  102. {
  103. $data = [];
  104. if ($name == '') {
  105. $info = $this->dao->getOne(['status' => 1]);
  106. } else {
  107. $info = $this->dao->getOne(['template_name' => $name]);
  108. }
  109. if ($info) {
  110. $info = $info->toArray();
  111. if ($info['value']) {
  112. $data = json_decode($info['value'], true);
  113. foreach ($data as $key => &$item) {
  114. if ($key == 'customerService') {
  115. foreach ($item as $k => &$v) {
  116. $v['routine_contact_type'] = sys_config('routine_contact_type', 0);
  117. }
  118. }
  119. }
  120. }
  121. }
  122. return $data;
  123. }
  124. /**
  125. * 添加表单
  126. * @return array
  127. * @throws \FormBuilder\Exception\FormBuilderException
  128. */
  129. public function createForm()
  130. {
  131. $field = array();
  132. $title = '添加模板';
  133. $field[] = Form::input('name', '页面名称', '')->required();
  134. $field[] = Form::input('template_name', '页面类型', '')->required();
  135. return create_form($title, $field, Url::buildUrl('/diy/create'), 'POST');
  136. }
  137. /**
  138. * 获取商品数据
  139. * @param array $where
  140. * @return array
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\DbException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. */
  145. public function ProductList(array $where)
  146. {
  147. /** @var StoreProductServices $StoreProductServices */
  148. $StoreProductServices = app()->make(StoreProductServices::class);
  149. /** @var StoreBargainServices $StoreBargainServices */
  150. $StoreBargainServices = app()->make(StoreBargainServices::class);
  151. /** @var $StoreCombinationServices StoreCombinationServices */
  152. $StoreCombinationServices = app()->make(StoreCombinationServices::class);
  153. /** @var $StoreSeckillServices StoreSeckillServices */
  154. $StoreSeckillServices = app()->make(StoreSeckillServices::class);
  155. $type = $where['type'];
  156. unset($where['type']);
  157. $data = [];
  158. switch ($type) {
  159. case 0:
  160. $data = $StoreProductServices->searchList($where);
  161. break;
  162. //秒杀
  163. case 2:
  164. $data = $StoreSeckillServices->getDiySeckillList($where);
  165. break;
  166. //拼团
  167. case 3:
  168. $data = $StoreCombinationServices->getDiyCombinationList($where);
  169. break;
  170. case 4:
  171. $where['is_hot'] = 1;
  172. $data = $StoreProductServices->searchList($where);
  173. break;
  174. case 5:
  175. $where['is_new'] = 1;
  176. $data = $StoreProductServices->searchList($where);
  177. break;
  178. case 6:
  179. $where['is_benefit'] = 1;
  180. $data = $StoreProductServices->searchList($where);
  181. break;
  182. case 7:
  183. $where['is_best'] = 1;
  184. $data = $StoreProductServices->searchList($where);
  185. break;
  186. //砍价
  187. case 8:
  188. $data = $StoreBargainServices->getDiyBargainList($where);
  189. break;
  190. }
  191. return $data;
  192. }
  193. /**
  194. * 前台获取首页数据接口
  195. * @param array $where
  196. */
  197. public function homeProductList(array $where, int $uid)
  198. {
  199. /** @var StoreProductServices $StoreProductServices */
  200. $StoreProductServices = app()->make(StoreProductServices::class);
  201. /** @var StoreBargainServices $StoreBargainServices */
  202. $StoreBargainServices = app()->make(StoreBargainServices::class);
  203. /** @var $StoreCombinationServices StoreCombinationServices */
  204. $StoreCombinationServices = app()->make(StoreCombinationServices::class);
  205. /** @var $StoreSeckillServices StoreSeckillServices */
  206. $StoreSeckillServices = app()->make(StoreSeckillServices::class);
  207. $type = $where['type'];
  208. $data = [];
  209. switch ($type) {
  210. case 0:
  211. $where['type'] = $where['isType'] ?? 0;
  212. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  213. break;
  214. //秒杀
  215. case 2:
  216. $data = $StoreSeckillServices->getHomeSeckillList($where);
  217. break;
  218. //拼团
  219. case 3:
  220. $data = $StoreCombinationServices->getHomeList($where);
  221. break;
  222. case 4:
  223. $where['is_hot'] = 1;
  224. $where['type'] = $where['isType'] ?? 0;
  225. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  226. break;
  227. case 5:
  228. $where['is_new'] = 1;
  229. $where['type'] = $where['isType'] ?? 0;
  230. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  231. break;
  232. case 6:
  233. $where['is_benefit'] = 1;
  234. $where['type'] = $where['isType'] ?? 0;
  235. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  236. break;
  237. case 7:
  238. $where['is_best'] = 1;
  239. $where['type'] = $where['isType'] ?? 0;
  240. $data['list'] = $StoreProductServices->getGoodsList($where, $uid);
  241. break;
  242. //砍价
  243. case 8:
  244. $data = $StoreBargainServices->getHomeList($where);
  245. break;
  246. }
  247. return $data;
  248. }
  249. /**
  250. * 分类、个人中心、一键换色
  251. * @param string $name
  252. * @return mixed
  253. */
  254. public function getColorChange(string $name)
  255. {
  256. return $this->dao->value(['template_name' => $name, 'type' => 1], 'value');
  257. }
  258. public function getMemberData()
  259. {
  260. $info = $this->dao->get(['template_name' => 'member', 'type' => 1]);
  261. $status = (int)$info['value'];
  262. $order_status = $info['order_status'] ? (int)$info['order_status'] : 1;
  263. $color_change = (int)$this->getColorChange('color_change');
  264. /** @var SystemGroupDataServices $systemGroupDataServices */
  265. $systemGroupDataServices = app()->make(SystemGroupDataServices::class);
  266. /** @var SystemGroupServices $systemGroupServices */
  267. $systemGroupServices = app()->make(SystemGroupServices::class);
  268. $menus_gid = $systemGroupServices->value(['config_name' => 'routine_my_menus'], 'id');
  269. $banner_gid = $systemGroupServices->value(['config_name' => 'routine_my_banner'], 'id');
  270. $routine_my_menus = $systemGroupDataServices->getGroupDataList(['gid' => $menus_gid]);
  271. $routine_my_menus = $routine_my_menus['list'] ?? [];
  272. $routine_my_banner = $systemGroupDataServices->getGroupDataList(['gid' => $banner_gid]);
  273. $routine_my_banner = $routine_my_banner['list'] ?? [];
  274. $my_banner_status = $routine_my_banner[0]['status'] ?? 1;
  275. return compact('status', 'order_status', 'routine_my_menus', 'routine_my_banner', 'color_change', 'my_banner_status');
  276. }
  277. /**
  278. * 保存个人中心数据配置
  279. * @param array $data
  280. * @return bool
  281. * @throws \think\db\exception\DataNotFoundException
  282. * @throws \think\db\exception\DbException
  283. * @throws \think\db\exception\ModelNotFoundException
  284. */
  285. public function memberSaveData(array $data)
  286. {
  287. /** @var SystemGroupDataServices $systemGroupDataServices */
  288. $systemGroupDataServices = app()->make(SystemGroupDataServices::class);
  289. if (!$data['status']) throw new AdminException('参数错误');
  290. $info = $this->dao->get(['template_name' => 'member', 'type' => 1]);
  291. if ($info) {
  292. $info->my_banner_status = $data['my_banner_status'];
  293. $info->value = $data['status'];
  294. $info->order_status = $data['order_status'];
  295. $info->update_time = time();
  296. $res = $info->save();
  297. } else {
  298. throw new AdminException('个人中心模板不存在');
  299. }
  300. if ($data['routine_my_banner']) $res1 = $systemGroupDataServices->saveAllData($data['routine_my_banner'], 'routine_my_banner');
  301. if ($data['routine_my_menus']) $res1 = $systemGroupDataServices->saveAllData($data['routine_my_menus'], 'routine_my_menus');
  302. return true;
  303. }
  304. /**
  305. * 获取底部导航
  306. * @param string $template_name
  307. * @return array|mixed
  308. */
  309. public function getNavigation(string $template_name)
  310. {
  311. if ($template_name) {
  312. $value = $this->dao->value(['template_name' => $template_name], 'value');
  313. } else {
  314. $value = $this->dao->value(['status' => 1, 'type' => 1], 'value');
  315. if (!$value) {
  316. $value = $this->dao->value(['template_name' => 'default'], 'value');
  317. }
  318. }
  319. $navigation = [];
  320. if ($value) {
  321. $value = json_decode($value, true);
  322. foreach ($value as $item) {
  323. if (isset($item['name']) && strtolower($item['name']) === 'pagefoot') {
  324. $navigation = $item;
  325. break;
  326. }
  327. }
  328. }
  329. return $navigation;
  330. }
  331. }