wang jun 3 年 前
コミット
fcc243c68b

+ 21 - 19
application/index/controller/Base.php

@@ -8,26 +8,28 @@ class Base extends Controller
     protected $param;
     protected function initialize()
     {
-        $url = request()->baseUrl();
-        $orurl = $url;
-        $orurlarr = array_filter(explode('/', $url));
-        $urlarr = array_filter(explode('/', strtolower($url)));
-        if (count($orurlarr) >= 4) {
-            $url = $urlarr[3] . "/" . $urlarr[4];
-            $ischeck = true;
-            $ignorelist = $this->getignoreurl();
-            foreach ($ignorelist as $key => $value) {
-                if ($url == $value) {
-                    $ischeck = false;
-                }
-            }
-            /*if ($ischeck) {
-            $this->checkToken();
-            }*/
+        /*
+    $url = request()->baseUrl();
+    $orurl = $url;
+    $orurlarr = array_filter(explode('/', $url));
+    $urlarr = array_filter(explode('/', strtolower($url)));
+    if (count($orurlarr) >= 4) {
+    $url = $urlarr[3] . "/" . $urlarr[4];
+    $ischeck = true;
+    $ignorelist = $this->getignoreurl();
+    foreach ($ignorelist as $key => $value) {
+    if ($url == $value) {
+    $ischeck = false;
+    }
+    }
+    //if ($ischeck) {
+    // $this->checkToken();
+    //}
 
-            $functionname = $orurlarr[4];
-            $this->checkrules($functionname);
-        }
+    //$functionname = $orurlarr[4];
+    //$this->checkrules($functionname);
+    }
+     */
     }
     private function getignoreurl()
     {

+ 1 - 21
application/index/controller/Company.php

@@ -3,7 +3,7 @@
  * @Author: wang jun
  * @Date: 2022-01-18 10:57:14
  * @Last Modified by: wang jun
- * @Last Modified time: 2022-01-18 16:56:01
+ * @Last Modified time: 2022-01-19 10:27:57
  * 微信类
  */
 namespace app\index\controller;
@@ -13,26 +13,6 @@ use think\Controller;
 
 class Company extends Base
 {
-    /**
-     * 设置请求数据规则
-     * 20220107
-     * wj
-     */
-    protected function setrules()
-    {
-        $list = [
-            'newinfo' => [
-                ['name' => 'openid', 'title' => 'openid', 'require' => true, 'type' => 'string'],
-            ],
-            'getinfobyid' => [
-                ['name' => 'id', 'title' => 'id', 'require' => true, 'type' => 'string'],
-            ],
-            'updatebyid' => [
-                ['name' => 'id', 'title' => 'id', 'require' => true, 'type' => 'string'],
-            ],
-        ];
-        return $list;
-    }
     public function newinfo()
     {
         $param = request()->param();

+ 7 - 29
application/index/controller/User.php

@@ -3,7 +3,7 @@
  * @Author: wang jun
  * @Date: 2022-01-18 10:57:14
  * @Last Modified by: wang jun
- * @Last Modified time: 2022-01-18 15:31:24
+ * @Last Modified time: 2022-01-19 10:26:29
  * 微信类
  */
 namespace app\index\controller;
@@ -13,35 +13,15 @@ use think\Controller;
 
 class User extends Base
 {
-    /**
-     * 设置请求数据规则
-     * 20220107
-     * wj
-     */
-    protected function setrules()
-    {
-        $list = [
-            'newinfo' => [
-                ['name' => 'openid', 'title' => 'openid', 'require' => true, 'type' => 'string'],
-            ],
-            'realauth' => [
-                ['name' => 'openid', 'title' => 'openid', 'require' => true, 'type' => 'string'],
-            ],
-            'authcompany' => [
-                ['name' => 'openid', 'title' => 'openid', 'require' => true, 'type' => 'string'],
-            ],
-        ];
-        return $list;
-    }
     public function newinfo()
     {
         $param = request()->param();
         $l_u = new userlogic();
-        $id = $l_u->newinfo($param);
-        if (empty($id)) {
-            return backjson(0, "新增失败");
+        $result = $l_u->newinfo($param);
+        if (1 != $result['status']) {
+            return backjson(0, $result['msg']);
         }
-        return backjson(1, "新增成功", ['id' => $id]);
+        return backjson(200, $result['data']);
     }
     public function realauth()
     {
@@ -50,9 +30,8 @@ class User extends Base
         $result = $l_u->realauthbyopenid($param);
         if (1 != $result['status']) {
             return backjson(0, $result['msg']);
-        } else {
-            return backjson(200, $result['data']);
         }
+        return backjson(200, $result['data']);
     }
     public function authcompany()
     {
@@ -61,8 +40,7 @@ class User extends Base
         $result = $l_u->updateiscompanybyopenid($param);
         if (1 != $result['status']) {
             return backjson(0, $result['msg']);
-        } else {
-            return backjson(200, $result['data']);
         }
+        return backjson(200, $result['data']);
     }
 }

+ 92 - 0
application/index/logic/baselogic.php

@@ -0,0 +1,92 @@
+<?php
+/*
+ * @Author: wang jun
+ * @Date: 2022-01-18 11:12:23
+ * @Last Modified by: wang jun
+ * @Last Modified time: 2022-01-19 10:33:22
+ * 微信类
+ */
+namespace app\index\logic;
+
+use think\Facade\Log;
+
+class baselogic
+{
+
+    private function getignoreurl()
+    {
+        $list = [];
+        return $list;
+    }
+    /**
+     * 根据配置验证param
+     * wj
+     * 20220119
+     * [[name=>'',title=>'',type=>'',regex=>"",require=>booler]]
+     */
+    protected function checkparam($functionname, $arr)
+    {
+        $param = $arr;
+        $rules = $this->setrules();
+        if (isset($rules[$functionname])) {
+            try {
+                $list = $rules[$functionname];
+                $namelist = array_column($list, 'name');
+                if (count($namelist) != count($list) || count($list) != count(array_filter($namelist))) {
+                    throw new \Exception("规则name设置错误");
+                }
+                $titlelist = array_column($list, 'title');
+                if (count($titlelist) != count($list) || count($list) != count(array_filter($titlelist))) {
+                    throw new \Exception("规则title设置错误");
+                }
+                foreach ($list as $key => $value) {
+                    $name = $value['name'];
+                    $title = $value['title'];
+                    //必填
+                    if ($value['require']) {
+                        if (!isset($param[$name])) {
+                            throw new \Exception($title . '未填');
+                        }
+                    }
+                    if (!isset($param[$name])) {
+                        continue;
+                    }
+                    $paramvalue = $param[$name];
+                    //类型
+                    if (isset($value['type'])) {
+                        $tpe = $value['type'];
+                        switch ($tpe) {
+                            case 'string':
+                                if (!is_string($paramvalue) || empty($paramvalue)) {
+                                    throw new \Exception($title . '格式错误');
+                                }
+                                break;
+                            case 'numeric':
+                                if (!is_numeric($paramvalue)) {
+                                    throw new \Exception($title . '格式错误');
+                                }
+                                break;
+                            case 'array':
+                                if (!is_array($paramvalue)) {
+                                    throw new \Exception($title . '格式错误');
+                                }
+                                break;
+                        }
+                    }
+                    //正则
+                    if (isset($value['regex'])) {
+                        $regex = $value['regex'];
+                        if (preg_match($regex, $paramvalue)) {
+                            throw new \Exception($title . '正则格式错误');
+                        }
+                    }
+                }
+                return backarr(1, 'success');
+            } catch (\Exception $e) {
+                $msg = $e->getMessage();
+                Log::error($msg);
+                return backarr(0, "请求错误");
+            }
+        }
+    }
+}

+ 34 - 2
application/index/logic/companylogic.php

@@ -3,7 +3,7 @@
  * @Author: wang jun
  * @Date: 2022-01-18 11:12:23
  * @Last Modified by: wang jun
- * @Last Modified time: 2022-01-18 16:44:19
+ * @Last Modified time: 2022-01-19 10:28:59
  * 微信类
  */
 namespace app\index\logic;
@@ -11,8 +11,28 @@ namespace app\index\logic;
 use app\index\model\companymodel;
 use app\index\model\usermodel;
 
-class companylogic
+class companylogic extends baselogic
 {
+    /**
+     * 设置请求数据规则
+     * 20220107
+     * wj
+     */
+    protected function setrules()
+    {
+        $list = [
+            'newinfo' => [
+                ['name' => 'openid', 'title' => 'openid', 'require' => true, 'type' => 'string'],
+            ],
+            'getinfobyid' => [
+                ['name' => 'id', 'title' => 'id', 'require' => true, 'type' => 'string'],
+            ],
+            'updatebyid' => [
+                ['name' => 'id', 'title' => 'id', 'require' => true, 'type' => 'string'],
+            ],
+        ];
+        return $list;
+    }
     /**
      * 新建信息
      * wj
@@ -20,6 +40,10 @@ class companylogic
      */
     public function newinfo($arr)
     {
+        $result = $this->checkparam(__FUNCTION__, $arr);
+        if (1 != $result['status']) {
+            return $result;
+        }
         $m_u = new usermodel();
         $m_c = new companymodel();
         $openid = $arr['openid'];
@@ -43,6 +67,10 @@ class companylogic
      */
     public function getinfobyid($arr)
     {
+        $result = $this->checkparam(__FUNCTION__, $arr);
+        if (1 != $result['status']) {
+            return $result;
+        }
         $id = $arr['id'];
         $m_c = new companymodel();
         $info = $m_c->getinfobyid($id);
@@ -58,6 +86,10 @@ class companylogic
      */
     public function updatebyid($arr)
     {
+        $result = $this->checkparam(__FUNCTION__, $arr);
+        if (1 != $result['status']) {
+            return $result;
+        }
         $id = $arr['id'];
         unset($arr['id']);
         $m_c = new companymodel();

+ 38 - 3
application/index/logic/userlogic.php

@@ -3,15 +3,35 @@
  * @Author: wang jun
  * @Date: 2022-01-18 11:12:23
  * @Last Modified by: wang jun
- * @Last Modified time: 2022-01-18 15:46:20
+ * @Last Modified time: 2022-01-19 10:24:58
  * 微信类
  */
 namespace app\index\logic;
 
 use app\index\model\usermodel;
 
-class userlogic
+class userlogic extends baselogic
 {
+    /**
+     * 设置请求数据规则
+     * 20220107
+     * wj
+     */
+    protected function setrules()
+    {
+        $list = [
+            'newinfo' => [
+                ['name' => 'openid', 'title' => 'openid', 'require' => true, 'type' => 'string'],
+            ],
+            'realauthbyopenid' => [
+                ['name' => 'openid', 'title' => 'openid', 'require' => true, 'type' => 'string'],
+            ],
+            'updateiscompanybyopenid' => [
+                ['name' => 'openid', 'title' => 'openid', 'require' => true, 'type' => 'string'],
+            ],
+        ];
+        return $list;
+    }
     /**
      * 新建用户
      * wj
@@ -19,9 +39,16 @@ class userlogic
      */
     public function newinfo($arr)
     {
+        $result = $this->checkparam(__FUNCTION__, $arr);
+        if (1 != $result['status']) {
+            return $result;
+        }
         $m_u = new usermodel();
         $id = $m_u->insertData($arr);
-        return $id;
+        if (empty($id)) {
+            return backarr(0, "新增失败");
+        }
+        return backarr(1, "新增成功", ['id' => $id]);
     }
     /**
      * 实名认证
@@ -30,6 +57,10 @@ class userlogic
      */
     public function realauthbyopenid($arr)
     {
+        $result = $this->checkparam(__FUNCTION__, $arr);
+        if (1 != $result['status']) {
+            return $result;
+        }
         $openid = $arr['openid'];
         $m_u = new usermodel();
         $info = $m_u->getinfobyopenid($openid);
@@ -101,6 +132,10 @@ class userlogic
      */
     public function updateiscompanybyopenid($arr)
     {
+        $result = $this->checkparam(__FUNCTION__, $arr);
+        if (1 != $result['status']) {
+            return $result;
+        }
         $openid = $arr['openid'];
         $m_u = new usermodel();
         $info = $m_u->getinfobyopenid($openid);