123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\WechatUser;
- use think\App;
- use think\facade\Validate;
- use think\facade\Db;
- class WeChat extends Base {
- public $wechat;
- public function __construct(App $App) {
- parent::__construct($App);
- $wechat_conf=["appid"=>env("WECHAT.APPID"),"appsecret"=>env("WECHAT.APPSECRET")];
- $this->wechat=new \Wechat($wechat_conf);
- }
- // 获取用户信息{subscribe,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time,[unionid]}
- public function getinfo(){
- $code =$this->request->only(["code"=>"","openid"=>""],"post","trim");
- $valid =Validate::rule([
- "code|微信授权Code"=>"requireWithout:openid|max:255|min:5",
- "openid|微信openid"=>"requireWithout:code|max:255|min:5",
- ]);
- if($valid->check($code)==false) return error_show(1004,$valid->getError());
- if($code['openid']==""){
- $uid =$this->wechat->getAccessTokenByCode($code['code']);
- if($uid==false){
- return error_show(1004,"未获取到用户数据");
- }
- if(!isset($uid['openid'])|| $uid['openid']=='') return error_show(1004,"未获取到用户数据");
- }else$uid['openid']=$code['openid'];
- $Dbuser =Db::name("wechat_user")->where(["openid"=>$uid['openid']])->findOrEmpty();
- if(empty($Dbuser)){
- if($code['openid']!="") return error_show(1004,"未找到用户数据");
- $useinfo = $this->wechat->getUserInfo($uid['openid']);
- if($useinfo==false)return error_show(1004,"未获取到用户数据");
- $Dbuser = [
- "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"=>'',
- "is_show"=>0,
- "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);
- }
- $Dbuser["companyArr"]=$Dbuser['companyArr']!=''?json_decode($Dbuser['companyArr'],true):[];
- $Dbuser["is_show"]=explode(",",$Dbuser["is_show"]);
- return app_show(0,"获取成功",$Dbuser);
- }
- //网页端调用jssdk配置config
- public function getConfig(){
- $url =$this->request->only(["url"=>""],"post","trim");
- $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 app_show(0,"获取成功",$baseconfig);
- }
-
- public function list(){
- $post =$this->request->only(['mobile'=>'',"nickname"=>"","page"=>1,"size"=>10],'post','trim');
- $where=[];
- if($post['mobile']!='')$where[]=["mobile","like","%{$post['mobile']}%"];
- if($post['nickname']!='')$where[]=["nickname","like","%{$post['nickname']}%"];
- $list =(new WechatUser())->where($where)->json(['companyArr'])->paginate(["page"=>$post['page'],"list_rows"=>$post['size']]);
- $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
- }
-
- public function save(){
- $post =$this->request->only([
- "id"=>"",
- 'mobile'=>'',
- 'nickname'=>'',
- 'is_show'=>'',
- 'companyArr'=>[],
- ],'post','trim');
- $valid =Validate::rule([
- "id|主键id"=>"require|number",
- "mobile|手机号"=>"require|mobile",
- "nickname|名称"=>"require",
- "is_show|可查看报表"=>"require",
- "companyArr|可查看企业"=>"require|array"
- ]);
- if($valid->check($post)==false)$this->error($valid->getError());
- $model = new WechatUser();
- $info =$model->findOrEmpty($post['id']);
- if($info->isEmpty())$this->error("未找到用户数据");
- $info->mobile = $post['mobile'];
- $info->nickname = $post['nickname'];
- $info->is_show = $post['is_show'];
- $info->companyArr = json_encode($post['companyArr'],JSON_UNESCAPED_UNICODE);
- $info->save();
- $this->success('更新成功');
- }
-
- }
|