Memberbonus.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Db;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  10. * 网站地址: https://www.valimart.net/
  11. * ----------------------------------------------------------------------------
  12. *
  13. * ============================================================================
  14. * 领取红包控制器
  15. */
  16. class Memberbonus extends MobileMember {
  17. public function initialize() {
  18. parent::initialize();
  19. Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/memberbonus.lang.php');
  20. }
  21. /**
  22. * @api {POST} api/Memberbonus/get_receive_list 红包记录
  23. * @apiVersion 1.0.0
  24. * @apiGroup Memberbonus
  25. *
  26. * @apiHeader {String} X-DS-KEY 用户授权token
  27. *
  28. * @apiSuccess {String} code 返回码,10000为成功
  29. * @apiSuccess {String} message 返回消息
  30. * @apiSuccess {Object} result 返回数据
  31. */
  32. public function get_receive_list() {
  33. $bonus_model=model('bonus');
  34. $bonuslog_list=$bonus_model->getBonusreceiveList(array('member_id'=>$this->member_info['member_id']),$this->pagesize);
  35. $result = array_merge(array('log_list' => $bonuslog_list), mobile_page($bonus_model->page_info));
  36. ds_json_encode(10000, '',$result);
  37. }
  38. /**
  39. * @api {POST} api/Memberbonus/receive 活动红包领取
  40. * @apiVersion 1.0.0
  41. * @apiGroup Memberbonus
  42. *
  43. * @apiHeader {String} X-DS-KEY 用户授权token
  44. *
  45. * @apiParam {Int} bonus_id 活动ID
  46. *
  47. * @apiSuccess {String} code 返回码,10000为成功
  48. * @apiSuccess {String} message 返回消息
  49. * @apiSuccess {Object} result 返回数据
  50. */
  51. public function receive() {
  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. }