Money.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\service;
  3. use app\service\ConfServiceFacade;
  4. use app\service\api\UserServiceFacade;
  5. use laytp\traits\Error;
  6. use think\facade\Cache;
  7. use think\facade\Config;
  8. use think\facade\Request;
  9. use EasyWeChat\Factory;
  10. use think\facade\Db;
  11. class Money{
  12. use Error;
  13. /**
  14. * 初始化
  15. * @param $token
  16. * @return bool
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\DbException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. */
  21. public function cash($post)
  22. {
  23. $model = new \app\model\Extract();
  24. $payPrice = 0; //扣除手续费后的实际到账金额
  25. $conf = ConfServiceFacade::groupGet('system.config');
  26. $loginUserInfo = UserServiceFacade::getUserInfo();
  27. $post['uid'] = $loginUserInfo['id'];
  28. $money = $loginUserInfo['now_money'];
  29. if($conf['userExtractRate'] > 0){
  30. $payPrice = $post['extract_price'] * (100- $conf['userExtractRate'])/100;
  31. } else {
  32. $payPrice = $post['extract_price'];
  33. }
  34. $post['rate'] = $conf['userExtractRate'];//服务手续费率
  35. $post['real_price'] = $payPrice;//实际到账金额
  36. $post['service_price'] = bcsub($post['extract_price'],$payPrice,2) ?? 0;;//服务费
  37. // 更新用户余额
  38. $balance = bcsub($money,$post['extract_price'],2) ?? 0;
  39. $post['balance'] = $balance;//用户余额
  40. // print_r($payPrice);
  41. //可提现余额
  42. if($payPrice > $money){
  43. $this->setError('可提现金额不足,此次提现金额超出可提现余额');
  44. return false;
  45. }
  46. if($post['extract_price'] < $conf['userExtractMinPrice']){
  47. $this->setError('提现金额不能小于' . $conf['userExtractMinPrice'] . '元');
  48. return false;
  49. }
  50. if($payPrice <= 0){
  51. $this->setError('提现金额不能小于或等于0元');
  52. return false;
  53. }
  54. Db::startTrans();
  55. try{
  56. $saveRes = $model->save($post);
  57. if (!$saveRes) throw new \Exception('保存基础信息失败');
  58. $post['id'] = $model->id;
  59. $balanceRes = \app\model\User::where(['id'=>$loginUserInfo['id']])->update(['now_money' => $balance]);
  60. if(!$balanceRes){
  61. $this->setError('提现金额不能小于或等于0元');
  62. return false;
  63. }
  64. Db::commit();
  65. return true;
  66. }catch (\Exception $e) {
  67. Db::rollback();
  68. $this->setError('数据库异常,操作失败');
  69. return false;
  70. }
  71. }
  72. }