WeChat.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\WechatUser;
  4. use think\App;
  5. use think\facade\Validate;
  6. use think\facade\Db;
  7. class WeChat extends Base {
  8. public $wechat;
  9. public function __construct(App $App) {
  10. parent::__construct($App);
  11. $wechat_conf=["appid"=>env("WECHAT.APPID"),"appsecret"=>env("WECHAT.APPSECRET")];
  12. $this->wechat=new \Wechat($wechat_conf);
  13. }
  14. // 获取用户信息{subscribe,openid,nickname,sex,city,province,country,language,headimgurl,subscribe_time,[unionid]}
  15. public function getinfo(){
  16. $code =$this->request->only(["code"=>"","openid"=>""],"post","trim");
  17. $valid =Validate::rule([
  18. "code|微信授权Code"=>"requireWithout:openid|max:255|min:5",
  19. "openid|微信openid"=>"requireWithout:code|max:255|min:5",
  20. ]);
  21. if($valid->check($code)==false) return error_show(1004,$valid->getError());
  22. if($code['openid']==""){
  23. $uid =$this->wechat->getAccessTokenByCode($code['code']);
  24. if($uid==false){
  25. return error_show(1004,"未获取到用户数据");
  26. }
  27. if(!isset($uid['openid'])|| $uid['openid']=='') return error_show(1004,"未获取到用户数据");
  28. }else$uid['openid']=$code['openid'];
  29. $Dbuser =Db::name("wechat_user")->where(["openid"=>$uid['openid']])->findOrEmpty();
  30. if(empty($Dbuser)){
  31. if($code['openid']!="") return error_show(1004,"未找到用户数据");
  32. $useinfo = $this->wechat->getUserInfo($uid['openid']);
  33. if($useinfo==false)return error_show(1004,"未获取到用户数据");
  34. $Dbuser = [
  35. "openid"=>$useinfo['openid'],
  36. "nickname"=>$useinfo['nickname'],
  37. "mobile"=>'',
  38. "gender"=>$useinfo['sex'],
  39. "avatar"=>$useinfo['headimgurl'],
  40. "subscribe_time"=>date("Y-m-d H:i:s",$useinfo['subscribe_time']),
  41. "addr"=>"{$useinfo['province']}/{$useinfo['city']}/{$useinfo['country']}",
  42. "status"=>$useinfo['subscribe'],
  43. "companyArr"=>'',
  44. "is_show"=>0,
  45. "addtime"=>date("Y-m-d H:i:s"),
  46. "updatetime"=>date("Y-m-d H:i:s")
  47. ];
  48. $Dbuser['id']=Db::name("wechat_user")->insertGetId($Dbuser);
  49. }else
  50. {
  51. $Dbuser["updatetime"]=date("Y-m-d H:i:s");
  52. Db::name("wechat_user")->save($Dbuser);
  53. }
  54. $Dbuser["companyArr"]=$Dbuser['companyArr']!=''?json_decode($Dbuser['companyArr'],true):[];
  55. $Dbuser["is_show"]=explode(",",$Dbuser["is_show"]);
  56. return app_show(0,"获取成功",$Dbuser);
  57. }
  58. //网页端调用jssdk配置config
  59. public function getConfig(){
  60. $url =$this->request->only(["url"=>""],"post","trim");
  61. $valid =Validate::rule(["url|微信授权地址"=>"require|max:255|min:5"]);
  62. if($valid->check($url)==false) return error_show(1004,$valid->getError());
  63. $baseconfig =$this->wechat->getJsTicket();
  64. if($baseconfig==false){
  65. return error_show(1002,"签名获取失败");
  66. }
  67. $baseconfig =$this->wechat->getJsSign($url['url']);
  68. if($baseconfig==false){
  69. return error_show(1004,"签名获取失败");
  70. }
  71. return app_show(0,"获取成功",$baseconfig);
  72. }
  73. public function list(){
  74. $post =$this->request->only(['mobile'=>'',"nickname"=>"","page"=>1,"size"=>10],'post','trim');
  75. $where=[];
  76. if($post['mobile']!='')$where[]=["mobile","like","%{$post['mobile']}%"];
  77. if($post['nickname']!='')$where[]=["nickname","like","%{$post['nickname']}%"];
  78. $list =(new WechatUser())->where($where)->json(['companyArr'])->paginate(["page"=>$post['page'],"list_rows"=>$post['size']]);
  79. $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  80. }
  81. public function save(){
  82. $post =$this->request->only([
  83. "id"=>"",
  84. 'mobile'=>'',
  85. 'nickname'=>'',
  86. 'is_show'=>'',
  87. 'companyArr'=>[],
  88. ],'post','trim');
  89. $valid =Validate::rule([
  90. "id|主键id"=>"require|number",
  91. "mobile|手机号"=>"require|mobile",
  92. "nickname|名称"=>"require",
  93. "is_show|可查看报表"=>"require",
  94. "companyArr|可查看企业"=>"require|array"
  95. ]);
  96. if($valid->check($post)==false)$this->error($valid->getError());
  97. $model = new WechatUser();
  98. $info =$model->findOrEmpty($post['id']);
  99. if($info->isEmpty())$this->error("未找到用户数据");
  100. $info->mobile = $post['mobile'];
  101. $info->nickname = $post['nickname'];
  102. $info->is_show = $post['is_show'];
  103. $info->companyArr = json_encode($post['companyArr'],JSON_UNESCAPED_UNICODE);
  104. $info->save();
  105. $this->success('更新成功');
  106. }
  107. }