AccountController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * 账户模块
  4. * @author wesmiler
  5. */
  6. namespace app\api\controller;
  7. use app\weixin\model\AccountLog;
  8. use app\weixin\model\HeartMeal;
  9. use app\weixin\model\Member;
  10. use app\weixin\model\Wechat;
  11. class AccountController extends BaseController
  12. {
  13. /**
  14. * 爱心充值
  15. * @throws \think\db\exception\DataNotFoundException
  16. * @throws \think\db\exception\ModelNotFoundException
  17. * @throws \think\exception\DbException
  18. */
  19. public function doRecharge(){
  20. $money = input('money', 0);
  21. $mealId = input('meal_id', 0);
  22. if(empty($money)){
  23. showJson(1004, 4001);
  24. }
  25. if(empty($mealId)){
  26. showJson(1004, 4006);
  27. }
  28. $mealInfo = HeartMeal::where(['id'=> $mealId,'status'=> 1])->find();
  29. $money = isset($mealInfo['price'])? $mealInfo['price'] : 0;
  30. if(empty($mealInfo)){
  31. showJson(1004, 4007);
  32. }
  33. if($mealInfo['heart']<=0 || $mealInfo['price']<=0){
  34. showJson(1004, 4008);
  35. }
  36. //
  37. if($mealInfo['limit_buy']>0){
  38. $rechargeNum = db('user_recharge_log')->where(['user_id'=> $this->userId,'type'=>1,'source_id'=> $mealId,'status'=>2])
  39. ->count('id');
  40. if($rechargeNum>= $mealInfo['limit_buy']){
  41. showJson(1004, lang(4009,['num'=>$mealInfo['limit_buy']]));
  42. }
  43. }
  44. $info = cmf_get_option('site_info');
  45. $minRechargeMoney = isset($info['min_recharge'])? floatval($info['min_recharge']) : 0;
  46. if($mealId<=0 && $minRechargeMoney && $money<$minRechargeMoney){
  47. showJson(1004, '最低充值金额为:'.$minRechargeMoney.'元');
  48. }
  49. $orderSn = makeTradeNo('RH', $this->userId);
  50. $memberInfo = Member::where(['id'=> $this->userId])->field('openid,balance')->find();
  51. $balance = isset($memberInfo['balance'])? moneyFormat($memberInfo['balance'], 2) : 0.00;
  52. $openid = isset($memberInfo['openid'])? $memberInfo['openid'] : '';
  53. $order = [
  54. 'order_sn'=> $orderSn,
  55. 'money'=> $money,
  56. 'user_id'=> $this->userId,
  57. 'source_id'=> $mealId,
  58. 'balance'=> $balance,
  59. 'remark'=> "余额充值:{$money}元",
  60. 'created_at'=> date('Y-m-d H:i:s')
  61. ];
  62. $orderId = db('user_recharge_log')->insertGetId($order);
  63. if($orderId){
  64. // 获取OPENID
  65. if (empty($openid)) {
  66. showJson(1004, 2010);
  67. }
  68. $order = [
  69. 'orderNo' => $orderSn,
  70. 'amount' => $money,
  71. 'openid' => $openid,
  72. 'body' => '余额充值订单支付',
  73. ];
  74. $params = Wechat::jsapiUnifiedorder($order,'recharge');
  75. saveLogCache('cache:rechargeOrderPay:unifiedorder_result', var_export($params, true));
  76. $code = isset($params['code']) ? $params['code'] : '';
  77. if ($code == 'error') {
  78. showJson(1004, $params['message']);
  79. }
  80. // 更新订单参数
  81. $prepayId = isset($params['prepay_id']) ? $params['prepay_id'] : '';
  82. unset($params['prepay_id']);
  83. showJson(1005, '充值提交成功', $params);
  84. }else{
  85. showJson(1004, '充值提交失败');
  86. }
  87. }
  88. /**
  89. * 获取账户变动记录
  90. */
  91. public function getAccountLogs(){
  92. $params = input();
  93. $type = input('lt',2);
  94. $pageSize = input('pageSize',20);
  95. $params['user_id'] = $this->userId;
  96. $dataList = AccountLog::getLogs($params, $pageSize);
  97. showJson(1005,1001, $dataList);
  98. }
  99. /**
  100. * 获取充值记录
  101. */
  102. public function getRechargeLog(){
  103. $params = input();
  104. $type = input('lt',2);
  105. $pageSize = input('pageSize',20);
  106. $params['user_id'] = $this->userId;
  107. $dataList = Member::getRechargeLog($params, $pageSize);
  108. showJson(1005,1001, $dataList);
  109. }
  110. /**
  111. * 获取提现记录
  112. */
  113. public function getWithdrawLog(){
  114. $params = input();
  115. $pageSize = input('pageSize',20);
  116. $params['type'] = 1;
  117. $params['change_type'] = 2;
  118. $params['user_id'] = $this->userId;
  119. $dataList = Member::getBalanceLog($params, $pageSize);
  120. showJson(1005,1001, $dataList);
  121. }
  122. /**
  123. * 获取收益记录
  124. */
  125. public function getIncomeLog(){
  126. $params = input();
  127. $pageSize = input('pageSize',20);
  128. $type = input('type',0);
  129. $params['type'] = $type && $type != '全部'? $type : 'income';
  130. $params['user_id'] = $this->userId;
  131. $dataList = Member::getBalanceLog($params, $pageSize);
  132. showJson(1005,1001, $dataList);
  133. }
  134. }
  135. ?>