You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. <?php
  2. namespace App\Http\Logic;
  3. use App\Common\ReturnData;
  4. use App\Http\Model\UserRecharge;
  5. use App\Http\Model\UserMoney;
  6. use App\Http\Requests\UserRechargeRequest;
  7. use Validator;
  8. class UserRechargeLogic extends BaseLogic
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. public function getModel()
  15. {
  16. return new UserRecharge();
  17. }
  18. public function getValidate($data, $scene_name)
  19. {
  20. //数据验证
  21. $validate = new UserRechargeRequest();
  22. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  23. }
  24. //列表
  25. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  26. {
  27. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  28. if($res['count'] > 0)
  29. {
  30. foreach($res['list'] as $k=>$v)
  31. {
  32. $res['list'][$k] = $this->getDataView($v);
  33. }
  34. }
  35. return $res;
  36. }
  37. //分页html
  38. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  39. {
  40. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  41. return $res;
  42. }
  43. //全部列表
  44. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  45. {
  46. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  47. /* if($res)
  48. {
  49. foreach($res as $k=>$v)
  50. {
  51. $res[$k] = $this->getDataView($v);
  52. }
  53. } */
  54. return $res;
  55. }
  56. //详情
  57. public function getOne($where = array(), $field = '*')
  58. {
  59. $res = $this->getModel()->getOne($where, $field);
  60. if(!$res){return false;}
  61. $res = $this->getDataView($res);
  62. return $res;
  63. }
  64. //添加
  65. public function add($data = array(), $type=0)
  66. {
  67. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  68. $data['recharge_sn'] = date('YmdHis').rand(1000,9999);
  69. $data['created_at'] = time();
  70. $validator = $this->getValidate($data, 'add');
  71. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  72. $res = $this->getModel()->add($data,$type);
  73. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  74. return ReturnData::create(ReturnData::FAIL);
  75. }
  76. //修改
  77. public function edit($data, $where = array())
  78. {
  79. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  80. $validator = $this->getValidate($data, 'edit');
  81. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  82. $res = $this->getModel()->edit($data,$where);
  83. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  84. return ReturnData::create(ReturnData::FAIL);
  85. }
  86. //删除
  87. public function del($where)
  88. {
  89. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  90. $validator = $this->getValidate($where,'del');
  91. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  92. $res = $this->getModel()->del($where);
  93. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  94. return ReturnData::create(ReturnData::FAIL);
  95. }
  96. /**
  97. * 数据获取器
  98. * @param array $data 要转化的数据
  99. * @return array
  100. */
  101. private function getDataView($data = array())
  102. {
  103. return getDataAttr($this->getModel(),$data);
  104. }
  105. /**
  106. * 充值成功之后修改记录信息,回调信息
  107. * @param int $data['pay_time'] 实际充值时间
  108. * @param int $data['pay_type'] 充值类型:1微信,2支付宝
  109. * @param float $data['pay_money'] 充值金额
  110. * @param string $data['trade_no'] 支付流水号
  111. * @return array
  112. */
  113. public function paySuccessChangeRechargeInfo($data, $where)
  114. {
  115. if(empty($where) || empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  116. $user_recharge = $this->getModel()->getOne($where);
  117. if(!$user_recharge){return false;}
  118. DB::beginTransaction();
  119. $data['updated_at'] = time();
  120. $data['status'] = UserRecharge::COMPLETE_PAY;
  121. $res = $this->getModel()->edit($data,$where);
  122. if($res)
  123. {
  124. //添加用户余额记录并增加用户余额
  125. $user_money_data['user_id'] = $user_recharge->user_id;
  126. $user_money_data['type'] = UserMoney::USER_MONEY_INCREMENT;
  127. $user_money_data['money'] = $data['pay_money'];
  128. $user_money_data['des'] = UserRecharge::USER_RECHARGE_DES;
  129. $user_money = logic('UserMoney')->add($user_money_data);
  130. if($user_money['code'] != ReturnData::SUCCESS){DB::rollBack();return false;}
  131. DB::commit();
  132. return true;
  133. }
  134. DB::rollBack();
  135. return false;
  136. }
  137. }