You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

508 lines
17 KiB

1 year ago
  1. const app = getApp()
  2. let subDomain = '' // 子域名,没有就等于
  3. // const API_BASE_URL = 'https://apartmentcloud.xiaozhisz.cn/' // 主域名
  4. const API_BASE_URL = 'http://test.zhiweisz.cn/' // 主域名
  5. // const API_BASE_URL = 'https://yktms-cs.utsz.edu.cn/' // 主域名
  6. import JSEncrypt from '../js_sdk/jsencrypt-Rsa/jsencrypt/jsencrypt.min.js';
  7. function setCode(str, text) {
  8. let pubKey = str; // ES6 模板字符串 引用 rsa 公钥
  9. let encryptStr = new JSEncrypt();
  10. encryptStr.setPublicKey(pubKey); // 设置 加密公钥
  11. let data = encryptStr.encrypt(text.toString()); // 进行加密
  12. return data;
  13. }
  14. let tenantIdEnter='100'
  15. function setTenantId(tenantId){
  16. tenantIdEnter=tenantId
  17. wx.setStorageSync('tenantId',tenantId)
  18. }
  19. const request = (url, method, data,id='',no='') => {
  20. wx.showLoading({
  21. title: '加载中'
  22. });
  23. let _url = API_BASE_URL + subDomain + url.replace("{id}",id)
  24. var userToken = wx.getStorageSync('token')
  25. let tenantId=tenantIdEnter//wx.getStorageSync('tenantId')
  26. if (method == "GET") {
  27. return new Promise((resolve, reject) => {
  28. let header={}
  29. if(no=='0'){
  30. header={
  31. 'Content-Type': 'application/json',
  32. 'tenantId': tenantId
  33. }
  34. }else{
  35. header={
  36. 'Content-Type': 'application/json',
  37. 'Authorization': 'Bearer ' + (userToken ? userToken.access_token : ''),
  38. 'tenantId': tenantId
  39. }
  40. }
  41. wx.request({
  42. url: _url,
  43. method: method,
  44. data: data,
  45. dataType: 'json',
  46. header: header,
  47. success(request) {
  48. if (request.statusCode === 401) {
  49. wx.showToast({
  50. title: 'token已过期,请重新登录',
  51. duration: 2000,
  52. icon: 'none'
  53. })
  54. wx.reLaunch({
  55. url: '/pages/login/login',
  56. success: (res) => {
  57. wx.removeStorageSync('token')
  58. },
  59. fail: (res) => {
  60. console.log(res)
  61. }
  62. })
  63. }
  64. resolve(request.data)
  65. wx.hideLoading()
  66. },
  67. fail(error) {
  68. reject(error)
  69. wx.hideLoading()
  70. },
  71. complete(aaa) {
  72. // 加载完成
  73. }
  74. })
  75. })
  76. } else if (method == "POST") {
  77. return new Promise((resolve, reject) => {
  78. let header={}
  79. console.log(no,'nonon')
  80. if(no=='0'){
  81. header={
  82. 'tenantId': tenantId
  83. }
  84. }else{
  85. header={
  86. 'Authorization': 'Bearer ' + (userToken ? userToken.access_token : ''),
  87. 'tenantId': tenantId
  88. }
  89. }
  90. wx.request({
  91. url: _url,
  92. method: method,
  93. data: data,
  94. header: header,
  95. dataType: 'json',
  96. success(request) {
  97. if (request.statusCode === 401) {
  98. wx.showToast({
  99. title: 'token已过期,请重新登录',
  100. duration: 2000,
  101. icon: 'none'
  102. })
  103. wx.reLaunch({
  104. url: '/pages/login/login',
  105. success: (res) => {
  106. wx.removeStorageSync('token')
  107. },
  108. fail: (res) => {
  109. console.log(res)
  110. }
  111. })
  112. }
  113. resolve(request.data)
  114. wx.hideLoading()
  115. },
  116. fail(error) {
  117. reject(error)
  118. wx.hideLoading()
  119. },
  120. complete(aaa) {
  121. // 加载完成
  122. }
  123. })
  124. })
  125. }
  126. }
  127. const requestOne = (url, method, data) => {
  128. wx.showLoading({
  129. title: '加载中'
  130. });
  131. let _url = API_BASE_URL + subDomain + url
  132. let tenantId=tenantIdEnter//wx.getStorageSync('tenantId')
  133. return new Promise((resolve, reject) => {
  134. wx.request({
  135. url: _url,
  136. method: method,
  137. data: data,
  138. header: {
  139. 'content-type': 'application/json',
  140. 'tenantId': tenantId
  141. },
  142. dataType: 'json',
  143. success(request) {
  144. if (request.statusCode === 401) {
  145. wx.showToast({
  146. title: 'token已过期,请重新登录',
  147. duration: 2000,
  148. icon: 'none'
  149. })
  150. wx.reLaunch({
  151. url: '/pages/login/login',
  152. success: (res) => {
  153. wx.removeStorageSync('token')
  154. },
  155. fail: (res) => {
  156. console.log(res)
  157. }
  158. })
  159. }else if(request.statusCode === 400){
  160. // wx.showToast({
  161. // title: 'token已过期,请重新登录',
  162. // duration: 2000,
  163. // icon: 'none'
  164. // })
  165. // wx.reLaunch({
  166. // url: '/pages/login/login',
  167. // success: (res) => {
  168. // wx.removeStorageSync('signature')
  169. // wx.removeStorageSync('token')
  170. // },
  171. // fail: (res) => {
  172. // console.log(res)
  173. // }
  174. // })
  175. }
  176. resolve(request.data)
  177. wx.hideLoading()
  178. },
  179. fail(error) {
  180. reject(error)
  181. wx.hideLoading()
  182. },
  183. complete(aaa) {
  184. // 加载完成
  185. }
  186. })
  187. })
  188. }
  189. const request1 = (url, method, data) => {
  190. wx.showLoading({
  191. title: '加载中'
  192. });
  193. let _url = API_BASE_URL + subDomain + url
  194. let tenantId=tenantIdEnter//wx.getStorageSync('tenantId')
  195. return new Promise((resolve, reject) => {
  196. wx.request({
  197. url: _url,
  198. method: method,
  199. data: data,
  200. header: {
  201. 'content-type': 'application/x-www-form-urlencoded',
  202. 'tenantId': tenantId
  203. },
  204. dataType: 'json',
  205. success(request) {
  206. if (request.statusCode === 401) {
  207. wx.showToast({
  208. title: 'token已过期,请重新登录',
  209. duration: 2000,
  210. icon: 'none'
  211. })
  212. wx.reLaunch({
  213. url: '/pages/login/login',
  214. success: (res) => {
  215. wx.removeStorageSync('token')
  216. },
  217. fail: (res) => {
  218. console.log(res)
  219. }
  220. })
  221. }
  222. resolve(request.data)
  223. wx.hideLoading()
  224. },
  225. fail(error) {
  226. reject(error)
  227. wx.hideLoading()
  228. },
  229. complete(aaa) {
  230. // 加载完成
  231. }
  232. })
  233. })
  234. }
  235. const request2 = (url, method, data) => {
  236. wx.showLoading({
  237. title: '加载中'
  238. });
  239. let _url = API_BASE_URL + subDomain + url
  240. var userToken = wx.getStorageSync('token')
  241. let tenantId=tenantIdEnter//wx.getStorageSync('tenantId')
  242. if (method == "GET") {
  243. return new Promise((resolve, reject) => {
  244. wx.request({
  245. url: _url,
  246. method: method,
  247. data: data,
  248. dataType: 'json',
  249. header: {
  250. 'Content-Type': 'application/json',
  251. 'tenantId': tenantId
  252. },
  253. success(request) {
  254. resolve(request.data)
  255. wx.hideLoading()
  256. },
  257. fail(error) {
  258. reject(error)
  259. wx.hideLoading()
  260. },
  261. complete(aaa) {
  262. // 加载完成
  263. }
  264. })
  265. })
  266. } else if (method == "POST") {
  267. return new Promise((resolve, reject) => {
  268. wx.request({
  269. url: _url,
  270. method: method,
  271. data: data,
  272. header: {
  273. 'Authorization': 'Bearer ' + (userToken ? userToken.access_token : ''),
  274. 'tenantId': tenantId
  275. },
  276. dataType: 'json',
  277. success(request) {
  278. if (request.statusCode === 401) {
  279. wx.showToast({
  280. title: 'token已过期,请重新登录',
  281. duration: 2000,
  282. icon: 'none'
  283. })
  284. wx.reLaunch({
  285. url: '/pages/login/login',
  286. success: (res) => {
  287. wx.removeStorageSync('token')
  288. },
  289. fail: (res) => {
  290. console.log(res)
  291. }
  292. })
  293. }
  294. resolve(request.data)
  295. wx.hideLoading()
  296. },
  297. fail(error) {
  298. reject(error)
  299. wx.hideLoading()
  300. },
  301. complete(aaa) {
  302. // 加载完成
  303. }
  304. })
  305. })
  306. }
  307. }
  308. const request3 = (url, method, data) => {
  309. wx.showLoading({
  310. title: '加载中'
  311. });
  312. let _url = subDomain + url
  313. var userToken = wx.getStorageSync('token')
  314. let tenantId=tenantIdEnter//wx.getStorageSync('tenantId')
  315. if (method == "GET") {
  316. return new Promise((resolve, reject) => {
  317. wx.request({
  318. url: _url,
  319. method: method,
  320. data: data,
  321. dataType: 'json',
  322. header: {
  323. 'Content-Type': 'application/json',
  324. 'Authorization': 'Bearer ' + (userToken ? userToken.access_token : ''),
  325. 'tenantId': tenantId
  326. },
  327. success(request) {
  328. if (request.statusCode === 401) {
  329. wx.showToast({
  330. title: 'token已过期,请重新登录',
  331. duration: 2000,
  332. icon: 'none'
  333. })
  334. wx.reLaunch({
  335. url: '/pages/login/login',
  336. success: (res) => {
  337. wx.removeStorageSync('token')
  338. },
  339. fail: (res) => {
  340. console.log(res)
  341. }
  342. })
  343. }
  344. resolve(request.data)
  345. wx.hideLoading()
  346. },
  347. fail(error) {
  348. reject(error)
  349. wx.hideLoading()
  350. },
  351. complete(aaa) {
  352. // 加载完成
  353. }
  354. })
  355. })
  356. } else if (method == "POST") {
  357. return new Promise((resolve, reject) => {
  358. wx.request({
  359. url: _url,
  360. method: method,
  361. data: data,
  362. header: {
  363. 'Authorization': 'Bearer ' + (userToken ? userToken.access_token : ''),
  364. 'tenantId': tenantId
  365. },
  366. dataType: 'json',
  367. success(request) {
  368. if (request.statusCode === 401) {
  369. wx.showToast({
  370. title: 'token已过期,请重新登录',
  371. duration: 2000,
  372. icon: 'none'
  373. })
  374. wx.reLaunch({
  375. url: '/pages/login/login',
  376. success: (res) => {
  377. wx.removeStorageSync('token')
  378. },
  379. fail: (res) => {
  380. console.log(res)
  381. }
  382. })
  383. }
  384. resolve(request.data)
  385. wx.hideLoading()
  386. },
  387. fail(error) {
  388. reject(error)
  389. wx.hideLoading()
  390. },
  391. complete(aaa) {
  392. // 加载完成
  393. }
  394. })
  395. })
  396. }
  397. }
  398. /**
  399. * 小程序的promise没有finally方法自己扩展下
  400. */
  401. Promise.prototype.finally = function (callback) {
  402. var Promise = this.constructor;
  403. return this.then(
  404. function (value) {
  405. Promise.resolve(callback()).then(
  406. function () {
  407. return value;
  408. }
  409. );
  410. },
  411. function (reason) {
  412. Promise.resolve(callback()).then(
  413. function () {
  414. throw reason;
  415. }
  416. );
  417. }
  418. );
  419. }
  420. const throttle = (fn,delay)=>{
  421. let oldDate = Date.now();
  422. return function(){
  423. let args = arguments;
  424. let newDate = Date.now();
  425. let that = this;
  426. if(newDate-oldDate>delay){
  427. fn.apply(that,args);
  428. //倘若时间差大于延长时间 就更新一次旧时间
  429. oldDate = Date.now();
  430. }
  431. }
  432. }
  433. module.exports = {
  434. request,
  435. request1,
  436. request2,
  437. requestOne,
  438. setCode,
  439. setTenantId,
  440. throttle,
  441. login: data => request1('oauth/token', 'POST', data), // 登录
  442. oauthKey: (data) => request2('oauth/key', 'GET', data), // 登录code
  443. getDeviceItems: (data) => request('v1/device/get/items', 'POST', data), //获取设备
  444. getSiteList: (data) => request('v1/site/get/list', 'GET', data), //获取房间列表信息
  445. getIntegration: (data) => request('v1/card/get/integration', 'GET', data), //获取首页列表
  446. getuserInfo: (data) => request('v1/account/user/info', 'GET', data), //获取账号信息
  447. getSiteTenant: (data) => request('v1/site/get/tenant', 'GET', data), //获取已入住人员信息
  448. openLock: (data) => request('v1/device/isp/openLock', 'POST', data), //远程开门
  449. addCertificates: (data) => request('v1/device/add/certificate', 'POST', data), //添加凭证
  450. getOpenRecord: (data) => request('v1/device/get/open/record', 'POST', data), //获取开门数据
  451. getDeviceOperateUserInfo: (data) => request('v1/device/isp/operateUserInfo', 'POST', data), //获取开门数据
  452. bindDeviceSite: (data) => request('v1/device/bind/site', 'POST', data), //绑定房间
  453. unbindDeviceSite: (data) => request('v1/device/unbind/site', 'POST', data), //解除绑定房间
  454. unbindCard: (data) => request('v1/site/unbind/card', 'POST', data), //人员解绑房间
  455. sendCommand: (data) => request('v1/device/send/command', 'POST', data), //通用接口
  456. sendCommandOne: (data) => requestOne('v1/device/send/bleCommand', 'POST', data), //通用接口
  457. sendCommandGet: (data) => request('v1/device/send/command', 'GET', data), //通用接口
  458. ZGregisterDevice: (data) => request('v1/zg/registerDevice', 'POST', data), //ZG注册平台
  459. addCard: (data) => request('v1/card/add', 'POST', data), //创建人员(卡)
  460. bindCard: (data) => request('v1/site/bind/card', 'POST', data), //绑定房间
  461. deleteDevice: (data) => request('v1/device/delete/device', 'POST', data), //删除设备
  462. deleteZGDevice: (data) => request('v1/zg/deleteDevice', 'POST', data), //删除ZG设备
  463. getTree: (data) => request('v1/site/get/tree', 'GET', data), //获取房源tree
  464. getCertificates: (data) => request('v1/device/certificate/list', 'GET', data), //获取凭证列表
  465. ZGduserOperation: (data) => request('v1/zg/userOperation', 'POST', data), //添加人员
  466. getDeviceResult: (data) => request('v1/device/get/command/result', 'POST', data), //查询反馈日志列表
  467. deleteCertificates: (data) => request('v1/device/delete/certificate', 'POST', data), //凭证列表删除
  468. sendEmergence: (data) => request('v1/device/send/emergence', 'POST', data), //发送离线密码短信
  469. updatePush: (data) => request('v1/push/paas/update/push', 'POST', data), //修改反馈日志状态
  470. editPassword: (data) => request('v1/account/edit/password', 'POST', data), //修改登录账户
  471. fingerprintTransfer: (data) => request('v1/device/isp/fingerprintTransfer', 'POST', data), //添加指纹
  472. getDeviceRegisterManager: (data) => request('v1/device/isp/registerManager', 'POST', data), //重置添加管理员
  473. orderDeviceData: (data) => request('v1/device/order/deviceData', 'GET', data),//获取指定锁所有非703凭证数据
  474. updateCertificates: (data) => request('v1/device/update/certificate', 'POST', data),//凭证列表修改
  475. editStatusBySerial: (data,url) => request(url, 'GET', data),//凭证列表修改
  476. editPushBySerial: (data) => request('v1/device/editPush/bySerial', 'POST', data),//指纹ID记录传输
  477. registerDeviceAll:(data)=>request('v1/device/all/registerDevice', 'POST', data),//注册设备集成接口
  478. getlistNum:(data)=>request('v1/card/get/list', 'POST', data),//获取人员
  479. onlyTenant:(data)=>request('v1/card/only/tenant', 'GET', data),//查询房间和人员信息
  480. atCardUser: data=>requestOne('v1/account/at/cardUser', 'POST', data),//租客登录
  481. getConfig:data=>request('v1/auto/get/config', 'GET', data),
  482. getOperateList:data=>request('v1/log/operate/list', 'POST', data),
  483. getDepartmentUsers:(data,id)=>request('work/wechat/department/users/{id}', 'GET', data,id),//获取企业id
  484. getDepartment:data=>request('work/wechat/get/department', 'GET', data),//获取企业用户列表
  485. convertToUserid:data=>request('work/wechat/convert_to_userid', 'GET', data),//用openid换企业账号id
  486. createWXCardUser:data=>request('v1/card/create/WXCard/user', 'GET', data),//用openid换企业账号id
  487. getLeasesList:(data)=>request('v1/leases/get/list','POST',data),//获取合约
  488. getOpenId:data=>requestOne('work/wechat/jscode/session', 'GET', data),//获取openid
  489. editCertificate: (data) => requestOne('v1/card/ts/editCertificate', 'POST', data),//凭证状态修改
  490. tsdeleteCertificates: (data) => requestOne('v1/device/delete/certificate', 'POST', data), //凭证列表删除
  491. tsGetCertificate:(data)=>requestOne('v1/card/ts/getCertificate', 'POST', data),//租客端获取凭证列表
  492. tsAddCertificate:(data)=>requestOne('v1/card/ts/addCertificate', 'POST', data),//租客端添加凭证
  493. toWXCorpUser:(data)=>requestOne('v1/account/to/WXCorp/user', 'POST', data),//租客端登录
  494. getApartmentCardInfo:(data)=>requestOne('v1/account/get/apartmentCardInfo', 'POST', data),// 获取合同信息,交租订单
  495. createOrderPay:(data)=>requestOne('v1/pay/createOrder', 'POST', data),// 创建交租订单
  496. miniPrepayOrder:(data)=>requestOne('v1/pay/miniPrepayOrder','POST',data),//租客端创建订单
  497. minGetOpenLocks:(data)=>requestOne('v1/card/ts/getOpenLocks','POST',data),//租客端获取开锁记录
  498. getBleUnlockRecord:(data)=>requestOne('v1/card/ts/getBleUnlockRecord','POST',data),//租客端获取开锁记录
  499. updateCard:(data)=>requestOne('v1/account/edit/myKey','POST',data),
  500. getDeviceOperateUserInfo:(data)=>requestOne('v1/device/isp/operateUserInfo','POST',data),
  501. // bleUnlockRecord: (data) => request('v1/zg/bleUnlockRecord','POST', data),//上传蓝牙开锁记录
  502. // bleRspRecord: (data) => request3('http://192.168.1.34:8900/iot/v1/ble/deviceReport','POST', data),//传输蓝牙返回数据包
  503. }