qinbo
2022-01-21 e8af50a22b2c521416d85eb9926904dc377daa97
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"use script"; //开发环境建议开启严格模式
//该js是对后端服务请求的统一入口封装方法
 
//是否有服务端,false时存储数据在浏览器缓存内
var hasServer = false;
 
var baseUrl = "http://192.168.0.40:9002";
 
//统一访问后台接口方法
function sendAjax(opts) {
  console.log("请求信息:\n" + JSON.stringify(opts)); //日志
  $.ajax({
    url: baseUrl + "/" + opts.url,
    type: opts.type || "post",
    dataType: opts.dataType || "json",
    timeout: opts.timeout || 12000,
    contentType: opts.contentType,
    data: opts.data,
    crossDomain: true,
    beforeSend: function (request) {
      if (opts.loading) {
        haoutil.loading.show(opts.loading);
      }
      localforage.getItem("access_token").then((token) => {
        if (token) {
          request.setRequestHeader("Authorization", "Bearer " + token);
        }
      });
    },
    success: function (result, status, xhr) {
      if (opts.loading) {
        haoutil.loading.hide();
      }
      console.log("返回结果:\n" + JSON.stringify(result)); //日志
      if (result.msg == "操作成功") {
        if (opts.success) {
          opts.success(result.data);
        }
      } else if (result.access_token) {
        if (opts.success) {
          opts.success(result);
        }
      } else {
        haoutil.msg(result.msg);
        if (opts.error) {
          opts.error();
        }
      }
    },
    error: function (data) {
      if (opts.loading) {
        haoutil.loading.hide();
      }
      if (opts.noError) {
        return;
      }
 
      console.log("服务访问出错:\n" + JSON.stringify(data)); //日志
 
      if (data.status == 401 && window.top) {
        top.location.href = "/theme/login.html";
      }
      var msg = opts.errorMsg || "服务访问出错(" + data.status + "):" + data.responseText;
      haoutil.msg(msg);
 
      if (opts.error) {
        opts.error();
      }
    },
  });
}
 
//模拟登录
function sendLoginAjax(arg) {
  var username = arg.username;
  var password = arg.password;
  var client_id = arg.client_id || "d809b060-9506-44b8-9689-d804d1804778";
  var client_secret = arg.client_secret || "e5572d49-70b4-43bd-96ba-380556e66692";
  if (!username || !password || !client_id || !client_secret) {
    return;
  }
  if (!hasServer) {
    return;
  }
  sendAjax({
    url: "public/login",
    data: "username=" + username + "&password=" + password + "&client_id=" + client_id + "&client_secret=" + client_secret,
    type: "post",
    success: function (data) {
      localforage.setItem("access_token", data.access_token);
      localforage.setItem("refresh_token", data.refresh_token);
      //获取当前用户的userId
      getUserId();
    },
  });
}
 
var userId;
function getUserId() {
  sendAjax({
    url: "v1/user/me",
    type: "get",
    success: function (data) {
      userId = data.userId;
    },
  });
}
 
//模拟登录
if (hasServer) {
  sendLoginAjax({
    username: "admin",
    password: "admin",
  });
}