oauthModel.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. class wxgzh_oauthClassModel extends wxgzhModel
  3. {
  4. public function initWxgzh()
  5. {
  6. $this->settable('wouser');
  7. }
  8. /**
  9. * 调整到获取
  10. */
  11. public function oauthto($dlx='we')
  12. {
  13. $this->readwxset();
  14. if($this->appid==''){
  15. $this->returnerrmsg($dlx, '没有配置公众号');
  16. return false;
  17. }
  18. $state = $this->rock->get('state','bang');
  19. $redurl = ''.getconfig('outurl',URL).'?d='.$dlx.'&a=oauthback&m=login&state='.$state.'';
  20. $redirect_uri = urlencode($redurl);
  21. $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state='.$state.'#wechat_redirect';
  22. $this->rock->location($url);
  23. return true;
  24. }
  25. public function returnerrmsg($dlx='we',$errmsg, $ocan='')
  26. {
  27. $url = '?d='.$dlx.'&m=login&errmsg='.$this->rock->jm->base64encode($errmsg).''.$ocan.'';
  28. $this->rock->location($url);
  29. exit();
  30. }
  31. /**
  32. * 得到openid获取用户信息
  33. */
  34. public function oauthback()
  35. {
  36. $code = $this->rock->get('code');
  37. $state = $this->rock->get('state');
  38. if($code=='')return '无法取得微信授权';
  39. $this->readwxset();
  40. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appid.'&secret='.$this->secret.'&code='.$code.'&grant_type=authorization_code';
  41. $result = c('curl')->getcurl($url);
  42. $openid = '';
  43. $access_token = '';
  44. $errmsg = '无法获取微信用户openid';
  45. if($result != ''){
  46. $arr = json_decode($result);
  47. if(isset($arr->openid))$openid = $arr->openid;
  48. if(isset($arr->access_token))$access_token = $arr->access_token;
  49. if(isset($arr->errmsg))$errmsg = $arr->errmsg;
  50. }
  51. if($openid != ''){
  52. $this->rock->savecookie('wxopenid', $openid);
  53. $nuarr = $this->getone("`openid`='$openid'");
  54. $uoid = 0;
  55. //不要去重复拉起微信用户信息了
  56. if($nuarr){
  57. $uoid = (int)$nuarr['id'];
  58. $this->update(array(
  59. 'optdt' => $this->rock->now,
  60. 'uid' => $this->adminid
  61. ), $uoid); //更新最后时间
  62. return $nuarr;
  63. }
  64. $gurl = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
  65. $result = c('curl')->getcurl($gurl);//拉取用户信息
  66. if($result != ''){
  67. $arr = json_decode($result, true);
  68. if(isset($arr['errcode']) && $arr['errcode']!=0){
  69. $errmsg = $arr['errmsg'];
  70. }else{
  71. $where = 'id='.$uoid.'';
  72. $uarr['openid'] = $arr['openid'];
  73. $uarr['nickname'] = $arr['nickname'];
  74. $uarr['sex'] = $arr['sex'];
  75. $uarr['province'] = $arr['province'];
  76. $uarr['city'] = $arr['city'];
  77. $uarr['country'] = $arr['country'];
  78. $uarr['headimgurl'] = $arr['headimgurl'];
  79. $uarr['optdt'] = $this->rock->now;
  80. $uarr['ip'] = $this->rock->ip;
  81. $uarr['uid'] = $this->adminid;
  82. if($uoid==0){
  83. $uarr['adddt'] = $this->rock->now;
  84. $where = '';
  85. }
  86. $bo = $this->record($uarr, $where);
  87. $errmsg = '';//为空说明对了
  88. if(!$bo)$errmsg = $this->db->error();
  89. }
  90. }else{
  91. $errmsg = '无法获取微信用户信息';
  92. }
  93. }
  94. if($errmsg==''){
  95. return $uarr;
  96. }else{
  97. return $errmsg;
  98. }
  99. }
  100. /**
  101. * 读取当前绑定微信用户信息
  102. */
  103. public function getbdwx($uid)
  104. {
  105. $rs = $this->getone('`uid`='.$uid.'','`openid`,`nickname`,`headimgurl`');
  106. if(!$rs)$rs['nickname'] = '';
  107. return $rs;
  108. }
  109. }
粤ICP备19079148号