FlowerTicket.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Core\Auth;
  4. use App\Models\FlowerTicketModel;
  5. use Illuminate\Http\Request;
  6. class FlowerTicket extends Controller
  7. {
  8. /**
  9. * @param Request $request
  10. * @return array
  11. * @api {Post} /api/msy/flowerticket/store 创建兑换卡
  12. * @apiName 兑换卡生成
  13. * @apiGroup flowerticket
  14. *
  15. * @apiParam {int} validity_at 过期时间
  16. * @apiParam {int} red_flower 红花数量
  17. * @apiParam {int} gold_flower 金花数量
  18. * @apiParam {int} jsk 解锁卡
  19. * @apiParam {int} count 生成数量
  20. *
  21. * @apiSuccess {json}
  22. * {
  23. * 'code': 200,
  24. * 'message': 'success',
  25. * 'data': {
  26. * 'success': 2,
  27. * 'fail': 1,
  28. * 'ticket_ids': [1, 2, 3]
  29. * }
  30. * }
  31. */
  32. public function store(Request $request)
  33. {
  34. $this->validate($request, [
  35. 'validity_at' => 'required',
  36. 'red_flower' => 'required',
  37. 'gold_flower' => 'required',
  38. 'jsk' => 'required',
  39. 'count' => 'required',
  40. ]);
  41. $count = $request->input('count');
  42. $ticketids = array();
  43. for ($i = 0; $i < $count; $i++) {
  44. try {
  45. $data = array(
  46. 'validity_at' => $request->input('validity_at'),
  47. 'code' => bin2hex(\openssl_random_pseudo_bytes(4)),
  48. 'red_flower' => $request->input('red_flower'),
  49. 'gold_flower' => $request->input('gold_flower'),
  50. 'jsk' => $request->input('jsk')
  51. );
  52. $flowerTicketModel = new FlowerTicketModel();
  53. $ticket = $flowerTicketModel->fill($data);
  54. if ($ticket->save()) {
  55. array_push($ticketids, $ticket->id);
  56. }
  57. } catch (\Exception $e) {
  58. }
  59. }
  60. return array(
  61. 'code' => 200,
  62. 'message' => 'success',
  63. 'data' => [
  64. 'success' => count($ticketids),
  65. 'fail' => $count - count($ticketids),
  66. 'ticket_ids' => $ticketids
  67. ],
  68. );
  69. }
  70. /**
  71. * @param Request $request
  72. * @param int $ticket_id
  73. * @return array
  74. * @api {Post} /api/msy/flowerticket/:ticket_id/update ticket_id 兑换码id
  75. * @apiName 更新兑换码
  76. * @apiGroup flowerticket
  77. *
  78. * @apiParam {int} validity_at 过期时间
  79. * @apiParam {int} red_flower 红花数量
  80. * @apiParam {int} gold_flower 金花数量
  81. * @apiParam {int} jsk 解锁卡
  82. * @apiParam {int} count 生成数量
  83. *
  84. * @apiSuccess {json}
  85. * {
  86. * 'code': 200,
  87. * 'message': 'success'
  88. * }
  89. */
  90. public function update(Request $request, int $ticket_id)
  91. {
  92. $this->validate($request, [
  93. 'validity_at' => '',
  94. 'red_flower' => '',
  95. 'gold_flower' => '',
  96. 'jsk' => '',
  97. ]);
  98. $flowerTicketModel = new FlowerTicketModel();
  99. $ticket = $flowerTicketModel->find($ticket_id);
  100. if (collect($ticket)->isEmpty()) {
  101. return array(
  102. 'code' => 101,
  103. 'message' => '参数错误'
  104. );
  105. }
  106. $ticket->fill($request->toArray());
  107. if ($ticket->save()) {
  108. return array(
  109. 'code' => 200,
  110. 'message' => 'success'
  111. );
  112. } else {
  113. return array(
  114. 'code' => 501,
  115. 'message' => '数据库错误'
  116. );
  117. }
  118. }
  119. /**
  120. * @param int $ticket_id
  121. * @return array
  122. * @throws \Exception
  123. * @api {Get} /api/msy/flowerticket/:ticket_id/delete ticket_id 兑换码id
  124. * @apiName 删除兑换码
  125. * @apiGroup flowerticket
  126. *
  127. * @apiSuccess {json}
  128. * {
  129. * 'code': 200,
  130. * 'message': 'success'
  131. * }
  132. *
  133. * @apiFail {json}
  134. * {
  135. * 'code': 102,
  136. * 'message': '该兑换码已被领取'
  137. * }
  138. */
  139. public function delete(int $ticket_id)
  140. {
  141. $flowerTicketModel = new FlowerTicketModel();
  142. $ticket = $flowerTicketModel->find($ticket_id);
  143. if (collect($ticket)->isEmpty()) {
  144. return array(
  145. 'code' => 101,
  146. 'message' => '参数错误'
  147. );
  148. }
  149. if (!empty($ticket->uid)) {
  150. return array(
  151. 'code' => 102,
  152. 'message' => '该兑换码已被领取'
  153. );
  154. }
  155. if ($ticket->delete()) {
  156. return array(
  157. 'code' => 200,
  158. 'message' => 'success'
  159. );
  160. } else {
  161. return array(
  162. 'code' => 501,
  163. 'message' => '数据库错误'
  164. );
  165. }
  166. }
  167. /**
  168. * @param int $page
  169. * @return array
  170. * @api {Get} /api/msy/flowerticket/list/:page page 页数
  171. * @apiName 获取兑换码列表
  172. * @apiGroup flowerticket
  173. *
  174. * @apiSuccess {json}
  175. * {
  176. * 'code': 200,
  177. * 'message': 'success',
  178. * 'data': {
  179. * 'count': '总数',
  180. * 'page': '页数',
  181. * 'limit': '每页数量',
  182. * 'tickets': [{
  183. * 'id': '兑换码id',
  184. * 'uid': '领取人uid',
  185. * 'code': '兑换码'
  186. * }]
  187. * }
  188. * }
  189. */
  190. public function getList(int $page = 1)
  191. {
  192. $flowerTicketModel = new FlowerTicketModel();
  193. $tickets = $flowerTicketModel->take(100)->skip(($page - 1) * 100)->get();
  194. $count = $flowerTicketModel->count();
  195. return array(
  196. 'code' => 200,
  197. 'message' => 'success',
  198. 'data' => [
  199. 'count' => $count,
  200. 'page' => $page,
  201. 'limit' => 100,
  202. 'tickets' => $tickets
  203. ]
  204. );
  205. }
  206. /**
  207. * @param Request $request
  208. * @return array
  209. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  210. * @api {Get} api/msy/flowerticket/claim 领取兑换码
  211. * @apiName 领取兑换码
  212. * @apiGroup flowerticket
  213. *
  214. * @apiParam {string} code 兑换码code
  215. *
  216. * @apiSuccess {json}
  217. * {
  218. * 'code': 200,
  219. * 'message': 'success'
  220. * }
  221. */
  222. public function claim(Request $request)
  223. {
  224. $uid = Auth::auth();
  225. $this->validate($request, [
  226. 'code' => 'required'
  227. ]);
  228. $code = $request->input('code');
  229. $flowerTicketModel = new FlowerTicketModel();
  230. $ticket = $flowerTicketModel->where('code', $code)->first();
  231. if (collect($ticket)->isEmpty()) {
  232. return array(
  233. 'code' => 101,
  234. 'message' => '兑换码错误'
  235. );
  236. }
  237. if ($ticket->validity_at < time() || !empty($ticket->uid)) {
  238. return array(
  239. 'code' => 102,
  240. 'message' => '兑换码失效'
  241. );
  242. }
  243. \DB::beginTransaction();
  244. try {
  245. \DB::table('kdgx_msy_flower_ticket')->where('id', $ticket->id)->update(['uid' => $uid]);
  246. \DB::table('kdgx_partner_charge_user')->where('uid', $uid)->increment('red_flower', $ticket->red_flower);
  247. \DB::table('kdgx_partner_charge_user')->where('uid', $uid)->increment('gold_flower', $ticket->gold_flower);
  248. $ticketController = new Ticket();
  249. for ($i = 0; $i < $ticket->jsk; $i++) {
  250. $ticketController->create(5, $uid);
  251. }
  252. \DB::table('kdgx_partner_charge_pay_logs')->create([
  253. 'uid' => $uid,
  254. 'create_at' => time(),
  255. 'type' => 11,
  256. 'red_flower' => $ticket->red_flower,
  257. 'gold_flower' => $ticket->gold_flower,
  258. 'jsk' => $ticket->jsk
  259. ]);
  260. \DB::commit();
  261. return array(
  262. 'code' => 200,
  263. 'message' => 'success'
  264. );
  265. } catch (\Exception $e) {
  266. \DB::rollBack();
  267. return array(
  268. 'code' => 501,
  269. 'message' => $e->getMessage()
  270. );
  271. }
  272. }
  273. }