发表于: 2018-01-23 21:21:05
1 759
今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)
完成了短信的验证以及上传头像删除云空旧头像
短信使用的是腾讯云的SMS,代码如下.
package com.util;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import java.util.ArrayList;
/**
* @author Arike
* Create_at 2018/1/23 15:08
*/
public class Sms {
public static void send(String code,String phone){
//假设短信模板 id 为 123,模板内容为:测试短信,{1},{2},{3},上学。
SmsSingleSender sender = null;
try {
sender = new SmsSingleSender(appid,"xxxxxxxx");
} catch (Exception e) {
e.printStackTrace();
}
ArrayList<String> params = new ArrayList<String>();
params.add(code);
params.add("3");
SmsSingleSenderResult result = null;
try {
result = sender.sendWithParam("86", phone, 78870, params, "", "", "");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(result);
}
}
controller
//发送短信
@RequestMapping(value = "/u/phone", method = RequestMethod.GET)
public String sendCode(HttpSession session) {
//通过当前登录用户session获取到用户信息
User user = userDao.selectUser((String) session.getAttribute("name"));
//获取6位数验证码
String code = Encrypt.code();
//将验证码存储到Session中
session.setAttribute("code",code);
Sms.send(code, user.getPhone());
//Session中添加一个状态用于判断是否发送短信
session.setAttribute("phoneCount", 1);
return "redirect:/u/user";
}
//验证短信
@RequestMapping(value = "/u/phoneCode", method = RequestMethod.POST)
public String phoneCode(String code,HttpSession session) {
//判断用户输入的验证码是否正确,争取就执行SQL更新数据库验证字段,并删除Session中无效字段
if(session.getAttribute("code").equals(code)){
userDao.upPhone((String) session.getAttribute("name"));
session.removeAttribute("code");
session.removeAttribute("phoneCount");
}
return "redirect:/u/user";
}
验证码方法
/**
* 获取一个6位数的验证码
* @return
*/
public static String code() {
String code = "";
for (int i = 0; i < 6; i++) {
code = code + (int) (Math.random() * 10);
}
return code;
}
页面的一些判断
<div>
<span>${phone}</span>
<c:if test="${phoneConf==0}">
<c:if test="${phoneCount==1}">
<br>
请输入验证码
<br>
<span>
<form action="${pageContext.request.contextPath}/u/phoneCode" method="post">
<input type="text" name="code">
<input type="submit" value="提交"/>
</form>
</span>
</c:if>
<c:if test="${phoneCount==null}"><a href="${pageContext.request.contextPath}/u/phone">点击发送短信</a> </c:if>
</c:if>
<c:if test="${phoneConf==1}">
手机号已验证 </c:if>
</div>
看一下效果:
点击发送
输入验证码
验证成功
接下来是图片上传之后删除之前对应的头像:
先创建阿里OSS的删除接口:
public static boolean del(String key){
// 创建OSSClient实例
OSSClient client = getOSSClient();
// 删除Object
try {
client.deleteObject("head-file", key);
return true;
} catch (OSSException e) {
logger.error("删除失败");
return false;
} catch (ClientException e) {
logger.error("删除失败");
return false;
}
}
controller,加上了删除方法.
//上传头像
@RequestMapping(value = "/u/file", method = RequestMethod.POST)
public String fileup(MultipartFile file, HttpSession session) throws IOException {
//获取到输入流
InputStream is = file.getInputStream();
//通过split方法用.分割文件名,并获取到最大索引位置的字符串(肯定就是后缀名了,不管他有多少个.)
String[] name = file.getOriginalFilename().split("\\.");
String end = name[name.length - 1];
User user = userDao.selectUser((String) session.getAttribute("name"));
//如果头像不为null才执行删除方法.
if(user.getHead()!=null){
//通过正则匹配切割数据库里的头像链接拿到文件真实路径.
String[]head = user.getHead().split("com/");
//执行删除方法.
Oss.del(head[head.length-1]);
}
user.setHead(Oss.fileUp(is, end));
userDao.upHead(user);
return "redirect:/u/user";
}
尝试一下:
当前OSS中的头像
执行更换操作:
这样就可以有效的管理OSS资源.
明天计划的事情:(一定要写非常细致的内容)
将任务7的数据迁移完成,进入任务8
遇到的问题:(遇到什么困难,怎么解决的)
null
收获:(通过今天的学习,学到了什么知识)
第三方API涵盖了很多方法以及功能,需要自己好好熟读,认知.
评论