Browse Source

修改bug

chenbo 4 years ago
parent
commit
19dc4b626e
2 changed files with 29 additions and 4 deletions
  1. 16 4
      lib/common/weixin_public.class.php
  2. 13 0
      lib/reader.class.php

+ 16 - 4
lib/common/weixin_public.class.php

@@ -53,17 +53,29 @@ class weixin_public
 
     public function getAllInfo($code)
     {
-        $info = self::getAcessTokenAndOpenId($code);
+        $reader = array();
 
+        $info = self::getAcessTokenAndOpenId($code);
         if (empty($info['access_token']) || empty($info['openid'])) {
-            return $info;
+            return $reader;
         }
 
         $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$info['access_token']."&openid=".$info['openid']."&lang=zh_CN";
         $jsonInfo = file_get_contents($url);
         //$jsonInfo = iconv('ISO-8859-1', 'UTF-8', $jsonInfo);
-        $ret = json_decode($jsonInfo, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
-        return $ret;
+        $rs = json_decode($jsonInfo, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
+
+        $attr['country']    = $rs['country'];
+        $attr['province']   = $rs['province'];
+        $attr['city']       = $rs['city'];
+        $attr['headimgurl'] = $rs['headimgurl'];
+        $attr['openid']     = $rs['openid'];
+        $attr['nickname']   = $rs['nickname'];
+        $attr['sex']        = $rs['sex'];
+        $reader = Reader::getInfoByOpenId($info['openid']);
+        Reader::addOrUpdate($reader['id'], $attr);
+
+        return Reader::getInfoByOpenId($info['openid']);
     }
 
 

+ 13 - 0
lib/reader.class.php

@@ -38,6 +38,19 @@ class Reader{
         return $Table_reader->update($id, $attrs);
     }
 
+    static public function addOrUpdate($id, $attrs)
+    {
+        $Table_reader = new Table_reader();
+
+        if(empty($id)) {
+            $id = $Table_reader->add($attrs);
+        } else {
+            $Table_reader->update($id, $attrs);
+        }
+
+        return $id;
+    }
+
     static public function addCountByOpenId($openId)
     {
         $Table_reader = new Table_reader();