wugg 1 year ago
parent
commit
a3cc908e6a

+ 77 - 0
app/admin/controller/WeChat.php

@@ -0,0 +1,77 @@
+<?php
+
+
+namespace app\admin\controller;
+
+use think\App;
+use app\admin\BaseController as Base;
+use think\facade\Validate;
+use think\facade\Db;
+class WeChat extends Base {
+	public $wechat;
+	public function __construct(App $App) {
+		parent::__construct($App);
+		$wechat_conf =env("wechat");
+		$this->wechat=new \think\Wechat($wechat_conf);
+	}
+//		获取用户信息
+//	    "userid": "zhangsan",
+//	 *    "name": "李四",
+//	 *    "department": [1, 2],
+//	 *    "position": "后台工程师",
+//	 *    "mobile": "15913215421",
+//	 *    "gender": 1,     //性别。gender=0表示男,=1表示女
+//	 *    "tel": "62394",
+//	 *    "email": "zhangsan@gzdev.com",
+//	 *    "weixinid": "lisifordev",        //微信号
+//	 *    "avatar": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLA3W..../0",   //头像url。注:如果要获取小图将url最后的"/0"改成"/64"即可
+//	 *    "status": 1      //关注状态: 1=已关注,2=已冻结,4=未关注
+//	 *    "extattr": {"attrs":[{"name":"爱好","value":"旅游"},{"name":"卡号","value":"1234567234"}]}
+	public function getinfo(){
+		$code =$this->request->only(["code"=>""],"post","trim");
+		$valid =Validate::rule(["code|微信授权Code"=>"require|max:255|min:5"]);
+		if($valid->check($code)==false) return error_show(1004,$valid->getError());
+
+		$uid =$this->wechat->getUserId($code['code']);
+		if($uid==false){
+			return error_show(1004,"未获取到用户数据");
+		}
+		if(!isset($uid['UserId'])|| $uid['UserId'])	return error_show(1004,"未获取到用户数据");
+		$Dbuser =Db::name("wechat_user")->where(["userid"=>$uid['UserId']])->findOrEmpty();
+		if(empty($Dbuser)){
+			$useinfo = $this->wechat->getUserInfo($uid['UserId']);
+			if($useinfo==false)return error_show(1004,"未获取到用户数据");
+			$Dbuser = [
+				"userid"=>$useinfo['userid'],
+				"position"=>$useinfo['userid'],
+				"mobile"=>$useinfo['userid'],
+				"gender"=>$useinfo['userid'],
+				"tel"=>$useinfo['userid'],
+				"email"=>$useinfo['userid'],
+				"weixinid"=>$useinfo['userid'],
+				"avatar"=>$useinfo['userid'],
+				"status"=>$useinfo['userid'],
+				"extattr"=>$useinfo['userid'],
+				"addtime"=>date("Y-m-d H:i:s"),
+				"updatetime"=>date("Y-m-d H:i:s")
+				];
+			$Dbuser['id']=Db::name("wechat_user")->insertGetId($Dbuser);
+		}else
+			{
+				$Dbuser["updatetime"]=date("Y-m-d H:i:s");
+				Db::name("wechat_user")->save($Dbuser);
+			}
+		return app_show(0,"获取成功",$Dbuser);
+	}
+	//网页端调用jssdk配置config
+	public function getConfig(){
+		$url =$this->request->only(["url"=>""],"post","trim");
+		$valid =Validate::rule(["code|微信授权Code"=>"require|max:255|min:5"]);
+		if($valid->check($url)==false) return error_show(1004,$valid->getError());
+		$baseconfig =$this->wechat->getJsSign($url['url']);
+		if($baseconfig==false){
+			return error_show(1004,"为获取到用户数据");
+		}
+		return app_show(0,"获取成功",$baseconfig);
+	}
+}

+ 1 - 0
app/admin/controller/index.php

@@ -16,6 +16,7 @@ class index
      */
     public function index()
     {
+
     }
 
     /**

+ 4 - 1
app/admin/middleware/checkToken.php

@@ -18,7 +18,10 @@ class checkToken
 		"admin/departstat",
 		"admin/departEveryDay",
 		"admin/companyEveryMonth",
-		"admin/totalstat"];
+		"admin/wechat_getinfo",
+		"admin/wechat_getconfig",
+		"admin/totalstat"
+		];
     /**
      * 处理请求
      *

+ 3 - 0
app/admin/route/app.php

@@ -255,3 +255,6 @@ route::rule("companytipsadd","admin/DepartTips/create");//网络部录单分类
 route::rule("departtipslist","admin/DepartTips/departlist");//网络部录单分类
 route::rule("departtipsadd","admin/DepartTips/departcreate");//网络部录单分类
 
+route::rule("wechat_getinfo","admin/WeChat/getinfo");//微信授权获取用户信息
+route::rule("wechat_getconfig","admin/WeChat/getConfig");//微信获取页面jssdk配置
+

+ 2 - 1
composer.json

@@ -24,7 +24,8 @@
         "topthink/framework": "^6.0.0",
         "topthink/think-orm": "^2.0",
         "topthink/think-multi-app": "^1.0",
-        "phpoffice/phpexcel": "^1.8"
+        "phpoffice/phpexcel": "^1.8",
+        "dodgepudding/wechat-php-sdk": "^1.1"
     },
     "require-dev": {
         "symfony/var-dumper": "^4.2",

+ 58 - 1
composer.lock

@@ -4,8 +4,65 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "3aa191934404180c982ae2eea0633938",
+    "content-hash": "ca8f414803112b39761003d8a64f421e",
     "packages": [
+        {
+            "name": "dodgepudding/wechat-php-sdk",
+            "version": "1.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/dodgepudding/wechat-php-sdk.git",
+                "reference": "3a9b9601a07797497e8c428cfd20a29d37a66a35"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/dodgepudding/wechat-php-sdk/zipball/3a9b9601a07797497e8c428cfd20a29d37a66a35",
+                "reference": "3a9b9601a07797497e8c428cfd20a29d37a66a35",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": ">=5.1.0"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "dodgepudding\\wechat\\sdk\\": ""
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "LGPL"
+            ],
+            "authors": [
+                {
+                    "name": "dodgepudding",
+                    "email": "dodgepudding@gmail.com"
+                },
+                {
+                    "name": "binsee",
+                    "email": "binsee@163.com"
+                }
+            ],
+            "description": "the wechat api library without framework dependency",
+            "keywords": [
+                "sample",
+                "thinkphp",
+                "wechat",
+                "weixin"
+            ],
+            "support": {
+                "issues": "https://github.com/dodgepudding/wechat-php-sdk/issues",
+                "source": "https://github.com/dodgepudding/wechat-php-sdk",
+                "wiki": "https://github.com/dodgepudding/wechat-php-sdk/wiki"
+            },
+            "time": "2015-03-15T17:07:41+00:00"
+        },
         {
             "name": "phpoffice/phpexcel",
             "version": "1.8.2",