| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- /**
- * 账户模块
- * @author wesmiler
- */
- namespace app\api\controller;
- use app\weixin\model\AccountLog;
- use app\weixin\model\HeartMeal;
- use app\weixin\model\Member;
- use app\weixin\model\Wechat;
- class AccountController extends BaseController
- {
- /**
- * 爱心充值
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function doRecharge(){
- $money = input('money', 0);
- $mealId = input('meal_id', 0);
- if(empty($money)){
- showJson(1004, 4001);
- }
- if(empty($mealId)){
- showJson(1004, 4006);
- }
- $mealInfo = HeartMeal::where(['id'=> $mealId,'status'=> 1])->find();
- $money = isset($mealInfo['price'])? $mealInfo['price'] : 0;
- if(empty($mealInfo)){
- showJson(1004, 4007);
- }
- if($mealInfo['heart']<=0 || $mealInfo['price']<=0){
- showJson(1004, 4008);
- }
- //
- if($mealInfo['limit_buy']>0){
- $rechargeNum = db('user_recharge_log')->where(['user_id'=> $this->userId,'type'=>1,'source_id'=> $mealId,'status'=>2])
- ->count('id');
- if($rechargeNum>= $mealInfo['limit_buy']){
- showJson(1004, lang(4009,['num'=>$mealInfo['limit_buy']]));
- }
- }
- $info = cmf_get_option('site_info');
- $minRechargeMoney = isset($info['min_recharge'])? floatval($info['min_recharge']) : 0;
- if($mealId<=0 && $minRechargeMoney && $money<$minRechargeMoney){
- showJson(1004, '最低充值金额为:'.$minRechargeMoney.'元');
- }
-
- $orderSn = makeTradeNo('RH', $this->userId);
- $memberInfo = Member::where(['id'=> $this->userId])->field('openid,balance')->find();
- $balance = isset($memberInfo['balance'])? moneyFormat($memberInfo['balance'], 2) : 0.00;
- $openid = isset($memberInfo['openid'])? $memberInfo['openid'] : '';
- $order = [
- 'order_sn'=> $orderSn,
- 'money'=> $money,
- 'user_id'=> $this->userId,
- 'source_id'=> $mealId,
- 'balance'=> $balance,
- 'remark'=> "余额充值:{$money}元",
- 'created_at'=> date('Y-m-d H:i:s')
- ];
- $orderId = db('user_recharge_log')->insertGetId($order);
- if($orderId){
- // 获取OPENID
- if (empty($openid)) {
- showJson(1004, 2010);
- }
- $order = [
- 'orderNo' => $orderSn,
- 'amount' => $money,
- 'openid' => $openid,
- 'body' => '余额充值订单支付',
- ];
-
- $params = Wechat::jsapiUnifiedorder($order,'recharge');
- saveLogCache('cache:rechargeOrderPay:unifiedorder_result', var_export($params, true));
- $code = isset($params['code']) ? $params['code'] : '';
- if ($code == 'error') {
- showJson(1004, $params['message']);
- }
- // 更新订单参数
- $prepayId = isset($params['prepay_id']) ? $params['prepay_id'] : '';
- unset($params['prepay_id']);
- showJson(1005, '充值提交成功', $params);
- }else{
- showJson(1004, '充值提交失败');
- }
- }
- /**
- * 获取账户变动记录
- */
- public function getAccountLogs(){
- $params = input();
- $type = input('lt',2);
- $pageSize = input('pageSize',20);
- $params['user_id'] = $this->userId;
- $dataList = AccountLog::getLogs($params, $pageSize);
- showJson(1005,1001, $dataList);
- }
- /**
- * 获取充值记录
- */
- public function getRechargeLog(){
- $params = input();
- $type = input('lt',2);
- $pageSize = input('pageSize',20);
- $params['user_id'] = $this->userId;
- $dataList = Member::getRechargeLog($params, $pageSize);
- showJson(1005,1001, $dataList);
- }
- /**
- * 获取提现记录
- */
- public function getWithdrawLog(){
- $params = input();
- $pageSize = input('pageSize',20);
- $params['type'] = 1;
- $params['change_type'] = 2;
- $params['user_id'] = $this->userId;
- $dataList = Member::getBalanceLog($params, $pageSize);
- showJson(1005,1001, $dataList);
- }
- /**
- * 获取收益记录
- */
- public function getIncomeLog(){
- $params = input();
- $pageSize = input('pageSize',20);
- $type = input('type',0);
- $params['type'] = $type && $type != '全部'? $type : 'income';
- $params['user_id'] = $this->userId;
- $dataList = Member::getBalanceLog($params, $pageSize);
- showJson(1005,1001, $dataList);
- }
- }
- ?>
|