刚刚接触微信就要做一个表单提交功能,需求是这样的只能在数据库中存在的手机号看到表单。下面通过本文给大家分享使用YII2框架实现微信公众号中表单提交功能,感兴趣的朋友一起看看吧
刚接触微信,要做一个在手机上的表单提交功能。
需求有这些:
只能在数据库中存在的手机号看到表单。
表单可以重复提交。
第一次进入表单需要验证
分享出去的页面,别人进入后也需要验证。
因为每个手机在同一个公众号当中的openid是唯一性的。所以在手机查看这个表单页面的时候,就将这个openid存到数据库中,方便下次提交可以验证。
下面是我的代码。使用的是YII2框架。
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public function actionCallback( $code , $state ){
$model = new tp_tstz_proposal();
$model1 = new tp_tstz_staff();
$appid = '';
$secret = '';
$curl = new curl\Curl();
$wxresponse = $curl ->get('https:
. '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code');
$wxresult = json_decode( $wxresponse );
if (isset( $wxresult ->errcode) && $wxresult ->errcode > 0){
return $this ->render('login');
}
$openid = $wxresult ->openid;
$result = $model1 ::find()->where(['openid'=> $openid ])->one();
if ( count ( $result )>0){
$key =123456;
return $this ->render('view',['model'=> $model ,'key'=> $key ]);
} else {
return $this ->render('tel',['model'=> $model1 ,'openid'=> $openid ]);
}
}`
|
Salin selepas log masuk
view层
很简单的重定向页面
1 | header('Location:https:
|
Salin selepas log masuk
返回的路径就是进入controller的路径。
在表单页面,我先做了一个简单的认证
1 2 3 | if (!isset( $key )){
header('Location:http:
}
|
Salin selepas log masuk
判断是否是从分享的页面来的,如果是从分享的页面来就要重新验证,判断是否在数据库中有此手机的openid。没有就进行手机号码的验证。
大概就是这样了,我第一个简单的微信公众号项目。
Atas ialah kandungan terperinci 使用YII2框架开发实现微信公众号中表单提交功能教程详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!