harmonyos 华为 node.js mongodb arkTS开发鸿蒙OS个人商城案例【2027最新 新年限定开发案例QAQ】 - pages/detail&#39

📅 2026-11-30 👤 拼多多助力营销技巧 📂 pdd帮助力 👁️ 529 次阅读

= []

@State selectedItemId : number = 0

@State one:string = ''

private changeValue:string = this.sousuoValue?.['sousuoValue']

build() {

Column() {

Header()

Search({ value: this.changeValue, placeholder: '请输入搜索的商品',})

.searchButton('搜索')

.width(300)

.height(40)

.backgroundColor('#F5F5F5')

.placeholderColor(Color.Grey)

.placeholderFont({ size: 14, weight: 400 })

.textFont({ size: 14, weight: 400 })

// 点击搜索

.onSubmit((value: string) => {

axios({

method: "post",

url: 'http://localhost:3000/shangpins/products/',

data: {

changeValue: this.changeValue

}

}).then(res => {

console.log(JSON.stringify(res.data.data));

this.items = res.data.data

}).catch(error => {

console.error(error);

});

})

// 输入搜索

.onChange((value: string) => {

this.changeValue = value

console.log(this.changeValue)

axios({

method: "post",

url: 'http://localhost:3000/shangpins/products/',

data: {

changeValue: value

}

}).then(res => {

console.log(JSON.stringify(res.data.data));

this.items = res.data.data

}).catch(error => {

console.error(error);

});

})

.margin(20)

List({ space: 20, initialIndex: 0 }){

ForEach(this.items, (item) => {

ListItem() {

Row(){

Image(item.img)

.alt($r('app.media.icon'))// 使用alt,在网络图片加载成功前使用占位图

.width(150)

.height(180)

Column({space:10}){

Text(item.name)

.fontWeight(800)

.margin(20)

Text(item.detail)

Text('¥:'+item.price)

.fontColor('red')

.fontWeight(FontWeight.Bold)

.fontSize(20)

}

.width(180)

}.alignItems(VerticalAlign.Top)

}

.onClick(() => {

this.selectedItemId = item._id;

// 获取点击物品的id

this.one = item._id

console.log(JSON.stringify(this.one))

router.pushUrl({

url: 'pages/detail',

params: {

id: this.one,

}

})

})

})

}

.height('90%')

.listDirection(Axis.Vertical) // 排列方向

.divider({ strokeWidth: 2, color: 0xFFFFFF, startMargin: 20, endMargin: 20 }) // 每行之间的分界线

.edgeEffect(EdgeEffect.Spring) // 滑动到边缘无效果

}

.width('100%')

.height('100%')

}

//上一个页面跳转过来之后查询的数据

onPageShow() {

console.log('wwww' + this.sousuoValue?.['sousuoValue']);

axios({

method: "post",

url: 'http://localhost:3000/shangpins/products/',

data: {

changeValue: this.sousuoValue?.['sousuoValue'] // 修正获取参数的方式

}

}).then(res => {

console.log(JSON.stringify(res.data.data));

this.items = res.data.data

}).catch(error => {

console.error(error);

});

}

}

WangjiMima.ets

import { Header } from '../components/Toubu'

import axios from '@ohos/axios'

import router from '@ohos.router'

@Entry

@Component

struct Index {

// 上传数据

@State zhanghao: string = ''

@State mima: string = ''

build() {

Column() {

Header()

.margin(20)

TextInput({placeholder:'原账号'})

.margin(20)

.height(50)

.onChange(value =>{

console.log(value)

this.zhanghao = value

})

.backgroundColor('#36D2')

TextInput({placeholder:'新密码'})

.margin({ left:20,right:20,bottom:20 })

.height(50)

.onChange(value =>{

console.log(value)

this.mima = value

})

.backgroundColor('#36D2')

Button('修改密码')

.width(200)

.onClick(()=>{

axios({

method: "post",

url: 'http://localhost:3000/users/upd',

data:{

zhanghao:this.zhanghao,

newmima:this.mima

},

}).then(res => {

console.info('result:' + JSON.stringify(res.data));

{

router.pushUrl({

url: 'pages/NewApp_one',

})

}

}).catch(error => {

console.error(error);

})

})

}

.width('100%')

.height('100%')

}

}

XiugaiXinxi.ets

import { Header } from '../components/Toubu'

@Entry

@Component

struct XiugaiXinxi {

@State message: string = '修改信息'

build() {

Column() {

Header()

Text(this.message)

.fontSize(50)

.fontWeight(FontWeight.Bold)

}

.width('100%')

.height('100%')

}

}

zhuce.ets

import { Header } from '../components/Toubu'

import axios from '@ohos/axios'

import router from '@ohos.router'

