Recharge.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\api\controller\service;
  3. use app\common\controller\Api;
  4. use fast\Random;
  5. use think\Db;
  6. /**
  7. * 首页接口
  8. */
  9. class Recharge extends Api
  10. {
  11. protected $noNeedLogin = [''];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 用户充值余额
  15. * @return void
  16. */
  17. public function recharge11()
  18. {
  19. $uid = $this->auth->id;
  20. $money = model('app\api\model\service\User')->where('id',$uid)->value('money');
  21. $type = input('type','');
  22. if($type == 'recharge')
  23. {
  24. if (!$this->request->isPost()) {
  25. $this->error('请求方式异常');
  26. }
  27. $price = sprintf("%.2f",input('price',''));
  28. $price <=0 && $this->error('充值金额错误');
  29. $orderId = 'Recharge'.Random::alnum(5).'-'.$uid.'-'.time();
  30. $paytype = input('paytype/d',0);
  31. $re = '';
  32. Db::startTrans();
  33. try{
  34. $recharge = new \app\api\model\service\Recharge(['user_id'=>$uid,'price'=>$price,'orderId'=>$orderId,'paytype'=>$paytype]);
  35. $recharge->allowField(true)->save();
  36. $re = \addons\service\library\Pay::payOrder(['amount'=>$price,'orderid'=>$orderId,'title'=>'充值余额'],$paytype,$uid,0);
  37. Db::commit();
  38. } catch (Exception $e) {
  39. Db::rollback();
  40. $this->error('支付拉起失败',$e->getMessage());
  41. }
  42. $this->success('支付信息返回成功',$re);
  43. }
  44. $this->success('信息返回成功',$money);
  45. }
  46. public function recharge()
  47. {
  48. $uid = $this->auth->id;
  49. $money = model('app\api\model\service\User')->where('id',$uid)->value('money');
  50. $packageList = model('app\api\model\service\RechargePackage')->where('state',1)->field('id,price,gift_price')->order('price desc')->select();
  51. $type = input('type','');
  52. if($type == 'recharge')
  53. {
  54. if (!$this->request->isPost()) {
  55. $this->error('请求方式异常');
  56. }
  57. $packageId = input('recharge_package_id','');
  58. $package = model('app\api\model\service\RechargePackage')->where(['state'=>1,'id'=>$packageId])->field('id,price,gift_price')->find();
  59. !$package && $this->error('充值套餐信息异常,请联系管理员');
  60. $price = sprintf("%.2f",$package['price']);
  61. $price <=0 && $this->error('充值金额错误');
  62. $gift_price = sprintf("%.2f",$package['gift_price']);
  63. $orderId = 'Recharge'.Random::alnum(5).'-'.$uid.'-'.time();
  64. $paytype = input('paytype/d',0);
  65. $re = '';
  66. Db::startTrans();
  67. try{
  68. $recharge = new \app\api\model\service\Recharge(['user_id'=>$uid,'recharge_package_id'=>$package['id'],'price'=>$price,'gift_price'=>$gift_price,'orderId'=>$orderId,'paytype'=>$paytype]);
  69. $recharge->allowField(true)->save();
  70. $re = \addons\service\library\Pay::payOrder(['amount'=>$price,'orderid'=>$orderId,'title'=>'充值余额'],$paytype,$uid,0);
  71. Db::commit();
  72. } catch (Exception $e) {
  73. Db::rollback();
  74. $this->error('支付拉起失败',$e->getMessage());
  75. }
  76. $this->success('支付信息返回成功',$re);
  77. }
  78. $this->success('信息返回成功',['money'=>$money,'packageList'=>$packageList]);
  79. }
  80. /**
  81. * 充值记录列表
  82. * @return void
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. * @throws \think\exception\DbException
  86. */
  87. public function list()
  88. {
  89. if (!$this->request->isPost()) {
  90. $this->error('请求方式异常');
  91. }
  92. $uid = $this->auth->id;
  93. $page = input('page/d',1);
  94. $list = model('app\api\model\service\Recharge')
  95. ->where(['user_id'=>$uid,'state'=>1])
  96. ->field('id,price,gift_price,orderId,paytype,createtime')
  97. ->order('id desc')
  98. ->page($page)
  99. ->limit(10)
  100. ->select();
  101. $this->success('信息返回成功',$list);
  102. }
  103. }