| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- // 设备授权相关接口封装
- import { confirm as showAppConfirm } from '@/utils/confirm.js'
- // 获取设备授权信息:入参 imei,返回 { authorized: boolean, hospSign?: string, hospitalName?, deptName?, contactPhone? }
- // 移除调试模块
- // 统一的错误提示方法(H5不支持error图标,这里统一用none)
- function showErrorToast(message) {
- try {
- const text = String(message || '请求失败');
- uni.showToast({ title: text, icon: 'none' });
- } catch (e) {
- // 忽略异常
- }
- }
- // 统一的错误弹窗提示
- function showErrorModal(message) {
- try {
- const text = String(message || '请求失败');
- // 使用公共弹窗组件 app-confirm
- showAppConfirm({
- title: '提示',
- content: text,
- confirmText: '我知道了',
- cancelText: '取消',
- type: 'default'
- }).then(() => {});
- } catch (e) {
- // 忽略异常
- }
- }
- export function fetchDeviceAuthorization(imei) {
- console.log('fetchDeviceAuthorization 调用:', { imei });
- // 实际接口 - 直接使用uni.request调用外部接口
- return new Promise((resolve, reject) => {
- const queryUrl = `http://demo.kcim.cn/region/register/queryEnrollInfo?deviceCode=${encodeURIComponent(imei)}`;
- console.log('发送真实接口请求(POST, deviceCode在URL):', queryUrl);
- uni.request({
- url: queryUrl,
- method: 'POST',
- data: {},
- header: {
- 'Content-Type': 'application/json'
- },
- success: (res) => {
- console.log('授权查询接口返回:', res);
- if (res.statusCode === 200) {
- // 根据接口返回的数据结构来解析状态
- const data = res.data;
- const result = parseAuthResponse(data, imei);
- resolve(result);
- } else {
- const errorMsg = `接口请求失败: ${res.statusCode}`;
- console.error(errorMsg, res);
- showErrorToast(errorMsg);
- reject(new Error(errorMsg));
- }
- },
- fail: (error) => {
- console.error('授权查询接口失败:', error);
- const errorMsg = error.errMsg || error.message || '网络请求失败,请检查网络连接';
- showErrorToast(errorMsg);
- reject(new Error(errorMsg));
- }
- });
- });
- }
- // 移除 Debug 覆盖逻辑
- // 解析授权查询接口的响应数据
- function parseAuthResponse(data, imei) {
- console.log('解析授权响应数据:', data);
-
- if (!data) {
- return {
- status: 'unauthorized',
- authorized: false,
- message: '查询失败'
- };
- }
-
- switch (data.code) {
- case 200:
- // 已授权
- const authData = data.data || {};
- return {
- status: 'authorized',
- authorized: true,
- hospSign: authData.hospSign,
- region: authData.region || authData.host || authData.domain || '',
- message: data.msg || '设备已授权'
- };
-
- case 201:
- // 已提交申请待审核
- const reviewData = data.data || {};
- return {
- status: 'reviewing',
- authorized: false,
- applyInfo: {
- hospital: reviewData.hospInfo || '',
- department: reviewData.enrollDepartment || '',
- name: reviewData.enrollName || '',
- phone: reviewData.enrollPhone || '',
- remark: reviewData.comment || '',
- imei: imei,
- applyTime: formatDate(reviewData.enrollTime)
- },
- message: data.msg || '申请审核中'
- };
-
- case 203:
- // 设备已过期
- const expData = data.data || {};
- return {
- status: 'expired',
- authorized: false,
- applyInfo: {
- hospital: expData.hospInfo || '',
- department: expData.enrollDepartment || '',
- name: expData.enrollName || '',
- phone: expData.enrollPhone || '',
- remark: expData.comment || '',
- imei: imei,
- applyTime: formatDate(expData.enrollTime)
- },
- message: data.msg || '授权已到期'
- };
-
- case 204:
- default:
- // 未找到申请信息,需要提交申请
- return {
- status: 'unauthorized',
- authorized: false,
- message: data.msg || '设备未授权'
- };
- }
- }
- // 格式化日期
- function formatDate(dateStr) {
- if (!dateStr) return '';
-
- try {
- const date = new Date(dateStr);
- const year = date.getFullYear();
- const month = String(date.getMonth() + 1).padStart(2, '0');
- const day = String(date.getDate()).padStart(2, '0');
- return `${year}年${month}月${day}日`;
- } catch (e) {
- return dateStr;
- }
- }
- // 提交授权申请:入参申请表
- export function submitAuthorizationApply(payload) {
- console.log('submitAuthorizationApply 调用:', payload);
- // 转换参数格式为接口要求的格式
- const requestData = {
- deviceCode: payload.imei,
- hospInfo: payload.hospital,
- enrollName: payload.name,
- enrollPhone: payload.phone,
- enrollDepartment: payload.department,
- comment: payload.remark || ''
- };
-
- // 不再支持 Debug 覆盖,统一真实接口
-
- // 真实接口提交
- return new Promise((resolve, reject) => {
- console.log('发送申请提交请求:', requestData);
- uni.request({
- url: 'http://demo.kcim.cn/region/register/submitEnrollInfo',
- method: 'POST',
- data: requestData,
- header: {
- 'Content-Type': 'application/json'
- },
- success: (res) => {
- console.log('申请提交接口返回:', res);
- if (res.statusCode === 200) {
- // 兼容多种返回结构:
- // 1) { code: 200, msg, data }
- // 2) { success: true/false, errorMessage, showType }
- // 3) { data: { success: false, errorMessage, ... } }
- const body = res.data || {};
- const inner = body && body.data && typeof body.data === 'object' ? body.data : body;
- const isSuccess = (body && body.code === 200) || (body && body.success === true) || (inner && inner.success === true);
- if (isSuccess) {
- const successMsg = body.msg || body.message || inner.msg || '提交成功';
- uni.showToast({ title: successMsg, icon: 'success' });
- resolve(true);
- } else {
- const msgFromBody = body && (body.msg || body.message || body.errorMessage);
- const msgFromInner = inner && (inner.msg || inner.message || inner.errorMessage);
- const errorMsg = msgFromBody || msgFromInner || '提交失败';
- const showType = (body && body.showType) || (inner && inner.showType);
- // showType=4 一般表示需要弹窗提示,这里统一弹窗
- if (showType === 4) {
- showErrorModal(errorMsg);
- } else {
- showErrorModal(errorMsg);
- }
- reject(new Error(errorMsg));
- }
- } else {
- const errorMsg = `接口请求失败: ${res.statusCode}`;
- showErrorModal(errorMsg);
- reject(new Error(errorMsg));
- }
- },
- fail: (error) => {
- console.error('申请提交接口失败:', error);
- showErrorModal('网络请求失败');
- reject(error);
- }
- });
- });
- }
|