剛接觸微信,要做一個在手機上的表單提交功能。
需求有這些:
因為每個手機在同一個公眾號當中的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]);    }  }`view層
很簡單的重定向頁面
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');返回的路徑就是進入controller的路徑。
在表單頁面,我先做了一個簡單的認證
if(!isset($key)){  header('Location:http://jifen.wendu.cn/ts/web/index.php?r=say/login');}判斷是否是從分享的頁面來的,如果是從分享的頁面來就要重新驗證,判斷是否在數據庫中有此手機的openid。沒有就進行手機號碼的驗證。
大概就是這樣了,我第一個簡單的微信公眾號項目。
總結
以上所述是小編給大家介紹的使用YII2框架實現微信公眾號中表單提交功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答
圖片精選