2008服务器做网站,做企鹅号的视频素材网站,网站开发课程改革,推荐网站制作公司一、效果展示 通过在页面直接调用 userLogin(params) 方法#xff0c;获取登录令牌 二、申请网络权限 访问网络时候首先需要申请网络权限#xff0c;需要修改 src/main 目录下的 module.json5 文件#xff0c;加入 requestPermissions 属性#xff0c;详见官方文档 【声明权…一、效果展示 通过在页面直接调用 userLogin(params) 方法获取登录令牌 二、申请网络权限 访问网络时候首先需要申请网络权限需要修改 src/main 目录下的 module.json5 文件加入 requestPermissions 属性详见官方文档 【声明权限】https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/declare-permissions-V5 {module: {// ...requestPermissions:[{name : ohos.permission.INTERNET,usedScene: {abilities: [FormAbility],when:inuse}},{name : ohos.permission.GET_NETWORK_INFO,usedScene: {abilities: [FormAbility],when:inuse}}]}
}三、实现代码 共分为 7 个步骤其中第 1、2、3、5 步都是创建基础实体第 4 步为请求工具封装、第 6 步为API封装、第 7 步为页面调用测试我会在最后贴上文件目录结构 1、创建常量类 CommonConstant.ets 在这个常量类中主要定义后端服务IP端口以及一些需要使用的状态码信息 /*** HTTP API*/
export class Domain {// 后端服务IPstatic readonly SERVER: string http://localhost:8999}/*** HTTP状态类*/
export class HttpStatus {// 成功static readonly SUCCESS: number 200// 失败static readonly ERROR: number 500}export const enum ContentType {// 响应体类型JSON application/json}2、创建请求实体类 RequestBody.ets 这个请求实体类主要可以实现简单的传输【请求地址、请求方式、请求参数】这三个参数再经过封装的工具去发送网络请求 export class RequestBody {url: string;method: string;data: Object;constructor() {this.url this.method GETthis.data new Object}
}3、创建响应实体类 ResponseResult.ets 这个响应实体主要对应后台的返回参数比如我这里定义的后台响应码为【code】信息为【msg】响应数据为【data】这个可以自行修改 export class ResponseResult {code: number;msg: string | Resource;data: ArrayBuffer | Object | stringconstructor() {this.code 0;this.msg ;this.data ;}
}4、创建请求工具类 HttpRequest.ets 详见官方文档 【HTTP数据请求】https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/http- request-V5 【异步并发概述 (Promise和async/await)】https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/async-concurrency-overview-V5#promise // 这里需要引入第 2 步和第 3 步创建的请求和响应实体
import { ResponseResult } from ../response/ResponseResult
import { RequestBody } from ../response/RequestBody// 这里需要引入第 1 步公共常量类
import { Domain, HttpStatus, ContentType } from ../constant/CommonConstant// 这里引用 HTTP 库
import http from ohos.net.http// HTTP响应处理时候需要的库
import { BusinessError } from kit.BasicServicesKit;class HttpRequest {request(requestBody: RequestBody) {const promise: PromiseResponseResult new Promise((resolve: Function, reject: Function) {console.log(创建HTTP请求请求地址 Domain.SERVER requestBody.url 请求方式 requestBody.method 请求体 JSON.stringify(requestBody.data))// 创建HTTP请求const httpRequest http.createHttp()// 在这里发送 HTTP 请求并且需要设置Url、Header、Body等信息httpRequest.request(Domain.SERVER requestBody.url,{method: isPost(requestBody.method) ? http.RequestMethod.POST : http.RequestMethod.GET,readTimeout: 10000,header: {Content-Type: ContentType.JSON},connectTimeout: 10000,extraData: requestBody.data},(err: BusinessError, data: http.HttpResponse) {// 回调之后判断是否提示 Errorif (!err) {let result ${data.result}let resultJSON: ResponseResult JSON.parse(result)// 判断响应码是否成功if (data.responseCode HttpStatus.SUCCESS) {if (resultJSON.code 0) {console.info(请求成功 resultJSON.data)resolve(resultJSON)} else {reject(new Error( resultJSON.msg))errorDialog( resultJSON.msg)}} else {reject(new Error( resultJSON.msg))errorDialog( resultJSON.msg)}// 当该请求使用完毕时调用destroy方法主动销毁httpRequest.destroy();} else {console.error(ERROR: JSON.stringify(err));if (err.code 2300007) {errorDialog(无法连接至服务器请稍后重试)}if (err.code 2300028) {errorDialog(当前网络环境不稳定请稍后重试)}// 当该请求使用完毕时调用destroy方法主动销毁httpRequest.destroy();reject(new Error(当前网络环境不稳定请稍后重试))}})})return promise;}
}const httpRequest new HttpRequest();
export default httpRequest as HttpRequest;// 判断是否是GET方法
function isGet(method: string): boolean | undefined {if (method http.RequestMethod.GET) {return true;}return false;
}// 判断是否是POST方法
function isPost(method: string): boolean | undefined {if (method http.RequestMethod.POST) {return true;}return false;
}// 提示错误
function errorDialog(msg: string) {console.error(msg)
}5、创建一个用户登录实体 userInfo.ets 这个用户实体类包含用户的账号和密码信息是登录所需 export class UserInfo {account: string;password: string;constructor() {this.account this.password }
}6、创建一个用户API user.ets 这个API的作用就是统一格式统一使用【url】、【data】、【method】这三个进行网络请求也是在请求工具类 HttpRequest.ets 基础上的二次封装 // 这里引入第 4 步创建的请求工具类
import HttpRequest from ../request/HttpRequest// 这里引入第 3 步创建的响应体
import { ResponseResult } from ../response/ResponseResult// 这里引入第 5 步创建的自定义用户对象主要用于定义各参数的类型
import { UserInfo } from ../../entity/userInfo// 用户登录 API
export function userLogin(params: UserInfo): PromiseResponseResult {return HttpRequest.request({url: /auth/login,data: params,method:POST})
}7、在页面尝试调用 index.ets 最后在我们的页面启动时直接调用 user.ets 中的 userLogin 方法进行登录测试 // 这里需要引入第 6 步创建的用户API
import { userLogin } from ../common/api/user;Entry
Component
struct Index {// 在进入页面时调用 userLogin 接口进行测试aboutToAppear(): void {userLogin({account: admin, password: Admin2024}).then(res {console.info(页面成功获取到用户信息 res.data)})}build() {RelativeContainer() {}}
}四、目录结构 总共 7 个步骤创建了 7 个文件希望对您有所帮助 文章转载自: http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.fnzbx.cn.gov.cn.fnzbx.cn http://www.morning.mzskr.cn.gov.cn.mzskr.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.kdldx.cn.gov.cn.kdldx.cn http://www.morning.zhiheliuxue.com.gov.cn.zhiheliuxue.com http://www.morning.bpmmq.cn.gov.cn.bpmmq.cn http://www.morning.pmghz.cn.gov.cn.pmghz.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn http://www.morning.lqypx.cn.gov.cn.lqypx.cn http://www.morning.dndjx.cn.gov.cn.dndjx.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.qbzdj.cn.gov.cn.qbzdj.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.fnfhs.cn.gov.cn.fnfhs.cn http://www.morning.tqsnd.cn.gov.cn.tqsnd.cn http://www.morning.bnrnb.cn.gov.cn.bnrnb.cn http://www.morning.krtky.cn.gov.cn.krtky.cn http://www.morning.roymf.cn.gov.cn.roymf.cn http://www.morning.zzfjh.cn.gov.cn.zzfjh.cn http://www.morning.tpchy.cn.gov.cn.tpchy.cn http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn http://www.morning.xcszl.cn.gov.cn.xcszl.cn http://www.morning.mltsc.cn.gov.cn.mltsc.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn http://www.morning.rnpt.cn.gov.cn.rnpt.cn http://www.morning.rgwz.cn.gov.cn.rgwz.cn http://www.morning.jfgmx.cn.gov.cn.jfgmx.cn http://www.morning.lgnz.cn.gov.cn.lgnz.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.gcfg.cn.gov.cn.gcfg.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.hhqjf.cn.gov.cn.hhqjf.cn http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.lqynj.cn.gov.cn.lqynj.cn http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.zlrsy.cn.gov.cn.zlrsy.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.yslfn.cn.gov.cn.yslfn.cn http://www.morning.sffkm.cn.gov.cn.sffkm.cn http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.rzscb.cn.gov.cn.rzscb.cn http://www.morning.rykgh.cn.gov.cn.rykgh.cn http://www.morning.djpps.cn.gov.cn.djpps.cn http://www.morning.ylzdx.cn.gov.cn.ylzdx.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.gthwz.cn.gov.cn.gthwz.cn http://www.morning.fnpyk.cn.gov.cn.fnpyk.cn http://www.morning.mbaiwan.com.gov.cn.mbaiwan.com http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.bgxgq.cn.gov.cn.bgxgq.cn http://www.morning.bpmnx.cn.gov.cn.bpmnx.cn http://www.morning.lszjq.cn.gov.cn.lszjq.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.nyplp.cn.gov.cn.nyplp.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.jrdbq.cn.gov.cn.jrdbq.cn http://www.morning.gtkyr.cn.gov.cn.gtkyr.cn http://www.morning.przc.cn.gov.cn.przc.cn http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.kjcfz.cn.gov.cn.kjcfz.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.zxznh.cn.gov.cn.zxznh.cn http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.bphqd.cn.gov.cn.bphqd.cn