first commit
This commit is contained in:
3
pages/index/index.acss
Normal file
3
pages/index/index.acss
Normal file
@@ -0,0 +1,3 @@
|
||||
button + button {
|
||||
margin-top: 32rpx;
|
||||
}
|
||||
54
pages/index/index.axml
Normal file
54
pages/index/index.axml
Normal file
@@ -0,0 +1,54 @@
|
||||
<view class="page">
|
||||
<form onSubmit="onSubmit" onReset="onReset">
|
||||
<view class="page-section">
|
||||
<view class="form-line" />
|
||||
<view class="form-row">
|
||||
<!-- -->
|
||||
<view class="form-row-label">地址</view>
|
||||
<view class="form-row-content">
|
||||
<input name="apiurl" class="input" placeholder="https://" onBlur="postdb" />
|
||||
</view>
|
||||
</view>
|
||||
<!-- -->
|
||||
<!-- -->
|
||||
<view class="page-section">
|
||||
<picker onChange="bindPickerChange" value="{{index}}" range="{{databaseList}}" >
|
||||
<view class="form-row">
|
||||
<view class="form-row-label">账套</view>
|
||||
<input name="databaseName" class="form-row-content" value="{{databaseList[index]}}" placeholder="****"/>
|
||||
</view>
|
||||
</picker>
|
||||
</view>
|
||||
<!-- -->
|
||||
<!-- -->
|
||||
<view class="form-row">
|
||||
<view class="form-row-label">用户</view>
|
||||
<view class="form-row-content">
|
||||
<input name="username" class="input" placeholder="***@**.com" />
|
||||
</view>
|
||||
</view>
|
||||
<!-- -->
|
||||
<!-- -->
|
||||
<view class="form-row">
|
||||
<view class="form-row-label">密码</view>
|
||||
<view class="form-row-content">
|
||||
<input name="password" class="input" placeholder="*****" password />
|
||||
</view>
|
||||
</view>
|
||||
<!-- -->
|
||||
<!-- -->
|
||||
<view class="page-section-btns">
|
||||
<view>
|
||||
<button type="ghost" size="mini" formType="reset">重置</button></view>
|
||||
<view>
|
||||
<button type="primary" size="mini" formType="submit">提交</button></view>
|
||||
</view>
|
||||
</view>
|
||||
<!-- -->
|
||||
</form>
|
||||
<view class="page-section">
|
||||
<view class="page-section-demo">
|
||||
<text> v1.0.9 </text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
329
pages/index/index.js
Normal file
329
pages/index/index.js
Normal file
@@ -0,0 +1,329 @@
|
||||
Page({
|
||||
data: {
|
||||
apiurl: '',
|
||||
username: '',
|
||||
password: '',
|
||||
databaseList: '',
|
||||
index: 0, //数据库选择的页面ID
|
||||
databaseName: '',
|
||||
Uid: null, //登录获取的信息保存本地设备
|
||||
},
|
||||
onLoad(query) {
|
||||
// 页面加载
|
||||
console.info(`胜佳Page onLoad with query: ${JSON.stringify(query)}`);
|
||||
// 检查本地缓存中是否有登录信息 要四个不为空才渲染//
|
||||
const loginInfo = dd.getStorageSync({
|
||||
key: 'loginInfo'
|
||||
}).data;
|
||||
if (loginInfo && loginInfo.apiurl && loginInfo.databaseName && loginInfo.username && loginInfo.password && loginInfo.uid > 0) {
|
||||
const {
|
||||
apiurl,
|
||||
databaseName,
|
||||
username,
|
||||
password
|
||||
} = loginInfo;
|
||||
// 这里假定 apiurl 可以从其他地方获取或者有默认值';
|
||||
this.setData({
|
||||
apiurl,
|
||||
databaseName,
|
||||
username,
|
||||
password
|
||||
});
|
||||
dd.switchTab({
|
||||
url: '/pages/deal/deal',
|
||||
success: () => {
|
||||
dd.showToast({
|
||||
content: '登录成功,已跳转页面',
|
||||
type:'success',
|
||||
duration: 1000,
|
||||
success() {
|
||||
console.log('toast end');
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
} else {
|
||||
console.log('本地缓存中未获取到登录信息');
|
||||
}
|
||||
|
||||
},
|
||||
onReady() {
|
||||
// 页面加载完成
|
||||
},
|
||||
onShow() {
|
||||
// 页面显示
|
||||
},
|
||||
onHide() {
|
||||
// 页面隐藏
|
||||
},
|
||||
onUnload() {
|
||||
// 页面被关闭
|
||||
},
|
||||
onTitleClick() {
|
||||
// 标题被点击
|
||||
},
|
||||
onPullDownRefresh() {
|
||||
// 页面被下拉
|
||||
},
|
||||
onReachBottom() {
|
||||
// 页面被拉到底部
|
||||
},
|
||||
onShareAppMessage() {
|
||||
// 返回自定义分享信息
|
||||
return {
|
||||
title: '钉钉胜佳',
|
||||
desc: 'My App description',
|
||||
path: 'pages/index/index',
|
||||
};
|
||||
},
|
||||
onSubmit(e) {
|
||||
if (!e || !e.detail || !e.detail.value) {
|
||||
|
||||
dd.showToast({
|
||||
content: '传入的事件对象或其属性未定义',
|
||||
type: 'none',
|
||||
duration: 3000,
|
||||
success() {
|
||||
console.log('toast end');
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const {
|
||||
apiurl,
|
||||
databaseName
|
||||
} = this.data;
|
||||
const {
|
||||
username: username,
|
||||
password: password
|
||||
} = e.detail.value;
|
||||
|
||||
this.setData({
|
||||
apiurl,
|
||||
databaseName,
|
||||
username,
|
||||
password
|
||||
});
|
||||
|
||||
if (!databaseName || !apiurl) {
|
||||
dd.showToast({
|
||||
content: '账套或地址有为空',
|
||||
type: 'none',
|
||||
duration: 3000,
|
||||
success() {
|
||||
console.log('toast end');
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const data = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": "call",
|
||||
"id": 2,
|
||||
"params": {
|
||||
"service": "common",
|
||||
"method": "authenticate",
|
||||
"args": [
|
||||
databaseName,
|
||||
username,
|
||||
password,
|
||||
{}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
dd.httpRequest({
|
||||
url: `${apiurl}/jsonrpc`,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
data: JSON.stringify(data),
|
||||
success: (res) => {
|
||||
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||
reject(new Error(`HTTP error! status: ${res.statusCode}`));
|
||||
return;
|
||||
}
|
||||
const result = res.data;
|
||||
if (Object.prototype.hasOwnProperty.call(result, 'error')) {
|
||||
reject(new Error(`认证失败: ${JSON.stringify(result.error)}`));
|
||||
return;
|
||||
}
|
||||
const uid = result.result;
|
||||
|
||||
try {
|
||||
const loginInfo = {
|
||||
apiurl,
|
||||
uid,
|
||||
username,
|
||||
password,
|
||||
databaseName
|
||||
};
|
||||
dd.setStorageSync({
|
||||
key: 'loginInfo',
|
||||
data: loginInfo
|
||||
});
|
||||
} catch (e) {
|
||||
reject(new Error(`保存登录信息到本地缓存失败: ${e.message}`));
|
||||
return;
|
||||
}
|
||||
|
||||
if (uid) {
|
||||
dd.switchTab({
|
||||
url: '/pages/deal/deal',
|
||||
success: () => {
|
||||
dd.showToast({
|
||||
content: '登录成功,已跳转页面',
|
||||
type: 'success',
|
||||
duration: 3000,
|
||||
success() {
|
||||
console.log('toast end');
|
||||
}
|
||||
});
|
||||
resolve(uid);
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(new Error(`页面跳转失败: ${JSON.stringify(err)}`));
|
||||
}
|
||||
});
|
||||
} else {
|
||||
reject(new Error('认证失败,未获取到有效的用户 ID'));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(new Error(`请求失败: ${JSON.stringify(err)}`));
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch((error) => {
|
||||
dd.showToast({
|
||||
content: error.message,
|
||||
type: 'none',
|
||||
duration: 3000,
|
||||
success() {
|
||||
console.log('toast end');
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
onReset() {
|
||||
dd.clearStorageSync();
|
||||
this.setData({
|
||||
apiurl: '',
|
||||
username: '',
|
||||
password: ''
|
||||
});
|
||||
},
|
||||
//失去聚焦触发根据url获取数据库列表
|
||||
postdb(e) {
|
||||
const apiurl = e.detail.value;
|
||||
console.log(apiurl)
|
||||
if (!apiurl) {
|
||||
return;
|
||||
}
|
||||
const data = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": "call",
|
||||
"params": {
|
||||
"service": "db",
|
||||
"method": "list",
|
||||
"args": []
|
||||
},
|
||||
"id": 1
|
||||
};
|
||||
|
||||
new Promise((resolve, reject) => {
|
||||
dd.httpRequest({
|
||||
url: `${apiurl}/jsonrpc`,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
data: JSON.stringify(data),
|
||||
success: (res) => {
|
||||
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||
reject(new Error(`HTTP error! status: ${res.statusCode}`));
|
||||
return;
|
||||
}
|
||||
const result = res.data;
|
||||
if ('error' in result) {
|
||||
resolve([]);
|
||||
} else if ('result' in result) {
|
||||
if (result['result'].length > 0) {
|
||||
// this.setData({
|
||||
// username: '',
|
||||
// password: '',
|
||||
// });
|
||||
}
|
||||
resolve(result['result']);
|
||||
} else {
|
||||
reject(new Error('请求发生错误'));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
})
|
||||
.then((databaseList) => {
|
||||
this.setData({
|
||||
databaseList,
|
||||
});
|
||||
if (databaseList.length > 0) {
|
||||
this.setData({
|
||||
databaseName: databaseList[0],
|
||||
index: 0,
|
||||
apiurl: apiurl
|
||||
});
|
||||
dd.showToast({
|
||||
content: '成功获取可用数据库列表',
|
||||
type: 'success',
|
||||
duration: 1000,
|
||||
success() {
|
||||
console.log('toast end');
|
||||
},
|
||||
});
|
||||
} else {
|
||||
dd.showToast({
|
||||
content: '未获取到可用的数据库。',
|
||||
type: 'none',
|
||||
duration: 1000,
|
||||
success() {
|
||||
console.log('toast end');
|
||||
},
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
dd.showToast({
|
||||
content: '请求发生错误',
|
||||
type: 'none',
|
||||
duration: 1000,
|
||||
success() {
|
||||
console.log('toast end');
|
||||
},
|
||||
fail() {
|
||||
console.log(error.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
//
|
||||
//数据库列表选择逻辑层获取name值
|
||||
bindPickerChange(e) {
|
||||
const selectedDatabase = this.data.databaseList[e.detail.value]; // 根据索引获取数据库名称
|
||||
if (selectedDatabase) {
|
||||
this.setData({
|
||||
databaseName: selectedDatabase, // 设置数据库名称
|
||||
index: e.detail.value, // 更新索引
|
||||
});
|
||||
} else {
|
||||
console.log('未找到对应的数据库');
|
||||
}
|
||||
},
|
||||
//
|
||||
});
|
||||
3
pages/index/index.json
Normal file
3
pages/index/index.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"defaultTitle": "访问"
|
||||
}
|
||||
Reference in New Issue
Block a user