123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace app\service;
- use app\service\ConfServiceFacade;
- use app\service\api\UserServiceFacade;
- use laytp\traits\Error;
- use think\facade\Cache;
- use think\facade\Config;
- use think\facade\Request;
- use EasyWeChat\Factory;
- use think\facade\Db;
- class Money{
- use Error;
- /**
- * 初始化
- * @param $token
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function cash($post)
- {
- $model = new \app\model\Extract();
- $payPrice = 0; //扣除手续费后的实际到账金额
- $conf = ConfServiceFacade::groupGet('system.config');
- $loginUserInfo = UserServiceFacade::getUserInfo();
- $post['uid'] = $loginUserInfo['id'];
- $money = $loginUserInfo['now_money'];
- if($conf['userExtractRate'] > 0){
- $payPrice = $post['extract_price'] * (100- $conf['userExtractRate'])/100;
- } else {
- $payPrice = $post['extract_price'];
- }
- $post['rate'] = $conf['userExtractRate'];//服务手续费率
- $post['real_price'] = $payPrice;//实际到账金额
- $post['service_price'] = bcsub($post['extract_price'],$payPrice,2) ?? 0;;//服务费
- // 更新用户余额
- $balance = bcsub($money,$post['extract_price'],2) ?? 0;
- $post['balance'] = $balance;//用户余额
- // print_r($payPrice);
- //可提现余额
- if($payPrice > $money){
- $this->setError('可提现金额不足,此次提现金额超出可提现余额');
- return false;
- }
- if($post['extract_price'] < $conf['userExtractMinPrice']){
- $this->setError('提现金额不能小于' . $conf['userExtractMinPrice'] . '元');
- return false;
- }
- if($payPrice <= 0){
- $this->setError('提现金额不能小于或等于0元');
- return false;
- }
- Db::startTrans();
- try{
- $saveRes = $model->save($post);
- if (!$saveRes) throw new \Exception('保存基础信息失败');
- $post['id'] = $model->id;
- $balanceRes = \app\model\User::where(['id'=>$loginUserInfo['id']])->update(['now_money' => $balance]);
- if(!$balanceRes){
- $this->setError('提现金额不能小于或等于0元');
- return false;
- }
- Db::commit();
- return true;
- }catch (\Exception $e) {
- Db::rollback();
- $this->setError('数据库异常,操作失败');
- return false;
- }
- }
- }
|