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.

179 lines
5.4 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
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. if($res->count() > 0)
  42. {
  43. foreach($res as $k=>$v)
  44. {
  45. $res[$k] = $this->getDataView($v);
  46. }
  47. }
  48. return $res;
  49. }
  50. //全部列表
  51. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  52. {
  53. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  54. if($res)
  55. {
  56. foreach($res as $k=>$v)
  57. {
  58. $res[$k] = $this->getDataView($v);
  59. }
  60. }
  61. return $res;
  62. }
  63. //详情
  64. public function getOne($where = array(), $field = '*')
  65. {
  66. $res = $this->getModel()->getOne($where, $field);
  67. if(!$res){return false;}
  68. $res = $this->getDataView($res);
  69. return $res;
  70. }
  71. //添加
  72. public function add($data = array(), $type=0)
  73. {
  74. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  75. $data['recharge_sn'] = date('YmdHis').rand(1000,9999);
  76. $data['created_at'] = $data['updated_at'] = time();
  77. $validator = $this->getValidate($data, 'add');
  78. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  79. $res = $this->getModel()->add($data,$type);
  80. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  81. return ReturnData::create(ReturnData::FAIL);
  82. }
  83. //修改
  84. public function edit($data, $where = array())
  85. {
  86. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  87. $validator = $this->getValidate($data, 'edit');
  88. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  89. $data['updated_at'] = time();
  90. $res = $this->getModel()->edit($data,$where);
  91. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  92. return ReturnData::create(ReturnData::FAIL);
  93. }
  94. //删除
  95. public function del($where)
  96. {
  97. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  98. $validator = $this->getValidate($where,'del');
  99. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  100. $res = $this->getModel()->del($where);
  101. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  102. return ReturnData::create(ReturnData::FAIL);
  103. }
  104. /**
  105. * 数据获取器
  106. * @param array $data 要转化的数据
  107. * @return array
  108. */
  109. private function getDataView($data = array())
  110. {
  111. return getDataAttr($this->getModel(),$data);
  112. }
  113. /**
  114. * 充值成功之后修改记录信息,回调信息
  115. * @param int $data['pay_time'] 实际充值时间
  116. * @param int $data['pay_type'] 充值类型:1微信,2支付宝
  117. * @param float $data['pay_money'] 充值金额
  118. * @param string $data['trade_no'] 支付流水号
  119. * @return array
  120. */
  121. public function paySuccessChangeRechargeInfo($data, $where)
  122. {
  123. if(empty($where) || empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  124. $user_recharge = $this->getModel()->getOne($where);
  125. if(!$user_recharge){return false;}
  126. DB::beginTransaction();
  127. $data['updated_at'] = time();
  128. $data['status'] = UserRecharge::COMPLETE_PAY;
  129. $res = $this->getModel()->edit($data,$where);
  130. if($res)
  131. {
  132. //添加用户余额记录并增加用户余额
  133. $user_money_data['user_id'] = $user_recharge->user_id;
  134. $user_money_data['type'] = UserMoney::USER_MONEY_INCREMENT;
  135. $user_money_data['money'] = $data['pay_money'];
  136. $user_money_data['des'] = UserRecharge::USER_RECHARGE_DES;
  137. $user_money = logic('UserMoney')->add($user_money_data);
  138. if($user_money['code'] != ReturnData::SUCCESS){DB::rollBack();return false;}
  139. DB::commit();
  140. return true;
  141. }
  142. DB::rollBack();
  143. return false;
  144. }
  145. }