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.

203 lines
6.9 KiB

6 years ago
4 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 Illuminate\Support\Facades\DB;
  4. use App\Common\ReturnData;
  5. use App\Http\Model\UserMoney;
  6. use App\Http\Model\UserWithdraw;
  7. use App\Http\Requests\UserWithdrawRequest;
  8. use Validator;
  9. class UserWithdrawLogic extends BaseLogic
  10. {
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. }
  15. public function getModel()
  16. {
  17. return new UserWithdraw();
  18. }
  19. public function getValidate($data, $scene_name)
  20. {
  21. //数据验证
  22. $validate = new UserWithdrawRequest();
  23. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  24. }
  25. //列表
  26. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  27. {
  28. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  29. if($res['count'] > 0)
  30. {
  31. foreach($res['list'] as $k=>$v)
  32. {
  33. $res['list'][$k] = $this->getDataView($v);
  34. }
  35. }
  36. return $res;
  37. }
  38. //分页html
  39. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  40. {
  41. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  42. if($res->count() > 0)
  43. {
  44. foreach($res as $k=>$v)
  45. {
  46. $res[$k] = $this->getDataView($v);
  47. $res[$k]->user = model('User')->getOne(['id'=>$v->user_id]);
  48. }
  49. }
  50. return $res;
  51. }
  52. //全部列表
  53. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  54. {
  55. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  56. if($res)
  57. {
  58. foreach($res as $k=>$v)
  59. {
  60. $res[$k] = $this->getDataView($v);
  61. }
  62. }
  63. return $res;
  64. }
  65. //详情
  66. public function getOne($where = array(), $field = '*')
  67. {
  68. $res = $this->getModel()->getOne($where, $field);
  69. if(!$res){return false;}
  70. $res = $this->getDataView($res);
  71. return $res;
  72. }
  73. //添加
  74. public function add($data = array(), $type=0)
  75. {
  76. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  77. $validator = $this->getValidate($data, 'add');
  78. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  79. if(isset($data['pay_password']) && !empty($data['pay_password'])){}else{return ReturnData::create(ReturnData::PARAMS_ERROR,null,'请输入支付密码');}
  80. $user = model('User')->getOne(array('id'=>$data['user_id'],'pay_password'=>$data['pay_password']));
  81. if(!$user){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'支付密码错误');}
  82. unset($data['pay_password']);
  83. $min_withdraw_money = sysconfig('CMS_MIN_WITHDRAWAL_MONEY'); //最低可提现金额
  84. if($user->money<$data['money']){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'余额不足');}
  85. if($user->money<$min_withdraw_money){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'用户金额小于最小提现金额');}
  86. if($data['money']<$min_withdraw_money){return ReturnData::create(ReturnData::PARAMS_ERROR,null,'提现金额不得小于最小提现金额');}
  87. DB::beginTransaction();
  88. $data['add_time'] = time();
  89. $res = $this->getModel()->add($data,$type);
  90. if($res)
  91. {
  92. //添加用户余额记录并扣除用户余额
  93. $user_money_data['user_id'] = $data['user_id'];
  94. $user_money_data['type'] = UserMoney::USER_MONEY_DECREMENT;
  95. $user_money_data['money'] = $data['money'];
  96. $user_money_data['des'] = UserWithdraw::USER_WITHDRAW_DES;
  97. $user_money = logic('UserMoney')->add($user_money_data);
  98. if($user_money['code'] != ReturnData::SUCCESS){DB::rollBack();return false;}
  99. DB::commit();
  100. return ReturnData::create(ReturnData::SUCCESS,$res);
  101. }
  102. DB::rollBack();
  103. return ReturnData::create(ReturnData::FAIL);
  104. }
  105. //修改
  106. public function edit($data, $where = array())
  107. {
  108. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  109. $validator = $this->getValidate($data, 'edit');
  110. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  111. $res = $this->getModel()->edit($data,$where);
  112. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  113. return ReturnData::create(ReturnData::FAIL);
  114. }
  115. //删除
  116. public function del($where)
  117. {
  118. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  119. $validator = $this->getValidate($where,'del');
  120. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  121. $res = $this->getModel()->edit(['delete_time'=>time()],$where);
  122. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  123. return ReturnData::create(ReturnData::FAIL);
  124. }
  125. /**
  126. * 数据获取器
  127. * @param array $data 要转化的数据
  128. * @return array
  129. */
  130. private function getDataView($data = array())
  131. {
  132. return getDataAttr($this->getModel(),$data);
  133. }
  134. /**
  135. * 取消/拒绝提现
  136. * @param int $where['id'] 提现id
  137. * @param int $data['status'] status=3取消或status=4拒绝
  138. * @param string $data['re_note'] 理由,选填
  139. * @return array
  140. */
  141. public function userWithdrawSuccessConfirm($data, $where)
  142. {
  143. if(empty($where) || empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  144. if($data['status']!=3 || $data['status']!=4){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  145. $user_withdraw = $this->getModel()->getOne($where);
  146. if(!$user_withdraw){return false;}
  147. DB::beginTransaction();
  148. $data['updated_at'] = time();
  149. $res = $this->getModel()->edit($data,$where);
  150. if($res)
  151. {
  152. //添加用户余额记录并增加用户余额
  153. $user_money_data['user_id'] = $user_withdraw->user_id;
  154. $user_money_data['type'] = UserMoney::USER_MONEY_INCREMENT;
  155. $user_money_data['money'] = $user_withdraw->money;
  156. $user_money_data['des'] = '提现退回';
  157. $user_money = logic('UserMoney')->add($user_money_data);
  158. if($user_money['code'] != ReturnData::SUCCESS){DB::rollBack();return false;}
  159. DB::commit();
  160. return true;
  161. }
  162. DB::rollBack();
  163. return false;
  164. }
  165. }