malike 发表于 2018-2-11 11:49:45


            刚接触微信,要做一个在手机上的表单提交功能。
需求有这些:

[*]只能在数据库中存在的手机号看到表单。
[*]表单可以重复提交。
[*]第一次进入表单需要验证
[*]分享出去的页面,别人进入后也需要验证。

因为每个手机在同一个公众号当中的openid是唯一性的。所以在手机查看这个表单页面的时候,就将这个openid存到数据库中,方便下次提交可以验证。
下面是我的代码。使用的是YII2框架。
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]);
    }
}`
页: [1]
查看完整版本: 使用YII2框架实现微信公众号中表单提交功能