Bind.php 3.5 KB

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