170
社区成员




Assignment 1 | Front-end and Back-end Separation Contacts Programming |
---|---|
Course for This Assignment | 2401_MU_SE_FZU |
Assignment Requirements | First Assignment -- Front-end and Back-end Separation Contacts Programming |
Name | Molan Xue |
Student ID | MU: 22125281 FZU:832201130 |
Objectives of This Assignment | Using web frameworks or other platforms to implement a front-end and back-end separated contacts management system with basic functionalities like adding, modifying, and deleting contacts. |
Other Reference | 1. WeChat Mini Program Design Guide 2. Javascript Style Guide 3. WeChat Developer Tools 4. The Art of Construction |
Developing a Separated Address Book System
The first assignment focuses on creating a contact management system that emphasizes the separation of front-end and back-end functionalities. This assignment includes three core functions:
After successfully implementing these basic functions, the project must be deployed to a cloud server, ensuring accessibility and usability.
Personal Software Process Stages | Estimated Time(minutes) | Actual Time(minutes) |
---|---|---|
Planning | 180 | 240 |
• Estimate | 180 | 240 |
Development | 1260 | 1680 |
• Analysis | 180 | 240 |
• Design Spec | 180 | 240 |
• Design Review | 90 | 120 |
• Coding Standard | 60 | 80 |
• Design | 180 | 240 |
• Coding | 480 | 720 |
• Code Review | 60 | 80 |
• Test | 30 | 40 |
Reporting | 360 | 480 |
• Test Repor | 90 | 120 |
• Size Measurement | 60 | 80 |
• Postmortem & Process Improvement Plan | 210 | 280 |
Sum | 1800 | 2400 |
After modifying the operation, the data in the Mini Program Cloud Development Console:
Click the Delete Contact button at the bottom of the screen:

The mini program will allow deleting contact information:

After deleting the operation, the data in the Mini Program Cloud Development Console:
The mini program will allow uploading local photos to complete contact information:

