lvhao před 11 hodinami
rodič
revize
7f2a99d671

+ 54 - 21
core/CoreApp/controllers/Lyerpapi/v1/User.php

@@ -9,35 +9,68 @@ class User extends Lyapi_Controller{
         parent::__construct(); // 暂时注释,或者使用一个新的不校验的基类
         // 简便做法:复制 Lyapi_Controller 的部分代码但不调用 _check_api_auth
        // $this->load->driver('cache'); // 加载缓存驱动
+       $this->load->_model("Model_logic_tools","logic_tools");
+       $this->load->_model("Model_user",'user');
+       $this->load->_model("Model_power",'power');
+        
     }
 
     public function login() {
-        $user_id =123;
-        // ... 验证账号密码成功,得到 $user_id
-        // $data = [
-        //     'user_id' => $user_id,
-        //     'created_at' => time()
-        // ];
-        // $this->cache->save("lvhaoceshi1", $data, 100);
-        echo "<pre>";
-        print_r($this->cache->get("lvhaoceshi1"));
-        die;
-        //$this->load->driver('cache'); // 确保缓存可用
+
+        if($this->input->method(TRUE) != 'POST'){
+
+            $this->_json_error('请求方式错误','405');
+        }
+        $json_str = $this->input->raw_input_stream;
+        $data = json_decode($json_str,true);
+        $account = $data['account'];
+        $pass = $data['pass'];
+        $account = $this->logic_tools->toolsjiemi($account,"v!frlbpnjgir6alv","k!2w94m6jt!6ook4");
+        $pass = $this->logic_tools->toolsjiemi($pass,"v!frlbpnjgir6alv","k!2w94m6jt!6ook4");
+
+        $pass = sha1($pass);
+        $userinfo = $this->user->find('userid = "'.$account.'"');
+        if(empty($userinfo)){
+            $this->_json_error('账号不存在','500');
+        }
+        if($userinfo['userpass'] != $pass){
+            $this->_json_error('密码错误','500');
+        }
+        $power = $this->power->read($userinfo['power']);
+        if(empty($power)){
+            $this->_json_error('角色未设置','500');
+        }
+        if(empty($power['lyapiid'])){
+            $this->_json_error('权限未设置','500');
+        }
+        $lyapiids = explode("|",trim($power['lyapiid'],"|"));
+        $res = $this->power->_lyapi();
+        $lyapi_list = $res['lyapi_list'];
+        $all_arr = [];
+        foreach($lyapi_list as $v){
+            if(in_array($v['id'],$lyapiids)){
+                $all_arr[] = $v['shortname'];
+            }
+        }
+
 
         // 生成唯一 token(可以用 JWT 或随机字符串)
         $token = bin2hex(random_bytes(32));
         $cache_key = 'token:' . md5($token);
 
         // 写入缓存,有效期7200秒(2小时)
-        $this->cache->save($cache_key, $user_id, 7200);
-
-        // 返回 token 给客户端
-        $this->output
-            ->set_content_type('application/json')
-            ->set_output(json_encode([
-                'status' => true,
-                'token'  => $token,
-                'expire' => 7200
-            ]));
+        $this->cache->save($cache_key, [
+            'username'=>$userinfo['userid'],
+            'mobile'=>'',
+            'token'=>$token,
+            'power'=>$all_arr
+        ], 7200);
+
+        $this->_json_error("登陆成功",200,[
+            'username'=>$userinfo['userid'],
+            'mobile'=>'',
+            'token'=>$token,
+            'lypower'=>implode(',',$all_arr)
+        ]);
     }
 }

+ 36 - 0
core/CoreApp/controllers/Power.php

