Browse Source

oauth

master
ZLW-PC\Administrator 7 years ago
parent
commit
bbf201f2ea
  1. 11
      app/Common/ReturnData.php
  2. 102
      app/Http/Controllers/Api/OrderController.php
  3. 22
      app/Http/Controllers/Api/UserController.php
  4. 55
      app/Http/Controllers/Weixin/UserController.php
  5. 2
      app/Http/Logic/UserMoneyLogic.php
  6. 4
      resources/views/weixin/cart/index.blade.php
  7. 4
      resources/views/weixin/goods/goodsDetail.blade.php
  8. 1
      routes/web.php

11
app/Common/ReturnData.php

@ -149,4 +149,15 @@ class ReturnData
return false;
}
public static function getCodeText($code)
{
$res = '';
if (isset(self::$codeTexts[$code]))
{
$res = self::$codeTexts[$code];
}
return $res;
}
}

102
app/Http/Controllers/Api/OrderController.php

@ -198,108 +198,6 @@ class OrderController extends CommonController
return $this->getLogic()->del($where);
}
//订单状态修改
public function orderStatusUpdate(Request $request)
{
$type = $request->input('type','');
if($request->input('id', '')!=''){$where['id'] = $request->input('id');}
$where['user_id'] = Token::$uid;
if($type=='' || $where['id'] =='')
{
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
//修改订单状态,1设为支付,2设为取消,3设为确认收货,4设为退款退货,5设为删除,6设为已评价
if($type == 1)
{
//判断订单是否存在或本人
$where['order_status'] = 0;
$where['pay_status'] = 0;
$order = model('Order')->getOne($where);
if(!$order){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'订单不存在');}
//判断用户余额是否足够
$user_money = DB::table('user')->where(array('id'=>Token::$uid))->value('money');
if($order['order_amount']>$user_money){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'余额不足');}
$data['pay_status'] = 1;
$data['pay_money'] = $order['order_amount']; //支付金额
$data['pay_id'] = $request->input('pay_id','');
$data['pay_time'] = $request->input('pay_time',time());
if($request->input('pay_name', '')!=''){$data['pay_name'] = $request->input('pay_name');}
//扣除用户余额
if($data['pay_money']<=0 || !DB::table('user')->where(array('id'=>Token::$uid))->decrement('money', $data['pay_money']))
{
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
//增加用户余额记录
DB::table('user_money')->insert(array('user_id'=>Token::$uid,'type'=>1,'money'=>$data['pay_money'],'des'=>'订单余额支付','user_money'=>DB::table('user')->where(array('id'=>Token::$uid))->value('money'),'add_time'=>time()));
}
elseif($type == 2)
{
//判断订单是否存在或本人
$where['order_status'] = 0;
$where['pay_status'] = 0;
$order = Order::where($where)->first();
if(!$order){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'订单不存在');}
$data['order_status'] = 1;
}
elseif($type == 3)
{
//判断订单是否存在或本人
$where['order_status'] = 0;
$where['refund_status'] = 0;
$where['pay_status'] = 1;
$order = Order::where($where)->first();
if(!$order){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'订单不存在');}
$data['order_status'] = 3;
$data['shipping_status'] = 2;
$data['refund_status'] = 0;
$data['is_comment'] = 0;
}
elseif($type == 4)
{
//判断订单是否存在或本人
$where['order_status'] = 3;
$where['refund_status'] = 0;
$order = Order::where($where)->first();
if(!$order){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'订单不存在');}
$data['refund_status'] = 1;
}
elseif($type == 5)
{
//判断订单是否存在或本人
$order = Order::where(array('order_status'=>3,'refund_status'=>0))->orWhere(array('order_status'=>1))->orWhere(array('order_status'=>2))->first();
if(!$order){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'订单不存在');}
$data['is_delete'] = 1;
}
elseif($type == 6)
{
//判断订单是否存在或本人
$where['order_status'] = 3;
$where['refund_status'] = 0;
$order = Order::where($where)->first();
if(!$order){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'订单不存在');}
$data['is_comment'] = 1;
}
//修改订单状态
if (isset($data))
{
Order::modify($where,$data);
}
return ReturnData::create(ReturnData::SUCCESS);
}
//商城支付宝app支付
public function orderAlipayApp(Request $request)
{

22
app/Http/Controllers/Api/UserController.php

@ -237,7 +237,7 @@ class UserController extends CommonController
$data['head_img'] = $request->input('head_img','');
$data['nickname'] = $request->input('nickname','');
$data['parent_id'] = 0;if($request->input('parent_id',null)!=null){$data['parent_id'] = $request->input('parent_id');}
$data['user_name'] = date('YmdHis').dechex(rand(1000,9999));
$data['user_name'] = date('YmdHis').dechex(date('His').rand(1000,9999));
$data['password'] = md5('123456');
if ($data['openid']=='')
@ -245,17 +245,15 @@ class UserController extends CommonController
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
if (model('User')->getOne(array('openid'=>$data['openid'])))
{
return $this->getLogic()->wxLogin(array('openid'=>$data['openid']));
}
//添加用户
$res = $this->getLogic()->wxRegister($data);
if($res['code'] != ReturnData::SUCCESS){return $res;}
//更新用户名user_name,微信登录没有用户名
model('User')->edit(array('user_name'=>'u'.$res['code']['data']['uid']),array('id'=>$res['code']['data']['uid']));
if (!model('User')->getOne(array('openid'=>$data['openid'])))
{
//添加用户
$res = $this->getLogic()->wxRegister($data);
if($res['code'] != ReturnData::SUCCESS){return $res;}
//更新用户名user_name,微信登录没有用户名
model('User')->edit(array('user_name'=>date('Ymd').'u'.$res['data']['uid']),array('id'=>$res['data']['uid']));
}
return $this->getLogic()->wxLogin(array('openid'=>$data['openid']));
}

