<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use app\index\model\Sms;
use app\index\model\User;
use app\index\model\Config;
use app\index\model\UserQq;
use app\index\model\Order;
use app\index\model\Shop;
use alipay\alipaynotify;
use think\Cookie;
use think\Session;
class Api extends \think\Controller
{
public function login(){
$username = input('username');
$password = input('password');
if (!$username) {
# code...
return jsonp('abd', 200);
return 3;
}
if (!$password) {
# code...
return json(['data'=>6,'code'=>404,'message'=>'密码不能为空']);
return 4;
}
if ($username=='13034892752' and $password =='123456') {
# code...
return 200;
}else{
return 2;
}
}
public function qq()
{
$config = new Config();
$config = $config->where('name', 'qq')
->find();
$app_id = $config->appid;
$app_secret = $config->appkey;
$my_url = $config->my_url;
$code = input("code");
if ($code) {
# code...
$code = Cookie::set('code',$code,200000);
}
$code = Cookie::get('code');
<br/>";
<br/>";
$token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
if (strpos($response, "callback") !== false)
{
$lpos = strpos($response, "(");
$rpos = strrpos($response, ")");
$response = substr($response, $lpos + 1, $rpos - $lpos -1);
$msg = json_decode($response);
if (isset($msg->error))
{
<h3>error:</h3>" . $msg->error;
<h3>msg :</h3>" . $msg->error_description;
exit;
}
}
$params = array();
parse_str($response, $params);
$graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'];
$str = file_get_contents($graph_url);
if (strpos($str, "callback") !== false)
{
$lpos = strpos($str, "(");
$rpos = strrpos($str, ")");
$str = substr($str, $lpos + 1, $rpos - $lpos -1);
}
$user = json_decode($str);
if (isset($user->error))
{
<h3>error:</h3>" . $user->error;
<h3>msg :</h3>" . $user->error_description;
exit;
}
$openid = $user->openid;
$graph_url = "https://graph.qq.com/user/get_user_info?access_token=" . $params['access_token']."&oauth_consumer_key=".$app_id."&openid=" . $user->openid;
$str = file_get_contents($graph_url);
$user_from_qq = json_decode($str);
<img src=" . $user->figureurl_qq_1 . ">";
if (!$user_from_qq->nickname) {
return "没有昵称";
}
$user = new UserQq();
$user = $user->where('openid', $openid)
->where('type', 0)
->find();
if (!$user) {
# code..
$user = new UserQq();
$user->openid = $openid;
$user->nickname = $user_from_qq->nickname ;
$user->figureurl_qq_1 = $user_from_qq->figureurl_qq_1 ;
$user->figureurl_qq_2 = $user_from_qq->figureurl_qq_2 ;
$user->gender = $user_from_qq->gender ;
$user->year = $user_from_qq->year ;
$user->type = 0 ;
$user->save();
$user = $user->where('openid', $openid)
->where('type', 0)
->find();
}else{
}
cookie('openid', $user_from_qq->nickname, 3600000);
cookie('nickname', $user_from_qq->nickname, 3600000);
cookie('figureurl_qq_2', $user_from_qq->figureurl_qq_2, 3600000);
if (!$user->user_id) {
# code..
session('openid_id', $user->id);
$this->redirect('index/index/register', ['cate_id' => 2]);
return $this->success('登录成功,绑定账号', 'index/index/register');
}
$user = User::get($user->user_id);
cookie('phone', null);
cookie('token', null);
Cookie('phone', $user->phone, 3600000);
Cookie('token', $user->token, 3600000);
Cookie('user_id', $user->id, 3600000);
if (Session::get('total_fee') > 0) {
Session::set('phone', $user->phone);
$this->redirect('member/payReturn');
}
if($user->phone=="18210787405"){
Cookie::set('admin', 1, 3600000);
}
return $this->success('登录成功^_^', 'index/index/index');
return "qq登录";
}
public function json()
{
echo '{"code":0,"msg":"","count":1000,"data":[{"id":10000,"username":"user-0","sex":"女","city":"城市-0","sign":"签名-0","experience":255,"logins":24,"wealth":82830700,"classify":"作家","score":57},{"id":10001,"username":"user-1","sex":"男","city":"城市-1","sign":"签名-1","experience":884,"logins":58,"wealth":64928690,"classify":"词人","score":27},{"id":10002,"username":"user-2","sex":"女","city":"城市-2","sign":"签名-2","experience":650,"logins":77,"wealth":6298078,"classify":"酱油","score":31},{"id":10003,"username":"user-3","sex":"女","city":"城市-3","sign":"签名-3","experience":362,"logins":157,"wealth":37117017,"classify":"诗人","score":68},{"id":10004,"username":"user-4","sex":"男","city":"城市-4","sign":"签名-4","experience":807,"logins":51,"wealth":76263262,"classify":"作家","score":6},{"id":10005,"username":"user-5","sex":"女","city":"城市-5","sign":"签名-5","experience":173,"logins":68,"wealth":60344147,"classify":"作家","score":87},{"id":10006,"username":"user-6","sex":"女","city":"城市-6","sign":"签名-6","experience":982,"logins":37,"wealth":57768166,"classify":"作家","score":34},{"id":10007,"username":"user-7","sex":"男","city":"城市-7","sign":"签名-7","experience":727,"logins":150,"wealth":82030578,"classify":"作家","score":28},{"id":10008,"username":"user-8","sex":"男","city":"城市-8","sign":"签名-8","experience":951,"logins":133,"wealth":16503371,"classify":"词人","score":14},{"id":10009,"username":"user-9","sex":"女","city":"城市-9","sign":"签名-9","experience":484,"logins":25,"wealth":86801934,"classify":"词人","score":75}]}';
}
public function jack()
{
$config = new Config();
$config = $config->where('name', 'alipay')
->find();
$app_id = $config->appid;
$appkey = $config->appkey;
$email = $config->email;
$alipay_config['partner'] = $config->appid;
$alipay_config['seller_email'] = $config->email;
$alipay_config['key'] = $config->appkey;
$alipay_config['sign_type'] = strtoupper('MD5');
$alipay_config['input_charset']= strtolower('utf-8');
$alipay_config['cacert'] = getcwd().'\\cacert.pem';
$alipay_config['transport'] = 'http';
$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyReturn();
$result = '';
if($verify_result)
$out_trade_no = $_GET['out_trade_no'];
$trade_no = $_GET['trade_no'];
$trade_status = $_GET['trade_status'];
if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
$result = "支付成功";
$phone = Cookie::get('phone');
$body = input('param.body');
$rand = '';
$subject = input('param.subject');
$total_fee = input('param.WIDtotal_fee');
$buyer_id = input('param.buyer_id');
$buyer_email = input('param.buyer_email');
$total_fee = input('param.total_fee');
$out_trade_no = input('param.out_trade_no');
if($body==1008611 or $body==1008612){
$phone = '';
}
Session::set('phone',$phone);
Session::set('body',$body);
Session::set('rand',$rand);
Session::set('subject',$subject);
Session::set('total_fee',$total_fee);
Session::set('buyer_id',$buyer_id);
Session::set('buyer_email',$buyer_email);
Session::set('total_fee',$total_fee);
Session::set('out_trade_no',$out_trade_no);
$this->redirect('member/payReturn',302);
$phone = Session::get('phone');
$body = Session::get('body');
$rand = Session::get('rand');
$subject = Session::get('subject');
$total_fee = Session::get('total_fee');
$buyer_id = Session::get('buyer_id');
$buyer_email = Session::get('buyer_email');
$total_fee = Session::get('total_fee');
$out_trade_no = Session::get('out_trade_no');
$map['id'] = $body;
$price = Shop::where($map)
->value('price');
if ($total_fee>=$price) { } else {
$body = 40; }
if ($body==135) {
$rand = Order::where('body', $body)
->where('phone', $phone)
->whereTime('create_time', 'yesterday')
->value('rand');
}
$order = Order::create([
'phone' => $phone,
'body' => $body,
'rand' => $rand+1,
'subject' => $subject,
'total_fee' => $total_fee,
'buyer_id' => $buyer_id,
'buyer_email' => $buyer_email,
'out_trade_no' => $out_trade_no,
]);
if ($body==105) {
User::where('body', $body)
->update(['rand' => 105]);
$expiration_time = User::where('phone', $phone)
->whereTime('create_time','>=', 'today')
->value('expiration_time');
if ($expiration_time<time()){
$expiration_time = time() ;
$start_time = time() ;
User::where('phone', $phone)
->update(['start_time' => $start_time,'rand'=> 1]);
}
if ($total_fee=33) {
$expiration_time = $expiration_time+ (3600*24*99) ;
}elseif ($total_fee=188) {
$expiration_time = $expiration_time+ (3600*24*188) ;
}elseif ($total_fee>=320) {
$expiration_time = $expiration_time+ (3600*24*366) ;
}else{
$expiration_time = $expiration_time+ (3600*24*32) ;
}
User::where('phone', $phone)
->update(['expiration_time' => $expiration_time]);
}
if ($phone='15966982315'){
Session::set('name','thinkphp'); $this->redirect('index/login', ['id' => $body]);
}
exit('<script>top.location.href="../index/view/id/'.$body.'"</script>');
}
else {
echo "trade_status=".$_GET['trade_status'];
}
<br />";
}
else {
$result = "支付失败";
}
$this->assign('result',$result); return $this->fetch();
}
public function sms()
{
if (Request::instance()->isPost() ) {
$se = 0;
$url = $_SERVER["HTTP_REFERER"]; $str = str_replace("http://","",$url); $strdomain = explode("/",$str); $domain = $strdomain[0]; if(strstr($domain,'baidu.com')){
$se = 1;
}
else if(strstr($domain,'google.cn')){
$se = 1;
}
if($_SERVER['SERVER_NAME'] = $domain){ }else{
return "wrong";
}
} else {
return "wait";
}
$config = new Config();
$config = $config->where('name', 'sms')
->find();
$username = $config->username;
$password = $config->password;
$my_url = $config->my_url;
header("content-type:text/html; charset=utf-8");
$tom = input('s');
$rand = rand(1000,9999); $cha = $my_url .'?username='.$username.'&password='.$password.'&content=验证码:'.$rand.'【高血压】&receiver='.$tom ;
$fp = file_get_contents($cha);
$xml = simplexml_load_string($fp);
$data = json_decode(json_encode($xml),TRUE);
echo $data['message'];
if ($data['message']=='非法请求,一个号码一天内提交超过了五次'){
return " 【请使用你今天收到的上条短信验证码登录即可 ! 】";
}
if ($data['message']=='短信提交成功') {
# code...
echo "(";
$user = Sms::create([
'phone' => $tom,
'rand' => $rand
]);
echo ":";
}
}
public function alipay()
{
return $this->fetch();
}
public function alipayReturnUrl()
{
import('alipay/tom.php');
require("alipay/tom.php");
require("alipay/tom.php");
require("alipay/lib/alipay_notify.class.php");
exit();
require("alipay/alipay.config.php");
require("alipay/lib/alipay_core.function.php");
require("alipay/lib/alipay_md5.function.php");
require("alipay/lib/alipay_notify.class.php");
$alipayNotify = new AlipayNotify($alipay_config);
$verify_result = $alipayNotify->verifyReturn();
if($verify_result)
$out_trade_no = $_GET['out_trade_no'];
$trade_no = $_GET['trade_no'];
echo "您的订单号是:" .$trade_no;
$trade_status = $_GET['trade_status'];
if($_GET['trade_status'] == 'TRADE_FINISHED' || $_GET['trade_status'] == 'TRADE_SUCCESS') {
}
else {
echo "trade_status=".$_GET['trade_status'];
}
echo "购买成功了...<br />";
}
else {
echo "付款失败";
}
}
public function demo(){
dump("演示一下 api跨域访问");
$url = "http://open.gaoxueya.com/tp5/public/index.php/index/bbs/add";
$data=array(
"title" => "用机器人来发帖了,我来采集你的内容了",
"content" => "加个验证码吧,不然被攻击了"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS , http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
}
public function weibo(){
$config = new Config();
$config = $config->where('name', 'weibo')
->find();
$app_id = $config->appid;
$app_secret = $config->appkey;
$my_url = $config->my_url;
$code = input('code');
$url = "https://api.weibo.com/oauth2/access_token?client_id=".$app_id."&client_secret=".$app_secret."&grant_type=authorization_code&redirect_uri=".$my_url."&code=" .$code;
header("Content-Type:text/html;charset=utf-8");
<br />';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
$data = curl_exec($curl);
curl_close($curl);
$result = json_decode($data, true);
<pre>';
</pre> 1233';
$access_token = $result['access_token'];
$uid = $result['uid'];
$tom = "https://api.weibo.com/2/users/show.json?access_token=". $access_token ."&uid=" . $uid;
$data = file_get_contents($tom,'rb');
$data = json_decode($data, true);
$openid = $uid ;
$user = new UserQq();
$user = $user->where('openid', $openid)->where('type', 1)
->find();
if (!$user) {
# code..
$user = new UserQq;
$user->openid = $openid;
$user->nickname = $data['name'] ;
$user->figureurl_qq_1 = $data['avatar_large'] ;
$user->figureurl_qq_2 = $data['avatar_hd'] ;
$user->gender = $data['gender'] ;
$user->year = $data['province'] ;
$user->type = 1 ;
$user->save();
$user = $user->where('openid', $openid)->where('type', 1)
->find();
}else{
}
cookie('openid', $openid, 3600000);
cookie('nickname', $data['name'], 3600000);
cookie('figureurl_qq_2', $data['avatar_hd'], 3600000);
if (!$user->user_id) {
# code..
session('openid_id', $user->id);
$this->redirect('index/index/register', ['cate_id' => 2]);
return $this->success('登录成功,绑定账号', 'index/index/register');
}
$user = User::get($user->user_id);
cookie('phone', null);
cookie('token', null);
Cookie('phone', $user->phone, 3600000);
Cookie('token', $user->token, 3600000);
Cookie('user_id', $user->id, 3600000);
if (Session::get('total_fee') > 0) {
Session::set('phone', $user->phone);
$this->redirect('member/payReturn');
}
if($user->phone=="18210787405"){
Cookie::set('admin', 1, 3600000);
}
return $this->success('登录成功^_^', 'index/index/index');
}
public function domain(){
$domain = 'Rinuo.com';
$cha = 'http://panda.www.net.cn/cgi-bin/check.cgi?area_domain='.$domain ;
$fp = file_get_contents($cha,'rb');
$xml = simplexml_load_string($fp);
$data = json_decode(json_encode($xml),TRUE);
$this->assign('data',$data);
return $this->fetch();
}
}