<?php
namespace app\index\controller;
use app\panapi\model\Account;
use think\Controller;
class Wx extends Controller
{
private $appid='appid';
private $secret='secret';
private $redirect_url='redirect_url';
private $url='https://open.weixin.qq.com/connect/oauth2/authorize';
private $url2='https://api.weixin.qq.com/sns/oauth2/access_token';
public function get_code(){
$account_id=$this->request->param('account_id',298);
$v=Account::where('account_id',$account_id)->value('openid');
if(!empty($v)){
return json([
'status'=>1,
'msg'=>'已有微信id',
'data'=> $v
]);
}
$params=[
'appid'=>$this->appid,
'response_type'=>'code',
'scope'=>'snsapi_base',
];
$params=http_build_query($params).'&redirect_uri='.urlencode($this->redirect_url).'&state='.$account_id.'&#wechat_redirect';
$this->redirect($this->url.'?'.$params);
}
public function save_info(){
$params=[
'appid'=>$this->appid,
'secret'=>$this->secret,
'code'=>input('param.code'),
'grant_type'=>'authorization_code'
];
$res=panApi($this->url2,$params);
$res=json_decode($res,true);
$data1['openid']=$res['openid'];
$data2['account_id']=input('param.state');
Account::update($data1,$data2);
return json([
'status'=>1,
'msg'=>'账户写入微信ID成功',
'data'=> $data1['openid']
]);
}
}
|