@@ -139,6 +139,10 @@ class Power extends Start_Controller {
 			$excelid = $this->input->post('excelid',true);
 			$post['exceltext'] = "|".ltrim($exceltext,'|');
 			$post['excelid'] = "|".ltrim($excelid,'|');
+			$lyapiid = $this->input->post('lyapiid',true);
+			$lyapitext = $this->input->post('lyapitext',true);
+			$post['lyapitext'] = "|".ltrim($lyapitext,'|');
+			$post['lyapiid'] = "|".ltrim($lyapiid,'|');
 			if($this->power->save($post,$id))
         	{
          		echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
@@ -250,6 +254,38 @@ class Power extends Start_Controller {
 				}
 			}
 		    echo json_encode(array('msg'=>($an),'power'=>($power),'success'=>true));
+		}elseif(isset($post['lyapi'])){
+			$lyapi = $this->input->post('lyapi',true);
+			$rows = $lyapi;
+			$res = $this->power->_lyapi();
+			$dataa = $res['type_list'];
+			$datab = $res['lyapi_list'];
+			$an = array();
+			foreach ($dataa as $key=>$value) 
+		    {
+			    foreach ($datab as $ke=>$va) 
+		        {
+			    	if($va['classid'] == $value['id'])
+			        {
+						$value['bn'][]=$va;
+			        }
+			     }//循环出二级导航航
+				 $an[] = $value;
+            }
+			$power = array();
+			if($rows != "null")
+			{
+			    $power = $this->power->read($rows);//找出次ID的权限内容
+				if($power['lyapiid'])
+				{
+			        $power = explode('|',trim($power['lyapiid'],'|'));//数组化权限内容
+				}
+				else
+				{
+					$power = array();
+				}
+			}
+		    echo json_encode(array('msg'=>($an),'power'=>($power),'success'=>true));
 		}
     }
 

+ 28 - 0
core/CoreApp/core/Lin_Controller.php

@@ -170,5 +170,33 @@ abstract class Lyapi_Controller extends CI_Controller {
 		parent::__construct();
 		$this->load->driver('cache');
 		$this->cache = $this->cache;
+
+		
+		//  header('Access-Control-Allow-Origin: *');
+		//  header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
+		//  header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
+		//  if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
+		// 	 header('HTTP/1.1 200 OK');
+		// 	 exit();
+		//  }
+		 $uri_path = $this->uri->uri_string();
+		 if($uri_path != "lyapi/v1/user/login"){
+			$this->_check_api_auth();
+		 }
+		 
 	}
+	function  _check_api_auth(){
+		$this->_json_error('请登录',401);
+	}
+
+	 /**
+     * 统一返回 JSON 错误并终止脚本
+     */
+    protected function _json_error($msg, $code = 401,$data = []) {
+		exit(json_encode([
+			'code' => $code,
+			'msg'=>$msg,
+			'data' => $data
+		],JSON_UNESCAPED_UNICODE));
+    }
 }

+ 23 - 0
core/CoreApp/models/Model_power.php

@@ -12,4 +12,27 @@ class Model_power extends Lin_Model
 	{
 		return  $this->find("powertext like '%$powertext%'");	
 	}
+
+	public function _lyapi(){
+		$type_list = [
+			[
+				'id'=>100001,
+				'title'=>"仓库管理",
+				'sort'=>10,
+			]
+		];
+		$lyapi_list = [
+			[
+				'id'=>1,
+				'classid'=>100001,
+				'shortname'=>'cksccktp',
+				'title'=>"上传出库图片",
+				'sort'=>10,
+			]
+		];
+		return [
+			'type_list' => $type_list,
+			'lyapi_list' => $lyapi_list,
+		];
+	}
 }  //end class

+ 85 - 0
template/erp/power_edit.html

@@ -1,4 +1,18 @@
 {Template header}