55
app/Http/Controllers/Weixin/UserController.php

@ -4,6 +4,7 @@ namespace App\Http\Controllers\Weixin;
use App\Http\Controllers\Weixin\CommonController;
use Illuminate\Http\Request;
use App\Common\ReturnCode;
use App\Common\ReturnData;
use App\Common\WechatAuth;
use App\Common\Helper;
@ -27,7 +28,7 @@ class UserController extends CommonController
$res = curl_request($url,$postdata,'GET');
$data['user_info'] = $res['data'];
if($res['code'] != ReturnCode::SUCCESS_CODE){unset($_SESSION['weixin_user_info']);$this->error_jump('请先登录',route('weixin_login'));}
if($res['code'] != ReturnData::SUCCESS){unset($_SESSION['weixin_user_info']);$this->error_jump('请先登录',route('weixin_login'));}
return view('weixin.user.index', $data);
}
@ -509,7 +510,7 @@ class UserController extends CommonController
$url = env('APP_API_URL')."/user_goods_history_delete";
$res = curl_request($url,$postdata,'POST');
if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump(ReturnCode::FAIL);}
if($res['code'] != ReturnData::SUCCESS){$this->error_jump(ReturnCode::FAIL);}
$this->success_jump(ReturnCode::SUCCESS);
}
@ -523,7 +524,7 @@ class UserController extends CommonController
$url = env('APP_API_URL')."/user_goods_history_clear";
$res = curl_request($url,$postdata,'POST');
if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump(ReturnCode::FAIL);}
if($res['code'] != ReturnData::SUCCESS){$this->error_jump(ReturnCode::FAIL);}
$this->success_jump(ReturnCode::SUCCESS);
}
@ -531,43 +532,39 @@ class UserController extends CommonController
//微信网页授权登录
public function oauth(Request $request)
{
$wechat_auth = new WechatAuth(sysconfig('CMS_WX_APPID'),sysconfig('CMS_WX_APPSECRET'));
// 获取code码,用于和微信服务器申请token。 注:依据OAuth2.0要求,此处授权登录需要用户端操作
if(!isset($_GET['code']))
if (!isset($_SESSION['weixin_oauth']['userinfo']))
{
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$callback_url = $http_type . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; //回调地址,当前页面
//生成唯一随机串防CSRF攻击
$state = md5(uniqid(rand(), true));
$_SESSION['weixin_oauth']['state'] = $state; //存到SESSION
$authorize_url = $wechat_auth->get_authorize_url($callback_url, $state);
$wechat_auth = new WechatAuth(sysconfig('CMS_WX_APPID'),sysconfig('CMS_WX_APPSECRET'));
header("Location: $authorize_url");exit;
}
// 依据code码去获取openid和access_token,自己的后台服务器直接向微信服务器申请即可
if (isset($_GET['code']))
{
// 获取code码,用于和微信服务器申请token。 注:依据OAuth2.0要求,此处授权登录需要用户端操作
if(!isset($_GET['code']))
{
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$callback_url = $http_type . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; //回调地址,当前页面
//生成唯一随机串防CSRF攻击
$state = md5(uniqid(rand(), true));
$_SESSION['weixin_oauth']['state'] = $state; //存到SESSION
$authorize_url = $wechat_auth->get_authorize_url($callback_url, $state);
header("Location: $authorize_url");exit;
}
// 依据code码去获取openid和access_token,自己的后台服务器直接向微信服务器申请即可
$_SESSION['weixin_oauth']['code'] = $_GET['code'];
if($_GET['state'] != $_SESSION['weixin_oauth']['state'])
{
exit("您访问的页面不存在或已被删除!");
$this->error_jump('您访问的页面不存在或已被删除');
}
//得到 access_token 与 openid
$_SESSION['weixin_oauth']['token'] = $wechat_auth->get_access_token($_GET['code']);
}
// 依据申请到的access_token和openid,申请Userinfo信息。
if (isset($_SESSION['weixin_oauth']['token']))
{
// 依据申请到的access_token和openid,申请Userinfo信息。
$_SESSION['weixin_oauth']['userinfo'] = $wechat_auth->get_user_info($_SESSION['weixin_oauth']['token']['access_token'], $_SESSION['weixin_oauth']['token']['openid']);
}
$postdata = array(
'openid' => $_SESSION['weixin_oauth']['token']['openid'],
'openid' => $_SESSION['weixin_oauth']['userinfo']['openid'],
'unionid' => isset($_SESSION['weixin_oauth']['userinfo']['unionid']) ? $_SESSION['weixin_oauth']['userinfo']['unionid'] : '',
'nickname' => $_SESSION['weixin_oauth']['userinfo']['nickname'],
'sex' => $_SESSION['weixin_oauth']['userinfo']['sex'],
@ -578,11 +575,9 @@ class UserController extends CommonController
);
$url = env('APP_API_URL')."/wx_oauth_register";
$res = curl_request($url,$postdata,'POST');
if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump('系统错误');}
if($res['code'] != ReturnData::SUCCESS){$this->error_jump('系统错误');}
$_SESSION['weixin_user_info'] = $res['data'];
header('Location: '.route('weixin_user'));exit;
}
@ -617,7 +612,7 @@ class UserController extends CommonController
$url = env('APP_API_URL')."/wx_login";
$res = curl_request($url,$postdata,'POST');
if($res['code'] != ReturnCode::SUCCESS_CODE){$this->error_jump('登录失败');}
if($res['code'] != ReturnData::SUCCESS){$this->error_jump('登录失败');}
$_SESSION['weixin_user_info'] = $res['data'];

