BorrowMoneyController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\api\controller\manege;
  3. use app\models\manage\BorrowMoneyProduct;
  4. use app\models\manage\UserBorrowMoney;
  5. use app\models\trade\CashTradeOrder;
  6. use app\Request;
  7. use crmeb\services\CacheService;
  8. use crmeb\services\UtilService;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. use think\Exception;
  13. class BorrowMoneyController
  14. {
  15. /**
  16. * 算力产品
  17. * @param Request $request
  18. * @return mixed
  19. */
  20. public function lst(Request $request)
  21. {
  22. $page = $request->get('page', 1);
  23. $limit = $request->get('limit', 10);
  24. return app('json')->success('ok', BorrowMoneyProduct::getList((int)$page, (int)$limit));
  25. }
  26. /**
  27. * @param $id
  28. * @param Request $request
  29. * @return mixed
  30. * @throws DataNotFoundException
  31. * @throws DbException
  32. * @throws ModelNotFoundException
  33. */
  34. public function detail($id, Request $request)
  35. {
  36. return app('json')->success('ok', BorrowMoneyProduct::valid()->where('id', $id)->find()->toArray());
  37. }
  38. public function buy($id, Request $request)
  39. {
  40. $user = $request->user();
  41. list($num,) = UtilService::postMore(
  42. [
  43. ['num', 0,],
  44. ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
  45. // var_dump($user);
  46. return md5(md5($item)) == $user['trade_pwd'];
  47. }], ['请输入交易密码', '交易密码错误']],
  48. ], $request, true);
  49. $res = BorrowMoneyProduct::buyPoroduct($id, $request->uid(), $num);
  50. if ($res) {
  51. return app('json')->success('借款成功');
  52. } else {
  53. return app('json')->fail(BorrowMoneyProduct::getErrorInfo());
  54. }
  55. }
  56. /**
  57. * 我的理财
  58. * @param Request $request
  59. * @return mixed
  60. */
  61. public function myList(Request $request)
  62. {
  63. $page = $request->get('page', 1);
  64. $limit = $request->get('limit', 10);
  65. $where['status'] = $request->get('status', '');
  66. return app('json')->success('ok', UserBorrowMoney::getList((int)$page, (int)$limit, $where));
  67. }
  68. public function finish($id, Request $request)
  69. {
  70. $num = $request->put('num', 0);
  71. $info = UserBorrowMoney::valid()->where('uid', $request->uid())->where('id', $id)->find();
  72. if (!$info) {
  73. return app('json')->fail('数据参数异常');
  74. }
  75. UserBorrowMoney::beginTrans();
  76. try {
  77. $res = UserBorrowMoney::endManege($id, $num);
  78. if ($res) {
  79. UserBorrowMoney::commitTrans();
  80. return app('json')->success('已还款');
  81. } else {
  82. UserBorrowMoney::rollbackTrans();
  83. return app('json')->fail(UserBorrowMoney::getErrorInfo());
  84. }
  85. } catch (Exception $e) {
  86. UserBorrowMoney::rollbackTrans();
  87. return app('json')->fail($e->getMessage());
  88. }
  89. }
  90. }