WeChat.php 4.2 KB

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