1、完善用户信息
用户在首次登录时需要完善个人信息,包括性别、昵称、生日、城市、头像等。其中,头像数据需要做图片上传,这里采用阿里云的OSS服务作为我们的图片服务器,并且对头像要做人脸识别,非人脸照片不得上传。
- 首次登录时(手机号码不存在),需要创建用户存入数据库中
- 客户端检测首次登录需要完善用户信息
- 填写用户基本信息
- 上传用户头像(需要人脸认证)
1.1、阿里云OSS
实现图片上传服务,需要有存储的支持,那么我们的解决方案将以下几种:
- 直接将图片保存到服务的硬盘(springmvc将的文件上传)
- 优点:开发便捷,成本低
- 缺点:扩容困难
- 使用分布式文件系统进行存储
- 优点:容易实现扩容
- 缺点:开发复杂度稍大(有成熟的产品可以使用,比如:FastDFS)
- 使用第三方的存储服务
- 优点:开发简单,拥有强大功能,免维护
- 缺点:付费
在本套课程中选用阿里云的OSS服务进行图片存储。
1.1.1、概述
对象存储服务(Object Storage Service,OSS)是一种海量、安全、低成本、高可靠的云存储服务,适合存放任意类型的文件。容量和处理能力弹性扩展,多种存储类型供选择,全面优化存储成本。
地址:对象存储 OSS_云存储服务_企业数据管理_存储-阿里云
1.1.2、账号申请
购买服务
使用第三方服务最大的缺点就是需要付费,下面,我们看下如何购买开通服务。
购买下行流量包: (不购买也可以使用,按照流量付费)
说明:OSS的上行流量是免费的,但是下行流量是需要购买的。
创建Bucket
使用OSS,首先需要创建Bucket,Bucket翻译成中文是水桶的意思,把存储的图片资源看做是水,想要盛水必须得有桶,就是这个意思了。
进入控制台,阿里云登录 - 欢迎登录阿里云,安全稳定的云计算服务平台
选择Bucket后,即可看到对应的信息,如:url、消耗流量等 :
文件管理:
查看文件:
1.1.3、抽取模板工具
和发送短信类似,阿里云OSS也是采用自定义工具的形式进行封装
OssProperties
tanhua-autoconfig创建配置类
1. @Data 2. @ConfigurationProperties(prefix = "tanhua.oss") 3. public class OssProperties { 4. 5. private String accessKey; 6. private String secret; 7. private String bucketName; 8. private String url; //域名 9. private String endpoint; 10. }
OssTemplate
tanhua-autoconfig创建模板对象
1. package com.tanhua.autoconfig.template; 2. 3. import com.aliyun.oss.OSS; 4. import com.aliyun.oss.OSSClientBuilder; 5. import com.tanhua.autoconfig.properties.OssProperties; 6. 7. import java.io.InputStream; 8. import java.text.SimpleDateFormat; 9. import java.util.Date; 10. import java.util.UUID; 11. 12. public class OssTemplate { 13. 14. private OssProperties properties; 15. 16. public OssTemplate(OssProperties properties) { 17. this.properties = properties; 18. } 19. 20. /** 21. * 文件上传 22. * 1:文件名称 23. * 2:输入流 24. */ 25. public String upload(String filename, InputStream is) { 26. //3、拼写图片路径 27. filename = new SimpleDateFormat("yyyy/MM/dd").format(new Date()) 28. +"/"+ UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf(".")); 29. 30. 31. // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。 32. String endpoint = properties.getEndpoint(); 33. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 34. String accessKeyId = properties.getAccessKey(); 35. String accessKeySecret = properties.getSecret(); 36. 37. // 创建OSSClient实例。 38. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret); 39. 40. // 填写Byte数组。 41. // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。 42. ossClient.putObject(properties.getBucketName(), filename, is); 43. 44. // 关闭OSSClient。 45. ossClient.shutdown(); 46. 47. String url = properties.getUrl() +"/" + filename; 48. return url; 49. } 50. }
TanhuaAutoConfiguration
TanhuaAutoConfiguration加入配置
1. @EnableConfigurationProperties({ 2. SmsProperties.class, 3. OssProperties.class 4. }) 5. public class TanhuaAutoConfiguration { 6. 7. @Bean 8. public SmsTemplate smsTemplate(SmsProperties properties) { 9. return new SmsTemplate(properties); 10. } 11. 12. @Bean 13. public OssTemplate ossTemplate(OssProperties properties) { 14. return new OssTemplate(properties); 15. } 16. }









