Micro-trust Public Platform Development Tutorial (vi) To obtain the example _javascript of the character two-dimension code

Source: Internet
Author: User
Tags string format ticket

First, Function introduction

In the promotion, we can tell each other, our micro-letter public account is what, customers can go to search, and then attention. Two-dimensional code to provide us with a great convenience, as long as a simple scan, you can pay attention to.

If you have already noticed, jump into the dialog screen immediately. In our promotion, is no longer a simple text, can be a character of the two-dimensional code, presumably will be very vivid.

Micro-letters provide a good support for two-dimensional codes, and can generate two-dimensional codes for different scenarios as needed. Here we'll describe how to get and use two-dimensional code.

Note: Limited service number, and the micro-letter certification, the cost of 300

Second, the related interface

In order to meet the needs of User channel extension analysis, the public platform provides an interface to generate two dimensional code with parameters. Using this interface, you can get a number of two-dimensional codes with different scene values, and the public number can receive the event push after the user scans.

There are currently 2 types of two-dimensional code, which is a temporary two-dimensional code and a permanent two-dimensional code, the former has an expiration time, the maximum is 1800 seconds, but can generate a large number, the latter has no expiration time, a small number (current parameters only support 1--1000). Two two-dimensional codes are suitable for account binding, user source statistics and other scenarios.

When a user scans a two-D code with scene value, the following two events may be pushed:

1. If the user is not yet concerned about the public number, the user can focus on the public number, after which the micro-letter will be brought to the developer with the scene value of attention events.

2. If the user has been concerned about the public number, the user will automatically enter the session after the scan, the micro-letter will also be with the scene value scanning events to the developer.

The process of getting a two-dimensional code with parameters involves two steps, first creating a two-dimensional code ticket, and then swapping the ticket to the specified URL for a two-dimensional code.

Create a two-dimensional code ticket

Each time you create a two-dimensional code ticket need to provide a developer's own set of parameters (SCENE_ID), respectively, to introduce the temporary two-dimensional code and the permanent two-dimensional code to create two-dimensional code ticket process.

Temporary two-dimensional code Request description

HTTP request mode: Post
url:https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=token
post data format: JSON
Post Data example: {"Expire_seconds": 1800, "Action_name": "Qr_scene", "Action_info": {"SCENE": {"scene_id": 123}}}

Permanent two-dimensional code Request description

HTTP request mode: Post
url:https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=token
post data format: JSON
Post Data example: {"Action_name": "Qr_limit_scene", "Action_info": {"SCENE": {"scene_id": 123}}}

Parameter description

Parameters Description
Expire_seconds The two-dimensional code valid time, in seconds. Max no more than 1800.
Action_name Two-dimensional code type, Qr_scene for temporary, Qr_limit_scene for permanent
Action_info Two-dimensional code details
scene_id Scene value ID, temporary two-dimensional code is 32-bit integral type, permanent two-dimensional code maximum value is 1000

Return description

Correct JSON returns the result:

{"Ticket": "gqg28doaaaaaaaaaasxodhrwoi8vd2vpeglulnfxlmnvbs9xl0fuwc1dnmzuvehvmvp4ndnmrnnraaieeslvuqmecacaaa==", " Expire_seconds ": 1800}

Parameters Description
Ticket Obtains the two-dimensional code ticket, by virtue of this ticket can in the valid time exchange for the two-dimensional code.
Expire_seconds The valid time of the two-dimensional code, in seconds. Max no more than 1800.

Bad JSON return Example:

{"Errcode": 40013, "errmsg": "Invalid AppID"}

Global Return Code description

To debug the interface using the Web Debugging tool

In exchange for two-dimensional code through ticket

After acquiring two-dimensional code ticket, the developer can exchange ticket for two-dimensional code picture. Please note that this interface can be invoked without the login state.

Request Description
HTTP GET request (please use HTTPS protocol) Https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET

Return description

Ticket in the correct case, the HTTP return code is 200 and is a picture that can be displayed or downloaded directly.
The HTTP headers (examples) are as follows:

Accept-ranges:bytes
cache-control:max-age=604800
connection:keep-alive
content-length:28026
Content-type:image/jpg
date:wed, Oct 2013 06:37:10 GMT expires:wed
, Oct 2013 14:37:10 +0800
Server: nginx/1.4.1

Error case (such as ticket illegal) returns an HTTP error code of 404.

Third, the concrete realization

Still based on the previous robot case to add functionality, directly look at the code.

<summary>///Two-dimensional code manager///</summary> public class Dimensionalcodemanager {///<summary>///Pro When two-dimensional code address///</summary>///uses String.Format, the report: string format error because there are {//private const string temp_url = "{\" expire_seconds\
 ": 1800, \" action_name\ ": \" qr_scene\ ", \" action_info\ ": {\" scene\ ": {\" scene_id\ ": {0}}}}"; <summary>///Workaround, replace one {of the original string with two {///</summary> private const string Temp_json_data = ' {{\ ' expire_s
 Econds\ ": 1800, \" action_name\ ": \" qr_scene\ ", \" Action_info\ ": {{\" scene\ ": {{\" scene_id\ ": {0}}}}"; <summary>///Permanent two-dimensional code address///</summary> Private Const string Permanent_url = "{{\" action_name\ ": \" Qr_limi
 T_scene\ ", \" Action_info\ ": {{\" scene\ ": {{\ scene_id\": {0}}}}} "; <summary>///get TICKET URL///</summary> Private Const string Get_ticket_url = "Https://api.weixin.qq
 . com/cgi-bin/qrcode/create?access_token={0} "; <summary>///get two-dimensional code URL///</summary> Private Const STRing Get_code_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}"; <summary>///ticket///</summary>///<param name= "Sceneid" > Scene based on scene ID id</param> ;p Aram Name= "Istemp" > is a temporary two-dimensional code </param>///<returns></returns> private static string GetTicket (
  int Sceneid, bool istemp) {string result = NULL; String data = String.
  Empty; if (istemp) {data = string.
  Format (Temp_json_data, sceneid.tostring ()); else {if (Sceneid > 0 && sceneid <= 1000) {data = string.
  Format (Permanent_url, Sceneid);
  else {//scene_id is not valid return null; } String Ticketjson = Httputility.getdata (string.

  Format (Get_ticket_url,context.accesstoken));
  XDocument doc = Xmlutility.parsejson (Ticketjson, "root"); XElement root = Doc.
  Root; if (root!= null) {XElement ticket = root.
  Element ("Ticket"); if (ticket!= null) {result = ticket.
  Value;
 } return result; }///<sumMary>///Create temporary two-dimensional code///</summary>///<param name= "Sceneid" > Scene id,int type </param>///<returns>
  </returns> public static string generatetemp (int sceneid) {String ticket = GetTicket (sceneid,true);
  if (ticket = = null) {return null; Return Httputility.getdata (String.
 Format (Get_code_url, ticket)); ///<summary>///Create temporary two-dimensional code///</summary>///<param name= "Sceneid" > Scene id,int type </param>///& lt;returns></returns> public static string generatepermanent (int sceneid) {String ticket = GetTicket (Sceneid
  , false);
  if (ticket = = null) {return null; Return Httputility.getdata (String.
 Format (Get_code_url, ticket));

 }
 }

  above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud-dwelling community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.