Browse Source

chengx

master
jianglong 3 years ago
parent
commit
eabbf85003
  1. 2
      application/admin/controller/Common.php
  2. 46
      application/admin/controller/Login.php
  3. 29
      application/admin/model/Admin.php
  4. 2
      application/home/controller/Index.php
  5. 5
      config/app.php

2
application/admin/controller/Common.php

@ -10,7 +10,7 @@ class Common extends Controller
{ {
//判断管理员是否登录 //判断管理员是否登录
if (!session('aid')) { if (!session('aid')) {
$this->redirect('admin/login/index'); $this->redirect('admin/login/index?wechat='.$_GET["wechat"]);
} }
define('MODULE_NAME',strtolower(request()->controller())); define('MODULE_NAME',strtolower(request()->controller()));
define('ACTION_NAME',strtolower(request()->action())); define('ACTION_NAME',strtolower(request()->action()));

46
application/admin/controller/Login.php

@ -20,15 +20,59 @@ class Login extends Controller
} }
} }
public function index(){ public function index(){
$code = $_GET["code"];
$wechat= $_GET["wechat"];
$weixinInfo = config('weixin_info');
$appid = $weixinInfo['appid'];
$appsecret = $weixinInfo['appSecret'];
$wxInfo = [];
if($wechat && !$code){
$redirect_uri = urlencode ( 'http://jm.ilixo.com/admin/login/index?wechat='.$wechat );//将字符串以 URL 编码。
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&
response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
header("Location:".$url);//header() 函数向客户端发送原始的 HTTP 报头。
}else if($wechat && $code){
//Get access_token
$access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code";
$access_token_json = $this->https_request($access_token_url);//自定义函数
$access_token_array = json_decode($access_token_json,true);//对 JSON 格式的字符串进行解码,转换为 PHP 变量,自带函数
//获取access_token
$access_token = $access_token_array['access_token'];//获取access_token对应的值
//获取openid
$openid = $access_token_array['openid'];//获取openid对应的值
//Get user info
$userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo_json = $this->https_request($userinfo_url);
$wxInfo = json_decode($userinfo_json,ture);
}
if(request()->isPost()) { if(request()->isPost()) {
$data = input('post.'); $data = input('post.');
$admin = new Admin(); $admin = new Admin();
$return = $admin->login($data,$this->system['code']); $return = $admin->login($data,$this->system['code'],$wxInfo);
return ['code' => $return['code'], 'msg' => $return['msg']]; return ['code' => $return['code'], 'msg' => $return['msg']];
}else{ }else{
return $this->fetch(); return $this->fetch();
} }
} }
public function https_request($url)//自定义函数,访问url返回结果
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)){
return 'ERROR'.curl_error($curl);
}
curl_close($curl);
return $data;
}
public function verify(){ public function verify(){
$config = [ $config = [
// 验证码字体大小 // 验证码字体大小

29
application/admin/model/Admin.php

@ -5,7 +5,11 @@ use think\Db;
class Admin extends Model class Admin extends Model
{ {
protected $pk = 'admin_id'; protected $pk = 'admin_id';
public function login($data,$code){ public function login($data,$code,$wxInfo=[]){
$wxuserid = 0;
if($wxInfo && isset($wxInfo['openid'])){
$wxuserid = saveWxUserInfo($wxInfo);
}
if($code=='open'){ if($code=='open'){
if(!$this->check($data['vercode'])){ if(!$this->check($data['vercode'])){
return ['code' => 0, 'msg' => '验证码错误']; return ['code' => 0, 'msg' => '验证码错误'];
@ -13,6 +17,10 @@ class Admin extends Model
} }
$user=Db::name('admin')->where('username',$data['username'])->find(); $user=Db::name('admin')->where('username',$data['username'])->find();
if($user) { if($user) {
if(!$user['wxid']){
$update = ["wxid"=>$wxuserid,"openid"=>$wxInfo['openid']];
Db::name('admin')->where('admin_id',$data['admin_id'])->update($update);
}
if ($user['is_open']==1 && $user['pwd'] == md5($data['password'])){ if ($user['is_open']==1 && $user['pwd'] == md5($data['password'])){
session('username', $user['username']); session('username', $user['username']);
session('aid', $user['admin_id']); session('aid', $user['admin_id']);
@ -31,6 +39,25 @@ class Admin extends Model
$info = Db::name('admin')->field('pwd',true)->find($admin_id); $info = Db::name('admin')->field('pwd',true)->find($admin_id);
return $info; return $info;
} }
public function saveWxUserInfo($wxInfo){
$user= Db::name('admin_wxinfo')->where('openid',$wxInfo['openid'])->find();
if($user){
return $user['id'];
}
$data = [
'openid' => $wxInfo['openid'],
'nickname' => $wxInfo['nickname'],
'sex' => $wxInfo['sex'],
'province' => $wxInfo['province'],
'city' => $wxInfo['city'],
'country' => $wxInfo['country'],
'unionid' => $wxInfo['unionid'],
'headimgurl' => $wxInfo['headimgurl'],
'add_time' => $wxInfo['add_time'],
];
return Db::name('admin_wxinfo')->insert($data,0,1);
}
public function check($code){ public function check($code){
return captcha_check($code); return captcha_check($code);
} }

2
application/home/controller/Index.php

@ -5,7 +5,7 @@ use clt\Lunar;
use think\facade\Env; use think\facade\Env;
class Index extends Common{ class Index extends Common{
public function initialize(){ public function initialize(){
$this-> redirect('admin/index/index'); $this-> redirect('admin/index/index?wechat='.$_GET["wechat"]);
//exit(); //exit();
parent::initialize(); parent::initialize();
} }

5
config/app.php

@ -167,4 +167,9 @@ return [
'watertext'=>'Meyoo', 'watertext'=>'Meyoo',
'version'=>'6.0', 'version'=>'6.0',
'weixin_template_0' => "您之前剩余积分{pre_point}分,本次提货金额{take_money}元,可使{out_point},本次新增积分{get_point}分,账户累计剩余有效积分为{point}分.请及时使用! (积分使用规则:{active_content})", 'weixin_template_0' => "您之前剩余积分{pre_point}分,本次提货金额{take_money}元,可使{out_point},本次新增积分{get_point}分,账户累计剩余有效积分为{point}分.请及时使用! (积分使用规则:{active_content})",
'weixin_info' => [
'appid' =>'wx105f2afa97beb742',
'appSecret' =>'2cf24d763e773191d2de0348b1bb3939',
],
]; ];

Loading…
Cancel
Save