HarmonyOS技术社区 · 2021年02月04日

抛弃床的温暖,只为了它丨云库一款跑在鸿蒙系统上的应用丨篇二

<span class="colour" style="color:rgb(51, 51, 51)">目录:</span>
约定服务端API返回结构
登录流程
华为云对象存储OBS介绍
HAP输入手机号获取验证码

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px"><span class="colour" style="color:rgb(224, 62, 45)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert"><span class="font" style="font-family:revert"><span class="size" style="font-size:18px">前文再续,章节上篇。</span></span></span></span></span></span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px">在<span class="colour" style="color:rgb(224, 62, 45)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert"><span class="font" style="font-family:revert"><span class="size" style="font-size:18px">篇一</span></span></span></span></span>,对整个项目要做什么,达到什么样的效果等做了简单的分析。本节将从服务端API约定、登录(没有注册,登录即注册)流程、长连接、华为云对象存储OBS基本概念介绍等对整个项目的技术层面做详细的介绍。</span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px"><span class="colour" style="color:rgb(22, 145, 121)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert"><span class="font" style="font-family:revert"><span class="size" style="font-size:18px">本节内容摘要:</span></span></span></span></span></span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px">     <span> </span><span class="colour" style="color:rgb(22, 145, 121)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert"> 1、约定服务端API返回结构</span></span></span></span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px">       <span class="colour" style="color:rgb(22, 145, 121)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert">2、登录流程</span></span></span></span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px"><span class="colour" style="color:rgb(22, 145, 121)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert">       3、华为云对象存储OBS介绍</span></span></span></span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px"><span class="colour" style="color:rgb(22, 145, 121)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert">       4、HAP输入手机号获取验证码</span></span></span></span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px">抛弃床的温暖,只为了它丨云库一款跑在鸿蒙系统上的应用丨篇二<span class="colour" style="color:rgb(126, 140, 141)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert"><span class="font" style="font-family:revert"><span class="size" style="font-size:24px">约定服务端API返回结构</span></span></span></span></span></span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px">封装一个好的返回结构,使得我们在处理数据的时候能够减少各种拆解。因此在初期搭建完服务端项目后,首要做的是封装一套合理的返回数据结构,使API返回结构体大体相似。</span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px"><span class="colour" style="color:rgb(126, 140, 141)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert"><span class="font" style="font-family:revert"><span class="size" style="font-size:18px">1)使用Java Enum枚举类,将公用的返回错误进行封装,比如请求接口成功,请求接口失败,登录验证成功等等。</span></span></span></span></span></span></span></span></span>

`package com.ming.harmonyos.photokit.conf;

/**
 * 枚举类对象
 */
public enum StatusCode {

    //公共
    SUCCESS(200, "成功"),
    FAILED(9999, "失败"),
    UNKNOWN_ERROR(9998, "未知异常"),
    SYSTEM_ERROR(9997, "系统异常"),

    REQUEST_PARAMETER_ERROR(1001, "请求参数错误"),
    REQUEST_ACCESS_TOKEN_ERROR(1002, "请求参数token值错误"),

    //手机号校验
    CHECK_CELL_PHONE_NUM_ERROR(2001, "手机号码输入错误"),
    LOGIN_VALID_CODE_ERROR(2002, "登录验证码错误"),
    LOGOUT_ERROR(2003, "退出失败"),
    LOGOUT_AUTH_ERROR(2004, "退出失败,token不存在");

    /**
     * 状态码
     */
    private int code;

    /**
     * 状态消息
     */
    private String message;

    StatusCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}
`

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px"><span class="colour" style="color:rgb(126, 140, 141)"><span class="font" style="font-family:revert"><span class="size" style="font-size:18px">2)、封装一套API返回的数据结构</span></span></span></span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px">我目前使用的有两种,一种是以true|false做请求状态,另一种是以数字的形式做请求状态。两种各有千秋,第一种相对第二种的话要做到具体的异常定位需要在返回消息中加入异常码。本项目只是简单的示例项目,因此我选择第二种,这种方便控制。</span></span></span></span>

