In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
How to use cloud function SCF to easily implement a hot information Mini Program, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
The first step is environment configuration.
Open WeChat Mini Programs Development IDE and create a Mini Program project. AppID needs to register one on Mini Program's official website, and then select Mini Program-Cloud Development for the backend service.
Note that in previous versions of IDE, there will be an option for Tencent Cloud in the blue box. In fact, they all use Tencent Cloud services, so just choose Mini Program-Cloud Development.
Click New and an interface will appear.
The scaffolding of Wechat developer tools has created some template code for us. Today, Pig's foot is our cloudfunctions part, that is, how to use Tencent Cloud to provide data services for the news Mini Program we are about to write.
Before development, we found that the console reported an error indicating that we did not activate the cloud service. We found that the button on Cloud Development in the top toolbar of Wechat developer tool is grayed out. Click in to prompt us to activate the service, indicating that we have not activated the cloud development service. Click on it to create a new one.
After the configuration, you may be related to the cost, do not worry, the default configuration is completely free, if you do not have a large number of users, basically enough for your daily needs, for individual developers, quite friendly.
Step 2: cloud function development and deployment
After the cloud service is activated, you can deploy the cloud function provided by the scaffolding. You can see that the cloudfunctions folder indicates that the environment is not selected. We right-click to select the cloud development environment we just launched. Then expand the directory, aim at the login directory, right-click, and select
Then, close IDE, restart IDE, click the first button to get openid, and you can see that the acquisition of openid is successful.
This means that the link between activation and deployment of our cloud development environment has been opened, and the next thing to do is to write our own cloud functions. We are going to do a Mini Program for news consultation, so, generally speaking, find a news website that you often read and think the content is of good quality to see if there is any interface provided by others. Go in two steps:
If an API is provided, we can call the API directly in the cloud function, get the data, and feed it to the Mini Program frontend, which is the most convenient.
Usually there is no interface, what should I do at this time? One idea is to use the cloud function to crawl the content of the news website and store it on the cloud development db, and then the Mini Program end can read the content in the cloud development db, or directly generate a json through the crawler and return it to the mini program, without storing the db process. Its disadvantage is that there is no cache data, every time you have to crawl, users may wait a long time to see the news, the experience is not good. At present, db services are provided in the cloud development suite, so, now that it is provided, of course, it will be used directly to enhance the user experience.
Create a new cloud function
Click the new cloud function directly on this cloudfunctions and that folder. After success, you will see some scaffolding generated frame code in the directory.
/ / Cloud function entry file const cloud = require ('wx-server-sdk') cloud.init () / / Cloud function entry exports.main = async (event, context) = > {const wxContext = cloud.getWXContext () return {event, data:data}}
Most of them see this, where wxContext is the context of Mini Program. Here you can get some constant information such as the AppID of Mini Program.
Then, what is the parameter event? event is actually the parameter passed to this side by Mini Program:
GetNews:function () {wx.cloud.callFunction ({name: 'news', data: {hot_type:'views',//hot_type acceptable parameter views (views) | likes (likes) | comments (number of comments) category:'Article',//category acceptable parameter Article | GanHuo | Girl count:20} Success: res = > {console.log ('[Cloud function] [news] call') console.log (res) this.setData ({news:res.result.data})}, fail: err = > {console.error ('[Cloud function] [news] call failed', err)}})})}
For example, I call the cloud function news on Mini Program, passing the data as
{hot_type:'views',//hot_type acceptable parameter views (views) | likes (likes) | comments (number of comments) category:'Article',//category acceptable parameter Article | GanHuo | Girl count:20}
So this event is actually this object.
Well, now that we know some of the templates and parameters that scaffolding created for us, we can write business logic.
Get news logic
You need to send a network request to get the news. Here we use axios directly, but WeChat Mini Programs does not provide it here, so we need to execute it in the directory of the cloud function.
Npm i axios
Note that npm I must be executed in the directory generated by you by cloud function.
Then, you can write the Internet request happily.
/ / Cloud function entry file const cloud = require ('wx-server-sdk') const axios = require (' axios') cloud.init () async function getNews (category,hot_type Count) {console.log ('start getNews') let data = {} try {const url = `https://gank.io/api/v2/hot/${hot_type}/category/${category}/count/${count}` console.log (url) var res = await axios.get (url) data = res.data.data} catch (err) {console.log (err)} return data} / / Cloud function entry function exports.main = async (event Context) = > {const wxContext = cloud.getWXContext () const data = await getNews (event.category,event.hot_type,event.count) console.log (data) return {event, data:data}}
As mentioned above, I added a getNews method to the cloud function, and the three parameters accepted are passed by Mini Program. Then, after getting the result of the request, it is returned to the applet side. It is important to note that the returned body looks like this.
The result we actually get from the cloud function is wrapped in the inner result.
At this point, the hand-in-hand guide you to use cloud functions for Mini Program development is over. I believe this simple demo will open the door to how to use cloud functions to develop more interesting Mini Program.
For example, you can make a Mini Game, use the cloud function as the backend to relay the request, and let the cloud function achieve permission verification to protect your own server.
For example, you can make a parent-child photo album and use cloud functions to store pictures in Tencent Cloud for storage.
Or, you can be a chat room.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.