WechatQrcodeCateServices.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\wechat;
  13. use app\dao\wechat\WechatQrcodeCateDao;
  14. use app\services\BaseServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\FormBuilder as Form;
  17. use think\facade\Route as Url;
  18. /**
  19. *
  20. * Class WechatQrcodeCateServices
  21. * @package app\services\wechat
  22. * @mixin WechatQrcodeCateDao
  23. * @method getCateList() 分组列表
  24. */
  25. class WechatQrcodeCateServices extends BaseServices
  26. {
  27. /**
  28. * 构造方法
  29. * WechatReplyKeyServices constructor.
  30. * @param WechatQrcodeCateDao $dao
  31. */
  32. public function __construct(WechatQrcodeCateDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 添加编辑分组表单
  38. * @param int $id
  39. * @return array
  40. * @throws \FormBuilder\Exception\FormBuilderException
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function createForm($id = 0)
  46. {
  47. $info = [];
  48. if ($id) {
  49. $info = $this->dao->get($id);
  50. }
  51. $f[] = Form::hidden('id', $id);
  52. $f[] = Form::input('cate_name', '分组名称', $info['cate_name'] ?? '')->maxlength(30)->required();
  53. return create_form('添加分组', $f, Url::buildUrl('/app/wechat_qrcode/cate/save'), 'POST');
  54. }
  55. /**
  56. * 保存数据
  57. * @param $data
  58. * @return bool
  59. */
  60. public function saveData($data)
  61. {
  62. $id = $data['id'];
  63. $data['add_time'] = time();
  64. if ($id) {
  65. unset($data['id']);
  66. $res = $this->dao->update($id, $data);
  67. } else {
  68. $res = $this->dao->save($data);
  69. }
  70. if (!$res) throw new AdminException('保存失败');
  71. return true;
  72. }
  73. /**
  74. * 删除分组
  75. * @param int $id
  76. * @return bool
  77. */
  78. public function delCate(int $id = 0)
  79. {
  80. if (!$id) throw new AdminException('参数错误');
  81. if ($this->dao->get($id)) {
  82. $qrcodeServices = app()->make(WechatQrcodeServices::class);
  83. if ($qrcodeServices->count(['cate_id' => $id, 'is_del'=>0])) {
  84. throw new AdminException('该分组下有渠道码,暂不能删除');
  85. }
  86. $res = $this->dao->update($id, ['is_del' => 1]);
  87. if (!$res) throw new AdminException('删除失败');
  88. }
  89. return true;
  90. }
  91. }