轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 终极进阶 查看主题

微信小程序获得用户信息、session、openid和unionid的thinkphp代码

发布者: wangi4831 | 发布时间: 2018-5-28 18:21| 查看数: 6314| 评论数: 1|帖子模式

作者:未署名,来自原文地址 
我用thinkphp写了个公共类Common供参考

前端代码:

  1. //刷新用户信息
  2. function updateUserInfo(){
  3. wx.login({
  4. success: function(loginRes) {
  5. if (loginRes.code) {
  6. console.log('获取code成功!code:' + loginRes.code);
  7. wx.getUserInfo({
  8. success: function (userinfoRes) {
  9. console.log('获得用户信息成功!userInfo:');
  10. console.log(userinfoRes);
  11. var userInfoStr=JSON.stringify(userinfoRes);
  12. wx.request({
  13. url: ',
  14. data: {
  15. code: loginRes.code,
  16. userInfo:userInfoStr
  17. },
  18. header: {
  19. 'content-type': 'application/x-www-form-urlencoded'
  20. },
  21. method:'POST',
  22. success: function(res) {
  23. //缓存session及userInfo
  24. //var obj = JSON.parse(res.data);
  25. if(res.data.error==0){
  26. console.log('刷新缓存成功!');
  27. wx.setStorage({
  28. key:"session",
  29. data:{
  30. session:res.data.session,
  31. userInfo:userinfoRes.userInfo,
  32. expires:res.data.expires //超时时间戳
  33. },
  34. success:function(){
  35. console.log('写入缓存成功!');
  36. },
  37. fail:function(){
  38. console.log('写入缓存失败!');
  39. }
  40. })
  41. }else{
  42. console.log('刷新缓存失败!');
  43. console.log(res);
  44. }
  45. },
  46. fail:function(res){
  47. console.log('刷新session失败!');
  48. console.log(res)
  49. }
  50. });
  51. },
  52. fail:function(res){
  53. console.log('获取用户信息失败!' + res)
  54. }
  55. });
  56. }else {
  57. console.log('获取用户登录态失败!' + res.errMsg)
  58. }
  59. }
  60. });
  61. }

后端代码:

  1. <?php
  2. /*
  3. * 小程序公共类
  4. */
  5. namespace Weixin\Controller;
  6. use Think\Controller\RestController;
  7. class CommonController extends RestController{
  8. public function getSession(){
  9. $code=$_POST['code'];
  10. if(!$code){
  11. $data['errmsg']='code为空';
  12. $data['error']=1;
  13. $this->response($data,'json');
  14. exit;
  15. }
  16. if(!$_POST['userInfo']){
  17. $data['errmsg']='userInfo为空';
  18. $data['error']=1;
  19. $this->response($data,'json');
  20. exit;
  21. }
  22. $userInfo=json_decode($_POST['userInfo'],true);
  23. $return=$this->updateSession($code,$userInfo);
  24. $this->response($return,'json');
  25. }
  26. //刷新session
  27. private function updateSession($code,$userInfo){
  28. $return=$this->getOAuth($code);
  29. if(!$return['session_key']){
  30. $data['errmsg']='getOAuth函数报错:'.json_encode($return);
  31. $data['error']=1;
  32. return $data;
  33. exit;
  34. }
  35. $unionid_Data=json_decode($this->getUnionID($return['session_key'],$userInfo),true);
  36. if(!$unionid_Data['unionId']){
  37. $data['errmsg']='getUnionID函数报错:'.json_encode($unionid_Data);
  38. $data['error']=1;
  39. return $data;
  40. exit;
  41. }
  42. $wei_user_Model=M('user','wei_');
  43. $exist=$wei_user_Model->where(array('openid'=>$return['openid']))->find();
  44. if($exist){
  45. $save=array(
  46. 'unionid'=>$unionid_Data['unionId'],
  47. 'session_key'=>$return['session_key'],
  48. 'expires_in'=>$return['expires_in'],
  49. 'dateline'=>time()
  50. );
  51. $is_save=$wei_user_Model->where(array('openid'=>$return['openid']))->data($save)->save();
  52. }else{
  53. $add=array(
  54. 'openid'=>$return['openid'],
  55. 'unionid'=>$unionid_Data['unionId'],
  56. 'session_key'=>$return['session_key'],
  57. 'expires_in'=>$return['expires_in'],
  58. 'dateline'=>time()
  59. );
  60. $is_add=$wei_user_Model->data($add)->add();
  61. }
  62. if($is_save||$is_add){
  63. $wei_user_Data=$wei_user_Model->where(array('openid'=>$return['openid']))->find();
  64. $this->setSession($return['session_key']);
  65. $data['session']=session('session');
  66. $data['expires']=time()+$return['expires_in'];
  67. $data['error']=0;
  68. }else{
  69. $data['errmsg']="入库失败!";
  70. $data['error']=1;
  71. }
  72. return $data;
  73. }
  74. //设置session
  75. private function setSession($session){
  76. if(!empty($session)){
  77. session('session',$session);
  78. }
  79. }
  80. //发送code授权
  81. private function getOAuth($code){
  82. $url=';
  83. $data=array(
  84. 'appid'=>'wx888888888',//填写你自己的
  85. 'secret'=>'88888888888888',//填写你自己的
  86. 'js_code'=>$code,
  87. 'grant_type'=>'authorization_code'
  88. );
  89. $return=json_decode(https_request($url, $data),true);
  90. return $return;
  91. }
  92. //得到unionid 提交sessionKey、userinfo、
  93. private function getUnionID($sessionKey,$userInfo){
  94. if(!$userInfo['encryptedData']){
  95. return 'encryptedData为空';
  96. exit;
  97. }
  98. if(!$userInfo['iv']){
  99. return 'iv为空';
  100. exit;
  101. }
  102. vendor('wxBizDataCrypt.wxBizDataCrypt');//引入官方解密sdk
  103. $appid = 'wx88888888888888';//填写你自己的
  104. $encryptedData=$userInfo['encryptedData'];
  105. $iv = $userInfo['iv'];
  106. $pc = new \WXBizDataCrypt($appid, $sessionKey);
  107. $errCode = $pc->decryptData($encryptedData, $iv, $data );
  108. if ($errCode == 0) {
  109. return $data;
  110. } else {
  111. return $errCode;
  112. }
  113. }
  114. }

最新评论

AnDi 发表于 2022-6-20 09:22
源码分享

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表