获取图形验证码
<h5>简要描述</h5>
<ul>
<li>请求URL不是固定的,生成方式请查看[API总体说明]</li>
</ul>
<h5>请求URL</h5>
<ul>
<li><code>https://52.yunhu365.com/ErpServer/h5api/12116/st0908_1</code> </li>
</ul>
<h5>请求方式</h5>
<ul>
<li>post</li>
</ul>
<h5>Header</h5>
<table>
<thead>
<tr>
<th style="text-align: left;">header</th>
<th style="text-align: left;">必选</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;">sign</td>
<td style="text-align: left;">是</td>
<td>string</td>
<td>上传的json字符串经过md5签名的值</td>
</tr>
<tr>
<td style="text-align: left;">token</td>
<td style="text-align: left;">是</td>
<td>string</td>
<td>登录成功后返回的用户token,登录前传入空字符串</td>
</tr>
</tbody>
</table>
<h5>请求参数示例</h5>
<pre><code>{
"actionName": "getcode"
}
</code></pre>
<h5>json字段说明</h5>
<table>
<thead>
<tr>
<th style="text-align: left;">字段名</th>
<th style="text-align: left;">必选</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;">actionName</td>
<td style="text-align: left;">是</td>
<td>string</td>
<td>固定传getcode</td>
</tr>
</tbody>
</table>
<h5>成功返回示例</h5>
<pre><code>{
"code": 1,
"msg": "",
"data": {
"img": "data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAoCAYAAAAIeF9DAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAKySURBVGhD7ZeN0cMgCIYzXgbKONklq3STfJqqoCJimh\/uO9477tpGjPAg2mk3qZIBUSYDokwGRJkMiDIZEGUyIMpkQJTJgCiTAVEmA6JMBkSZDIgyGRBlMiDKZECU6T4gn3Wfp2mfpnlfP+G3lrbFjfNjkS1beNjTti+l77S4X0f12df5IV8qXmc+5JuAxAV644DgcbRxXD7rTPpEEzN1grnGgch9+\/HeAiRPVBvItqCFFNmDZw1\/XGWZr7QYQPl6x4CM+OJ452JhaZ7w\/TqlVhWtn9BycVEpgHl1acZCSSe3Qe85KCuKw+RAhnxRXlpLOqCEzxcJErEsMeE0EAhGEkQxhyA4AN6YvyiceY6VLgBywjftgKq4sLZrgaSX+iylhFBApBUM4\/AugjbBJQ8O+\/oV+UXgeN4DmHTONxVgJ97rgKSqCYsSAmm1q69ocO1WhsW9IyQVJ2cUyCnfni7bIRB8WqcQiHSHwLhffBn9ktSLgFx2hmStKooF4h\/7Z864Kse9Oo2TJ1rWJoJeBxJ2Xvh2XmWriuoAgeftgxkOfmcEEL7dIX\/1QFCRhV9OCiaqYu4BwYtwViY3g+Ht3wLJ8\/ATELJVRXWBeOU3lsrcvPU7UACdROsHUsTv1nkeSKtVRYmAfAXX2GjgUydVCkQO7tDTQIr\/MnGNp4HUSRQYe02lRLenBImdj\/Zt6kkgBQy8PuVAYEvjIod3cwmgfZt6CkgaS6\/tx0OdUXox3bIkSW2OSRXGtMPRBD8BpNgZVKG8BgQvjq5gqPC65fTOh8Hzw+t2IPkB3lrWe0Bw0soxWSU1gkzzO8taITMvp5uB4BbPnWkvAvEqrn2V8cnpnWPSzXHoViC9OJEFj+slAvJV9SfQmehmdIgIdvjy4HQnkOLsYC24mJTIgCiTAVEmA6JK+\/4Hq8GewEvHDv0AAAAASUVORK5CYII=",
"codeid": 1
}
}
</code></pre>
<h5>成功返回示例的参数说明</h5>
<table>
<thead>
<tr>
<th style="text-align: left;">参数名</th>
<th style="text-align: left;">类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;">code</td>
<td style="text-align: left;">string</td>
<td>错误码,1表示成功</td>
</tr>
<tr>
<td style="text-align: left;">msg</td>
<td style="text-align: left;">string</td>
<td>如果有错误此值不为空,否则值为空字符串</td>
</tr>
<tr>
<td style="text-align: left;">data</td>
<td style="text-align: left;">object</td>
<td>返回的数据对象</td>
</tr>
<tr>
<td style="text-align: left;">img</td>
<td style="text-align: left;">string</td>
<td>验证码的图片</td>
</tr>
<tr>
<td style="text-align: left;">codeid</td>
<td style="text-align: left;">string</td>
<td>验证码ID,在获取手机号时需要上传此值</td>
</tr>
</tbody>
</table>
<h5>失败返回示例</h5>
<pre><code>{
"code": 0,
"msg": "请求不合法"
}
</code></pre>
<h5>失败返回示例的参数说明</h5>
<table>
<thead>
<tr>
<th style="text-align: left;">参数名</th>
<th style="text-align: left;">类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;">code</td>
<td style="text-align: left;">string</td>
<td>0表示产生错误</td>
</tr>
<tr>
<td style="text-align: left;">msg</td>
<td style="text-align: left;">string</td>
<td>如果有错误时此值不为空</td>
</tr>
</tbody>
</table>
<h5>备注</h5>
<p><strong>示例代码:</strong> </p>
<pre><code class="language-language"><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
<script>
//全局对象AppConfig存放服务器传来的数据
var AppConfig = {};
AppConfig.serverurl = "http://127.0.0.1/ErpServer/h5api/10001/sa2022_1";
AppConfig.token = "";
AppConfig.singkey = "";
AppConfig.userid = 0;//用户登录后服务器传来的用户id
AppConfig.username = "";//用户登录后服务器传来的用户名
AppConfig.shopid = 0;//用户登录后服务器传来的店铺id
AppConfig.shopname = "";//用户登录后服务器传来的店铺名
AppConfig.shoplogo = "";//用户登录后服务器传来的店铺logo
//获取图形验证码
function getcode() {
var obj = new Object();
obj.actionName = "getcode";
postdata(obj);
}
//获取店铺信息
function getshop() {
var obj = new Object();
obj.actionName = "getshop";
postdata(obj);
}
//用户登录
function login() {
debugger;
var obj = new Object();
obj.actionName = "login";
obj.logintype = 0;
obj.mbphone = "18062639596";
obj.upw = "123456";
postdata(obj);
}
function postdata(obj) {
//将对象obj转换为json字符串
var jsondata = JSON.stringify(obj);
//数据签名
var sign = "";
if (AppConfig.token == null || AppConfig.token.length == 0)
sign = calculateMD5("json=" + jsondata);
else
sign = calculateMD5("json=" + jsondata + "&key=" + AppConfig.singkey + AppConfig.token);
sign = sign.toUpperCase();
debugger;
//发送请求
var xhr = new XMLHttpRequest();
xhr.open('POST', AppConfig.serverurl, true);
xhr.setRequestHeader('Content-Type', 'application/json');
//在header中传递token
xhr.setRequestHeader('token', AppConfig.token);
//将sign放在header中传递
xhr.setRequestHeader('sign', sign);
xhr.onload = function () {
if (this.status == 200) {
var res = this.responseText;
if (res == null || res.length == 0) {
alert("返回数据为空");
return;
}
document.getElementById("id_error").innerHTML = res;
//解析返回数据
var obj = JSON.parse(res);
if (obj.code == 1) {
debugger;
document.getElementById("id_img").src = obj.data.img;
document.getElementById("id_img").style.display = "";
} else {
if (obj.code == 2) {
//店铺未上线,跳转店铺建立中的页面
} else if (obj.code == 3) {
//店铺已关闭,跳转到警请期待页面
} else if (obj.code == 4) {
//店铺已到期,跳转到续费页面
} else if (obj.code == 5) {
//请重新登录,跳转到登录页面
} else if (obj.code == 6) {
//超时或未登录,跳转到登录页面
} else if (obj.code == 7) {
//签名不正确,跳转到登录页面
}
}
}
};
//发送数据
xhr.send(jsondata);
}
// 使用 CryptoJS 计算 MD5 哈希值
function calculateMD5(value) {
return CryptoJS.MD5(value).toString();
}
getcode();
</script>
<div id="id_error"></div>
<img alt="" src="" id="id_img" style="display:none">
</body>
</html></code></pre>