bianjunhui 2 lat temu
rodzic
commit
2487490844

+ 51 - 1
application/admin/controller/Raprize.php

@@ -89,7 +89,6 @@ class Raprize extends Backend
         }
         $params = $this->preExcludeFields($params);
         $ra_id=session('ra_id');
-
         $params['r_id']=$ra_id;
         $raffle = \app\admin\model\Raffle::getById($ra_id);
         $params['r_name']=$raffle->name;
@@ -122,4 +121,55 @@ class Raprize extends Backend
         }
         $this->success();
     }
+
+    public function edit($ids = null)
+    {
+        $row = $this->model->get($ids);
+        if (!$row) {
+            $this->error(__('No Results were found'));
+        }
+        $adminIds = $this->getDataLimitAdminIds();
+        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
+            $this->error(__('You have no permission'));
+        }
+        if (false === $this->request->isPost()) {
+            $this->view->assign('row', $row);
+            return $this->view->fetch();
+        }
+        $params = $this->request->post('row/a');
+        if (empty($params)) {
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $params = $this->preExcludeFields($params);
+        $ra_id=session('ra_id');
+        $params['r_id']=$ra_id;
+        $raffle = \app\admin\model\Raffle::getById($ra_id);
+        $params['r_name']=$raffle->name;
+        $category=\app\common\model\Category::getById($params['t_type']);
+        $params['t_type_name']=$category->name;
+        $prize=\app\admin\model\Prize::getById($params['p_id']);
+        $params['p_name']=$prize->name;
+        $params['price']=$prize->price;
+        $params['money']=$prize->money;
+        $result = false;
+        Db::startTrans();
+        try {
+            //是否采用模型验证
+            if ($this->modelValidate) {
+                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
+                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
+                $row->validateFailException()->validate($validate);
+            }
+            $result = $row->allowField(true)->save($params);
+            Db::commit();
+        } catch (ValidateException|PDOException|Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if (false === $result) {
+            $this->error(__('No rows were updated'));
+        }
+        $this->success();
+    }
+
 }

+ 69 - 0
application/api/controller/Raffle.php

@@ -0,0 +1,69 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use app\common\model\Jwttoken;
+use app\admin\model\Raffle as Raf;
+use think\Db;
+use function fast\e;
+
+/**
+ * 首页接口
+ */
+class Raffle extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+    /**
+     * 首页
+     *
+     */
+    public function getRaffle()
+    {
+        $token = $this->request->server('HTTP_TOKEN');
+        if(empty($token)){
+            $this->error('请输入Token');
+        }
+        $jwt = new Jwttoken();
+        $info =$jwt->verifyJwt($token);
+        if($info['status']!=0){
+            $msg =$info['msg'];
+            $this->error($msg);
+        }
+        $userinfo =$info['info'];
+        $web=$userinfo['website'];
+        $list =  Db::name('raffle')->where("website='{$web}' and status ='normal' ")->order('id desc')->find();
+        $data['id']=$list['id'];
+        $data['website']=$list['website'];
+        $data['name']=$list['name'];
+        $data['image']=$list['image'];
+        $data['prize']=$this->getPrize($list['id']);
+        $this->success('',$data);
+    }
+
+    public function getPrize($rid){
+        $list =  Db::name('raprize')->where("r_id='$rid' and status ='normal' ")->order('id desc')->select();
+        $new=array();
+        foreach ($list as $k=>$v){
+            $new[$k]['title']=$v['t_type_name'];
+            $new[$k]['price']=$v['price'];
+            if($v['t_type_name']=='CASH'||$v['t_type_name']=='Coupon'){
+                $str='$';
+                $new[$k]['price']=$str.$v['price'];
+            }
+            if($v['t_type_name']=='DiscountCoupon'){
+                $str='%';
+                $new[$k]['price']=$v['price'].$str;
+            }
+            $new[$k]['image']=$v['image'];
+        }
+        return $new;
+    }
+
+
+
+
+
+}

+ 58 - 0
application/common/model/Jwttoken.php

@@ -0,0 +1,58 @@
+<?php
+namespace app\common\model;
+use Firebase\JWT\JWT;
+use Firebase\JWT\Key;
+
+class Jwttoken
+{
+    //生成token
+    public function createJwt($username)
+    {
+        $key    = md5('key'); //jwt的签发密钥,验证token的时候需要用到
+        $time   = time();
+        $expire = $time + 14400; //过期时间
+        $token  = array(
+            "username" => $username,
+            "iss"      => "",//签发组织
+            "aud"      => "", //签发作者
+            "iat"      => $time, //签发时间
+            "nbf"      => $time, //生效时间
+            "exp"      => $expire
+        );
+        $jwt    = JWT::encode($token, $key, 'HS256');
+        return $jwt;
+    }
+
+
+    //校验jwt权限API
+    public function verifyJwt($jwt)
+    {
+        $key = 'A6H1O#vJ*6QV3*sC';
+        try {
+            $jwtAuth  = JWT::decode($jwt, new Key($key, 'HS256'));
+            $authInfo = (array)$jwtAuth;
+            $msg['status']=0;
+            $msg['info']=$authInfo;
+            return $msg;
+        } catch (\Firebase\JWT\SignatureInvalidException $e) {
+            return [
+                'status' => 10002,
+                'msg'    => 'Token无效'
+            ];
+            exit;
+        } catch (\Firebase\JWT\ExpiredException $e) {
+            //Token过期
+            return [
+                'status' => 10003,
+                'msg'    => '登录信息已超时,请重新登录'
+            ];
+            exit;
+        } catch (Exception $e) {
+            return [
+                'status' => 10004,
+                'msg'    => '未知错误'
+            ];
+            exit;
+        }
+    }
+}