Recently, an article titled “The King of Vaccines” became popular in wechat moments, and the safety issue of vaccines was pushed to the forefront. The team of Tencent Security Anti-fraud Lab launched the “Tencent Peace of Mind Plan” mini-program, which is convenient for users to inquire about vaccine safety information.


How is such a heart-warming little program made?


“Tencent Peace of mind Program” small program is divided into three main services: small program front end, TARS agent service, TARS background service.


1. The front end of the small program is responsible for receiving users’ query requests and feedback results:

A query box is implemented in its page, and the input content in the query box constitutes a GET request sent to the interface URL.


1. // Small program code snippet

2. // Vaccine batch number query input box

3. <template>

4. <input

5. value=”{{input}}”

6. maxlength=”20″

7. bindinput=”handleInput” />

8. </template>

9.

10.// Vaccine input box response parameters

11.<script>

12.import wepy from ‘wepy’

13.export default class extends wepy.page {

14. data = {

15. input: ”

16.}

17. methods = {

18. handleInput(e) {

19. const val = e.detail.value

20. this.input = val

21. this.search()

22.},

23. async search() {

24. // Send the query request

25. / / a request similar to xxxx.url.com/queryVaccine?number=123

26. const resp = await wepy.request({

27. url: ‘xxxx.url.com/queryVaccine’,

28. data: {

29. number: this.input

30.}

31.})

32. const {data} = resp.data

33.}

34.}

35.}

36.</script>


2. The proxy service is responsible for parsing HTTP requests and sending them to the back end as TARS requests:

It USES the TARS – Node. JS, bind a interface URL (such as: xxxx.url.com/queryVaccine), through the Node. JS KOA2 framework provided by the get method parse request, and get the parameters, and then directly transfer request and call back-end TARS service interface.

1. // node.js koa2 gets the parameter code segment

2. const Koa = require(‘koa’)

3. const router = require(‘koa-router’)()

4. const koaBody = require(‘koa-bodyparser’)

5. // TARS tool

6. const Tars = require(‘tars_client’)

7. // Import the TARS file

8. Const vaccineProxy = require(‘ tars/VaccineObj’).app

9. const app = new Koa()

10.

11.// Listen for get requests from this path

12.// Interface for querying vaccine information by batch

13.router.get(‘/queryVaccine’, async ctx => {

14. // Get the variables in the get argument

15. / / xxxx.url.com/queryVaccine?nunber=123? After parameters can obtain

16. let {number} = ctx.query

17.

18. try {

19. const tars = Tars.stringToProxy(

20. vaccineProxy.VaccineObj, ‘App.Vaccine.VaccineObj’

21.)

22. // Call the tars interface and pass the parameter

23. let result = await tars.QueryVaccine(number)

24. // Get the result information in the result body

25. let info = result.response.arguments.vVaccine

26.

27. // Returns information to the front-end

28. ctx.body = info

29. } catch(e) {

30. console.log(e)

31. ctx.body = ‘error’

32.}

33.})

34.

35.// Load the route

36.app.use(router.routes())

37.

38.// Start the service

39.app.host = ‘localhost’

40.app.port = 80

41.const server = app.listen(app.port, app.host, () => {

42. console.log(‘Koa server listening on %s:%d’, server.address().address, server.address().port)

43.});

3. Query service is responsible for providing query results:

It provides a function to query the local memory. Firstly, the vaccine information data is regularly retrieved from the DB and written into the memory. After receiving the interface request, the result is queried in the memory and then returned to the calling service.

1. // Define the structure in the tars file

2. struct Vaccine

3. {

4. 0 require string vaccine ; // Name of vaccine

5. 1 require string number ; / / batch

6. 2 require string company ; / / the company

7. 3 require string state ; / / state

8.};

9.

10.// Create a vector of vaccine information and load the relevant content from the database into the structure

11.TC_Mysql::MysqlData oResults;

12.try

13. {

14. oResults = mysql.queryRecord(“select vaccine,number,company,state from vaccine;” );

15.}

16.catch (exception & e)

17. {

18. LOG->error() << “VaccineData error query: ” << e.what() << endl;

19. return -1;

20.}

21.const size_t oResultsCnt = oResults.size();

22.

23.vVaccine.clear();

24.for (size_t i=0; i<oResultsCnt; i++)

25. {

26. App::Vaccine stInfo;

27. stInfo.vaccine=oResults[i][“vaccine”];

28. stInfo.number=ToUpperCaseString(oResults[i][“number”]); // Batch numbers are uniformly normalized in uppercase

29. stInfo.company=oResults[i][“company”];

30. stInfo.state=oResults[i][“state”];

31.

32. vVaccine.push_back(stInfo);

33.}

34.

35.// Define the queryVaccine interface in VaccineObjImp. CPP

36.tars::Int32 VaccineObjImp::QueryVaccine(const std::string & strIn,vector
<:vaccine>
&vVaccine,tars::TarsCurrentPtr current)

37. {

38. CAttrReport::getInstance()->AttrReportCount(“QueryVaccine”);

39. LOG->debug()<<“QueryVaccine”<<endl;

40. // Call the actual request function

41. int ret=QueryVaccine(strIn,vVaccine);

42. return ret;

43.}

44.

45.// Definition of the actual request function QureyVaccine function

46.int QueryVaccine(const std::string & strIn,vector<App::Vaccine> & vMatchVaccine)

47. {

48. if(strIn.length()<3)

49. {

50. return -1;

51.}

52.

53. int size_cnt=0;

54.

55. const size_t cnt=VaccineHandle::getInstance()->vVaccine.size();

56. for(size_t i=0; i

57. {

58. if(size_cnt > SConfig::getInstance()->maxMatch)

59. break;

60.

61. The if (isNumberMatch (strIn, VaccineInfoHandle: : getInstance () – > vVaccineInfo [I] number)) / / fuzzy matching batch number

62. {

63. // Push the query contents into the query result vector

64. vNumberVaccine.push_back(VaccineHandle::getInstance()->vVaccine[i]);

65. size_cnt++;

66.}

67.}

68. return 0;

69.}


In this way, a small procedure with the function of querying background data is completed.

The convenient development and deployment of small programs, different from the version management of APP, can quickly update the page content without users’ awareness. With multi-language support and high availability, TARS can efficiently fulfill its requirements through multiple solutions and means without putting too much effort into the disaster recovery and fault tolerance processing of the service.


Purely from the perspective of code development efficiency, the combination of small program +TARS can realize a small program similar to the function of “Tencent Peace of mind Plan” efficiently and conveniently in one day under the condition of only one front-end + one back-end developer or a full stack developer.


If you have a different idea about the code, please leave a comment. TARS open source address: https://github.com/Tencent/Tars