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

Linux commands how to query WePY cloud development in Mini Program

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article is about how to query WePY cloud development in Mini Program with Linux commands. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Why WePY

First of all, why did you choose WePY?

At the beginning of the project selection, I can choose the underlying frameworks are WePy, MPVue, Taro, MinUI, these frameworks are well-done engineering frameworks, which can help Mini Program project long-term maintenance. Among them, Taro is excluded from the very beginning because it uses React that I am not familiar with. After I have seen MPVue, it is more likely to provide Mini Program conversion tools for Web developers than Vue-like tools for Mini Program developers, so it has been ruled out by me. Because MinUI itself only provides the component program and the support of npm and ES6/ES7, other commands still have to continue to use the function of Mini Program, and do not provide more support, the whole ecology is not yet rich, so it excludes MinUI.

In the end, I chose WePY. Before I started, I studied WePY to see what advantages there are in WePY. Overall, I think the advantages of WePY are as follows:

1. Provide a component-based solution similar to Vue: component-based development can improve the maintainability of the project, as your development cycle becomes longer, component-based will greatly affect your development experience.

two。 Provides support for ES6/ES7 syntax: JavaScript's much-maligned callback has been implemented more elegantly in ES6 and ES7.

3. Provides the ecology of Vue: unlike MinUI's alone, WePY has many Vue community ecological products, such as WePY-Redux, RxWX and a series of tools that people are used to use under Vue, which makes the development process smoother and the development experience more consistent.

4. Optimization of native API: many of the APIs provided by Mini Program are callback mode, but not Promise. We often need to repackage it ourselves when using it, which is troublesome. In WePY, WePY officially encapsulates a layer for us, and you can directly use the method encapsulated by WePY to reduce the workload of encapsulation.

5.Vue custom data setting: in WePY, you can assign values using this.xxx=xxx syntax, which is more comfortable and maintainable than the native setData method.

6. Provide a computed method: when developing Mini Program, we inevitably have to format the data, in the traditional Mini Program development, we need to map the data, and then modify it, but after using WePY, we can use the computed calculation property to format and adjust the data, which greatly improves the readability of the code.

These are the advantages of WePY that I value. Next, I'll talk about how to use cloud development in WePY.

Cloud development in WePY

I have written a lot on Mini Program and talked about some Mini Program courses, and people often ask me, can XXX be used in XXX? in this scenario, can cloud development be used in WePY?

The answer is, of course, yes.

Looking at this problem, you should first figure out what cloud development provides.

Cloud development provides data storage, file storage and computing power

It does not conflict with WePY's ability to provide WeChat Mini Programs with component-based development, so WePY and cloud development do not conflict, you can use cloud development in WePY.

Enable cloud development in WePY projects

Since WePY itself does not provide a template for cloud development (although you can now use the wepy init cloudkits/wepy-tcb-demo command to initialize a WePY project that contains cloud development examples), we need to add cloud development to the project ourselves.

Cloud development itself is integrated in wx. In the namespace of the, so you can directly use wx.cloud.xxx to invoke commands for cloud development without configuration. In addition, what is special is that you need to specify the cloud function directory to ensure that the WeChat Mini Programs developer tool can identify the cloud function directory.

It is important to note here that because the cloud development command itself supports Promise and Callback, you can call it directly using wx.cloud instead of using wepy.cloud. WePY has not officially encapsulated cloud development again.

You can create a new directory cloudfunctions in the root directory of the Mini Program project, then add a new configuration project cloudfunctionRoot to project.config.json and set its value to cloudfunctions. In this way, the WeChat Mini Programs developer tool can identify this directory as the directory of the cloud function and add a special directory name to it.

It should be noted here that SCF should be placed outside the source directory src of Mini Program, otherwise it will cause compilation error. I tried to find the configuration project in wepy.config.js about blocking the compilation check directory, but I couldn't find it, so I directly put this directory in the project root directory, which is at the same level as the src of the cloud function and Mini Program source code.

In this way, you have completed the reference to Mini Program Cloud development in WePY.

A pit that has been stepped on in the process of development.

Data should be set first for this assignment

When developing with WePY, we use this.xxx to modify the value of the data, but when I first started the development, when I encountered the first problem, I couldn't set the value of the data with this.xxx, and I couldn't get the corresponding value in the Mini Program interface.

Later, it turns out that if you want WePY to update and manage the data for you, you need to put the data to be passed to the page in the data object in the page instance, so that WePY will help you update and manage the data. Since this was not indicated in the document, I stepped in the pit.

After the subsequent analysis of WePY, understand this approach, because WePY did not use setData, but directly called this.xxx to modify, then WePY needs to know which variables should be sent to the page, otherwise, all the data in this will be transmitted to the page, which will lead to transmission time is too long, easy to let Mini Program exit, then, use data to limit the data method can be understood.

How to deal with the management of pure mobile data?

So far, cloud development does not provide a management method other than the official console of WeChat Mini Programs, which makes it difficult for us to build applications.

In order to provide better service, we decided to modify the model of the product. At first we considered the model in which the user submitted the translation and the team reviewed it, but took into account the lack of management and development costs. We decided to adjust the model to community self-purification. We are fully open to editing capabilities, and any user can submit data. At the same time, it is also possible to practice an application that is completely maintained by the community in China.

However, this kind of data that anyone can submit is likely to be used by others, so we have introduced the content security interface provided by WeChat Mini Programs officially for text security inspection, thus, as far as possible to avoid the impact of some illegal content on Mini Program.

If you use this API, you will know that access_token is required when the API is called, and Wechat's access_token acquisition API has both the address limit for initiating calls (which cannot be called in Mini Program) and the frequency of API requests (too fast may cause the Token cannot be obtained), so we decided to use cloud function to deal with this part of the function.

We use got as a library in the SCF to request the interface provided by Wechat, obtain access_token, and check content security. And, to make sure that access_token requests don't happen too frequently, we add some code to cache token.

Const result = await cache.get (); / / cache is the reference to the corresponding collection const now = (new Date) .valueOf (); const nextTime = now + 5400000 Let accessToken =''if (! result.data.length) {console.log ("enter the first acquisition process") const result = await got (accessTokenUrl) accessToken = JSON.parse (result.body) .access_token await cache.add ({data: {token: accessToken Time: nextTime}}) else {if (result.data [0] .time > now) {console.log ("existing token is valid") accessToken = result.data [0] .token} else {console.log ("existing token is invalid") const tokenResult = await got (accessTokenUrl) accessToken = JSON.parse (tokenResult.body). Access_token await cache.doc (result.data [0]. _ id). Update ({data: {token: accessToken) Time: nextTime}})}}

Through the above code, you can store a token in the cloud database and compare its expiration time. If you find that the token is about to expire, update the token to ensure that the request can be made normally.

Thank you for reading! This is the end of this article on "how to query WePY cloud development in Mini Program with Linux commands". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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

Servers

Wechat

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

12
Report