Example of how the YII2 framework implements the form submission feature in the WeChat public number

Source: Internet
Author: User
Tags openid
Just contact to do a form submission function, the requirement is that only in the database exists in the mobile phone number to see the form. The following through this article to share the use of the YII2 framework to achieve the public number of the form submission function, interested friends to see it together

Just contact, to do a form submit function on the phone.

Demand has these:

    1. The form can only be seen on a mobile phone number that exists in the database.

    2. Forms can be submitted repeatedly.

    3. The first time you enter a form requires validation

    4. Share out the page, others need to verify after entering.

Because the OpenID of each cell phone in the same public number is unique. So when the phone view this form page, the OpenID is stored in the database, so that the next submission can be verified.

Here's my Code. The YII2 framework is used.

Controller


Get the callback function Public function Actioncallback ($code, $state) {$model = new tp_tstz_proposal ();    $model 1= new Tp_tstz_staff ();    Open Platform website application AppID and secret key secret $appid = ";    $secret = ";    $curl = new Curl\curl ();  Get Access_token $wxresponse = $curl->get (' https://api.weixin.qq.com/sns/oauth2/access_token?appid= '. $appid. ' &secret= '. $secret. ' &code= '. $code.    ' &grant_type=authorization_code ');    $wxresult = Json_decode ($wxresponse); if (Isset ($wxresult->errcode) && $wxresult->errcode > 0) {//share, re-certify return $this->render (' Lo      Gin ');      Error in requesting authorization, print error code//echo Json_encode ($wxresult);    Exit    } $openid = $wxresult->openid;    $result = $model 1::find ()->where ([' OpenID ' = $openid])->one ();      If the OpenID exists, go to the form if (count ($result) >0) {$key = 123456;    return $this->render (' View ', [' model ' = ' $model, ' key ' = ' $key]); }else{return $this->render (' tel ', [' model ' = + $model 1, ' OpenID' = = $openid]); }  }`

View Layer

Very simple redirect page


Header (' Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8ba95fc51672e844&redirect_uri =http%3a%2f%2fjifen.wendu.cn%2fts%2fweb%2findex.php%3fr%3dproposal%2fcallback&response_type=code&scope =snsapi_base&state=123asd#wechat_redirect ');

The path returned is the path into the controller.

On the form page, I first made a simple certification


if (!isset ($key)) {  header (' Location:http://jifen.wendu.cn/ts/web/index.php?r=say/login ');}

Judging whether it is from the shared page, if it is from the shared page to be re-verified to determine whether the database has the phone's OpenID. There is no verification of the mobile phone number.

That's probably it, my first simple public number project.

Summarize

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.