Browse Source

充值

master
林一峰 7 years ago
parent
commit
ac1e506670
  1. 26
      app/Http/Controllers/Api/UserRechargeController.php
  2. 117
      app/Http/Controllers/Api/WechatAuthController.php
  3. 14
      app/Http/Controllers/Weixin/UserController.php
  4. 1
      app/Http/Model/UserRecharge.php
  5. 9
      resources/views/weixin/user/userRecharge.blade.php
  6. 91
      resources/views/weixin/user/userRechargeTwo.blade.php
  7. 8
      routes/web.php

26
app/Http/Controllers/Api/UserRechargeController.php

@ -33,16 +33,36 @@ class UserRechargeController extends CommonController
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//获取一条用户充值
public function userRechargeDetail(Request $request)
{
//参数
$data['id'] = $request->input('id', '');
if($data['id']=='')
{
return ReturnData::create(ReturnData::PARAMS_ERROR);
}
$data['user_id'] = Token::$uid;
$res = UserRecharge::getOne($data);
if($res === false)
{
return ReturnData::create(ReturnData::SYSTEM_FAIL);
}
return ReturnData::create(ReturnData::SUCCESS,$res);
}
//添加充值记录
public function userRechargeAdd(Request $request)
{
//参数
$data['money'] = $request->input('money','');
if($request->input('status', '') != ''){$data['status'] = $request->input('status');}
if($request->input('pay_type', '') != ''){$data['pay_type'] = $request->input('pay_type');}
$data['status'] = UserRecharge::UN_PAY; //0未处理,1已完成
$data['pay_type'] = $request->input('pay_type',''); //充值类型:1微信,2支付宝
$data['user_id'] = Token::$uid;
if($data['money']=='')
if($data['money']=='' || $data['pay_type']=='')
{
return ReturnData::create(ReturnData::PARAMS_ERROR);
}

117
app/Http/Controllers/Api/WechatAuthController.php

@ -1,117 +0,0 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\CommonController;
//微信auth2.0授权
class WechatAuthController extends CommonController
{
public $appid = 'wxba09d9f0fed4b84b'; //微信APPID,公众平台获取
public $appsecret = '332c2b1fc1eb282c0136b73723db4237'; //微信APPSECREC,公众平台获取
public $redirect_uri = "http://www.你的域名.cn/项目目录/index.php?m=分组&c=控制器&a=方法"; //微信回调地址,要跟公众平台的配置域名相同
public $code;
public $openid;
public $state = 123; //重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
public function auth(Request $request)
{
//如果$_SESSION中没有openid,说明用户刚刚登陆,就执行getCode、getOpenId、getUserInfo获取他的信息
if (!isset($_SESSION['openid']))
{
$this->code = $this->getCode();
$this->access_token = $this->getOpenId();
$userInfo = $this->getUserInfo();
//把用户的微信信息存到数据库中
/* if ($userInfo)
{
$ins = M('Wechat_user_info');
$map['openid'] = $userInfo['openid'];
$result = $ins->where($map)->find(); //根据OPENID查找数据库中是否有这个用户,如果没有就写数据库。继承该类的其他类,用户都写入了数据库中。
if (!$result)
{
$ins->add($userInfo);
}
$_SESSION['openid'] = $userInfo['openid']; //写到$_SESSION中。微信缓存很坑爹,调试时请及时清除缓存再试。
} */
}
}
/**
* @explain
* 获取code,用于获取openid和access_token
* @remark
* code只能使用一次,当获取到之后code失效,再次获取需要重新进入
* 不会弹出授权页面,适用于关注公众号后自定义菜单跳转等,如果不关注,那么只能获取openid
**/
public function getCode()
{
if (isset($_REQUEST["code"]))
{
return $_REQUEST["code"];
}
else
{
$str = "location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=" . $this->redirect_uri . "&response_type=code&scope=snsapi_userinfo&state=". $this->state ."#wechat_redirect";
header($str);
exit;
}
}
/**
* @explain
* 用于获取用户openid
**/
public function getOpenId()
{
$access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecret . "&code=" . $this->code . "&grant_type=authorization_code";
$access_token_json = $this->https_request($access_token_url);
$access_token_array = json_decode($access_token_json, TRUE);
return $access_token_array;
}
/**
* @explain
* 通过code获取用户openid以及用户的微信号信息
* @return
* @remark
* 获取到用户的openid之后可以判断用户是否有数据,可以直接跳过获取access_token,也可以继续获取access_token
* access_token每日获取次数是有限制的,access_token有时间限制,可以存储到数据库7200s. 7200s后access_token失效
**/
public function getUserInfo()
{
$userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$this->access_token['access_token'] ."&openid=" . $this->access_token['openid']."&lang=zh_CN";
$userinfo_json = $this->https_request($userinfo_url);
$userinfo_array = json_decode($userinfo_json, TRUE);
return $userinfo_array;
}
/**
* @explain
* 发送http请求,并返回数据
**/
public function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data))
{
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}

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

