Money.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace addons\ddrive\controller;
  3. use app\common\controller\Api;
  4. use addons\ddrive\model\Cash;
  5. use think\Db;
  6. /**
  7. * 余额接口
  8. */
  9. class Money extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. $this->withdraw = new \addons\ddrive\model\Withdraw();
  17. }
  18. /**
  19. * 获取余额日志
  20. *
  21. * @return void
  22. */
  23. public function withdrawList()
  24. {
  25. $pageSize = $this->request->param('pageSize', 10);
  26. $list = $this->withdraw->where('user_id', $this->auth->id)->order('id desc')->paginate($pageSize)->each(function ($item) {
  27. $item['createdate'] = date('Y-m-d H:i:s', $item['createtime']);
  28. return $item;
  29. });
  30. $this->success("提现成功", $list);
  31. }
  32. /**
  33. * 提现
  34. *
  35. * @return void
  36. */
  37. public function withdraw()
  38. {
  39. $money = $this->request->param('money', 0);
  40. if ($money <= 0) {
  41. $this->error('金额不正确');
  42. }
  43. $user = $this->auth->getUser();
  44. if ($user['money'] <= 0) {
  45. $this->error('用户余额不足');
  46. }
  47. $data = [
  48. 'user_id' => $user['id'],
  49. 'money' => $money,
  50. 'createtime' => time(),
  51. 'status' => 0,
  52. ];
  53. $res = $this->withdraw->insert($data);
  54. if ($res) {
  55. $log = [
  56. 'user_id' => $user['id'],
  57. 'money' => $money,
  58. 'before' => $user->money,
  59. 'after' => $user->money - $money,
  60. 'memo' => '用户提现',
  61. 'createtime' => time(),
  62. ];
  63. // 添加用户余额日志
  64. Db::name('user_money_log')->insert($log);
  65. // 修改用户余额
  66. $user->money = $user->money - $money;
  67. $user->save();
  68. $this->error('提现申请成功,请等待管理员审核');
  69. } else {
  70. $this->error($res['提现申请失败']);
  71. }
  72. }
  73. /**
  74. * 提交提现
  75. * @ApiMethod (POST)
  76. * @ApiParams (name="money", type="int", required=true, description="提现金额")
  77. * @ApiParams (name="account_number", type="int", required=true, description="收款账号")
  78. * @ApiParams (name="payee", type="int", required=true, description="收款人")
  79. */
  80. public function sbCash(){
  81. $params = $this->request->param();
  82. $data = [
  83. 'user_id' => $this->auth->id,
  84. 'money' => $params['money'],
  85. 'account_number' => $params['account_number'],
  86. 'payee' => $params['payee'],
  87. 'pay_type' => 2,
  88. ];
  89. $rechargeModel = new Cash();
  90. Db::startTrans();
  91. try {
  92. $id = $rechargeModel->addCash($data);
  93. } catch (\Exception $exception) {
  94. $this->error($exception->getMessage());
  95. }
  96. if (!$id) {
  97. $this->error($rechargeModel->getError());
  98. Db::rollback();
  99. } else {
  100. Db::commit();
  101. }
  102. $this->success('成功');
  103. }
  104. }