相關關鍵詞
關于我們
最新文章
使用YII2框架實現(xiàn)微信公眾號中表單提交功能
剛接觸微信,要做一個在手機上的表單提交功能。
需求有這些:
- 只能在數(shù)據(jù)庫中存在的手機號看到表單。
- 表單可以重復提交。
- 第一次進入表單需要驗證
- 分享出去的頁面,別人進入后也需要驗證。
因為每個手機在同一個公眾號當中的openid是唯一性的。所以在手機查看這個表單頁面的時候,就將這個openid存到數(shù)據(jù)庫中,方便下次提交可以驗證。
下面是我的代碼。使用的是YII2框架。
Controller
//獲得回調函數(shù) 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]); } }`