YinShiController.java 8.2 KB
package com.sincre.springboot.controller;


import com.alibaba.fastjson.JSON;
import com.sincre.springboot.common.MD5;
import com.sincre.springboot.utils.ApiHelper;
import com.sincre.springboot.utils.CacheHelper;
import com.sincre.springboot.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.sincre.springboot.ApiPlatform.YinShiServiceConfig;

@RestController
@RequestMapping("/YinShi")
@Api(value = "YinShiController", tags = "萤石对接接口")
public class YinShiController {

    private static String AccessToken = "at.2scte32926nu6q7j6adhlabg28emicz6-58f6w0596w-1ppubtz-uxh6dnv5x";

    @ApiOperation(value = "用于管理员获取accessToken")
    @GetMapping("/token")
    public String GetYinShiToken() {
        String appKey = YinShiServiceConfig.appKey;
        String appSecret = YinShiServiceConfig.appSecret;

        String url = YinShiServiceConfig.HostUrl + "lapp/token/get";
        Map<String, Object> map = new HashMap<>();

        map.put("appKey", appKey);
        map.put("appSecret", appSecret);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);
        JSONObject jsonObject = new JSONObject(result);
        JSONObject data = jsonObject.optJSONObject("data");
        YinShiServiceConfig.AccessToken = data.optString("accessToken");
        Date date = new Date();
        Date dateFu = new Date(data.optLong("expireTime"));
        CacheHelper.putYingshiYunToken(YinShiServiceConfig.AccessToken, (int) ((dateFu.getTime()-date.getTime())/1000));
        return ResultUtils.getInstance().resturnResultYingshi(result);
    }

    @ApiOperation(value = "增加子账号")
    @GetMapping("/addChildAccount")
    public String addChildAccount(@RequestParam String accountName, @RequestParam String password) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/create";
        Map<String, Object> map = new HashMap<>();
        //子账户密码,LowerCase(MD5(AppKey#密码明文))
        password = YinShiServiceConfig.appKey + "#" + password;
        try {
            password = MD5.md5(password, "").toLowerCase();
        } catch (Exception ex) {
            ex.getStackTrace();
        }
        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountName", accountName);
        map.put("password", password);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);
        return ResultUtils.getInstance().resturnResultYingshi(result);
    }

    @ApiOperation(value = "获取单个子账户信息")
    @GetMapping("getChildAccount")
    public String getChildAccount(@RequestParam String accountId, @RequestParam String accountName) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/get";
        Map<String, Object> map = new HashMap<>();
        //子账户密码,LowerCase(MD5(AppKey#密码明文))
        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        map.put("accountName", accountName);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        return result;
    }

    @ApiOperation(value = "获取子账号信息列表")
    @GetMapping("getChildAccountList")
    public String getChildAccountList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/list";
        Map<String, Object> map = new HashMap<>();
        //子账户密码,LowerCase(MD5(AppKey#密码明文))

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("pageStart", pageIndex);
        map.put("pageSize", pageSize);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        return result;
    }

    @ApiOperation(value = "修改当前子账户密码")
    @GetMapping("updateChildPassword")
    public String updateChildPassword(@RequestParam("accountId") String accountId, @RequestParam("newPassword") String newPassword, @RequestParam("oldPassword") String oldPassword) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/updatePassword";
        Map<String, Object> map = new HashMap<>();
        //子账户密码,LowerCase(MD5(AppKey#密码明文))

        oldPassword = YinShiServiceConfig.appKey + "#" + oldPassword;
        newPassword = YinShiServiceConfig.appKey + "#" + newPassword;
        try {
            oldPassword = MD5.md5(oldPassword, "").toLowerCase();
            newPassword = MD5.md5(newPassword, "").toLowerCase();
        } catch (Exception ex) {
            ex.getStackTrace();
        }
        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        map.put("oldPassword", oldPassword);
        map.put("newPassword", newPassword);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        return result;
    }

    @ApiOperation(value = "删除指定子账户")
    @GetMapping("deleteChildAccount")
    public String deleteChildAccount(@RequestParam String accountId) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/delete";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        return result;
    }


    @ApiOperation(value = "设置子账户的授权策略")
    @GetMapping("policySet")
    public String policySet(@RequestParam String accountId, @RequestParam String policy) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/policy/set";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        map.put("policy", policy);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        return result;
    }

    @ApiOperation(value = "增加子账户授权策略中的授权语句")
    @GetMapping("policyAdd")
    public String policyAdd(@RequestParam String accountId, @RequestParam String statement) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/statement/add";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        map.put("statement", statement);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        return result;
    }

    @ApiOperation(value = "删除子账户授权策略中指定设备的所有授权语句")
    @GetMapping("policyDelete")
    public String policyDelete(@RequestParam String accountId, @RequestParam String deviceSerial) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/statement/delete";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        map.put("deviceSerial", deviceSerial);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        return result;
    }

    @ApiOperation(value = "获取B模式子账户accessToken")
    @GetMapping("getChildAccountToken")
    public String getChildAccountToken(@RequestParam String accountId) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/token/get";
        Map<String, Object> map1 = new HashMap<>();

        map1.put("accessToken", YinShiServiceConfig.AccessToken);
        map1.put("accountId", accountId);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map1);

        return result;
    }

    

}