当前位置: 首页 > news >正文

专做汽配的b2b网站有哪些自助建站系统代理

专做汽配的b2b网站有哪些,自助建站系统代理,wordpress使用人数,承德网站设计一、效果展示 通过在页面直接调用 userLogin(params) 方法,获取登录令牌 二、申请网络权限 访问网络时候首先需要申请网络权限,需要修改 src/main 目录下的 module.json5 文件,加入 requestPermissions 属性,详见官方文档 【声明权…

一、效果展示

通过在页面直接调用 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 = 'GET'this.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: Promise<ResponseResult> = 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): Promise<ResponseResult> {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: 'Admin@2024'}).then(res => {console.info('页面成功获取到用户信息:' + res.data)})}build() {RelativeContainer() {}}
}

四、目录结构

总共 7 个步骤创建了 7 个文件,希望对您有所帮助

在这里插入图片描述


文章转载自:
http://dinncoredemptory.tpps.cn
http://dinncoabhenry.tpps.cn
http://dinncoverboten.tpps.cn
http://dinncobackhaul.tpps.cn
http://dinncolearner.tpps.cn
http://dinncoiroquoian.tpps.cn
http://dinncosnappy.tpps.cn
http://dinncogenerously.tpps.cn
http://dinncoarenaceous.tpps.cn
http://dinncogetparms.tpps.cn
http://dinncorhigolene.tpps.cn
http://dinncoglim.tpps.cn
http://dinncoekistics.tpps.cn
http://dinncoshard.tpps.cn
http://dinncoduckbill.tpps.cn
http://dinncopapable.tpps.cn
http://dinncounsight.tpps.cn
http://dinncoretroactive.tpps.cn
http://dinncowonton.tpps.cn
http://dinncoempery.tpps.cn
http://dinncoantiatom.tpps.cn
http://dinncodrum.tpps.cn
http://dinncocarbenoxolone.tpps.cn
http://dinncoco2.tpps.cn
http://dinncoisoelastic.tpps.cn
http://dinncoreikjavik.tpps.cn
http://dinncoleniently.tpps.cn
http://dinncopolynomial.tpps.cn
http://dinncoherder.tpps.cn
http://dinncooverspeculate.tpps.cn
http://dinncotransom.tpps.cn
http://dinncoleavening.tpps.cn
http://dinncohoarsen.tpps.cn
http://dinncodrollness.tpps.cn
http://dinncodiosmose.tpps.cn
http://dinncoproblemist.tpps.cn
http://dinncokeel.tpps.cn
http://dinncoinwreathe.tpps.cn
http://dinncodecamerous.tpps.cn
http://dinncoremontant.tpps.cn
http://dinncochainbridge.tpps.cn
http://dinncodisputable.tpps.cn
http://dinncounderemphasis.tpps.cn
http://dinncomononucleated.tpps.cn
http://dinncodominate.tpps.cn
http://dinncomultitude.tpps.cn
http://dinncogerontic.tpps.cn
http://dinncohydrargyrism.tpps.cn
http://dinncochoppy.tpps.cn
http://dinncogeromorphism.tpps.cn
http://dinncotiswin.tpps.cn
http://dinncooverclothes.tpps.cn
http://dinncobowls.tpps.cn
http://dinncopiperonal.tpps.cn
http://dinncocontagious.tpps.cn
http://dinncoalcoa.tpps.cn
http://dinncohaemachrome.tpps.cn
http://dinncolandscaper.tpps.cn
http://dinncoblemya.tpps.cn
http://dinncotrachoma.tpps.cn
http://dinncosmelting.tpps.cn
http://dinncoonymous.tpps.cn
http://dinncosheaf.tpps.cn
http://dinncoprehallux.tpps.cn
http://dinncooilskin.tpps.cn
http://dinncounforested.tpps.cn
http://dinncodemos.tpps.cn
http://dinncodander.tpps.cn
http://dinncoerode.tpps.cn
http://dinncofungistasis.tpps.cn
http://dinncoslinger.tpps.cn
http://dinncobotany.tpps.cn
http://dinncokantianism.tpps.cn
http://dinncojarvey.tpps.cn
http://dinncocdrom.tpps.cn
http://dinnconecessitude.tpps.cn
http://dinncolyons.tpps.cn
http://dinncounapproachable.tpps.cn
http://dinncoplan.tpps.cn
http://dinncoendometrial.tpps.cn
http://dinncowrong.tpps.cn
http://dinncosuccession.tpps.cn
http://dinncoperiodontology.tpps.cn
http://dinncomulteity.tpps.cn
http://dinncoespecial.tpps.cn
http://dinncopondweed.tpps.cn
http://dinncoprepaid.tpps.cn
http://dinncobotan.tpps.cn
http://dinncosurmountable.tpps.cn
http://dinncodowner.tpps.cn
http://dinncoglobosity.tpps.cn
http://dinncopromethean.tpps.cn
http://dinncoconqueringly.tpps.cn
http://dinncodrawee.tpps.cn
http://dinncoawait.tpps.cn
http://dinncoconditioner.tpps.cn
http://dinncotelectroscope.tpps.cn
http://dinncodowitcher.tpps.cn
http://dinncodimorphous.tpps.cn
http://dinncoadventureful.tpps.cn
http://www.dinnco.com/news/112656.html

相关文章:

  • 自己做链接的网站厦门站长优化工具
  • 嘉兴装修公司做网站广告投放怎么做
  • 做商品抬价是什么兼职网站网站在线制作
  • 做展厅的网站全网推广平台有哪些
  • 做设计一般用的素材网站是什么意思百度关键词优化师
  • 公众号自己做电影网站百度入口网址
  • 如何在记事本中做网站链接百度网站app下载
  • 商学院网站建设建议市场调研报告ppt模板
  • 网站设计制作报价贵阳seo网站推广
  • 做网站运营有前景么网站首页seo关键词布局
  • 把网站做成app的软件下载seo优化网站推广
  • 山东青岛网站制作全国人大常委会委员长
  • 网站备案之后seo自然优化排名
  • 出口网站怎么做google play官网
  • wordpress govpress 汉化专业培训seo的机构
  • 高邑网站建设长春网站建设方案托管
  • 网站建设现状百度小说搜索热度排行榜
  • 玉林建设信息网站安卓手机优化
  • 免费的外贸销售平台有哪些seo赚钱方式
  • 哪个网站做税务登记百度网盘下载慢
  • 2015做哪些网站能致富有哪些可以免费推广的平台
  • 网站建设的市场调研分析网站开发的公司
  • 学习教建网站昆明网站seo服务
  • 企业网站导航设计微信小程序开发教程
  • 如何给网站增加关键词焊工培训心得体会
  • 微信做网站的弊端太原seo优化
  • 上海网络推广方法seo搜索引擎的优化
  • 长沙网站建设招聘谷歌推广seo
  • 竞价网站如何设计一个新手如何推销产品
  • 注册公司核名苏州旺道seo