Withdraw.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Validate;
  7. /**
  8. *
  9. */
  10. class Withdraw extends Frontend
  11. {
  12. protected $layout = 'default';
  13. protected $noNeedLogin = [];
  14. protected $noNeedRight = ['*'];
  15. /**
  16. * 余额提现
  17. * @return string
  18. */
  19. public function withdraw()
  20. {
  21. $config = get_addon_config('withdraw');
  22. $this->view->assign('addonConfig', $config);
  23. $this->view->assign('title', __('Withdraw'));
  24. return $this->view->fetch();
  25. }
  26. /**
  27. * 余额日志
  28. * @return string
  29. */
  30. public function withdrawlog()
  31. {
  32. $withdrawloglist = \addons\withdraw\model\Withdraw::where(['user_id' => $this->auth->id])
  33. ->order('id desc')
  34. ->paginate(10);
  35. $this->view->assign('title', __('Withdraw log'));
  36. $this->view->assign('withdrawloglist', $withdrawloglist);
  37. return $this->view->fetch();
  38. }
  39. /**
  40. * 创建订单并发起支付请求
  41. * @throws \think\exception\DbException
  42. */
  43. public function submit()
  44. {
  45. $money = $this->request->request('money');
  46. $account = $this->request->request('account');
  47. $name = $this->request->request('name');
  48. $type = 'alipay';
  49. $token = $this->request->post('__token__');
  50. //验证Token
  51. if (!Validate::is($token, "token", ['__token__' => $token])) {
  52. $this->error("Token验证错误,请重试!", '', ['__token__' => $this->request->token()]);
  53. }
  54. //刷新Token
  55. $this->request->token();
  56. if ($money <= 0) {
  57. $this->error('提现金额不正确');
  58. }
  59. if ($money > $this->auth->money) {
  60. $this->error('提现金额超出可提现额度');
  61. }
  62. if (!$account) {
  63. $this->error("提现账户不能为空");
  64. }
  65. if (!$name) {
  66. $this->error("真实姓名不能为空");
  67. }
  68. if (!Validate::is($account, "email") && !Validate::is($account, "/^1\d{10}$/")) {
  69. $this->error("提现账户只能是手机号或Email");
  70. }
  71. $config = get_addon_config('withdraw');
  72. if (isset($config['minmoney']) && $money < $config['minmoney']) {
  73. $this->error('提现金额不能低于' . $config['minmoney'] . '元');
  74. }
  75. if ($config['monthlimit']) {
  76. $count = \addons\withdraw\model\Withdraw::where('user_id', $this->auth->id)->whereTime('createtime', 'month')->count();
  77. if ($count >= $config['monthlimit']) {
  78. $this->error("已达到本月最大可提现次数");
  79. }
  80. }
  81. Db::startTrans();
  82. try {
  83. $data = [
  84. 'orderid' => date("YmdHis") . sprintf("%08d", $this->auth->id) . mt_rand(1000, 9999),
  85. 'user_id' => $this->auth->id,
  86. 'money' => $money,
  87. 'type' => $type,
  88. 'account' => $account,
  89. 'name' => $name,
  90. ];
  91. \addons\withdraw\model\Withdraw::create($data);
  92. \app\common\model\User::money(-$money, $this->auth->id, "提现");
  93. Db::commit();
  94. } catch (Exception $e) {
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. }
  98. $this->success("提现申请成功!请等待后台审核!", url("withdraw/withdrawlog"));
  99. return;
  100. }
  101. /**
  102. * 企业支付通知和回调
  103. * @throws \think\exception\DbException
  104. */
  105. public function epay()
  106. {
  107. $type = $this->request->param('type');
  108. $paytype = $this->request->param('paytype');
  109. if ($type == 'notify') {
  110. $pay = \addons\epay\library\Service::checkNotify($paytype);
  111. if (!$pay) {
  112. echo '签名错误';
  113. return;
  114. }
  115. $data = $pay->verify();
  116. try {
  117. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  118. \addons\recharge\model\Order::settle($data['out_trade_no'], $payamount);
  119. } catch (Exception $e) {
  120. }
  121. echo $pay->success();
  122. } else {
  123. $pay = \addons\epay\library\Service::checkReturn($paytype);
  124. if (!$pay) {
  125. $this->error('签名错误');
  126. }
  127. //微信支付没有返回链接
  128. if ($pay === true) {
  129. $this->success("请返回网站查看支付状态!", "");
  130. }
  131. //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
  132. $this->success("恭喜你!充值成功!", url("user/index"));
  133. }
  134. return;
  135. }
  136. }