- Small program name: taxi together
- Project Address: Client: github.com/jrainlau/t….
Server: github.com/jrainlau/t….
- Small program QR code:
After a period of two evening work time efforts, finally put my first small program development completed and released online. The whole process is still smooth, because the MPVUE scheme is used for development, so you can enjoy the same smooth development experience with VUE; The background system uses Python3 +flask framework, using the least code to complete the small program’s background logic. In addition to development, I also really experienced the development process of a micro channel small program, including the use of developer tools, the release of the experience version, and the application for online and so on. All of these experiences are worth documenting, so I decided to write this article while the iron is hot.
I. Requirements & Functions
Since many colleagues in the company live in the same neighborhood, they often organize carpools in the company group for commuting. However, since we are totally dependent on chat history and have many colleagues carpooling to and from work, relying on group chat can easily erase messages and cause information confusion. In this case, it is perfectly possible to develop a small tool to solve these problems.
The initiator of carpooling will share the starting place, destination place and taxi information in the form of cards. By clicking the cards, the participants can choose to participate in carpooling, and see who the carpooling partner is, the information of the order and so on.
The interaction process is as follows:
As you can see, the logic is very simple, we just need to make sure that the four functions of creating a spell, sharing a spell, entering a spell, and exiting a spell are all right.
Requirements and functions have been determined, first of all, according to the introduction of the official website of the small program, register the small program and get the appId, then you can start the development of background logic.
Second, background logic development
Due to the short time and simple functions, we did not consider any complex scenarios such as high concurrency, but only the implementation of the functions. According to the logic of requirements, in fact, the background only needs to maintain two lists, which respectively store all current carpooling orders and all current users who participate in carpooling. Their data structure is as follows:
- All current single column tables
billsList
- A list of all current carpool users
inBillUsers
When a user confirms and shares a pool, it will directly create a pool and add the user to the list of all current users participating in the pool and the list of members of the pool:
Once these two lists are maintained, the next step is the concrete business logic.
For quick development, I use python3+flask framework schemes here. For those of you who don’t know Python, don’t be alarmed. The code is very simple and straightforward, so take a look.
Start by creating a new BillController class:
class BillController:
billsList = []
inBillUsers = []
Copy the code
Next, inside this class you will add the functions to create a quilt, get a quilt, participate in a quilt, exit a quilt, determine whether a user is in a quilt, and upload images.
1. Obtain the spell ordergetBill()
The method receives the spell ID from the client and retrieves the ID to see if a corresponding spell exists. If yes, the corresponding spell is returned. Otherwise, an error is reported to the client.
def getBill(self, ctx): ctxBody = ctx.form billId = ctxBody['billId'] try: return response([item for item in self.billsList if item['billId'] == billId][0]) except IndexError: Return response({'errMsg': 'Spell does not exist! ', 'billsList': self.billsList, }, 1)Copy the code
2. Create a single ordercreateBill()
This method receives user information and spell information from the client and adds them to billsList and inBillUsers, respectively.
def createBill(self, ctx): ctxBody = ctx.form user = { 'userId': ctxBody['userId'], 'billId': ctxBody['billId'], 'name': ctxBody['name'], 'avatar': ctxBody['avatar'] } bill = { 'billId': ctxBody['billId'], 'from': ctxBody['from'], 'to': ctxBody['to'], 'time': ctxBody['time'], 'members': [user]} if ctxBody['userId'] in [item['userId'] for item in self.inBillUsers]: return response({'errMsg': 'user is already in order! ' }, 1) self.billsList.append(bill) self.inBillUsers.append(user) return response({ 'billsList': self.billsList, 'inBillUsers': self.inBillUsers })Copy the code
Once created, the current billsList and inBillUsers are returned to the client.
3. Participate in orderingjoinBill()
Receives the user information and the spell ID from the client and adds the user to the spell and inBillUsers lists.
def joinBill(self, ctx): ctxBody = ctx.form billId = ctxBody['billId'] user = { 'userId': ctxBody['userId'], 'name': ctxBody['name'], 'avatar': ctxBody['avatar'], 'billId': ctxBody['billId'] } if ctxBody['userId'] in [item['userId'] for item in self.inBillUsers]: return response({ 'errMsg': 'The user is already ordering! ' }, 1) theBill = [item for item in self.billsList if item['billId'] == billId] if not theBill: return response({ 'errMsg': }, 1) theBill[0]['members'].appEnd (user) self.inbillUsers.append (user) return response({'billsList': self.billsList, 'inBillUsers': self.inBillUsers })Copy the code
4. Exit the single orderleaveBill()
Receive the user ID and spell ID from the client, then delete the user in the two lists.
This function also has another function. If it is found that the member corresponding to the spell ID is empty, it will consider the spell as invalid and directly delete the spell and the corresponding vehicle information picture.
def leaveBill(self, ctx): ctxBody = ctx.form billId = ctxBody['billId'] userId = ctxBody['userId'] indexOfUser = [i for i, member in enumerate(self.inBillUsers) if member['userId'] == userId][0] indexOfTheBill = [i for i, bill in enumerate(self.billsList) if bill['billId'] == billId][0] indexOfUserInBill = [i for i, Member in enumerate(self.billslist [indexOfTheBill]['members']) if member['userId'] == userId][0 Self.billslist [indexOfTheBill]['members'].pop(indexOfUserInBill) # Delete user self.inbillUsers.pop (indexOfUser) # If len(self.billslist [indexOfTheBill]['members']) == 0: imgPath = './imgs/' + self.billsList[indexOfTheBill]['img'].split('/getImg')[1] if os.path.exists(imgPath): os.remove(imgPath) self.billsList.pop(indexOfTheBill) return response({ 'billsList': self.billsList, 'inBillUsers': self.inBillUsers })Copy the code
5. Determine whether the user is in a single orderinBill()
After receiving the user ID sent by the client, inBillUsers will retrieve the corresponding spell of the user according to the user ID. If it can be retrieved, the spell it belongs to will be returned.
def inBill(self, ctx):
ctxBody = ctx.form
userId = ctxBody['userId']
if ctxBody['userId'] in [item['userId'] for item in self.inBillUsers]:
return response({
'inBill': [item for item in self.inBillUsers if ctxBody['userId'] == item['userId']][0],
'billsList': self.billsList,
'inBillUsers': self.inBillUsers
})
return response({
'inBill': False,
'billsList': self.billsList,
'inBillUsers': self.inBillUsers
})
Copy the code
6. Upload picturesuploadImg()
Receives the spell ID and picture resources from the client, stores the picture first, and then writes the path of the picture into the spell of the corresponding spell ID.
def uploadImg(self, ctx): billId = ctx.form['billId'] file = ctx.files['file'] filename = file.filename file.save(os.path.join('./imgs', IndexOfTheBill = [I for I, bill in enumerate(self.billsList) if bill['billId'] == billId][0] self.billsList[indexOfTheBill]['img'Copy the code