WechatQrcodeCateServices.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\wechat;
  12. use app\dao\wechat\WechatQrcodeCateDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\FormBuilder as Form;
  16. use think\facade\Route as Url;
  17. /**
  18. * Class WechatQrcodeCateServices
  19. * @package app\services\wechat
  20. * @method getCateList() 分类列表
  21. */
  22. class WechatQrcodeCateServices extends BaseServices
  23. {
  24. /**
  25. * WechatQrcodeCateServices constructor.
  26. * @param WechatQrcodeCateDao $dao
  27. */
  28. public function __construct(WechatQrcodeCateDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 添加编辑分类表单
  34. * @param int $id
  35. * @return array
  36. * @throws \FormBuilder\Exception\FormBuilderException
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function createForm($id = 0)
  42. {
  43. $info = $this->dao->get($id);
  44. $f[] = Form::hidden('id', $id);
  45. $f[] = Form::input('cate_name', '分组名称', $info['cate_name'] ?? '')->maxlength(10)->required();
  46. return create_form($id ? '修改分组' : '添加分组', $f, Url::buildUrl('/app/wechat_qrcode/cate/save'), 'POST');
  47. }
  48. /**
  49. * 保存数据
  50. * @param $data
  51. * @return bool
  52. */
  53. public function saveData($data)
  54. {
  55. $id = $data['id'];
  56. $data['add_time'] = time();
  57. if ($id) {
  58. unset($data['id']);
  59. $res = $this->dao->update($id, $data);
  60. } else {
  61. $res = $this->dao->save($data);
  62. }
  63. if (!$res) throw new AdminException('保存失败');
  64. return true;
  65. }
  66. /**
  67. * 删除分类
  68. * @param int $id
  69. * @return bool
  70. */
  71. public function delCate($id = 0)
  72. {
  73. $count = app()->make(WechatQrcodeServices::class)->count(['cate_id' => $id]);
  74. if ($count) throw new AdminException('该分类有下级分类,无法删除');
  75. if (!$id) throw new AdminException('参数错误');
  76. $res = $this->dao->update($id, ['is_del' => 1]);
  77. if (!$res) throw new AdminException('删除失败');
  78. return true;
  79. }
  80. }