You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
4.8 KiB
140 lines
4.8 KiB
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | YFCMF [ WE CAN DO IT MORE SIMPLE ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Author: rainfer <81818832@qq.com>
|
|
// +----------------------------------------------------------------------
|
|
namespace app\culture\controller;
|
|
|
|
use think\captcha\Captcha;
|
|
use think\Session;
|
|
use think\Log;
|
|
|
|
class Common extends Base{
|
|
public function _initialize(){
|
|
parent::_initialize();
|
|
$this->assign('current',[['title'=>'文化金融服务项目']]);
|
|
}
|
|
public function verify(){
|
|
$key=request()->param('key');
|
|
if(empty($key))$this->error('缺少参数');
|
|
return $this->verify_build($key);
|
|
}
|
|
protected function verify_build($id=''){
|
|
ob_end_clean();
|
|
$verify = new Captcha (config('verify'));
|
|
return $verify->entry($id);
|
|
}
|
|
/**
|
|
* 登录页面
|
|
* @return mixed|string
|
|
*/
|
|
public function login(){
|
|
$apitoken=session('api_token');
|
|
if(!empty($apitoken)){
|
|
$this->redirect(url('culture/user/index'));
|
|
}
|
|
$this->assign('current',[['title'=>'用户登录']]);
|
|
return $this->fetch(':login');
|
|
}
|
|
/**
|
|
* 退出
|
|
*/
|
|
public function logout(){
|
|
Session::delete('api_token');
|
|
$this->success("登出成功",url('culture/common/login'));
|
|
}
|
|
/**
|
|
* 注册小程序用户
|
|
*/
|
|
public function register(){
|
|
$token=md5('szcaee_culture');
|
|
$options = array(
|
|
'token'=>$token, //填写你设定的key
|
|
'appid'=>'wx5cf3b72f956ecbe1', //填写高级调用功能的app id, 请在微信开发模式后台查询
|
|
'appsecret'=>'281ad0e4de4c56fe39893d05c1222357', //填写高级调用功能的密钥
|
|
);
|
|
|
|
$we_obj = new \Wechat($options);
|
|
$code =input('code');
|
|
if ($code) {
|
|
Log::write($code);
|
|
try {
|
|
$json = $we_obj->getOauthAccessToken();
|
|
}catch (\Exception $e){
|
|
$this->redirect(url('culture/common/login'));
|
|
}
|
|
Log::write($json);
|
|
if (!$json) {
|
|
$this->redirect(url('culture/common/login'));
|
|
}
|
|
$openid=$json["openid"];
|
|
$unionid=$json["unionid"];
|
|
$access_token = $json['access_token'];
|
|
|
|
$wxuser=['open_id'=>$openid];
|
|
//缓存获取用户资料
|
|
$cache=cache($openid);
|
|
$nickname=$avatar='';
|
|
if($cache){
|
|
try {
|
|
$userinfo=json_decode($cache,true);
|
|
$nickname=$userinfo['nickname'];
|
|
$avatar=$userinfo['avatar'];
|
|
}catch (\Exception $e){}
|
|
}else{
|
|
$userinfo=$we_obj->getOauthUserinfo($access_token,$openid);
|
|
if ($userinfo && !empty($userinfo['nickname'])) {
|
|
$nickname=$userinfo['nickname'];
|
|
$avatar=$userinfo['headimgurl'];
|
|
}
|
|
$params=['nickname'=>$nickname,'avatar'=>$avatar];
|
|
cache($openid,json_encode($params));
|
|
}
|
|
if($nickname)$wxuser['nickname']=$nickname;
|
|
if($avatar)$wxuser['avatar']=$avatar;
|
|
$wxuser['unionid']=$unionid;
|
|
$wxuser['timestamp']=time();
|
|
|
|
$url=$this->hostpath."/api/auth/register";
|
|
$secret="098f6bcd4621d373cade4e832627b4f6";
|
|
$token=$this->getSign($secret, $wxuser);
|
|
$wxuser['token']=$token;
|
|
$result=['status'=>0];
|
|
Log::write($wxuser);
|
|
try {
|
|
$res=go_curl($url,'POST',$wxuser,['Accept'=>'application/json']);
|
|
Log::write($res);
|
|
$res=json_decode($res,true);
|
|
if(isset($res['status'])&&$res['status']){
|
|
$result['status']=$res['status'];
|
|
Session::delete('api_token');
|
|
session('api_token',$res['access_token']);
|
|
}
|
|
}catch (\Exception $e){
|
|
|
|
}
|
|
|
|
if($result['status']){
|
|
$this->redirect(url('culture/user/index'));
|
|
}else{
|
|
$this->redirect(url('culture/common/login'));
|
|
}
|
|
}
|
|
}
|
|
// 获取sign
|
|
protected function getSign($secret, $data)
|
|
{
|
|
if (isset($data['token']))
|
|
unset($data['token']);
|
|
// 对数组的值按key排序
|
|
ksort($data);
|
|
// 生成url的形式
|
|
$params = http_build_query($data);
|
|
// 生成sign
|
|
$sign = md5($params . $secret);
|
|
return $sign;
|
|
}
|
|
}
|