The core functions of the mini program include:
Design Steps | ||
---|---|---|
Layout Design | Use (scroll view) to display the contact list and support scrolling. | |
Use the (navigator) component to link to the contact details page. | ||
Design a floating letter index for quick and easy navigation. | ||
Style Design | Use wxss files to define the overall color tone and layout style, such as setting margins, font size, background color, etc., to make the interface more beautiful and user-friendly. |
Determine the Data Model Design a contacts collection to store contact information. Each contact document should include the following fields: | _Id: Unique identifier of the contact (automatically generated). |
Name: The name (string) of the contact person. | |
Phone: Contact's phone number (string). | |
Email: Contact's email address (string, optional). | |
Address: Contact's address (string, optional). | |
Create a Cloud Database Collection | Create a collection named contacts in the WeChat Cloud Development Console to ensure that the data model matches the design. |
Adding | Use the cloud function addContact to handle requests to add new contacts. |
---|---|
In cloud functions, receive contact information from the client and call the contacts. add() method to add a new contact to the database. | |
Modifying | Use the cloud function updateContact to handle requests to update contacts. |
Receive the id and new information of the contact to be updated, find the corresponding document in the database and update it. | |
Deleting | Use the cloud function deleteContact to handle requests to delete contacts. |
Receive the id of the contact to be deleted and delete the corresponding document from the database. |
Permission Control:
Deploying Cloud Functions: Deploy all designed cloud functions in the WeChat Cloud Development Console.
Functional testing:
<scroll-view class="contact" scroll-y="true" style="height:{{screenHeight}}rpx">
<view class="search"></view>
<block wx:for="{{filteredContacts}}" wx:key="letter">
<view class="group" wx:if="{{item.group.length != 0}}" id="{{item.letter}}">
<view class="header">{{item.letter}}</view>
<navigator class="card" wx:for="{{item.group}}" wx:for-item="star" wx:key="*this" url="/pages/card/card?id={{star._id}}">
<view class="name">{{star.name}}</view>
</navigator>
</view>
</block>
</scroll-view>
<input type="text" bindinput="onSearchInput" placeholder="Search for a contact" />
contactsCollection.get({
success: res => {
this.arrangeContact(res.data);
},
fail: err => {
console.error('Failed to Retrieve Contacts:', err);
}
});
if (/^[\u4e00-\u9fa5]+$/.test(contactName[0])) {
group.push(contactItem);
}
<view class="contact-detail">
<text>Name:{{contact.name}}</text>
<text>Phone:{{contact.phone}}</text>
<!-- Other Information -->
</view>
const id = this.options.id;
contactsCollection.doc(id).get({
success: res => {
this.setData({ contact: res.data });
}
});
cloud.init();
const db = cloud.database();
exports.main = async (event, context) => {
const { name, phone, email, address } = event; // Retrieve contact information from the event
try {
return await db.collection('contacts').add({
data: {
name,
phone,
email,
address
}
});
} catch (e) {
return {
error: e
};
}
};
### 5.2.2 Implemention of Modifying Operation
```javascript
// cloud/functions/updateContact/index.js
const cloud = require('wx-server-sdk');
cloud.init();
const db = cloud.database();
exports.main = async (event, context) => {
const { id, name, phone, email, address } = event; // Get the contact information to be modified
try {
return await db.collection('contacts').doc(id).update({
data: {
name,
phone,
email,
address
}
});
} catch (e) {
return {
error: e
};
}
};
// cloud/functions/deleteContact/index.js
const cloud = require('wx-server-sdk');
cloud.init();
const db = cloud.database();
exports.main = async (event, context) => {
const { id } = event; // Get the contact ID to be deleted
try {
return await db.collection('contacts').doc(id).remove();
} catch (e) {
return {
error: e
};
}
};
// Adding Contact
wx.cloud.callFunction({
name: 'addContact',
data: {
name: this.data.newContactName,
phone: this.data.newContactPhone,
email: this.data.newContactEmail,
address: this.data.newContactAddress
},
success: res => {
console.log('Contact added successfully', res);
// Update page status
},
fail: err => {
console.error('Failed to add contact', err);
}
});
// Get contact list
wx.cloud.callFunction({
name: 'loadContacts',
success: res => {
this.setData({
contacts: res.result // Update contact list
});
},
fail: err => {
console.error('Failed to retrieve contacts', err);
}
});
<view class="navi">
<view class="navitext">咖喱录</view>
</view>
<scroll-view class="contact" scroll-y="true" scroll-into-view="{{loc}}" style="height:{{screenHeight}}rpx">
<block wx:for="{{filteredContacts}}" wx:key="letter">
<view class="group" wx:if="{{item.group.length != 0}}" id="{{item.letter}}">
<view class="header">{{item.letter}}</view>
<navigator class="card" wx:for="{{item.group}}" wx:for-item="star" wx:key="*this" url="/pages/card/card?id={{star._id}}">
<view class="name">{{star.name}}</view>
</navigator>
</view>
</block>
</scroll-view>
loadContactsFromDatabase() {
contactsCollection.field({ name: true, _id: true }).get({
success: res => {
let contacts = res.data;
this.arrangeContact(contacts);
},
fail: err => {
console.error('Failed to retrieve contacts from database:', err);
}
});
}
filterContacts: function () {
const filteredContacts = this.data.contact.map(group => {
const filteredGroup = group.group.filter(contact =>
contact.name.toLowerCase().includes(searchTerm)
);
return { letter: group.letter, group: filteredGroup };
}).filter(group => group.group.length > 0);
this.setData({ filteredContacts: filteredContacts });
}
## 6.2 Backend
1. **Data Access**:
- Create a reference to the contacts collection for CRUD operations.
- Make data operations more intuitive, and subsequent retrieval, addition, and deletion of contacts can be done through this collection reference.
```javascript
const contactsCollection = db.collection('contacts');
contactsCollection.field({ name: true, _id: true }).get({
success: res => {
let contacts = res.data;
this.arrangeContact(contacts);
},
fail: err => {
console.error('Failed to retrieve contacts from database:', err);
}
});