`package com.ming.harmonyos.photokit.conf;

import lombok.Data;

import java.io.Serializable;

/**
 * 返回结果类统一封装
 */
@Data
public class ApiResultInfo<T> implements Serializable {

    /**
     * 状态码
     */
    private Integer code;
    /**
     * 消息
     */
    private String message;

    /**
     * 数据对象
     */
    private T data;

    public ApiResultInfo() {
    }

    public ApiResultInfo(Integer code, String message, T data) {
        this();
        setCode(code);
        setMessage(message);
        setData(data);
    }

    public static <T> ApiResultInfo<T> getFailedInstant(Integer code, String message, T data) {
        return new ApiResultInfo<>(code, message, data);
    }

    public static <T> ApiResultInfo<T> getInstant(T data) {
        return new ApiResultInfo<>(StatusCode.SUCCESS.getCode(),
                StatusCode.SUCCESS.getMessage(), data);
    }
}
`

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px"><span class="colour" style="color:rgb(126, 140, 141)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert"><span class="font" style="font-family:revert"><span class="size" style="font-size:18px">3)示例接口</span></span></span></span></span></span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px"><span class="colour" style="color:rgb(126, 140, 141)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert">这里只是一个登录(同时也是注册)接口,其他接口暂时未公开。</span></span></span></span></span></span></span>
接口信息

`接口地址:[http://xxx/yunku/login.do](http://xxx/yunku/login.do) 
请求示例:[http://xxx/yunku/login.do](http://xxx/yunku/login.do) 
支持协议:HTTP/HTTPS 
请求方式:POST 
返回格式:UTF8 JSON`

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px">请求参数</span></span></span></span>

名称类型必填示例值/默认值说明
\_cellPhoneNumString13800000000注册登录手机号
\_validCodeString546655获取的短信验证码

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px">返回示例</span></span></span></span>

`{
    "code": 200,
    "msg": "success",
    "data": {
        "authCode": "xxxxxx",
        "userInfo": {
            "iId": 1,
            "sPhone": "13800000000",
            "sNickName": "yx_13800000000",
           "sPath": "[http://xxxx/yx\_default.png](http://xxxx/yx_default.png)",
           "dVolume": 20,
           "dUsedVolume": 5,
           "tEdit": "2021-01-20" 
        }
    }
}`

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px">返回参数</span></span></span></span>

名称类型示例值说明
authCodeStringd20a3965-5eb8-484b-84ea-e1e4d755a223授权码
iIdint1用户标识码
sPhoneString13800000000手机号(也称账号)
sNickNameStringyx\_13800000000昵称(可修改,默认yx\_手机号组合)
sPathStringhttp://xxx/yx\_default.png头像(可修改,默认yx\_default.png)
dVolumeDouble20存储容量总量
dUsedVolumeDouble5已使用存储容量
tEditDateTime2021-01-20注册日期

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px"><span class="colour" style="color:rgb(126, 140, 141)"><span class="font" style="font-family:revert"><span class="size" style="font-size:revert"><span class="font" style="font-family:revert"><span class="size" style="font-size:24px">登录流程</span></span></span></span></span></span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px">登录即注册,用户第一次登录,验证通过后将用户信息以key-value的形式存入到Redis缓存中。如果是再次登录则存入新值,用户缓存有效期7天。</span></span></span></span>

<span class="highlight" style="background-color:rgb(255, 255, 255)"><span class="colour" style="color:rgb(51, 51, 51)"><span class="font" style="font-family:&quot;Microsoft Yahei&quot;, &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, Arial, sans-serif"><span class="size" style="font-size:16px">抛弃床的温暖,只为了它丨云库一款跑在鸿蒙系统上的应用丨篇二</span></span></span></span><span class="colour" style="color:rgb(51, 51, 51)"><span class="colour" style="color:rgb(126, 140, 141)">华为云对象存储OBS介绍</span></span>

<span class="colour" style="color:rgb(51, 51, 51)">对象存储服务(Object Storage Service,OBS)提供海量、安全、高可靠、低成本的数据存储能力,可供用户存储任意类型和大小的数据。适合企业备份/归档、视频点播、视频监控等多种数据存储场景。我们这里只是用于存储多媒体文件。</span>

<span class="colour" style="color:rgb(51, 51, 51)">对于现阶段,我们只需要了解OBS的桶、对象两个概念。桶是存储对象的容器,对象是数据存储的基本单位。可以对应我们系统的文件管理来看,桶相当于磁盘,比如D盘,在D盘根目录下只能存在一个同类型的文件,而对象就相当于文件夹中的文件。</span>
<span class="colour" style="color:rgb(51, 51, 51)">而云库中,用于可以通过HAP创建多个云库(桶),在云库中上传多媒体文件。</span>
查看更多章节>>>

<span class="colour" style="color:rgb(51, 51, 51)">作者:云昌白晓明</span>
想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

21_9.jpg

推荐阅读
关注数
3008
内容数
446
华为鸿蒙相关技术,活动及资讯,欢迎关注及加入创作
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息