Memberbonus.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Db;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 领取红包控制器
  11. */
  12. class Memberbonus extends MobileMember
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberbonus.lang.php');
  18. }
  19. /**
  20. * @api {POST} api/Memberbonus/get_receive_list 红包记录
  21. * @apiVersion 1.0.0
  22. * @apiGroup Memberbonus
  23. *
  24. * @apiHeader {String} X-DS-KEY 用户授权token
  25. *
  26. * @apiSuccess {String} code 返回码,10000为成功
  27. * @apiSuccess {String} message 返回消息
  28. * @apiSuccess {Object} result 返回数据
  29. */
  30. public function get_receive_list()
  31. {
  32. $bonus_model = model('bonus');
  33. $bonuslog_list = $bonus_model->getBonusreceiveList(array('member_id' => $this->member_info['member_id']), $this->pagesize);
  34. $result = array_merge(array('log_list' => $bonuslog_list), mobile_page($bonus_model->page_info));
  35. ds_json_encode(10000, '', $result);
  36. }
  37. /**
  38. * @api {POST} api/Memberbonus/receive 活动红包领取
  39. * @apiVersion 1.0.0
  40. * @apiGroup Memberbonus
  41. *
  42. * @apiHeader {String} X-DS-KEY 用户授权token
  43. *
  44. * @apiParam {Int} bonus_id 活动ID
  45. *
  46. * @apiSuccess {String} code 返回码,10000为成功
  47. * @apiSuccess {String} message 返回消息
  48. * @apiSuccess {Object} result 返回数据
  49. */
  50. public function receive()
  51. {
  52. $bonus_id = intval(input('param.bonus_id'));
  53. if ($bonus_id < 0) {
  54. ds_json_encode(10001, lang('param_error'));
  55. }
  56. $bonus_model = model('bonus');
  57. $condition = array();
  58. $condition[] = array('bonus_id', '=', $bonus_id);
  59. $bonus = $bonus_model->getOneBonus($condition); //获取当前红包的领取金额
  60. if ($bonus['bonus_begintime'] > TIMESTAMP) {
  61. ds_json_encode(10001, lang('bonus_not_begin'));
  62. }
  63. if ($bonus['bonus_type'] != 1) {
  64. ds_json_encode(10001, lang('bonus_type_error'));
  65. }
  66. if ($bonus['bonus_state'] == 2 || TIMESTAMP > $bonus['bonus_endtime']) {
  67. ds_json_encode(10001, lang('bonus_expire'));
  68. }
  69. if ($bonus['bonus_state'] == 3) {
  70. ds_json_encode(10001, lang('bonus_invalid'));
  71. }
  72. //判断当前用户是否领取过
  73. $condition = array();
  74. $condition[] = array('bonus_id', '=', $bonus_id);
  75. $condition[] = array('member_id', '=', $this->member_info['member_id']);
  76. $bonusreceive = $bonus_model->getOneBonusreceive($condition);
  77. if ($bonusreceive) {
  78. ds_json_encode(10001, sprintf(lang('bonusreceive_info'), date('Y-m-d H:i:s', $bonusreceive['bonusreceive_time']), $bonusreceive['bonusreceive_price']));
  79. }
  80. //获取未领取单个红包
  81. $condition = array();
  82. $condition[] = array('bonus_id', '=', $bonus_id);
  83. $condition[] = array('member_id', '=', 0);
  84. $bonusreceive = $bonus_model->getOneBonusreceive($condition);
  85. if (empty($bonusreceive)) {
  86. ds_json_encode(10001, lang('bonus_send_over'));
  87. }
  88. try {
  89. Db::startTrans();
  90. $res = $bonus_model->receiveBonus($this->member_info, $bonus, $bonusreceive, lang('receive_bonus'));
  91. if (!$res['code']) {
  92. throw new \think\Exception($res['msg'], 10006);
  93. }
  94. Db::commit();
  95. ds_json_encode(10000, sprintf(lang('get_bonus_info'), $bonusreceive['bonusreceive_price']));
  96. } catch (Exception $e) {
  97. Db::rollback();
  98. ds_json_encode(10001, $e->getMessage());
  99. }
  100. }
  101. }