123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- /**
- * 消息收费图片订单
- */
- class Orderimage extends Api
- {
- // 无需登录的接口,*表示全部
- protected $noNeedLogin = [];
- // 无需鉴权的接口,*表示全部
- protected $noNeedRight = ['*'];
- public function create()
- {
- $price = input('price',0);
- if($price <= 0){
- $this->error('价格不能小于零');
- }
- $message_id = input('message_id','','trim');
- $check = Db::name('order_image')->where('message_id',$message_id)->find();
- if($check){
- $this->error('消息重复');
- }
- $data = [
- 'user_id' => $this->auth->id,
- 'to_user_id' => input('to_user_id',0),
- 'message_id' => $message_id,
- 'image' => input('image',''),
- 'price' => $price,
- 'status' => 0,
- 'createtime' => time(),
- ];
- Db::name('order_image')->insertGetId($data);
- $this->success(1);
- }
- public function pay(){
- $message_id = input('message_id','','trim');
- $order = Db::name('order_image')->where('message_id',$message_id)->where('to_user_id',$this->auth->id)->find();
- if(!$order){
- $this->error('没有找到付费信息');
- }
- if($order['status'] != 0){
- $this->error('已经付费过了');
- }
- Db::startTrans();
- if($order['price'] > 0){
- //付钱
- $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,$order['user_id'],'gold',-$order['price'],111,'解锁付费图片','order_image',$order['id']);
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- //收益
- $money_to_gold = config('site.money_to_gold') ?: 10;
- $money = bcdiv($order['price'],$money_to_gold,2);
- //平台手续费
- $platrate = config('site.order_image_platrate');
- $plat_money = bcdiv(bcmul($money,$platrate,2),100,2);
- //减去手续费,得收益金额
- $shouyi = bcsub($money,$plat_money,2);
- $wallet_rs = model('wallet')->lockChangeAccountRemain($order['user_id'],$this->auth->id,'money',$shouyi,112,'付费图片收益','order_image',$order['id'],2);
- if($wallet_rs['status'] === false){
- Db::rollback();
- $this->error($wallet_rs['msg']);
- }
- }
- $rs = Db::name('order_image')->where('id',$order['id'])->update([
- 'status' => 1,
- 'paytime' => time(),
- 'platrate' => $platrate,
- 'platmoney' => $plat_money,
- 'shouyi' => $shouyi,
- ]);
- if($rs === false){
- Db::rollback();
- $this->error('付款失败');
- }
- Db::commit();
- $this->success();
- }
- }
|