123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- <?php
- namespace app\adminapi\controller\v1\user;
- use app\adminapi\controller\AuthController;
- use Exception;
- use FormBuilder\exception\FormBuilderException;
- use app\models\system\{SystemUserLevel, SystemUserTask};
- use crmeb\services\{FormBuilder as Form, UtilService};
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\facade\Route as Url;
- use crmeb\traits\CurdControllerTrait;
- class UserLevel extends AuthController
- {
- use CurdControllerTrait;
-
- public function read($id)
- {
- $info = SystemUserLevel::merSet($this->merId)->where('id', $id)->find();
- return $this->success(compact('info'));
- }
-
- public function create()
- {
- $where = UtilService::getMore(
- ['id', 0]
- );
- if ($where['id']) {
- $vipinfo = SystemUserLevel::merSet($this->merId)->where('id', $where['id'])->find();
- if (!$vipinfo) {
- return $this->fail('不存在等级');
- }
- $msg = '编辑会员等级';
- } else {
- $msg = '添加会员等级';
- }
- $field[] = Form::input('name', '等级名称', isset($vipinfo) ? $vipinfo->name : '')->col(Form::col(24));
- $field[] = Form::radio('is_forever', '是否为永久', isset($vipinfo) ? $vipinfo->is_forever : 0)->options([['label' => '永久', 'value' => 1], ['label' => '非永久', 'value' => 0]])->col(24);
- $field[] = Form::number('valid_date', '有效时间(天)', isset($vipinfo) ? $vipinfo->valid_date : 0)->min(0)->col(8);
- $field[] = Form::number('grade', '等级', isset($vipinfo) ? $vipinfo->grade : 0)->min(0)->col(8);
- $field[] = Form::number('discount', '享受折扣', isset($vipinfo) ? $vipinfo->discount : 0)->min(0)->col(8);
- $field[] = Form::frameImageOne('icon', '图标', Url::buildUrl('admin/widget.images/index', array('fodder' => 'icon')), isset($vipinfo) ? $vipinfo->icon : '')->icon('ios-add')->width('60%')->height('435px');
- $field[] = Form::frameImageOne('image', '会员背景', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), isset($vipinfo) ? $vipinfo->image : '')->icon('ios-add')->width('60%')->height('435px');
- $field[] = Form::radio('is_show', '是否显示', isset($vipinfo) ? $vipinfo->is_show : 0)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
- $field[] = Form::radio('show_commission', '是否显示佣金', isset($vipinfo) ? $vipinfo->show_commission : 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
- $field[] = Form::textarea('explain', '等级说明', isset($vipinfo) ? $vipinfo->explain : '');
- if ($where['id']) $field[] = Form::hidden('id', $where['id']);
- return $this->makePostForm($msg, $field, Url::buildUrl('/user/user_level'), 'POST');
- }
-
- public function save()
- {
- $data = UtilService::postMore([
- ['id', 0],
- ['name', ''],
- ['is_forever', 0],
- ['money', 0],
- ['is_pay', 0],
- ['valid_date', 0],
- ['grade', 0],
- ['discount', 0],
- ['icon', ''],
- ['image', ''],
- ['is_show', ''],
- ['show_commission', 1],
- ['explain', ''],
- ]);
- $id = $data['id'];
- $data['mer_id'] = $this->merId ?: '';
- if (!$data['name']) return $this->fail('请输入等级名称');
- if (!$data['grade']) return $this->fail('请输入等级');
- if (!$data['explain']) return $this->fail('请输入等级说明');
- if ($data['is_forever'] == 0 && !$data['valid_date']) return $this->fail('请输入有效时间(天)');
- if (!$data['icon']) return $this->fail('请上传等级图标');
- if (!$data['image']) return $this->fail('请上传等级背景图标');
- if (!$id && SystemUserLevel::be(['mer_id' => $data['mer_id'], 'is_del' => 0, 'grade' => $data['grade']])) return $this->fail('已检测到您设置过的会员等级,此等级不可重复');
- SystemUserLevel::beginTrans();
- try {
-
- if ($id) {
- if (SystemUserLevel::merSet($this->merId)->where('id', $id)->find())
- if (SystemUserLevel::edit($data, $id)) {
- SystemUserLevel::commitTrans();
- return $this->success('修改成功');
- } else {
- SystemUserLevel::rollbackTrans();
- return $this->fail('修改失败');
- }
- else return $this->fail('不存在等级');
- } else {
-
- $data['add_time'] = time();
- if (SystemUserLevel::create($data)) {
- SystemUserLevel::commitTrans();
- return $this->success('添加成功');
- } else {
- SystemUserLevel::rollbackTrans();
- return $this->fail('添加失败');
- }
- }
- } catch (Exception $e) {
- SystemUserLevel::rollbackTrans();
- return $this->fail($e->getMessage());
- }
- }
-
- public function get_system_vip_list()
- {
- $where = UtilService::getMore([
- ['page', 0],
- ['limit', 10],
- ['title', ''],
- ['is_show', ''],
- ]);
- $where['mer_id'] = $this->merId;
- return $this->success(SystemUserLevel::getSytemList($where));
- }
-
- public function delete($id)
- {
- if (!$id || !SystemUserLevel::merSet($this->merId)->where('id', $id)->find()) {
- return $this->fail('参数错误');
- }
- if (SystemUserLevel::edit(['is_del' => 1], $id))
- return $this->success('删除成功');
- else
- return $this->fail('删除失败');
- }
-
- public function set_show($is_show = '', $id = '')
- {
- if ($is_show == '' || $id == '') return $this->fail('缺少参数');
- $res = SystemUserLevel::merSet($this->merId)->where(['id' => $id])->update(['is_show' => (int)$is_show]);
- if ($res) {
- return $this->success($is_show == 1 ? '显示成功' : '隐藏成功');
- } else {
- return $this->fail($is_show == 1 ? '显示失败' : '隐藏失败');
- }
- }
-
- public function set_show_commission($show_commission = '', $id = '')
- {
- if ($show_commission == '' || $id == '') return $this->fail('缺少参数');
- $res = SystemUserLevel::merSet($this->merId)->where(['id' => $id])->update(['show_commission' => (int)$show_commission]);
- if ($res) {
- return $this->success($show_commission == 1 ? '显示成功' : '隐藏成功');
- } else {
- return $this->fail($show_commission == 1 ? '显示失败' : '隐藏失败');
- }
- }
-
- public function set_value($id)
- {
- $data = UtilService::postMore([
- ['field', ''],
- ['value', '']
- ], $this->request);
- if ($data['field'] == '' || $data['value'] == '') return $this->fail('缺少参数');
- if (SystemUserLevel::merSet($this->merId)->where(['id' => $id])->update([$data['field'] => $data['value']]))
- return $this->success('保存成功');
- else
- return $this->fail('保存失败');
- }
-
- public function get_task_list($level_id)
- {
- $where = UtilService::getMore([
- ['page', 1],
- ['limit', 10],
- ['name', ''],
- ['is_show', '']
- ], $this->request);
- if (!$level_id || !SystemUserLevel::merSet($this->merId)->where('id', $level_id)->find()) {
- return $this->fail('参数错误');
- }
- return $this->success(SystemUserTask::taskList($level_id, $where));
- }
-
- public function set_task_value($id)
- {
- $data = UtilService::postMore([
- ['field', ''],
- ['value', '']
- ]);
- if (!$id || !in_array(SystemUserTask::where('id', $id)->value('level_id'), SystemUserLevel::merSet($this->merId)->column('id'))) {
- return $this->fail('不存在任务');
- }
- if ($data['field'] == '' || $data['value'] == '') return $this->fail('缺少参数');
- if (SystemUserTask::where(['id' => $id])->update([$data['field'] => $data['value']]))
- return $this->success('保存成功');
- else
- return $this->fail('保存失败');
- }
-
- public function set_task_show($is_show = '', $id = '')
- {
- ($is_show == '' || $id == '') && $this->fail('缺少参数');
- if (!$id || !in_array(SystemUserTask::where('id', $id)->value('level_id'), SystemUserLevel::merSet($this->merId)->column('id'))) {
- return $this->fail('不存在任务');
- }
- $res = SystemUserTask::where(['id' => $id])->update(['is_show' => (int)$is_show]);
- if ($res) {
- return $this->success($is_show == 1 ? '显示成功' : '隐藏成功');
- } else {
- return $this->fail($is_show == 1 ? '显示失败' : '隐藏失败');
- }
- }
-
- public function set_task_must($is_must = '', $id = '')
- {
- ($is_must == '' || $id == '') && $this->fail('缺少参数');
- if (!$id || !in_array(SystemUserTask::where('id', $id)->value('level_id'), SystemUserLevel::merSet($this->merId)->column('id'))) {
- return $this->fail('不存在任务');
- }
- $res = SystemUserTask::where(['id' => $id])->update(['is_must' => (int)$is_must]);
- if ($res) {
- return $this->success('设置成功');
- } else {
- return $this->fail('设置失败');
- }
- }
-
- public function create_task()
- {
- $where = UtilService::getMore([
- ['id', 0],
- ['level_id', 0]
- ]);
- if ($where['id']) $tash = SystemUserTask::get($where['id']);
- $field[] = Form::select('task_type', '任务类型', isset($tash) ? $tash->task_type : '')->setOptions(function () {
- $list = SystemUserTask::getTaskTypeAll();
- $menus = [];
- foreach ($list as $menu) {
- $menus[] = ['value' => $menu['type'], 'label' => $menu['name'] . '----单位[' . $menu['unit'] . ']'];
- }
- return $menus;
- })->filterable(1);
- if ($where['id']) $field[] = Form::hidden('id', $where['id']);
- $field[] = Form::hidden('level_id', $where['level_id']);
- $field[] = Form::number('number', '限定数量', isset($tash) ? $tash->number : 0)->min(0)->col(24);
- $field[] = Form::number('sort', '排序', isset($tash) ? $tash->sort : 0)->min(0)->col(24);
- $field[] = Form::radio('is_show', '是否显示', isset($tash) ? $tash->is_show : 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(24);
- $field[] = Form::radio('is_must', '是否务必达成', isset($tash) ? $tash->is_must : 1)->options([['label' => '务必达成', 'value' => 1], ['label' => '完成其一', 'value' => 0]])->col(24);
- $field[] = Form::textarea('illustrate', '任务说明', isset($tash) ? $tash->illustrate : '');
- return $this->makePostForm('添加任务', $field, Url::buildUrl('/user/user_level/save_task'), 'POST');
- }
-
- public function save_task()
- {
- $data = UtilService::postMore([
- ['id', 0],
- ['level_id', 0],
- ['task_type', ''],
- ['number', 0],
- ['is_show', 0],
- ['sort', 0],
- ['is_must', 0],
- ['illustrate', ''],
- ]);
- if (!$data['level_id'] || !SystemUserLevel::merSet($this->merId)->where('id', $data['level_id'])->find()) {
- return $this->fail('缺少参数');
- }
- if (!$data['task_type']) return $this->fail('请选择任务类型');
- if ($data['number'] < 1) return $this->fail('请输入限定数量,数量不能小于1');
- $tash = SystemUserTask::getTaskType($data['task_type']);
- if ($tash['max_number'] != 0 && $data['number'] > $tash['max_number']) return $this->fail('您设置的限定数量超出最大限制,最大限制为:' . $tash['max_number']);
- $data['name'] = SystemUserTask::setTaskName($data['task_type'], $data['number']);
- try {
- if ($data['id']) {
- SystemUserTask::edit($data, $data['id']);
- return $this->success('修改成功');
- } else {
- $data['add_time'] = time();
- $data['real_name'] = $tash['real_name'];
- if (SystemUserTask::create($data))
- return $this->success('添加成功');
- else
- return $this->fail('添加失败');
- }
- } catch (Exception $e) {
- return $this->fail($e->getMessage());
- }
- }
-
- public function delete_task($id)
- {
- if (!$id) return $this->fail('缺少参数');
- if (!$id || !in_array(SystemUserTask::where('id', $id)->value('level_id'), SystemUserLevel::merSet($this->merId)->column('id'))) {
- return $this->fail('不存在任务');
- }
- if (SystemUserTask::del($id))
- return $this->success('删除成功');
- else
- return $this->fail('删除失败');
- }
- }
|