@Entry

@Component

struct Index {

// 上传数据

@State zhanghao: string = ''

@State mima: string = ''

@State zhanghao_find:string =''

@State mima_find:string =''

build() {

Column() {

Header()

.margin(20)

TextInput({placeholder:'注册账号'})

.margin(20)

.height(50)

.onChange(value =>{

console.log(value)

this.zhanghao = value

})

.backgroundColor('#36D2')

TextInput({placeholder:'注册密码'})

.margin({ left:20,right:20,bottom:20 })

.height(50)

.onChange(value =>{

console.log(value)

this.mima = value

})

.backgroundColor('#36D2')

Button('注册并登录')

.width(200)

.onClick(()=>{

axios({

method: "post",

url: 'http://localhost:3000/users/publish',

data:{

zhanghao:this.zhanghao,

mima:this.mima

},

}).then(res => {

console.info('result:' + JSON.stringify(res.data));

router.pushUrl({

url: 'pages/NewApp_one',

})

}).catch(error => {

console.error(error);

})

})

}

.width('100%')

.height('100%')

}

}

node.js后端架构

fenlei_api.js

// user_api.js

const express = require('express');

const router = express.Router();

const { fenlei } = require('../db');

router.use(express.urlencoded({ extended: true }));

router.use(express.json());

// 全部查询

router.get("/find_all", async (req, res) => { // Corrected function signature

try {

const results = await fenlei.find();

if (results.length > 0) {

// 如果找到匹配的记录,则返回所有匹配的记录

res.json({ data: results, message: "登录成功!" });

} else {

res.status(404).json({ message: "未找到匹配的记录" });

}

} catch (error) {

res.status(500).json({ message: "服务器内部错误" });

}

});

// 指定查询

router.get("/find/:name", async (req, res) => {

try {

const name = req.params.name;

// 使用 find 查询所有匹配指定 name 的数据记录

const results = await fenlei.find({ name });

if (results.length > 0) {

// 如果找到匹配的记录,则返回所有匹配的记录

res.json({ data: results, message: "登录成功!" });

} else {

res.status(404).json({ message: "未找到匹配的记录" });

}

} catch (error) {

res.status(500).json({ message: "服务器内部错误" });

}

});

module.exports = router;

gouwuche_api.js

// user_api.js

const express = require('express');

const router = express.Router();

const { gouwuche } = require('../db');

router.use(express.urlencoded({ extended: true }));

router.use(express.json());

// 全部查询

router.post("/find", async (req, res) => {

try {

const { username } = req.body;

// 使用 find 查询所有匹配指定 select_classify 的数据记录

const results = await gouwuche.find({ username });

if (results.length > 0) {

// 如果找到匹配的记录,则返回所有匹配的记录 拼多多帮助力入口

res.json({ data: results, message: "登录成功!" });

} else {

res.status(404).json({ message: "未找到匹配的记录" });

}

} catch (error) {

res.status(500).json({ message: "服务器内部错误" });

}

});

// 添加到购物车

router.post("/publish", async (req, res) => {

try {

const { username,name,detail,img,select_classifyprice,price } = req.body;

await gouwuche.create({

username,

name,

detail,

img,

select_classifyprice,

price

});

res.send("success");

} catch (error) {

res.send(error, "error");

}

});

module.exports = router;

shangpin_api.js

// user_api.js

const express = require('express');

const router = express.Router();

const { shangpin } = require('../db');

router.use(express.urlencoded({ extended: true }));

router.use(express.json());

// 全部查询

router.get("/find_all", async (req, res) => { // Corrected function signature

try {

const results = await shangpin.find();

if (results.length > 0) {

// 如果找到匹配的记录,则返回所有匹配的记录

res.json({ data: results, message: "登录成功!" });

} else {

res.status(404).json({ message: "未找到匹配的记录" });

}

} catch (error) {

res.status(500).json({ message: "服务器内部错误" });

}

});

// 指定查询

router.post("/find", async (req, res) => {

try {

const { select_classify } = req.body;

// 使用 find 查询所有匹配指定 select_classify 的数据记录

const results = await shangpin.find({ select_classify });

if (results.length > 0) {

// 如果找到匹配的记录,则返回所有匹配的记录

res.json({ data: results, message: "登录成功!" });

} else {

res.status(404).json({ message: "未找到匹配的记录" });

}

} catch (error) {

res.status(500).json({ message: "服务器内部错误" });

}

});

// 指定商品详情查询

router.post("/find_detail", async (req, res) => {

try {

const { _id } = req.body;

// 使用 find 查询所有匹配指定 _id 的数据记录

const results = await shangpin.find({ _id });

if (results.length > 0) {

// 如果找到匹配的记录,则返回所有匹配的记录

res.json({ data: results, message: "登录成功!" });

} else {

res.status(404).json({ message: "未找到匹配的记录" });

}

} catch (error) {

res.status(500).json({ message: "服务器内部错误" });

}

});

