|
@@ -7,51 +7,40 @@ use think\App;
|
|
|
use app\admin\BaseController as Base;
|
|
|
use think\facade\Validate;
|
|
|
use think\facade\Db;
|
|
|
+use think\Wechat as wchat;
|
|
|
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);
|
|
|
+ $wechat_conf=["appid"=>env("WECHAT.APPID"),"appsecret"=>env("WECHAT.APPSECRET")];
|
|
|
+ $this->wechat=new wchat($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"}]}
|
|
|
+// 获取用户信息{subscribe,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time,[unionid]}
|
|
|
+
|
|
|
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']);
|
|
|
+ $uid =$this->wechat->getAccessTokenByCode($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(!isset($uid['openid'])|| $uid['openid']=='') return error_show(1004,"未获取到用户数据");
|
|
|
+ $Dbuser =Db::name("wechat_user")->where(["openid"=>$uid['openid']])->findOrEmpty();
|
|
|
if(empty($Dbuser)){
|
|
|
- $useinfo = $this->wechat->getUserInfo($uid['UserId']);
|
|
|
+ $useinfo = $this->wechat->getUserInfo($uid['openid']);
|
|
|
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'],
|
|
|
+ "openid"=>$useinfo['openid'],
|
|
|
+ "nickname"=>$useinfo['nickname'],
|
|
|
+ "mobile"=>'',
|
|
|
+ "gender"=>$useinfo['sex'],
|
|
|
+ "avatar"=>$useinfo['headimgurl'],
|
|
|
+ "subscribe_time"=>date("Y-m-d H:i:s",$useinfo['subscribe_time']),
|
|
|
+ "addr"=>"{$useinfo['province']}/{$useinfo['city']}/{$useinfo['country']}",
|
|
|
+ "status"=>$useinfo['subscribe'],
|
|
|
+ "companyArr"=>[],
|
|
|
"addtime"=>date("Y-m-d H:i:s"),
|
|
|
"updatetime"=>date("Y-m-d H:i:s")
|
|
|
];
|
|
@@ -60,17 +49,24 @@ class WeChat extends Base {
|
|
|
{
|
|
|
$Dbuser["updatetime"]=date("Y-m-d H:i:s");
|
|
|
Db::name("wechat_user")->save($Dbuser);
|
|
|
+ $Dbuser["companyArr"]=$Dbuser['companyArr']!=''?json_encode($Dbuser['companyArr'],
|
|
|
+ JSON_UNESCAPED_UNICODE):[];
|
|
|
}
|
|
|
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"]);
|
|
|
+ $valid =Validate::rule(["url|微信授权地址"=>"require|max:255|min:5"]);
|
|
|
if($valid->check($url)==false) return error_show(1004,$valid->getError());
|
|
|
+
|
|
|
+ $baseconfig =$this->wechat->getJsTicket();
|
|
|
+ if($baseconfig==false){
|
|
|
+ return error_show(1002,"签名获取失败");
|
|
|
+ }
|
|
|
$baseconfig =$this->wechat->getJsSign($url['url']);
|
|
|
if($baseconfig==false){
|
|
|
- return error_show(1004,"为获取到用户数据");
|
|
|
+ return error_show(1004,"签名获取失败");
|
|
|
}
|
|
|
return app_show(0,"获取成功",$baseconfig);
|
|
|
}
|