Vbind.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\admin\controller\v1;
  4. use app\BaseViewController;
  5. use app\model\admin\Site;use app\Request;
  6. use library\exceptions\AuthException;
  7. use library\lib\weixina;
  8. use think\App;
  9. use think\facade\Db;
  10. class Vbind extends BaseViewController {
  11. private $siteData;
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. $request = \request();
  16. $secret_key = $request->get('secret_key');
  17. if(empty($secret_key)) {
  18. throw new AuthException('参数错误,请重新扫码', -9);
  19. }
  20. $this->siteData = (new Site)->where('secret_key',$secret_key)->find();
  21. }
  22. /**
  23. * 绑定数据
  24. */
  25. public function system_bind(Request $request){
  26. $token = $request->get('token');
  27. if(empty($token)) {
  28. $this->assign('error','token 错误,请重新扫码绑定!');
  29. return $this->display('binderror');
  30. }
  31. $token = str_replace(" ","+",$token);
  32. $str = crypto_decrypt(base64_decode($token),'md5_token');
  33. if(empty($str)) {
  34. $this->assign('error','token 错误,请重新扫码绑定!');
  35. return $this->display('binderror');
  36. }
  37. $strAr = explode('|',$str);
  38. if(count($strAr) != 2) {
  39. $this->assign('error','数据出错!');
  40. return $this->display('binderror');
  41. }
  42. $w = $this->weixinLogin();
  43. if(!$w[1]) {
  44. return $w[0];
  45. }
  46. $this->assign('user',$w[0]);
  47. $this->assign('site',$this->siteData);
  48. $this->assign('token',$token);
  49. $this->assign('secret_key',$this->siteData['secret_key']);
  50. return $this->display('bindqrcode');
  51. }
  52. public function system_bind_data(Request $request){
  53. $token = $request->post('token');
  54. if(empty($token)) {
  55. return app('json')->fail('token 错误,请重新扫码绑定!');
  56. }
  57. $token = str_replace(" ","+",$token);
  58. $str = crypto_decrypt(base64_decode($token),'md5_token');
  59. if(empty($str)) {
  60. return app('json')->fail('token 错误,请重新扫码绑定!');
  61. }
  62. $w = $this->weixinLogin();
  63. if(!$w[1]) {
  64. return $w[0];
  65. }
  66. $userInfo = $w[0];
  67. $count = Db::name("weixin_push_user")
  68. ->where('sassid',$this->siteData['sassid'])
  69. ->where('openid',$userInfo['openid'])
  70. ->count();
  71. if($count <= 0) {
  72. $d['type'] = 'admin';
  73. $d['sassid'] = $this->siteData['sassid'];
  74. $d['uid'] = 0;
  75. $d['time'] = time();
  76. $d['avatar'] = $userInfo['headimgurl'];
  77. $d['nickname'] = $userInfo['nickname'];
  78. $d['openid'] = $userInfo['openid'];
  79. Db::name("weixin_push_user")->insert($d);
  80. }
  81. return app('json')->success('操作成功');
  82. }
  83. public function bindsuccess(){
  84. return $this->display();
  85. }
  86. /**
  87. * 微信登录板顶
  88. */
  89. private function weixinLogin() {
  90. $weixinUser = cookie('weix_userinfo');
  91. if(!empty($weixinUser)) return [unserialize($weixinUser),true];
  92. $domain = \request()->url();
  93. cookie('w_url',$domain);
  94. $weixinA = new weixina;
  95. return [$weixinA->oauth('login'),false];
  96. }
  97. }