123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Db;
- use think\Exception;
- class UserCoupons extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = '*';
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = Db::name('user_coupons');
- }
- /**
- * 列表
- * @return void
- */
- public function getList()
- {
- try {
- $status = $this->request->param('status',1);//状态:1待使用,2已失效
- $userId = $this->auth->id;
- $companyId = $this->auth->company_id;
- $field = 'id,coupon_name,coupon_info,endtime,number,remain,check_code';
- $where['user_id'] = $userId;
- $where['company_id'] = $companyId;
- $whereOr = [];
- if ($status == 1) {
- $where['remain'] = ['gt',0];
- $whereOr['endtime'] = [['gt',time()],['eq',0],'or'];
- } else {
- $whereOr['remain'] = ['elt',0];
- $whereOr['endtime'] = [['lt',time()],['neq',0],'and'];
- }
- $result = $this->model->field($field)->where($where)->where(function($query) use ($whereOr){
- $query->whereOr($whereOr);
- })->order('createtime desc')->autopage()->select();
- if (!empty($result)) {
- foreach ($result as $key => &$value) {
- $value['endtime'] = empty($value['endtime']) ? '' : date('Y.m.d H:i:s',$value['endtime']);
- }
- }
- $this->success('获取成功',$result);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 核销码
- * @return void
- */
- public function writeOff()
- {
- try {
- $id = $this->request->param('id',0);
- $companyId = $this->auth->company_id;
- $userId = $this->auth->id;
- $where['id'] = $id;
- $where['company_id'] = $companyId;
- $where['user_id'] = $userId;
- $where['remain'] = ['gt',0];
- $modelData = $this->model->where($where)->find();
- if (empty($modelData)) {
- throw new Exception('未找到相关信息');
- }
- //验证
- if (($modelData['remain'] > 0 && $modelData['endtime'] < time() && $modelData['endtime'] > 0) || $modelData['remain'] < 1) {
- throw new Exception('该券已无法使用');
- }
- $text = 'coupon_'.$modelData['check_code'];
- $logo = '';
- $filRoute = '/uploads/temp/';
- $saveDir = ROOT_PATH.'public'.DS.'uploads'.DS.'temp'.DS;
- $fileStr = md5($text);
- $localpng = $saveDir.$fileStr.'.png';
- //验证存在直接返回
- $userCouponsUrl = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$filRoute.$fileStr.'.png';
- if (!file_exists($localpng)) {
- build_qrcode($text, $logo, $saveDir,$fileStr);
- }
- $result = [
- 'url' => $userCouponsUrl,
- 'check_code' => $modelData['check_code'],
- ];
- $this->success('获取成功',$result);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- }
- public function newtest()
- {
- $id = 5;
- $text = 'hexiaocoupon_'.$id;
- $logo = '';
- $filRoute = '/uploads/temp/';
- $saveDir = ROOT_PATH.'public\uploads\temp'.DS;
- $fileStr = md5('coupon_'.$id);
- $localpng = $saveDir.$fileStr.'.png';
- //验证存在直接返回
- $userCouponsUrl = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$filRoute.$fileStr.'.png';
- if (!file_exists($localpng)) {
- build_qrcode($text, $logo, $saveDir,$fileStr);
- }
- $result = [
- 'url' => $userCouponsUrl,
- ];
- $this->success('获取成功',$result);
- }
- }
|