页面文件
<#-- 页头 --> <#assign currNav = "deposit"> <#assign title="网校充值"> <#include "/root/commons/header.ftl"> <div class="container"> <form id="depositForm" name="depositForm" role="form" action="/root/depositlog/${schoolId}/deposit" method="post"> <div class="form-group"> <label for="schoolId">网校ID</label> <input type="text" class="form-control" id="schoolId" name="schoolId" placeholder="例如,udemy" value="${school.schoolId!''}" readonly="readonly"> <p class="help-block">全局唯一,不能和其他网校的ID相同</p> </div> <div class="form-group"> <label for="name">网校名称</label> <input type="text" class="form-control" id="name" name="name" placeholder="请填写网校名称" value="${school.name!''}" readonly="readonly"> </div> <div class="form-group"> <label for="duration">充值分钟数 <span id="chinaText" class="text-muted text-sm" style="margin-left:60px;font-size:12px;"> </span></label> <input type="number" class="form-control" id="duration" name="duration" placeholder=""> <p class="help-block">单位:分钟</p> </div> <div class="form-group"> <label for="amount">充值金额 <span id="moneyText" class="text-muted text-sm" style="margin-left:60px;font-size:12px;"> </span></label> <input type="text" class="form-control" id="amount" name="amount" placeholder=""> <p class="help-block">单位:元</p> </div> <div class="form-group"> <label for="note">备注</label> <input type="text" class="form-control" id="note" name="note" placeholder=""> </div> </form> <button id="submitBtn" class="btn btn-primary">提交充值数据</button> </div> <!-- /container --> <#-- 页脚开始 --> <#include "/root/commons/footerBegin.ftl"> <script src="/resources/js/bootbox.js"></script> <script> //在输入框内容变化的时候触发change $("#duration").on('input',function(e){ var value=$("#duration").val(); $.get("/root/depositlog/input/change",{number:value},function(data){ $("#chinaText").text(data+" 分钟"); }); }); $("#amount").on('input',function(e){ var value=$("#amount").val(); $.get("/root/depositlog/input/change",{number:value},function(data){ $("#moneyText").text(data+" 元"); }); }); $("#submitBtn").click(function(){ var schoolId=$("#schoolId").val(); var name=$("#name").val(); var duration=$("#duration").val(); var amount=$("#amount").val(); bootbox.dialog({ message: "<div id='schoolName'>网校名称:<span>"+name+"</span><span style='margin-left:10px'>schoolId-("+schoolId+")</span></div>"+ "<div>充值时长:<span>"+duration+" 分钟</span></div>"+ "<div>充值金额:<span>"+amount+" 元</span></div>", title: "充值确认", buttons: { success: { label: "确定", className: "btn-success", callback: function() { document.depositForm.submit(); } }, danger: { label: "取消", className: "btn-danger", callback: function() { return; } } } }); }); </script> <#-- 页脚结束 --> <#include "/root/commons/footerEnd.ftl">
Java类文件
package com.school.web.controller.root; import java.math.BigDecimal; import java.nio.charset.Charset; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.ServletRequestUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.school.business.DepositLogManager; import com.school.business.SchoolManager; import com.school.domain.DepositLog; import com.school.domain.Root; import com.school.domain.School; import com.school.stereotype.YesNoStatus; import com.school.util.Constants; /** * 充值管理的控制器。 */ @Controller("rootDepositManagementController") @RequestMapping("/root/depositlog") public class DepositManagementController extends AbstractRootController { private static final Logger LOG = LoggerFactory.getLogger(DepositManagementController.class); @Autowired private SchoolManager schoolManager; @Autowired private DepositLogManager depositLogManager; /** * 充值表单。 */ @RequestMapping(value = "/{schoolId}/deposit", method = RequestMethod.GET) public ModelAndView depositForm(@PathVariable String schoolId, HttpServletRequest request) { // 网校信息 School school = schoolManager.getSchool(schoolId); ModelAndView mav = this.createModelAndView(request); mav.setViewName("root/depositLog/deposit"); mav.addObject("schoolId", schoolId); mav.addObject("school", school); return mav; } /** * 处理充值。 */ @RequestMapping(value = "/{schoolId}/deposit", method = RequestMethod.POST) public @ResponseBody String depositPost(@PathVariable String schoolId, HttpServletRequest request) { int duration = ServletRequestUtils.getIntParameter(request, "duration", 0);// 时长分钟数,单位:分钟 double amount = ServletRequestUtils.getDoubleParameter(request, "amount", 0);// 价格 String tradingNote = ServletRequestUtils.getStringParameter(request, "note", null); // 备注 if (duration <= 0) { return "invalid duration"; } if (amount < 0) { return "invalid amount"; } // 当前管理员信息 Root root = (Root) (request.getSession().getAttribute(Constants.ROOT_SESSION_NAME)); // 交易名称 String tradingName = schoolId + "充值" + duration + ",费用:" + amount + "元"; // 充值信息 DepositLog depositLog = new DepositLog(); depositLog.setSchoolId(schoolId); depositLog.setTradingName(tradingName); depositLog.setTradingNote(tradingNote); depositLog.setDuration(duration); depositLog.setAmount(BigDecimal.valueOf(amount)); depositLog.setOperator(root.getNickname()); depositLog.setStatus(YesNoStatus.YES.getValue()); depositLog.setDateAdded(new Date()); depositLog.setLastModified(new Date()); int rows = depositLogManager.addDepositLog(depositLog); LOG.info(tradingName + ",处理结果:" + rows); return "success"; } /** * 查看最近100条充值记录。 */ @RequestMapping("/list") public ModelAndView depositLogs(HttpServletRequest request) { // 查询充值记录 List<DepositLog> depositLogs = depositLogManager.getDepositLogs(); ModelAndView mav = this.createModelAndView(request); mav.setViewName("root/depositLog/list"); mav.addObject("depositLogs", depositLogs); return mav; } /** * 将数字转换成中文数字 * @author Prosper * @throws UnsupportedEncodingException */ @RequestMapping(value="/input/change", method = {RequestMethod.POST,RequestMethod.GET}) public ResponseEntity<String> getCNint(HttpServletRequest request){ HttpHeaders headers = new HttpHeaders(); MediaType mediaType = new MediaType("text", "plain", Charset.forName("UTF-8")); //MediaType mediaType = new MediaType("application", "json", Charset.forName("UTF-8")); headers.setContentType(mediaType); //HttpStatus Yes=HttpStatus.OK; String str = request.getParameter("number"); if("".equals(str) && str==null){ return new ResponseEntity<String>("no", headers, HttpStatus.OK); }else{ Integer number = Integer.parseInt(str); String ri=intToZH(number); return new ResponseEntity<String>(ri, headers, HttpStatus.OK); } } public String intToZH(int i){ String[] zh = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}; String[] unit = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十"}; String str = ""; StringBuffer sb = new StringBuffer(String.valueOf(i)); sb = sb.reverse(); int r = 0; int l = 0; for (int j = 0; j < sb.length(); j++) { /** * 当前数字 */ r = Integer.valueOf(sb.substring(j, j+1)); if (j != 0) /** * 上一个数字 */ l = Integer.valueOf(sb.substring(j-1, j)); if (j == 0) { if (r != 0 || sb.length() == 1) str = zh[r]; continue; } if (j == 1 || j == 2 || j == 3 || j == 5 || j == 6 || j == 7 || j == 9) { if (r != 0) str = zh[r] + unit[j] + str; else if (l != 0) str = zh[r] + str; continue; } if (j == 4 || j == 8) { str = unit[j] + str; if ((l != 0 && r == 0) || r != 0) str = zh[r] + str; continue; } } return str; } }
时间: 2024-11-11 20:16:03