Detailed tutorial on using the YII2 framework to develop and implement the form submission function in WeChat public accounts

巴扎黑
Release: 2023-03-15 19:54:02
Original
1652 people have browsed it

When I first came into contact with WeChat, I had to create a form submission function. The requirement was that the form could only be seen by the mobile phone number that exists in the database. Through this article, I will share with you how to use the YII2 framework to implement the form submission function in WeChat public accounts. Friends who are interested should take a look.

I am new to WeChat and want to create a form submission function on a mobile phone.

The requirements are as follows:

  1. The form can only be seen on mobile phone numbers that exist in the database.

  2. The form can be submitted repeatedly.

  3. You need to verify when entering the form for the first time

  4. For shared pages, other people also need to verify after entering.

Because the openid of each mobile phone in the same official account is unique. Therefore, when viewing this form page on a mobile phone, the openid is saved in the database so that it can be verified next time it is submitted.

Below is my code. The YII2 framework is used.

Controller


//获得回调函数
 public function actionCallback($code,$state){
    $model = new tp_tstz_proposal();
    $model1= new tp_tstz_staff();
    // 微信开放平台网站应用的appid和秘钥secret
    $appid = '';
    $secret = '';
    $curl = new curl\Curl();
    //获取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){
      //分享出去,重新认证
     return $this->render('login');
      // 向微信请求授权时出错,打印错误码
      // echo json_encode($wxresult);
      // exit;
    }
    $openid=$wxresult->openid;
    $result=$model1::find()->where(['openid'=>$openid])->one();
    //如果OPENID存在就去表单
    if(count($result)>0){
      $key=123456;
      return $this->render('view',['model'=>$model,'key'=>$key]);
    }else{
      return $this->render('tel',['model'=>$model1,'openid'=> $openid]);
    }
  }`
Copy after login

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');
Copy after login

The returned path is the path to the controller.

On the form page, I first did a simple authentication


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

To determine whether it comes from the shared page, if it comes from the shared page It is necessary to re-verify to determine whether the openid of this mobile phone is in the database. If not, verify the mobile number.

That’s it, my first simple WeChat public account project.

The above is the detailed content of Detailed tutorial on using the YII2 framework to develop and implement the form submission function in WeChat public accounts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template