authorize.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // 设备授权相关接口封装
  2. import { confirm as showAppConfirm } from '@/utils/confirm.js'
  3. // 获取设备授权信息:入参 imei,返回 { authorized: boolean, hospSign?: string, hospitalName?, deptName?, contactPhone? }
  4. // 移除调试模块
  5. // 统一的错误提示方法(H5不支持error图标,这里统一用none)
  6. function showErrorToast(message) {
  7. try {
  8. const text = String(message || '请求失败');
  9. uni.showToast({ title: text, icon: 'none' });
  10. } catch (e) {
  11. // 忽略异常
  12. }
  13. }
  14. // 统一的错误弹窗提示
  15. function showErrorModal(message) {
  16. try {
  17. const text = String(message || '请求失败');
  18. // 使用公共弹窗组件 app-confirm
  19. showAppConfirm({
  20. title: '提示',
  21. content: text,
  22. confirmText: '我知道了',
  23. cancelText: '取消',
  24. type: 'default'
  25. }).then(() => {});
  26. } catch (e) {
  27. // 忽略异常
  28. }
  29. }
  30. export function fetchDeviceAuthorization(imei) {
  31. console.log('fetchDeviceAuthorization 调用:', { imei });
  32. // 实际接口 - 直接使用uni.request调用外部接口
  33. return new Promise((resolve, reject) => {
  34. const queryUrl = `http://demo.kcim.cn/region/register/queryEnrollInfo?deviceCode=${encodeURIComponent(imei)}`;
  35. console.log('发送真实接口请求(POST, deviceCode在URL):', queryUrl);
  36. uni.request({
  37. url: queryUrl,
  38. method: 'POST',
  39. data: {},
  40. header: {
  41. 'Content-Type': 'application/json'
  42. },
  43. success: (res) => {
  44. console.log('授权查询接口返回:', res);
  45. if (res.statusCode === 200) {
  46. // 根据接口返回的数据结构来解析状态
  47. const data = res.data;
  48. const result = parseAuthResponse(data, imei);
  49. resolve(result);
  50. } else {
  51. const errorMsg = `接口请求失败: ${res.statusCode}`;
  52. console.error(errorMsg, res);
  53. showErrorToast(errorMsg);
  54. reject(new Error(errorMsg));
  55. }
  56. },
  57. fail: (error) => {
  58. console.error('授权查询接口失败:', error);
  59. const errorMsg = error.errMsg || error.message || '网络请求失败,请检查网络连接';
  60. showErrorToast(errorMsg);
  61. reject(new Error(errorMsg));
  62. }
  63. });
  64. });
  65. }
  66. // 移除 Debug 覆盖逻辑
  67. // 解析授权查询接口的响应数据
  68. function parseAuthResponse(data, imei) {
  69. console.log('解析授权响应数据:', data);
  70. if (!data) {
  71. return {
  72. status: 'unauthorized',
  73. authorized: false,
  74. message: '查询失败'
  75. };
  76. }
  77. switch (data.code) {
  78. case 200:
  79. // 已授权
  80. const authData = data.data || {};
  81. return {
  82. status: 'authorized',
  83. authorized: true,
  84. hospSign: authData.hospSign,
  85. region: authData.region || authData.host || authData.domain || '',
  86. message: data.msg || '设备已授权'
  87. };
  88. case 201:
  89. // 已提交申请待审核
  90. const reviewData = data.data || {};
  91. return {
  92. status: 'reviewing',
  93. authorized: false,
  94. applyInfo: {
  95. hospital: reviewData.hospInfo || '',
  96. department: reviewData.enrollDepartment || '',
  97. name: reviewData.enrollName || '',
  98. phone: reviewData.enrollPhone || '',
  99. remark: reviewData.comment || '',
  100. imei: imei,
  101. applyTime: formatDate(reviewData.enrollTime)
  102. },
  103. message: data.msg || '申请审核中'
  104. };
  105. case 203:
  106. // 设备已过期
  107. const expData = data.data || {};
  108. return {
  109. status: 'expired',
  110. authorized: false,
  111. applyInfo: {
  112. hospital: expData.hospInfo || '',
  113. department: expData.enrollDepartment || '',
  114. name: expData.enrollName || '',
  115. phone: expData.enrollPhone || '',
  116. remark: expData.comment || '',
  117. imei: imei,
  118. applyTime: formatDate(expData.enrollTime)
  119. },
  120. message: data.msg || '授权已到期'
  121. };
  122. case 204:
  123. default:
  124. // 未找到申请信息,需要提交申请
  125. return {
  126. status: 'unauthorized',
  127. authorized: false,
  128. message: data.msg || '设备未授权'
  129. };
  130. }
  131. }
  132. // 格式化日期
  133. function formatDate(dateStr) {
  134. if (!dateStr) return '';
  135. try {
  136. const date = new Date(dateStr);
  137. const year = date.getFullYear();
  138. const month = String(date.getMonth() + 1).padStart(2, '0');
  139. const day = String(date.getDate()).padStart(2, '0');
  140. return `${year}年${month}月${day}日`;
  141. } catch (e) {
  142. return dateStr;
  143. }
  144. }
  145. // 提交授权申请:入参申请表
  146. export function submitAuthorizationApply(payload) {
  147. console.log('submitAuthorizationApply 调用:', payload);
  148. // 转换参数格式为接口要求的格式
  149. const requestData = {
  150. deviceCode: payload.imei,
  151. hospInfo: payload.hospital,
  152. enrollName: payload.name,
  153. enrollPhone: payload.phone,
  154. enrollDepartment: payload.department,
  155. comment: payload.remark || ''
  156. };
  157. // 不再支持 Debug 覆盖,统一真实接口
  158. // 真实接口提交
  159. return new Promise((resolve, reject) => {
  160. console.log('发送申请提交请求:', requestData);
  161. uni.request({
  162. url: 'http://demo.kcim.cn/region/register/submitEnrollInfo',
  163. method: 'POST',
  164. data: requestData,
  165. header: {
  166. 'Content-Type': 'application/json'
  167. },
  168. success: (res) => {
  169. console.log('申请提交接口返回:', res);
  170. if (res.statusCode === 200) {
  171. // 兼容多种返回结构:
  172. // 1) { code: 200, msg, data }
  173. // 2) { success: true/false, errorMessage, showType }
  174. // 3) { data: { success: false, errorMessage, ... } }
  175. const body = res.data || {};
  176. const inner = body && body.data && typeof body.data === 'object' ? body.data : body;
  177. const isSuccess = (body && body.code === 200) || (body && body.success === true) || (inner && inner.success === true);
  178. if (isSuccess) {
  179. const successMsg = body.msg || body.message || inner.msg || '提交成功';
  180. uni.showToast({ title: successMsg, icon: 'success' });
  181. resolve(true);
  182. } else {
  183. const msgFromBody = body && (body.msg || body.message || body.errorMessage);
  184. const msgFromInner = inner && (inner.msg || inner.message || inner.errorMessage);
  185. const errorMsg = msgFromBody || msgFromInner || '提交失败';
  186. const showType = (body && body.showType) || (inner && inner.showType);
  187. // showType=4 一般表示需要弹窗提示,这里统一弹窗
  188. if (showType === 4) {
  189. showErrorModal(errorMsg);
  190. } else {
  191. showErrorModal(errorMsg);
  192. }
  193. reject(new Error(errorMsg));
  194. }
  195. } else {
  196. const errorMsg = `接口请求失败: ${res.statusCode}`;
  197. showErrorModal(errorMsg);
  198. reject(new Error(errorMsg));
  199. }
  200. },
  201. fail: (error) => {
  202. console.error('申请提交接口失败:', error);
  203. showErrorModal('网络请求失败');
  204. reject(error);
  205. }
  206. });
  207. });
  208. }