@ -62,6 +62,20 @@ class UserController extends CommonController
return view('weixin.user.userRecharge');
}
//用户充值第二步,支付
public function userRechargeTwo($id)
{
$postdata = array(
'id' => $id,
'access_token' => $_SESSION['weixin_user_info']['access_token']
);
$url = env('APP_API_URL')."/user_recharge_detail";
$res = curl_request($url,$postdata,'GET');
$data['post'] = $res['data'];
return view('weixin.user.userRechargeTwo', $data);
}
//余额明细
public function userMoneyList(Request $request)
{

1
app/Http/Model/UserRecharge.php

@ -16,6 +16,7 @@ class UserRecharge extends BaseModel
*/
protected $guarded = array();
const UN_PAY = 0;
const COMPLETE_PAY = 1;
//获取列表

9
resources/views/weixin/user/userRecharge.blade.php

@ -19,6 +19,7 @@
<div class="floor account">
<div class="icon"><i class="fa fa-google-wallet"></i></div><br>
<div class="tit"><b style="color:#f23030;">1、填写金额</b> > 2、确认并支付 > 3、完成</div>
<input type="hidden" name="pay_type" id="pay_type" value="1">
<br>
<div style="margin:10px;"><input name="money" min="10" max="10000" type="text" id="money" placeholder="充值金额(元)、整数" style="width:100%;text-align:center;border:1px solid #bfbfbf;color:#999;box-sizing:border-box;" class="bottoma"></div>
<a style="margin:0 10px 10px 10px;background-color:#1aad19;text-align:center;color:#fff;border:1px solid #179e16;" class="bottoma" href="javascript:chongzhi();">开始充值</a>
@ -62,8 +63,10 @@ function chongzhi()
content: '确定要充值吗?'
,btn: ['确定', '取消']
,yes: function(){
var url = '<?php echo env('APP_API_URL')."/user_address_delete"; ?>';
$.post(url,{access_token:'<?php echo $_SESSION['weixin_user_info']['access_token']; ?>',id:id},function(res)
var url = '<?php echo env('APP_API_URL')."/user_recharge_add"; ?>';
var pay_type = $('#pay_type').val();
$.post(url,{access_token:'<?php echo $_SESSION['weixin_user_info']['access_token']; ?>',money:money,pay_type:pay_type},function(res)
{
//提示
layer.open({
@ -74,7 +77,7 @@ function chongzhi()
if(res.code==0)
{
location.reload();
location.href = '<?php echo substr(route('weixin_user_recharge_two',array('id'=>1)), 0, -1); ?>' + res.data;
}
else
{

91
resources/views/weixin/user/userRechargeTwo.blade.php

@ -0,0 +1,91 @@
<!DOCTYPE html><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>充值-支付</title><meta name="keywords" content="关键词"><meta name="description" content="描述"><meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport">
<link href="<?php echo env('APP_URL'); ?>/css/weixin/style.css" type="text/css" rel="stylesheet">
<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>
<link href="<?php echo env('APP_URL'); ?>/css/font-awesome.min.css" type="text/css" rel="stylesheet"></head><body>
<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>
<style>
.account{text-align:center;margin-top:30px;}
.account .icon{color:#FFCC00;font-size:100px;}
.account .tit{color:#000;font-size:18px;}
.bottoma{display:block;font-size:18px;padding:10px;border-radius:2px;}
</style>
<div class="floor account">
<div class="icon"><i class="fa fa-google-wallet"></i></div>
<div style="margin:10px;padding:10px;text-align:left;">
订单已于 <span style="color:#390;"><?php echo $post['created_at']; ?></span> 提交成功,请您尽快付款!<br>
订单号:<?php echo $post['id']; ?><br>
应付金额:<span style="color:#ff5500;font-size:18px;"><?php echo $post['money']; ?></span> 元<br><br>
请您在提交订单后30分钟内完成支付,否则订单会自动取消。<br><br>
</div>
<a style="margin:0 10px 10px 10px;background-color:#1aad19;text-align:center;color:#fff;border:1px solid #179e16;" class="bottoma" href="javascript:chongzhi();">去支付</a>
</div>
<script type="text/javascript" src="<?php echo env('APP_URL'); ?>/js/layer/mobile/layer.js"></script>
<script>
function chongzhi()
{
var money = $('#money').val();
var re = /^[0-9]+$/; //判断字符串是否为数字
if(money == '')
{
//提示
layer.open({
content: '请输入充值金额'
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
return false;
}
if(!re.test(money))
{
//提示
layer.open({
content: '金额格式不正确'
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
return false;
}
//询问框
layer.open({
content: '确定要充值吗?'
,btn: ['确定', '取消']
,yes: function(){
var url = '<?php echo env('APP_API_URL')."/user_recharge_add"; ?>';
var pay_type = $('#pay_type').val();
$.post(url,{access_token:'<?php echo $_SESSION['weixin_user_info']['access_token']; ?>',money:money,pay_type:pay_type},function(res)
{
//提示
layer.open({
content: res.msg
,skin: 'msg'
,time: 2 //2秒后自动关闭
});
if(res.code==0)
{
location.href = '<?php echo substr(route('weixin_user_recharge_two',array('id'=>1)), 0, -1); ?>' + res.data;
}
else
{
}
},'json');
}
});
}
</script>
</body></html>

8
routes/web.php

@ -84,6 +84,7 @@ Route::group(['prefix' => 'weixin', 'namespace' => 'Weixin', 'middleware' => ['w
Route::get('/user_money_list', 'UserController@userMoneyList')->name('weixin_user_money_list');
Route::get('/user_point_list', 'UserController@userPointList')->name('weixin_user_point_list');
Route::get('/user_recharge', 'UserController@userRecharge')->name('weixin_user_recharge');
Route::get('/user_recharge_two/{id}', 'UserController@userRechargeTwo')->name('weixin_user_recharge_two');
//优惠券、红包
Route::get('/user_bonus_list', 'UserController@userBonusList')->name('weixin_user_bonus_list');
//浏览记录
@ -143,10 +144,11 @@ Route::group(['prefix' => 'dataapi', 'namespace' => 'Api', 'middleware' => ['web
Route::post('/user_password_update', 'UserController@userPasswordUpdate'); //修改用户密码、支付密码
Route::get('/user_list', 'UserController@userList'); //用户列表
//用户充值
Route::post('/user_recharge_add', 'UserMoneyController@userRechargeAdd'); //添加余额明细
Route::post('/user_recharge_add', 'UserRechargeController@userRechargeAdd');
Route::get('/user_recharge_detail', 'UserRechargeController@userRechargeDetail');
//用户余额(钱包)
Route::get('/user_money_list', 'UserMoneyController@userMoneyList'); //用户余额明细
Route::post('/user_money_add', 'UserMoneyController@userMoneyAdd'); //添加余额明细
Route::get('/user_money_list', 'UserMoneyController@userMoneyList');
Route::post('/user_money_add', 'UserMoneyController@userMoneyAdd');
//浏览记录
Route::get('/user_goods_history_list', 'UserGoodsHistoryController@userGoodsHistoryList'); //我的足迹列表
Route::post('/user_goods_history_delete', 'UserGoodsHistoryController@userGoodsHistoryDelete'); //我的足迹删除一条

Loading…
Cancel
Save