Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop Mini Program Mobile Library

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to develop Mini Program Mobile Library". In the operation of practical cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Mini Program MINA Framework: a responsive data binding framework. Divided into two view layers (View) and logical layers (App Service)

Flex:flex flexible layout

Express: http Services Framework

Websocket: real-time push of front and back end messages

Mongoose: manipulating mongodb database

Pm2: server deployment using pm2, resident process

Code structure

There will be four files for each page in WeChat Mini Programs. Js.json.wxml.wxss

Js file is the logic of the page, json file is some configuration of the page, wxml is the page structure of Mini Program, wxss is the style of the page.

Encapsulate a http request

Const request = (obj) = > {

If (obj.header) {

Obj.header.sessionId = session.sessionId

} else {

Obj.header = {sessionId: session.sessionId}

}

Wx.request (obj)

}

Manually add sessionId to the request header because Mini Program does not have cookie.

Websocket

/ / Connect to websocket

Wx.connectSocket ({

Url: 'wss://liudongtushuguan.cn/socket?sessionId=' + session.sessionId

});

Wx.onSocketOpen (function (res) {

});

Wx.onSocketClose (function (res) {

Console.log ('websocket closed')

});

Wx.onSocketMessage (function (res) {/ / callback of received message

Let msg = JSON.parse (res.data)

Let msgs = that.data.borrowMessage

Msgs.unshift (msg)

That.setData ({borrowMessage: msgs})

});

/ / send socket messages

Let data = JSON.stringify ({

TargetId: bookData.ownerId

NickName: APP.globalData.userInfo.nickName

BookName: bookData.title

Time: new Date () .toLocaleString ()

BookId: bookId

WxNum: wxNum

PhoneNum: phoneNum

Msg: msg

});

Wx.sendSocketMessage ({

Data: data

});

Server side

Code directory

Express Framework implements http Service

Const https = require ('https')

Const fs = require ('fs')

Const express = require ('express')

Const cookieParser = require ('cookie-parser')

Const bodyParser = require ('body-parser')

Const app = express ()

Const queryString = require ('querystring')

Const URL = require ('url')

Const socket = require ('. / service/socket')

Const router = require ('. / routes/router') .router

/ / obtain certification certificate

Var key = fs.readFileSync ('. / key/2_www.liudongtushuguan.cn.key')

Var cert = fs.readFileSync ('. / key/1_www.liudongtushuguan.cn_bundle.crt')

Var options = {

Key: key

Cert: cert

}

App.use (cookieParser ())

App.use (bodyParser.json ())

Const httpsServer = https.createServer (options,app)

HttpsServer.listen (443, () = > {

Console.log ('listening 443 port')

});

Socket (httpsServer); / / websocket

App.use (router)

Mini Program stipulates that the https protocol must be used.

Websocket module

Const WebSocket = require ('ws'); / / use the ws module

Const queryString = require ('querystring')

Const URL = require ('url')

Const sessions = require ('. / session')

Module.exports = (httpServer) = > {

Const wss = new WebSocket.Server ({server: httpServer})

Wss.on ('connection', (ws, req) = > {

Let sessionId = queryString.parse (URL.parse (req.url) .query) .sessionId

Ws.id = sessionId

Ws.on ('message', (msg) = > {

Let msgObj = JSON.parse (msg)

If (sessions [msgObj.targetId]) {

Wss.clients.forEach ((client) = > {

If (client.id = msgObj.targetId) {

Let data = {

Time: msgObj.time

Borrower: msgObj.nickName

Book: msgObj.bookName

BorrowerId: sessions [sessionId]

BookId: msgObj.bookId

WxNum: msgObj.wxNum

PhoneNum: msgObj.phoneNum

Msg: msgObj.msg

}

Client.send (JSON.stringify (data))

}

});

}

});

});

}

Mongoose operates the database

Db.js:

Const mongoose = require ('mongoose')

Mongoose.connect ('mongodb://app:12345678@127.0.0.1/wxapp')

Const connection = mongoose.connection

Connection.once ('open', (err) = > {

If (err) {

Console.log ('Database connection failure')

} else {

Console.log ('Database opened')

}

});

Connection.on ('error', (err) = > {

Console.log ('Mongoose connected error' + err)

});

Connection.on ('disconnected', ()) = > {

Console.log ('Mongoose disconnected')

});

Module.exports = {

Connection: connection

Mongoose: mongoose

}

Model.js:

Const Schema = mongoose.Schema

Const UserSchema = new Schema ({

OnlyId: {type: String}

PublishedBooks: {type: Array}

BorrowedBooks: {type: Array}

BorrowMessages: Array

});

Const BookSchma = new Schema ({

Isbn: String

Title: String

Press: String

Author: String

Rate: String

Tags: String

Image: String

Status: {type: Boolean,default: true}

OwnerId: String

Owner: String

OwnerImage: String

});

Const userModel = mongoose.model ('user', UserSchema)

Const bookModel = mongoose.model ('book', BookSchma)

Module.exports = {

UserModel: userModel

BookModel: bookModel

}

Get the openId of Wechat users

The Mini Program front end will request the Wechat server to get a code, send the code to its own server, and then its own server will send a request to the Wechat server to get the unique identity openId of the Wechat user.

Const https = require ('https')

Const queryString = require ('querystring')

Const sessions = require ('. / session')

Module.exports = (req, res, next) = > {

Let code = req.query.code

Let otherRes = res

DATA.js_code = code

OPTION.path = PATH + queryString.stringify (DATA)

Let wxReq = https.request (OPTION, (res) = > {

If (res.statusCode = = 200) {

Let json =''

Res.on ('data', (data) = > {

Json+=data

});

Res.on ('end', () = > {

Json = JSON.parse (json)

Let openId = json.openid

Sessions [openId] = openId

OtherRes.type ('application/json')

OtherRes.json ({

Data: {'sessionId': openId}

});

OtherRes.status (200)

});

}

});

WxReq.end ()

}

Deploy using pm2

Install pm2

Npm install-g pm2

Start the application

Pm2 start app.js

This is the end of the content of "how to develop Mini Program Mobile Library". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report