ManyGreenController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\api\controller\many;
  3. use app\admin\model\system\SystemConfig;
  4. use app\models\many\Many;
  5. use app\models\many\ManyGreen;
  6. use app\models\many\ManyOrder;
  7. use app\models\store\StoreOrder;
  8. use app\models\user\User;
  9. use app\models\user\UserBill;
  10. use app\Request;
  11. use crmeb\services\CacheService;
  12. use crmeb\services\GroupDataService;
  13. use crmeb\services\QrcodeService;
  14. use crmeb\services\SystemConfigService;
  15. use crmeb\services\UtilService;
  16. use crmeb\services\upload\Upload;
  17. use think\facade\Db;
  18. /**
  19. * 账单类
  20. * Class UserBillController
  21. * @package app\api\controller\user
  22. */
  23. class ManyGreenController
  24. {
  25. /**
  26. * 绿卡记录
  27. * @param Request $request
  28. * @return mixed
  29. */
  30. public function list(Request $request)
  31. {
  32. $where = UtilService::getMore([
  33. ['page', 1],
  34. ['limit', 10],
  35. ['status'],
  36. ]);
  37. $where['uid'] = $request->uid();
  38. $list = \app\models\many\ManyGreen::list($where);
  39. return app('json')->success($list);
  40. }
  41. /**
  42. * 兑换绿卡
  43. * @param Request $request
  44. * @param $num
  45. * @return mixed
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function exchange(Request $request, $num)
  51. {
  52. if ($num < 1) return app('json')->fail('传入需要兑换数量');
  53. if (CacheService::get($request->uid())) return app('json')->fail('操作太快');
  54. $user = User::where('uid', $request->uid())->find();
  55. $money = $num * SystemConfig::getConfigValue('green');
  56. if ($user['green_integral'] < $money) return app('json')->fail('水滴不足');
  57. $user['green_integral'] -= $money;
  58. Db::startTrans();
  59. try {
  60. for ($i = 1; $i <= (int)$num; $i++){
  61. ManyGreen::create(['uid' => $request->uid(), 'type' => 1]);
  62. }
  63. $user->save();
  64. UserBill::income('兑换洒水壶',$request->uid(), 'green_integral', 'exchange', $money, 0,$user['green_integral'], '兑换'.$num.'个洒水壶');
  65. CacheService::set($request->uid(), 1, 3);
  66. Db::commit();
  67. return app('json')->success('兑换成功');
  68. } catch (\Exception $e) {
  69. Db::rollback();
  70. return app('json')->fail($e->getMessage());
  71. }
  72. }
  73. /**
  74. * 赠送绿卡
  75. * @param Request $request
  76. * @return mixed
  77. * @throws \think\db\exception\DataNotFoundException
  78. * @throws \think\db\exception\DbException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. */
  81. public function give(Request $request)
  82. {
  83. $data = UtilService::postMore([
  84. ['id'],
  85. ['uid']
  86. ]);
  87. if (empty($data['uid'])) return app('json')->fail('填写赠送用户ID');
  88. $user = User::where('uid', $data['uid'])->find();
  89. $green = ManyGreen::where('id', $data['id'])->where('uid', $request->uid())->find();
  90. if (!$user) return app('json')->fail('没有此用户');
  91. if (!$green) return app('json')->fail('没有这张绿卡');
  92. if ($green['type'] == 2) return app('json')->fail('赠送绿卡,不能进行赠送');
  93. $green['uid'] = $data['uid'];
  94. $green['type'] = 2;
  95. $res = $green->save();
  96. if ($res) return app('json')->success('赠送成功');
  97. return app('json')->fail('赠送失败');
  98. }
  99. public function ratio(Request $request)
  100. {
  101. return app('json')->success(['bl' => SystemConfig::getConfigValue('green')]);
  102. }
  103. }