UserChannel.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\user\UserChannel as ChannelModel;
  5. use crmeb\services\JsonService;
  6. use crmeb\services\UtilService;
  7. use crmeb\services\FormBuilder as Form;
  8. use think\facade\Route as Url;
  9. /**
  10. * Class UserGroup
  11. * @package app\admin\controller\user
  12. */
  13. class UserChannel extends AuthController
  14. {
  15. /**
  16. * 会员分组页面
  17. * @return string
  18. */
  19. public function index()
  20. {
  21. return $this->fetch();
  22. }
  23. /**
  24. * 分组列表
  25. */
  26. public function channelList()
  27. {
  28. $where = UtilService::getMore([
  29. ['page', 1],
  30. ['limit', 20],
  31. ]);
  32. return JsonService::successlayui(ChannelModel::getList($where));
  33. }
  34. /**
  35. * 添加/修改分组页面
  36. * @param int $id
  37. * @return string
  38. */
  39. public function addChannel($id = 0)
  40. {
  41. $group = ChannelModel::get($id);
  42. $f = [];
  43. if (!$group) {
  44. $f[] = Form::input('name', '渠道名称', '')->col(30);
  45. $f[] = Form::input('code', '渠道代码', '')->col(30);
  46. $f[] = Form::textarea('desc', '说明', '')->col(512)->rows(10);
  47. } else {
  48. $f[] = Form::input('name', '渠道名称', $group->getData('name'))->col(30);
  49. $f[] = Form::input('code', '渠道代码', $group->getData('code'))->col(30);
  50. $f[] = Form::textarea('desc', '说明', $group->getData('desc'))->col(512)->rows(10);
  51. }
  52. $form = Form::make_post_form('会员渠道', $f, Url::buildUrl('saveChannel', ['id' => $id]));
  53. $this->assign(compact('form'));
  54. return $this->fetch('public/form-builder');
  55. }
  56. /**
  57. * 添加/修改
  58. * @param int $id
  59. */
  60. public function saveChannel($id = 0)
  61. {
  62. $data = UtilService::postMore([
  63. ['name', ''],
  64. ['code', ''],
  65. ['desc', ''],
  66. ]);
  67. if ($id) {
  68. if (ChannelModel::where('id', $id)->update($data)) {
  69. return JsonService::success('修改成功');
  70. } else {
  71. return JsonService::fail('修改失败或者您没有修改什么!');
  72. }
  73. } else {
  74. if ($res = ChannelModel::create($data)) {
  75. return JsonService::success('保存成功', ['id' => $res->id]);
  76. } else {
  77. return JsonService::fail('保存失败!');
  78. }
  79. }
  80. }
  81. /**
  82. * 删除
  83. * @param $id
  84. * @throws \Exception
  85. */
  86. public function delete($id)
  87. {
  88. if (!$id) return $this->failed('数据不存在');
  89. if (!ChannelModel::be(['id' => $id])) {
  90. return $this->failed('渠道数据不存在');
  91. }
  92. if (!ChannelModel::where('id', $id)->delete()) {
  93. return JsonService::fail(ChannelModel::getErrorInfo('删除失败,请稍候再试!'));
  94. } else {
  95. return JsonService::successful('删除渠道成功!');
  96. }
  97. }
  98. }