UserCoupons.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. class UserCoupons extends Api
  7. {
  8. protected $noNeedLogin = [];
  9. protected $noNeedRight = '*';
  10. protected $model = null;
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->model = Db::name('user_coupons');
  15. }
  16. /**
  17. * 列表
  18. * @return void
  19. */
  20. public function getList()
  21. {
  22. try {
  23. $status = $this->request->param('status',1);//状态:1待使用,2已失效
  24. $userId = $this->auth->id;
  25. $companyId = $this->auth->company_id;
  26. $field = 'id,coupon_name,coupon_info,endtime,number,remain,check_code';
  27. $where['user_id'] = $userId;
  28. $where['company_id'] = $companyId;
  29. $whereOr = [];
  30. if ($status == 1) {
  31. $where['remain'] = ['gt',0];
  32. $whereOr['endtime'] = [['gt',time()],['eq',0],'or'];
  33. } else {
  34. $whereOr['remain'] = ['elt',0];
  35. $whereOr['endtime'] = [['lt',time()],['neq',0],'and'];
  36. }
  37. $result = $this->model->field($field)->where($where)->where(function($query) use ($whereOr){
  38. $query->whereOr($whereOr);
  39. })->order('createtime desc')->autopage()->select();
  40. if (!empty($result)) {
  41. foreach ($result as $key => &$value) {
  42. $value['endtime'] = empty($value['endtime']) ? '' : date('Y.m.d H:i:s',$value['endtime']);
  43. }
  44. }
  45. $this->success('获取成功',$result);
  46. } catch (Exception $e) {
  47. $this->error($e->getMessage());
  48. }
  49. }
  50. /**
  51. * 核销码
  52. * @return void
  53. */
  54. public function writeOff()
  55. {
  56. try {
  57. $id = $this->request->param('id',0);
  58. $companyId = $this->auth->company_id;
  59. $userId = $this->auth->id;
  60. $where['id'] = $id;
  61. $where['company_id'] = $companyId;
  62. $where['user_id'] = $userId;
  63. $where['remain'] = ['gt',0];
  64. $modelData = $this->model->where($where)->find();
  65. if (empty($modelData)) {
  66. throw new Exception('未找到相关信息');
  67. }
  68. //验证
  69. if (($modelData['remain'] > 0 && $modelData['endtime'] < time() && $modelData['endtime'] > 0) || $modelData['remain'] < 1) {
  70. throw new Exception('该券已无法使用');
  71. }
  72. $text = 'coupon_'.$modelData['check_code'];
  73. $logo = '';
  74. $filRoute = '/uploads/temp/';
  75. $saveDir = ROOT_PATH.'public'.DS.'uploads'.DS.'temp'.DS;
  76. $fileStr = md5($text);
  77. $localpng = $saveDir.$fileStr.'.png';
  78. //验证存在直接返回
  79. $userCouponsUrl = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$filRoute.$fileStr.'.png';
  80. if (!file_exists($localpng)) {
  81. build_qrcode($text, $logo, $saveDir,$fileStr);
  82. }
  83. $result = [
  84. 'url' => $userCouponsUrl,
  85. 'check_code' => $modelData['check_code'],
  86. ];
  87. $this->success('获取成功',$result);
  88. } catch (Exception $e) {
  89. $this->error($e->getMessage());
  90. }
  91. }
  92. public function newtest()
  93. {
  94. $id = 5;
  95. $text = 'hexiaocoupon_'.$id;
  96. $logo = '';
  97. $filRoute = '/uploads/temp/';
  98. $saveDir = ROOT_PATH.'public\uploads\temp'.DS;
  99. $fileStr = md5('coupon_'.$id);
  100. $localpng = $saveDir.$fileStr.'.png';
  101. //验证存在直接返回
  102. $userCouponsUrl = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$filRoute.$fileStr.'.png';
  103. if (!file_exists($localpng)) {
  104. build_qrcode($text, $logo, $saveDir,$fileStr);
  105. }
  106. $result = [
  107. 'url' => $userCouponsUrl,
  108. ];
  109. $this->success('获取成功',$result);
  110. }
  111. }