//模糊查询

router.post('/products', async (req, res) => {

try {

const changeValue = req.body.changeValue; // 修正获取请求体中的参数方式

// 使用正则表达式进行模糊查询

const results = await shangpin.find({ name: { $regex: changeValue, $options: 'i' } });

if (results.length > 0) {

// 如果找到匹配的记录,则返回所有匹配的记录

res.json({ data: results, message: "查询成功!" });

} else {

res.status(404).json({ message: "未找到匹配的记录" });

}

} catch (error) {

console.error(error);

res.status(500).json({ message: "服务器内部错误" });

}

});

module.exports = router;

user_api.js

// user_api.js

const express = require('express');

const router = express.Router();

const { users } = require('../db');

router.use(express.urlencoded({ extended: true }));

router.use(express.json());

// 注册账号

router.post("/publish", async (req, res) => {

try {

const { zhanghao, mima } = req.body;

await users.create({

zhanghao, mima

});

res.send("success");

} catch (error) {

res.send(error, "error");

}

});

// 注销账号

router.post("/del", async (req, res) => {

console.log(req.body.zhanghao);

try {

const { zhanghao } = req.body;

// 使用 deleteOne 删除指定 name 的数据

const result = await users.deleteOne({ zhanghao });

if (result.deletedCount === 1) {

res.send("success");

} else {

res.send("未找到匹配的记录");

}

} catch (error) {

res.send(error, "error");

}

});

// 修改账号密码

router.post("/upd", async (req, res) => {

try {

const { zhanghao, newmima } = req.body;

// 使用 updateOne 更新指定 name 的数据记录的 nianling 字段

const result = await users.updateOne({ zhanghao }, { $set: { mima: newmima } });

res.json({ message: "密码更新成功!", result });

} catch (error) {

res.status(500).json({ error: error.message });

}

});

// 账号登录

router.get("/find/:zhanghao/:mima", async (req, res) => {

try {

const zhanghao = req.params.zhanghao;

const mima = req.params.mima;

// 使用 find 查询所有匹配指定 name 的数据记录

const results = await users.find({ zhanghao, mima });

if (results.length > 0) {

// 如果找到匹配的记录,则返回所有匹配的记录

res.json({ data: results, message: "登录成功!" });

} else {

res.status(404).json({ message: "未找到匹配的记录" });

}

} catch (error) {

res.status(500).json({ message: "服务器内部错误" });

}

});

module.exports = router;

db.js

const mongoose = require('mongoose')

//连接mongodb数据库

mongoose.connect("mongodb://localhost:27017/node_one")

.then(() => {

console.log("数据库连接成功!")

})

.catch((err) => {

console.log("数据库连接失败!", err)

})

// 创建表用户表

const Users = new mongoose.Schema({

zhanghao: {

type: String,

},

mima: {

type: String

},

})

// 创建商品表

const Shangpin = new mongoose.Schema({

name: {

type: String,

},

detail: {

type: String

},

img:{

type: String

},

select_classify:{

type: String

},

price:{

type: String

}

})

//创建分类表

const Fenlei = new mongoose.Schema({

name: {

type: String,

},

})

// 创建购物车表

const Gouwuche = new mongoose.Schema({

username:{

type: String,

},

name: {

type: String,

},

detail: {

type: String

},

img:{

type: String

},

select_classify:{

type: String

},

price:{

type: String

}

})

const users = mongoose.model("Users", Users);

const shangpin = mongoose.model("Shangpin", Shangpin);

const fenlei = mongoose.model("Fenlei", Fenlei);

const gouwuche = mongoose.model("Gouwuche", Gouwuche);

module.exports = {

users,

shangpin,

fenlei,

gouwuche

}

index.js

// index.js

const express = require('express');

const app = express();

const userApi = require('./user_ctrl/user_api');

const shangpinApi = require('./shangpin_ctrl/shangpin_api');

const fenleiApi = require('./fenlei_ctrl/fenlei_api');

const gouwucheApi = require('./gouwuche_ctrl/gouwuche_api');

app.use('/users', userApi);

app.use('/shangpins', shangpinApi);

app.use('/gouwuche', gouwucheApi);

app.use('/fenleis', fenleiApi);

app.listen(3000, () => {

console.log('server running');

});

后端安装指令

1. 下载node.js框架

npm install express --save

2. 下载nodemon解决node代码更新的痛点

npm install nodemon -g

3. node.js连接mongodb数据库

npm install mongoose --save

好文阅读

返回顶部暗黑模式

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