Couponuser.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use Think\Db;
  5. /**
  6. * 会员管理
  7. *
  8. * @icon fa fa-user
  9. */
  10. class Couponuser extends Backend
  11. {
  12. /**
  13. * Couponuser模型对象
  14. * @var \app\admin\model\Couponuser
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Couponuser;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. $this->view->assign("genderList", $this->model->getGenderList());
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. $coupon_id = input('coupon_id', 0, 'intval'); //优惠券id
  35. if (!$coupon_id) {
  36. $coupon_id = input('ids', 0, 'intval'); //从优惠券列表第一次跳转过来
  37. }
  38. $this->assign('coupon_id', $coupon_id);
  39. $this->assignconfig('coupon_id', $coupon_id);
  40. //当前是否为关联查询
  41. $this->relationSearch = false;
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $list = $this->model
  51. ->where(['status' => 1])
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. $row->visible(['id','username','nickname','mobile','avatar','money','status','realname','gender','birthday','idcard','passport','emergencycontact','contactmobile','outdoorduration','invite_no','growthvalue','growthlevel','experiencelevel','experiencetime','maxlevel','freenumber']);
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 发放优惠券给用户
  65. */
  66. public function giveout() {
  67. $ids = input('ids', '', 'trim'); //用户id字符串
  68. $coupon_id = input('coupon_id', 0, 'intval'); //优惠券id
  69. if (!$ids || !$coupon_id) {
  70. $this->error('参数缺失');
  71. }
  72. $info = Db::name('coupon')->find($coupon_id);
  73. if (!$info) {
  74. $this->error('优惠券不存在');
  75. }
  76. if ($info['purpose'] != 3) {
  77. $this->error('优惠券类型错误');
  78. }
  79. $ids = explode(',', $ids);
  80. $coupon_data = [
  81. // 'user_id' => $ids,
  82. 'coupon_id' => $info['id'],
  83. 'title' => $info['title'],
  84. 'desc' => $info['desc'],
  85. 'type' => $info['type'],
  86. 'money' => $info['money'],
  87. 'minmoney' => $info['minmoney'],
  88. 'purpose' => $info['purpose'],
  89. 'starttime' => time(),
  90. 'endtime' => time() + $info['effectiveday'] * 86400,
  91. 'createtime' => time()
  92. ];
  93. $user_coupon = Db::name('user_coupon');
  94. foreach ($ids as &$v) {
  95. $coupon_data['user_id'] = $v;
  96. $coupon_rs = $user_coupon->insertGetId($coupon_data);
  97. // if (!$coupon_rs) {
  98. // $this->error('发放失败');
  99. // }
  100. }
  101. $this->success('发放成功');
  102. }
  103. }