DefaultController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace api\modules\v1\controllers;
  3. use api\libs\WxBizDecrypt;
  4. use yii\rest\ActiveController;
  5. use common\library\Apireturn;
  6. use Yii;
  7. /**
  8. * Default controller for the `Module` module
  9. */
  10. class DefaultController extends ActiveController
  11. {
  12. public $modelClass = 'common\models';
  13. /**
  14. * 小程序中使用code获取用户信息
  15. */
  16. public function actionWxInfo(){
  17. $code =\Yii::$app->request->post('code');
  18. header('content-type:application/json; charset=UTF-8;');
  19. if(empty($code))
  20. return Apireturn::sent(0,'NO CODE',200);
  21. $data = '';
  22. WxBizDecrypt::$appid = Yii::$app->params['wechatapi']['appidCompany'];
  23. WxBizDecrypt::$appSecret =Yii::$app->params['wechatapi']['secretCompany'];
  24. $result = WxBizDecrypt::getSessionkey($code,$data);
  25. if($result == WxBizDecrypt::$OK){
  26. $dataObj = json_decode($data);
  27. //$res['session_key'] =$dataObj->session_key;
  28. //$res['openid'] = $dataObj->openid;
  29. $sessionKey =$dataObj->session_key;
  30. $encryptedData = Yii::$app->request->post('encryptedData');
  31. $iv = Yii::$app->request->post('iv');
  32. $result = WxBizDecrypt::decryptData($encryptedData,$iv,$sessionKey,$data);
  33. if($result == WxBizDecrypt::$OK){
  34. $dataObj = json_decode($data);
  35. return Apireturn::sent(1,'success',200,$dataObj);
  36. }else{
  37. return Apireturn::sent(0,'解密用户信息出错',200);
  38. }
  39. }else{
  40. return Apireturn::sent(0,'获取用户信息出错',200);
  41. }
  42. }
  43. /**
  44. * 小程序解密用户信息
  45. */
  46. // public function actionWxDecrypt(){
  47. // $res = ['rtcode'=>4000,'rtmsg'=>'Error!'];
  48. // $sessionKey = $this->request->post('sessionKey');
  49. // $encryptedData = $this->request->post('encryptedData');
  50. // $iv = $this->request->post('iv');
  51. // $data = '';
  52. // $result = WxBizDecrypt::decryptData($encryptedData,$iv,$sessionKey,$data);
  53. // if($result == WxBizDecrypt::$OK){
  54. // return $res;
  55. // }else{
  56. // $res['rtmsg'] = 'Error!';
  57. // return json_encode($res);
  58. // }
  59. //
  60. // return json_encode($res);
  61. // }
  62. /**
  63. * 初始化账号
  64. * @return array
  65. */
  66. // public function actionGrantLogin(){
  67. // $openid = $this->request->post('openId');
  68. // $nickName = $this->request->post('nickName');
  69. // $gender = $this->request->post('gender');
  70. // $city = $this->request->post('city');
  71. // $province = $this->request->post('province');
  72. // $country = $this->request->post('country');
  73. // $headimg = $this->request->post('avatarUrl');
  74. // $unionId = $this->request->post('unionId');
  75. //
  76. // if(empty($openid)/* || empty($unionId)*/){
  77. // return $this->handlingErrors(4002,'授权失败');
  78. // }
  79. // $wechatmodel = UserWechat::find()->where('openid = :openid',[':openid'=>$openid])->one();
  80. //
  81. // if(empty($wechatmodel)){
  82. // $wechatmodel = new UserWechat();
  83. // $wechatmodel->openid = $openid;
  84. // $wechatmodel->nickname = $nickName;
  85. // $wechatmodel->gender = $gender;
  86. // $wechatmodel->city = $city;
  87. // $wechatmodel->province = $province;
  88. // $wechatmodel->country = $country;
  89. // $wechatmodel->headimg = $headimg;
  90. // $wechatmodel->unionid = $unionId;
  91. // $wechatmodel->created_at = time();
  92. // $wechatmodel->updated_at = time();
  93. // if ($wechatmodel->validate() && $wechatmodel->save()) {
  94. //
  95. // } else {
  96. // return $this->handlingErrors(4000, '账号生成失败',$wechatmodel->getErrors());
  97. // }
  98. // }else{
  99. //
  100. // //更新资料
  101. // $wechatmodel->nickname = $nickName;
  102. // $wechatmodel->gender = $gender;
  103. // $wechatmodel->city = $city;
  104. // $wechatmodel->province = $province;
  105. // $wechatmodel->country = $country;
  106. // $wechatmodel->headimg = $headimg;
  107. // $wechatmodel->updated_at = time();
  108. // if($wechatmodel->validate()&& $wechatmodel->save()){
  109. //
  110. // }else{
  111. // return $this->handlingErrors(4000, '账号信息更新失败');
  112. // }
  113. // }
  114. // $this->result['userid'] = $wechatmodel->id;
  115. // return $this->result;
  116. // }
  117. }