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.

511 lines
17 KiB

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