DiyServices.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 crmeb\exceptions\AdminException;
  20. use crmeb\services\FormBuilder as Form;
  21. use think\facade\Route as Url;
  22. use think\Log;
  23. /**
  24. *
  25. * Class DiyServices
  26. * @package app\services\diy
  27. */
  28. class DiyServices extends BaseServices
  29. {
  30. /**
  31. * DiyServices constructor.
  32. * @param DiyDao $dao
  33. */
  34. public function __construct(DiyDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * 获取DIY列表
  40. * @param array $where
  41. * @return array
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function getDiyList(array $where)
  47. {
  48. [$page, $limit] = $this->getPageValue();
  49. $list = $this->dao->getDiyList($where, $page, $limit);
  50. $count = $this->dao->count($where);
  51. return compact('list', 'count');
  52. }
  53. /**
  54. * 保存资源
  55. * @param int $id
  56. * @param array $data
  57. */
  58. public function saveData(int $id = 0, array $data)
  59. {
  60. if ($id) {
  61. $data['update_time'] = time();
  62. $res = $this->dao->update($id, $data);
  63. } else {
  64. $data['add_time'] = time();
  65. $data['update_time'] = time();
  66. $res = $this->dao->save($data);
  67. }
  68. if (!$res) throw new AdminException('保存失败');
  69. }
  70. /**
  71. * 删除DIY模板
  72. * @param int $id
  73. */
  74. public function del(int $id)
  75. {
  76. if ($id == 1) throw new AdminException('默认模板不能删除');
  77. $count = $this->dao->getCount(['id' => $id, 'status' => 1]);
  78. if ($count) throw new AdminException('该模板使用中,无法删除');
  79. $res = $this->dao->update($id, ['is_del' => 1]);
  80. if (!$res) throw new AdminException('删除失败,请稍后再试');
  81. }
  82. /**
  83. * 设置模板使用
  84. * @param int $id
  85. */
  86. public function setStatus(int $id)
  87. {
  88. $this->dao->update([['id', '<>', $id]], ['status' => 0]);
  89. $this->dao->update($id, ['status' => 1, 'type' => 0,'update_time' => time()]);
  90. }
  91. /**
  92. * 获取页面数据
  93. * @return array|\think\Model|null
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function getDiy($name)
  99. {
  100. $data = [];
  101. if ($name == '') {
  102. $info = $this->dao->getOne(['status' => 1]);
  103. } else {
  104. $info = $this->dao->getOne(['template_name' => $name]);
  105. }
  106. if ($info) {
  107. $info = $info->toArray();
  108. if($info['value']){
  109. $data = json_decode($info['value'], true);
  110. foreach ($data as $key=>&$item) {
  111. if ($key == 'customerService') {
  112. foreach ($item as $k=>&$v){
  113. $v['routine_contact_type'] = sys_config('routine_contact_type', 0);
  114. }
  115. }
  116. }
  117. }
  118. }
  119. return $data;
  120. }
  121. /**
  122. * 添加表单
  123. * @return array
  124. * @throws \FormBuilder\Exception\FormBuilderException
  125. */
  126. public function createForm()
  127. {
  128. $field = array();
  129. $title = '添加模板';
  130. $field[] = Form::input('name', '页面名称', '')->required();
  131. $field[] = Form::input('template_name', '页面类型', '')->required();
  132. return create_form($title, $field, Url::buildUrl('/diy/create'), 'POST');
  133. }
  134. /**
  135. * 获取商品数据
  136. * @param array $where
  137. * @return array
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\DbException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. */
  142. public function ProductList(array $where){
  143. /** @var StoreProductServices $StoreProductServices */
  144. $StoreProductServices = app()->make(StoreProductServices::class);
  145. /** @var StoreBargainServices $StoreBargainServices */
  146. $StoreBargainServices = app()->make(StoreBargainServices::class);
  147. /** @var $StoreCombinationServices StoreCombinationServices*/
  148. $StoreCombinationServices = app()->make(StoreCombinationServices::class);
  149. /** @var $StoreSeckillServices StoreSeckillServices*/
  150. $StoreSeckillServices = app()->make(StoreSeckillServices::class);
  151. $type = $where['type'];
  152. unset($where['type']);
  153. $data = [];
  154. switch ($type) {
  155. case 0:
  156. $data = $StoreProductServices->searchList($where);
  157. break;
  158. //秒杀
  159. case 2:
  160. $data = $StoreSeckillServices->getDiySeckillList($where);
  161. break;
  162. //拼团
  163. case 3:
  164. $data = $StoreCombinationServices->getDiyCombinationList($where);
  165. break;
  166. case 4:
  167. $where['is_hot'] = 1;
  168. $data = $StoreProductServices->searchList($where);
  169. break;
  170. case 5:
  171. $where['is_new'] = 1;
  172. $data = $StoreProductServices->searchList($where);
  173. break;
  174. case 6:
  175. $where['is_benefit'] = 1;
  176. $data = $StoreProductServices->searchList($where);
  177. break;
  178. case 7:
  179. $where['is_best'] = 1;
  180. $data = $StoreProductServices->searchList($where);
  181. break;
  182. //砍价
  183. case 8:
  184. $data = $StoreBargainServices->getDiyBargainList($where);
  185. break;
  186. }
  187. return $data;
  188. }
  189. /**
  190. * 前台获取首页数据接口
  191. * @param array $where
  192. */
  193. public function homeProductList(array $where,int $uid){
  194. /** @var StoreProductServices $StoreProductServices */
  195. $StoreProductServices = app()->make(StoreProductServices::class);
  196. /** @var StoreBargainServices $StoreBargainServices */
  197. $StoreBargainServices = app()->make(StoreBargainServices::class);
  198. /** @var $StoreCombinationServices StoreCombinationServices*/
  199. $StoreCombinationServices = app()->make(StoreCombinationServices::class);
  200. /** @var $StoreSeckillServices StoreSeckillServices*/
  201. $StoreSeckillServices = app()->make(StoreSeckillServices::class);
  202. $type = $where['type'];
  203. $data = [];
  204. switch ($type) {
  205. case 0:
  206. $where['type'] = $where['isType'] ?? 0;
  207. $data['list'] = $StoreProductServices->getGoodsList($where,$uid);
  208. break;
  209. //秒杀
  210. case 2:
  211. $data = $StoreSeckillServices->getHomeSeckillList($where);
  212. break;
  213. //拼团
  214. case 3:
  215. $data= $StoreCombinationServices->getHomeList($where);
  216. break;
  217. case 4:
  218. $where['is_hot'] = 1;
  219. $where['type'] = $where['isType'] ?? 0;
  220. $data['list'] = $StoreProductServices->getGoodsList($where,$uid);
  221. break;
  222. case 5:
  223. $where['is_new'] = 1;
  224. $where['type'] = $where['isType'] ?? 0;
  225. $data['list'] = $StoreProductServices->getGoodsList($where,$uid);
  226. break;
  227. case 6:
  228. $where['is_benefit'] = 1;
  229. $where['type'] = $where['isType'] ?? 0;
  230. $data['list'] = $StoreProductServices->getGoodsList($where,$uid);
  231. break;
  232. case 7:
  233. $where['is_best'] = 1;
  234. $where['type'] = $where['isType'] ?? 0;
  235. $data['list'] = $StoreProductServices->getGoodsList($where,$uid);
  236. break;
  237. //砍价
  238. case 8:
  239. $data = $StoreBargainServices->getHomeList($where);
  240. break;
  241. }
  242. return $data;
  243. }
  244. }