123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- declare (strict_types=1);
- namespace app\api\controller\v1;
- use app\BaseViewController;
- use app\model\admin\Site;
- use app\Request;
- use library\exceptions\AuthException;
- use library\lib\weixina;
- use think\App;
- use think\facade\Db;
- class Bind extends BaseViewController {
- private $siteData;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $request = \request();
- $secret_key = $request->get('secret_key');
- if(empty($secret_key)) {
- throw new AuthException('参数错误,请重新扫码', -9);
- }
- $this->siteData = (new Site)->where('secret_key',$secret_key)->find();
- }
- /**
- * 绑定数据
- */
- public function system_bind(Request $request){
- $token = $request->get('token');
- if(empty($token)) {
- $this->assign('error','token 错误,请重新扫码绑定!');
- return $this->display('binderror');
- }
- $token = str_replace(" ","+",$token);
- $str = crypto_decrypt(base64_decode($token),'md5_user_token');
- if(empty($str)) {
- $this->assign('error','token 错误,请重新扫码绑定!');
- return $this->display('binderror');
- }
- $strAr = explode('|',$str);
- if(count($strAr) != 2) {
- $this->assign('error','数据出错!');
- return $this->display('binderror');
- }
- $w = $this->weixinLogin();
- if(!$w[1]) {
- return $w[0];
- }
- $this->assign('user',$w[0]);
- $this->assign('site',$this->siteData);
- $this->assign('token',$token);
- $this->assign('secret_key',$this->siteData['secret_key']);
- return $this->display('bindqrcode');
- }
- public function system_bind_data(Request $request){
- $token = $request->post('token');
- if(empty($token)) {
- return app('json')->fail('token 错误,请重新扫码绑定!');
- }
- $token = str_replace(" ","+",$token);
- $str = crypto_decrypt(base64_decode($token),'md5_user_token');
- if(empty($str)) {
- return app('json')->fail('token 错误,请重新扫码绑定!');
- }
- $strAr = explode('|',$str);
- if(count($strAr) != 2) {
- $this->assign('error','数据出错!');
- return $this->display('binderror');
- }
- $w = $this->weixinLogin();
- if(!$w[1]) {
- return $w[0];
- }
- $userInfo = $w[0];
- $count = Db::name("weixin_push_user")
- ->where('sassid',$this->siteData['sassid'])
- ->where('openid',$userInfo['openid'])
- ->where('uid',$strAr[0])
- ->count();
- if($count <= 0) {
- $d['type'] = 'admin';
- $d['sassid'] = $this->siteData['sassid'];
- $d['uid'] = $strAr[0];
- $d['time'] = time();
- $d['avatar'] = $userInfo['headimgurl'];
- $d['nickname'] = $userInfo['nickname'];
- $d['openid'] = $userInfo['openid'];
- Db::name("weixin_push_user")->insert($d);
- }
- return app('json')->success('操作成功');
- }
- public function bindsuccess(){
- return $this->display();
- }
- /**
- * 微信登录板顶
- */
- private function weixinLogin() {
- $weixinUser = cookie('weix_userinfo');
- if(!empty($weixinUser)) return [unserialize($weixinUser),true];
- $domain = \request()->url();
- cookie('w_url',$domain);
- $weixinA = new weixina;
- return [$weixinA->oauth('login'),false];
- }
- }
|