+<style>
+    button{
+        width: 100px;
+        height: 26px;
+        line-height: 26px;
+        margin: 0 16px;
+        display: inline-block;
+        background: -webkit-gradient(linear, 0 0, 0 bottom, from(#fff), to(#f6f4f4));
+        border-radius: 5px;
+        border: 1px solid #999;
+        color: #0061b9;
+        cursor: pointer;
+    }
+</style>
 <body>
 <div class="warp">
 <div class="title winnone">权限管理 - 修改</div>
@@ -29,10 +43,81 @@
 <input type="hidden" name="excelid" value="{$power['excelid']}" class="excelid" /><span class="set excelidbn" data-rows="{$power['id']}" data-class="powerid">设置下载</span>
 </li>
 
+<li class="length">
+<em>手机权限:</em>
+<textarea name="lyapitext" class="must lyapitext" disabled="true" style="width:67%">{$power['lyapitext']}</textarea>
+<input type="hidden" name="lyapiid" value="{$power['lyapiid']}" class="lyapiid" /><span class="set lyapibn" data-rows="{$power['id']}" data-class="lyapiid">设置权限</span>
+</li>
+
 <div style="clear:both;"></div>
 </ul>
 <input type="hidden" name="id" value="{$power['id']}" />
 <div class="button"><font class="datasave">提 交</font> <font class="fh">关 闭</font></div>
 </div>
 <script>var addedit="/power/edit/";var rowsurl="/power/rows/";</script>
+<script>
+    //加载权限框
+$(".lyapibn").click(function() { 
+$(".list div .fj").html("");
+$(".list em").html("<button id='lv_qd_lyapi' onclick='lyapiclick()'>确 定</button> <button id='lv_qx' onclick='$(\".list\").hide();'>取 消</button>");
+$(".ts p").html("数据加载中...");
+$(".ts").fadeIn();
+ $.ajax({
+        url: rowsurl,
+        data: "lyapi="+$(this).data("rows"),
+        type: "POST",
+        dataType: "json",
+        success: function(b) {
+				for (i = 0; i < b.msg.length; i++) 
+				{
+				    $(".list div .fj").append("<li class="+ b.msg[i]["id"] +"><label><span><input name='check' type='checkbox' value=" + b.msg[i]["id"] + "></span><span>"+ b.msg[i]["title"]+"</span></label>");
+					if(b.msg[i]["bn"])
+					{
+					$(".list div .fj").children("."+b.msg[i]["id"]).append("<ul class='bn'>");
+					for (a = 0; a < b.msg[i]["bn"].length; a++) 
+				    {
+					    $(".list div .fj").children("."+b.msg[i]["id"]).children(".bn").append("<li class="+b.msg[i]["bn"][a]["id"]+"><label><span><input name='check' type='checkbox' value="+b.msg[i]["bn"][a]["id"]+"></span><span>"+b.msg[i]["bn"][a]["title"]+"</span></label>");
+						
+						if(b.msg[i]["bn"][a]["cn"])
+						{
+							$(".list div .fj").children("."+b.msg[i]["id"]).children(".bn").children("."+b.msg[i]["bn"][a]["id"]).append("<ul class='cn'>");
+						    for (v = 0; v < b.msg[i]["bn"][a]["cn"].length; v++) 
+				            {
+							   $(".list div .fj").children("."+b.msg[i]["id"]).children(".bn").children("."+b.msg[i]["bn"][a]["id"]).children(".cn").append("<li class="+b.msg[i]["bn"][a]["cn"][v]["id"]+"><label><span><input name='check' type='checkbox' value="+b.msg[i]["bn"][a]["cn"][v]["id"]+"></span><span>"+ b.msg[i]["bn"][a]["cn"][v]["title"] +"</span></label></li>");
+						    }
+							$(".list div .fj").children("."+b.msg[i]["id"]).children(".bn").children("."+b.msg[i]["bn"][a]["id"]).append("</ul>");
+						}
+						
+						$(".list div .fj ."+b.msg[i]["id"]+" ul").append("</li>");
+					}
+					$(".list div .fj").children("."+b.msg[i]["id"]).append("</ul>");
+					}
+					$(".list div .fj").append("</li>");
+				}
+				$.each(b.power, function(d, e) {
+					$("."+e).children().children().children().prop("checked",true);
+				})
+				$(".ts").fadeOut();
+				$(".list").show();
+        }
+    });
+
+});
+
+function lyapiclick(){
+    var a = "";
+    var b = "";
+    $(".list label").each(function() {
+        if ($(this).find("input[name='check']:checked").is(':checked')) {
+            a = a + $(this).find("input[name='check']:checked").val() + "|";
+            b = b + $(this).find("input[name='check']:checked").parent().next("span").text() + "|";
+        }
+    });
+    $(".lyapitext").val(b);
+    $(".lyapiid").val(a);
+    $(".list").hide();
+}
+
+
+</script>
 {Template footer}