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)); } }