123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace api\modules\v1\controllers;
- use api\libs\WxBizDecrypt;
- use yii\rest\ActiveController;
- use common\library\Apireturn;
- use Yii;
- /**
- * Default controller for the `Module` module
- */
- class DefaultController extends ActiveController
- {
- public $modelClass = 'common\models';
- /**
- * 小程序中使用code获取用户信息
- */
- public function actionWxInfo(){
- $code =\Yii::$app->request->post('code');
- header('content-type:application/json; charset=UTF-8;');
- if(empty($code))
- return Apireturn::sent(0,'NO CODE',200);
- $data = '';
- WxBizDecrypt::$appid = Yii::$app->params['wechatapi']['appidCompany'];
- WxBizDecrypt::$appSecret =Yii::$app->params['wechatapi']['secretCompany'];
- $result = WxBizDecrypt::getSessionkey($code,$data);
- if($result == WxBizDecrypt::$OK){
- $dataObj = json_decode($data);
- //$res['session_key'] =$dataObj->session_key;
- //$res['openid'] = $dataObj->openid;
- $sessionKey =$dataObj->session_key;
- $encryptedData = Yii::$app->request->post('encryptedData');
- $iv = Yii::$app->request->post('iv');
- $result = WxBizDecrypt::decryptData($encryptedData,$iv,$sessionKey,$data);
- if($result == WxBizDecrypt::$OK){
- $dataObj = json_decode($data);
- return Apireturn::sent(1,'success',200,$dataObj);
- }else{
- return Apireturn::sent(0,'解密用户信息出错',200);
- }
- }else{
- return Apireturn::sent(0,'获取用户信息出错',200);
- }
- }
- /**
- * 小程序解密用户信息
- */
- // public function actionWxDecrypt(){
- // $res = ['rtcode'=>4000,'rtmsg'=>'Error!'];
- // $sessionKey = $this->request->post('sessionKey');
- // $encryptedData = $this->request->post('encryptedData');
- // $iv = $this->request->post('iv');
- // $data = '';
- // $result = WxBizDecrypt::decryptData($encryptedData,$iv,$sessionKey,$data);
- // if($result == WxBizDecrypt::$OK){
- // return $res;
- // }else{
- // $res['rtmsg'] = 'Error!';
- // return json_encode($res);
- // }
- //
- // return json_encode($res);
- // }
- /**
- * 初始化账号
- * @return array
- */
- // public function actionGrantLogin(){
- // $openid = $this->request->post('openId');
- // $nickName = $this->request->post('nickName');
- // $gender = $this->request->post('gender');
- // $city = $this->request->post('city');
- // $province = $this->request->post('province');
- // $country = $this->request->post('country');
- // $headimg = $this->request->post('avatarUrl');
- // $unionId = $this->request->post('unionId');
- //
- // if(empty($openid)/* || empty($unionId)*/){
- // return $this->handlingErrors(4002,'授权失败');
- // }
- // $wechatmodel = UserWechat::find()->where('openid = :openid',[':openid'=>$openid])->one();
- //
- // if(empty($wechatmodel)){
- // $wechatmodel = new UserWechat();
- // $wechatmodel->openid = $openid;
- // $wechatmodel->nickname = $nickName;
- // $wechatmodel->gender = $gender;
- // $wechatmodel->city = $city;
- // $wechatmodel->province = $province;
- // $wechatmodel->country = $country;
- // $wechatmodel->headimg = $headimg;
- // $wechatmodel->unionid = $unionId;
- // $wechatmodel->created_at = time();
- // $wechatmodel->updated_at = time();
- // if ($wechatmodel->validate() && $wechatmodel->save()) {
- //
- // } else {
- // return $this->handlingErrors(4000, '账号生成失败',$wechatmodel->getErrors());
- // }
- // }else{
- //
- // //更新资料
- // $wechatmodel->nickname = $nickName;
- // $wechatmodel->gender = $gender;
- // $wechatmodel->city = $city;
- // $wechatmodel->province = $province;
- // $wechatmodel->country = $country;
- // $wechatmodel->headimg = $headimg;
- // $wechatmodel->updated_at = time();
- // if($wechatmodel->validate()&& $wechatmodel->save()){
- //
- // }else{
- // return $this->handlingErrors(4000, '账号信息更新失败');
- // }
- // }
- // $this->result['userid'] = $wechatmodel->id;
- // return $this->result;
- // }
- }
|