WithdrawLogic.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\basics\Logic;
  4. use app\common\enum\ClientEnum;
  5. use app\common\logic\AccountLogLogic;
  6. use app\common\model\{AccountLog, Withdraw, WithdrawApply};
  7. use app\common\model\user\User;
  8. use app\common\enum\WithdrawEnum;
  9. use app\common\server\ConfigServer;
  10. use app\common\server\WeChatServer;
  11. use think\facade\Db;
  12. use think\Exception;
  13. /***
  14. * Class WithdrawLogic 会员提现
  15. * @package app\api\logic
  16. */
  17. class WithdrawLogic extends Logic
  18. {
  19. /**
  20. * @notes 基础配置
  21. * @param $user_id
  22. * @return array
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @author suny
  27. * @date 2021/7/13 6:26 下午
  28. */
  29. public static function config($user_id)
  30. {
  31. $user = User::where('id', $user_id)->find();
  32. $config = [
  33. 'able_withdraw' => $user['earnings'] ? $user['earnings'] : 0,
  34. 'min_withdraw' => ConfigServer::get('withdraw', 'min_withdraw', 0),//最低提现金额;
  35. 'max_withdraw' => ConfigServer::get('withdraw', 'max_withdraw', 0),//最高提现金额;
  36. 'poundage_percent' => ConfigServer::get('withdraw', 'poundage', 0),//提现手续费;
  37. ];
  38. $types = ConfigServer::get('withdraw', 'type', [1]); //提现方式,若未设置默认为提现方式为钱包
  39. // 封装提现方式
  40. $config['type'] = [];
  41. if ($types) {
  42. foreach ($types as $value) {
  43. $config['type'][] = [
  44. 'name' => WithdrawEnum::getTypeDesc($value),
  45. 'value' => $value
  46. ];
  47. }
  48. }
  49. return $config;
  50. }
  51. /**
  52. * @notes 申请提现
  53. * @param $user_id
  54. * @param $post
  55. * @return int|string
  56. * @throws Exception
  57. * @throws \think\exception\PDOException
  58. * @author suny
  59. * @date 2021/7/13 6:26 下午
  60. */
  61. public static function apply($user_id, $post)
  62. {
  63. Db::startTrans();
  64. try {
  65. //提现手续费
  66. $poundage = 0;
  67. if ($post['type'] != WithdrawEnum::TYPE_BALANCE) {
  68. $poundage_config = ConfigServer::get('withdraw', 'poundage', 0);
  69. $poundage = $post['money'] * $poundage_config / 100;
  70. }
  71. $data = [
  72. 'sn' => createSn('withdraw_apply', 'sn'),
  73. 'batch_no' => createSn('withdraw_apply', 'batch_no','SJZZ'),
  74. 'user_id' => $user_id,
  75. 'type' => $post['type'],
  76. 'account' => $post['account'] ?? '',
  77. 'real_name' => $post['real_name'] ?? '',
  78. 'money' => $post['money'],
  79. 'left_money' => $post['money'] - $poundage,
  80. 'money_qr_code' => $post['money_qr_code'] ?? '',
  81. 'remark' => $post['remark'] ?? '',
  82. 'bank' => $post['bank'] ?? '',
  83. 'subbank' => $post['subbank'] ?? '',
  84. 'poundage' => $poundage,
  85. 'status' => 1, // 待提现
  86. 'create_time' => time(),
  87. 'client' => request()->user_info['client'],
  88. ];
  89. $withdraw_id = WithdrawApply::insertGetId($data);
  90. //提交申请后,扣减用户的佣金
  91. $user = User::find($user_id);
  92. $user->earnings = ['dec', $post['money']];
  93. $user->save();
  94. //增加佣金变动记录
  95. AccountLogLogic::AccountRecord(
  96. $user_id,
  97. $post['money'],
  98. 2,
  99. AccountLog::withdraw_dec_earnings,
  100. '',
  101. $withdraw_id,
  102. $data['sn']
  103. );
  104. Db::commit();
  105. return $withdraw_id;
  106. } catch (Exception $e) {
  107. Db::rollback();
  108. throw new Exception($e->getMessage());
  109. }
  110. }
  111. /**
  112. * @notes 提现记录
  113. * @param $user_id
  114. * @param $get
  115. * @param $page
  116. * @param $size
  117. * @return array
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. * @author suny
  122. * @date 2021/7/13 6:26 下午
  123. */
  124. public static function records($user_id, $get, $page, $size)
  125. {
  126. $count = WithdrawApply
  127. ::where(['user_id' => $user_id])
  128. ->count();
  129. $lists = WithdrawApply::where(['user_id' => $user_id])
  130. ->order('create_time desc')
  131. ->select();
  132. foreach ($lists as &$item) {
  133. $item['desc'] = '提现至' . WithdrawEnum::getTypeDesc($item['type']);
  134. $item['status_text'] = WithdrawEnum::getStatusDesc($item['status'], $item);
  135. $item['wechat_change_wait_receive'] = WithdrawEnum::wechatChangeIsWaitReceive($item);
  136. }
  137. $data = [
  138. 'list' => $lists,
  139. 'page' => $page,
  140. 'size' => $size,
  141. 'count' => $count,
  142. 'more' => is_more($count, $page, $size)
  143. ];
  144. return $data;
  145. }
  146. /**
  147. * @notes 提现详情
  148. * @param $id
  149. * @param $user_id
  150. * @return array
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\DbException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. * @author suny
  155. * @date 2021/7/13 6:26 下午
  156. */
  157. public static function info($id, $user_id)
  158. {
  159. $info = WithdrawApply::field('status, sn, create_time, type, money, left_money, poundage, pay_desc')
  160. ->where(['id' => $id, 'user_id' => $user_id])
  161. ->find()->toArray();
  162. if (!$info) {
  163. return [];
  164. }
  165. $info['pay_config'] = [
  166. 'mch_id' => WeChatServer::getPayConfig(ClientEnum::mnp)['mch_id'] ?? '',
  167. 'oa_appid' => ConfigServer::get('oa', 'app_id'),
  168. ];
  169. $info['wechat_change_wait_receive'] = WithdrawEnum::wechatChangeIsWaitReceive($info);
  170. $info['typeDesc'] = WithdrawEnum::getTypeDesc($info['type']);
  171. $info['statusDesc'] = WithdrawEnum::getStatusDesc($info['status'], $info);
  172. $info['pay_desc'] = json_decode($info['pay_desc'], true) ? : new \stdClass();
  173. return $info;
  174. }
  175. // 收款
  176. public static function receive($id, $user_id)
  177. {
  178. $withdraw = Db::name('withdraw_apply')->where(['id' => $id, 'user_id' => $user_id])->find();
  179. if (empty($withdraw['id'])){
  180. return '提现记录不存在';
  181. }
  182. if ($withdraw['status'] != WithdrawEnum::STATUS_ING || $withdraw['type'] != WithdrawEnum::TYPE_WECHAT_CHANGE){
  183. return '当前状态不能收款';
  184. }
  185. $update = [
  186. 'status' => 3,
  187. 'update_time' => time(),
  188. ];
  189. WithdrawApply::update($update, [ [ 'id', '=', $withdraw['id'] ] ]);
  190. return true;
  191. }
  192. }