2
app/Http/Logic/UserMoneyLogic.php

@ -78,7 +78,7 @@ class UserMoneyLogic extends BaseLogic
/**
* 添加一条记录,并增加或减少用户余额,会操作用户余额表,谨慎使用
* @param int $data['user_id'] 排序
* @param int $data['user_id'] 用户id
* @param int $data['type'] 0增加,1减少
* @param float $data['money'] 金额
* @param string $data['des'] 描述

4
resources/views/weixin/cart/index.blade.php

@ -83,9 +83,9 @@
<img src="<?php echo env('APP_URL'); ?>/images/weixin/nothing.png">
<p>购物车暂无商品</p>
<a href="<?php echo route('weixin'); ?>">去逛逛</a>
</div>
</div><br><br>
</div>
<br><br>
<!--猜你喜欢-start-->
<div class="floor">
<div class="banner_headline">

4
resources/views/weixin/goods/goodsDetail.blade.php

@ -4,13 +4,13 @@
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/jquery.min.js"></script>
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/weixin/mobile.js"></script>
<meta name="keywords" content="关键词"><meta name="description" content="描述"></head><body style="background-color:#f1f1f1;">
<div class="classreturn loginsignup">
<!-- <div class="classreturn loginsignup">
<div class="ds-in-bl return"><a href="javascript:history.back(-1);"><img src="<?php echo env('APP_URL'); ?>/images/weixin/return.png" alt="返回"></a></div>
<div class="ds-in-bl tit center"><span>商品详情</span></div>
<div class="ds-in-bl nav_menu"><a href="javascript:void(0);"><img src="<?php echo env('APP_URL'); ?>/images/weixin/class1.png" alt="菜单"></a></div>
</div>
@include('weixin.common.headerNav')
@include('weixin.common.headerNav') -->
<!--商品详情-start-->
<div class="goods_detail">

1
routes/web.php

@ -210,7 +210,6 @@ Route::group(['prefix' => 'dataapi', 'namespace' => 'Api', 'middleware' => ['web
//订单
Route::post('/order_add', 'OrderController@orderAdd'); //生成订单
Route::post('/order_update', 'OrderController@orderUpdate'); //订单修改
Route::post('/order_status_update', 'OrderController@orderStatusUpdate'); //订单状态修改
Route::get('/order_list', 'OrderController@orderList'); //订单列表
Route::get('/order_detail', 'OrderController@orderDetail'); //订单详情
Route::post('/order_yue_pay', 'OrderController@orderYuepay'); //订单余额支付

Loading…
Cancel
Save