DiyServices.php 14 KB

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