123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- namespace app\admin\controller\vote;
- use app\admin\controller\AuthController;
- use app\admin\model\system\SystemAttachment;
- use app\admin\model\system\SystemUserLevel;
- use Exception;
- use crmeb\services\{FormBuilder as Form,
- JsonService,
- UtilService as Util,
- JsonService as Json,
- UtilService
- };
- use FormBuilder\exception\FormBuilderException;
- use think\facade\Route as Url;
- use app\models\vote\{
- Vote as VoteModel, VoteSub, UserVote
- };
- /**
- * 图文管理
- * Class WechatNews
- * @package app\admin\controller\wechat
- */
- class Vote extends AuthController
- {
- /**
- * TODO 显示后台管理员添加的图文
- * @return mixed
- * @throws Exception
- */
- public function index()
- {
- $money_type = json_encode(sys_data('money_type'));
- $this->assign(compact('money_type'));
- $this->assign('count_vote', VoteModel::count());
- return $this->fetch();
- }
- /**
- * 获取vote表
- *
- * @return void
- */
- public function get_list()
- {
- $where = Util::getMore([
- ['page', 1],
- ['limit', 20],
- ['title', ''],
- ['status', ''],
- ['money_type', ''],
- ]);
- Json::successlayui(VoteModel::getList($where));
- }
- /**
- * 获取voteSub表
- *
- * @return void
- */
- public function get_sub_list()
- {
- $where = Util::getMore([
- ['page', 1],
- ['limit', 20],
- ['id', 0],
- ]);
- Json::successlayui(VoteSub::getList($where));
- }
- /**
- * 创建form表单
- *
- * @param int $id
- * @return string
- * @throws Exception
- * @throws FormBuilderException
- */
- public function create($id = 0)
- {
- $money_type = sys_data('money_type');
- $money_type_select = [];
- foreach ($money_type as $v) {
- $money_type_select[] = ['label' => $v['name'], 'value' => $v['code']];
- }
- if ($id) $info = VoteModel::get($id);
- $field[] = Form::input('vote_name', '投票标题', isset($info) ? $info->vote_name : '')->col(Form::col(24))->required();
- $field[] = Form::select('money_type', '投票目标', isset($info) ? $info->money_type : '')->options($money_type_select)->col(24)->required();
- $field[] = Form::number('money_precision', '币种精度', isset($info) ? $info->money_precision : 0)->min(0)->col(24)->placeholder('保留x位小数')->required();
- $field[] = Form::time('day_start', '完成后下一期开始时间', isset($info) ? $info->day_start : 0)->col(24)->placeholder('第二天时间');
- $field[] = Form::number('start_vote_num', '第一期投票数量', isset($info) ? $info->start_vote_num : 0)->min(0)->col(12)->step(0.00000001)->required();
- $field[] = Form::number('step_vote_num', '投票数量每期增量(%)', isset($info) ? $info->step_vote_num : 0)->min(0)->col(12)->required();
- $field[] = Form::number('start_max_vote_num', '第一期个人投票上限', isset($info) ? $info->start_max_vote_num : 0)->min(0)->col(12)->step(0.00000001)->required();
- $field[] = Form::number('step_max_vote_num', '投票上限每期增加', isset($info) ? $info->step_max_vote_num : 0)->min(0)->col(12)->step(0.00000001)->required();
- $field[] = Form::number('start_still_time', '第一期投票时长(小时)', isset($info) ? $info->start_still_time : 0)->min(0)->col(12)->required();
- $field[] = Form::number('step_still_time', '投票时长每期增加(小时)', isset($info) ? $info->step_still_time : 0)->min(0)->col(12)->required();
- $field[] = Form::number('success_back', '投票成功返还(不含本金)(%)', isset($info) ? $info->success_back : 0)->min(0)->col(12)->required();
- $field[] = Form::number('fail_back_now', '投票失败当期返还(%)', isset($info) ? $info->fail_back_now : 0)->min(0)->col(12)->required();
- $field[] = Form::number('fail_back_last', '投票失败上期返还(%)', isset($info) ? $info->fail_back_last : 0)->min(0)->col(12)->required();
- $field[] = Form::number('fail_back_before_last', '投票失败上上期返还(%)', isset($info) ? $info->fail_back_before_last : 0)->min(0)->col(12)->required();
- $field[] = Form::select('other_back_money_type', '未返还部分转化为', isset($info) ? $info->other_back_money_type : '')->options($money_type_select)->col(24);
- if (isset($info) && $info['status'] != 2)
- $field[] = Form::radio('status', '状态', isset($info) ? $info->status : 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
- $form = Form::make_post_form('添加投票', $field, Url::buildUrl('save', ['id' => $id]), 2);
- $this->assign(compact('form'));
- return $this->fetch('public/form-builder');
- }
- /**
- * 添加或者修改
- * @param $id
- * @return void
- */
- public function save($id = 0)
- {
- $data = UtilService::postMore([
- ['vote_name', ''],
- ['money_type', ''],
- ['money_precision', 0],
- ['start_vote_num', 0],
- ['step_vote_num', 0],
- ['start_max_vote_num', 0],
- ['step_max_vote_num', 0],
- ['start_still_time', 0],
- ['step_still_time', 0],
- ['success_back', 0],
- ['fail_back_now', 0],
- ['fail_back_last', 0],
- ['fail_back_before_last', 0],
- ['other_back_money_type', ''],
- ['status', 0],
- ['day_start', '10:00:00'],
- ]);
- VoteModel::beginTrans();
- try {
- //修改
- if ($id) {
- if (VoteModel::edit($data, $id)) {
- VoteModel::commitTrans();
- JsonService::successful('修改成功');
- } else {
- VoteModel::rollbackTrans();
- JsonService::fail('修改失败');
- }
- } else {
- //新增
- $data['add_time'] = time();
- $res = VoteModel::create($data);
- if ($res) {
- //生成第一期子投票
- VoteSub::create([
- 'vote_sub_name' => $data['vote_name'] . '第1期',
- 'vote_id' => $res->id,
- 'vote_num' => $data['start_vote_num'],
- 'max_vote_num' => $data['start_max_vote_num'],
- 'start_time' => time(),
- 'end_time' => time() + 3600 * $data['start_still_time'],
- 'loop' => 1,
- 'status' => 0,
- 'add_time' => time(),
- ]);
- VoteModel::commitTrans();
- JsonService::successful('添加成功');
- } else {
- VoteModel::rollbackTrans();
- JsonService::fail('添加失败');
- }
- }
- } catch (\Exception $e) {
- VoteModel::rollbackTrans();
- JsonService::fail($e->getMessage());
- }
- }
- /**
- * 会员详情
- */
- public function see($id = '')
- {
- $this->assign([
- 'id' => $id,
- 'vote_info' => VoteModel::getInfo($id),
- 'is_layui' => true,
- ]);
- return $this->fetch();
- }
- /**
- * 创建form表单
- *
- * @param int $id
- * @return string
- * @throws Exception
- * @throws FormBuilderException
- */
- public function create_sub($id = 0, $vid = 0)
- {
- if (!$id && !$vid) {
- $this->failed('参数异常');
- }
- if ($id) $info = VoteSub::get($id);
- if (isset($info)) $vid = $info['vote_id'];
- // var_dump($id);
- $field[] = Form::hidden('vote_id', $vid);
- $field[] = Form::input('vote_sub_name', '投票标题', isset($info) ? $info->vote_sub_name : '')->col(Form::col(24))->required();
- $field[] = Form::number('vote_num', '投票数量', isset($info) ? $info->vote_num : 0)->min(0)->col(12)->step(0.00000001)->required();
- $field[] = Form::number('max_vote_num', '个人投票上限', isset($info) ? $info->max_vote_num : 0)->min(0)->col(12)->step(0.00000001)->required();
- $field[] = Form::dateTime('start_time', '开始时间', isset($info) ? date('Y-m-d H:i:s', $info->start_time) : '');
- $field[] = Form::dateTime('end_time', '结束时间', isset($info) ? date('Y-m-d H:i:s', $info->end_time) : '');
- if ((isset($info) && $info['status'] != 2 && $info['status'] != 3) || !isset($info))
- $field[] = Form::radio('status', '状态', isset($info) ? $info->status : 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
- $form = Form::make_post_form('添加子投票', $field, Url::buildUrl('save_sub', ['id' => $id]), 2);
- $this->assign(compact('form'));
- return $this->fetch('public/form-builder');
- }
- /**
- * 添加或者修改
- * @param $id
- * @return void
- */
- public function save_sub($id = 0)
- {
- $data = UtilService::postMore([
- ['vote_sub_name', ''],
- ['vote_id', ''],
- ['vote_num', ''],
- ['max_vote_num', ''],
- ['end_time', ''],
- ['start_time', ''],
- ['status', 0],
- ]);
- VoteSub::beginTrans();
- try {
- if (!$data['vote_id']) JsonService::fail('请选择投票');
- if (!$data['start_time']) JsonService::fail('请输入开始时间');
- if (!$data['end_time']) JsonService::fail('请输入结束时间');
- $data['start_time'] = strtotime($data['start_time']);
- $data['end_time'] = strtotime($data['end_time']);
- if ($data['end_time'] <= $data['start_time']) JsonService::fail('结束时间要大于开始时间');
- if (!$id) $data['loop'] = VoteSub::where('vote_id', $data['vote_id'])->max('loop') + 1;
- //修改
- if ($id) {
- if (VoteSub::edit($data, $id)) {
- VoteSub::commitTrans();
- JsonService::successful('修改成功');
- } else {
- VoteSub::rollbackTrans();
- JsonService::fail('修改失败');
- }
- } else {
- //新增
- $data['add_time'] = time();
- $res = VoteSub::create($data);
- if ($res) {
- VoteModel::commitTrans();
- JsonService::successful('添加成功');
- } else {
- VoteModel::rollbackTrans();
- JsonService::fail('添加失败');
- }
- }
- } catch (\Exception $e) {
- VoteModel::rollbackTrans();
- JsonService::fail($e->getMessage());
- }
- }
- public function see_people($id = 0)
- {
- $this->assign([
- 'id' => $id,
- ]);
- return $this->fetch();
- }
- public function people_list()
- {
- $where = Util::getMore([
- ['page', 1],
- ['limit', 20],
- ['id', 0],
- ]);
- Json::successlayui(UserVote::getUserList($where));
- }
- }
|