123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use \think\Db;
- /*
- * 收益兑换金币
- * */
- class Exchange extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- public function _initialize()
- {
- parent::_initialize();
- }
- //兑换配置
- public function config(){
- $rs = Db::name('exchange')->order('id asc')->select();
- if ($rs) {
- foreach ($rs as &$v) {
- $v['money_to_gold'] = config('site.money_to_gold');
- }
- $arr = [
- 'id' => -1,
- 'money' => 0,
- 'gold' => 0,
- 'money_to_gold' => config('site.money_to_gold')
- ];
- array_push($rs, $arr);
- }
- $result = [
- 'config' => $rs,
- 'money' => model('wallet')->getWallet($this->auth->id,'money'),
- ];
- $this->success('success',$result);
- }
- //兑换金币
- public function exchangegold() {
- $id = input('id', 0, 'intval');
- $freemoney = input_post('free_money', 0, 'intval'); //自定义
- $uid = $this->auth->id;
- if(!$id && !$freemoney){
- $this->error('请选择或填写兑换金额');
- }
- if ($id) {
- //赋值money
- $exchange = Db::name('exchange')->where('id', $id)->find();
- if(!$exchange){
- $this->error('请选择或填写兑换金额');
- }
- $money = $exchange ? $exchange['money'] : 0;
- $gold = $exchange ? $exchange['gold'] : 0;
- }
- if ($freemoney) {
- $money = $freemoney;
- $bili = config('site.money_to_gold');
- $gold = bcmul($money,$bili,0);
- }
- if($money<=0) {
- $this->error('收益金额必须大于0');
- }
- if($money > 10000){
- $this->error('收益金额太大,请分批进行');
- }
- //验证余额
- $user_info = model('wallet')->getWallet($this->auth->id);
- if ($user_info['money'] < $money) {
- $this->success('您的余额不足', ['code' => 2]);
- }
- //开启事务
- Db::startTrans();
- //扣费
- $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'money',-$money,18,'兑换金币');
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- //赠送金币
- $wallet_rs = model('wallet')->lockChangeAccountRemain($uid,0,'gold',$gold,19,'获得兑换金币');
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- Db::commit();
- $this->success('兑换成功');
- }
- }
|