util.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. const app = getApp()
  2. //金额最多保留6位小数
  3. const decimal = num => {
  4. let numStr = num.toString()
  5. if (numStr.indexOf('.') > -1) {
  6. let intNum = numStr.split('.')[0]
  7. let decimal = numStr.split('.')[1]
  8. if (decimal.length > 6) {
  9. decimal = decimal.substring(0, 6)
  10. }
  11. return Number(intNum + '.' + decimal)
  12. } else {
  13. return num
  14. }
  15. }
  16. //数组去重
  17. const unique = arr => {
  18. return [...new Set(arr)]
  19. }
  20. //打开文档
  21. const openDoc = link => {
  22. wx.showLoading({
  23. title: '文档加载中...'
  24. })
  25. wx.downloadFile({
  26. url: link,
  27. success: res => {
  28. const filePath = res.tempFilePath
  29. wx.openDocument({
  30. filePath: filePath,
  31. success: res => {
  32. wx.hideLoading()
  33. }
  34. })
  35. }
  36. })
  37. }
  38. //退出登录
  39. const loginOut = () => {
  40. wx.clearStorage()
  41. toLogin()
  42. }
  43. //选择图片
  44. const chooseImage = params => {
  45. return new Promise((resolve, reject) => {
  46. let count = 1
  47. let sizeType = ['original', 'compressed']
  48. let sourceType = ['album', 'camera']
  49. if (params) {
  50. if (params.count) {
  51. count = params.count
  52. }
  53. if (params.sizeType) {
  54. sizeType = params.sizeType
  55. }
  56. if (params.sourceType) {
  57. sourceType = params.sourceType
  58. }
  59. }
  60. wx.chooseImage({
  61. count: count,
  62. sizeType: sizeType,
  63. sourceType: sourceType,
  64. success: res => {
  65. if (params.sendBefore) {
  66. params.sendBefore()
  67. }
  68. resolve(res)
  69. },
  70. fail: err => {
  71. reject(err)
  72. },
  73. complete: () => {
  74. if (params.complete) {
  75. params.complete()
  76. }
  77. }
  78. })
  79. })
  80. }
  81. //上传单个文件
  82. const uploadFile = params => {
  83. if (!params.filePath) {
  84. toast('未获取到文件临时路径,请重试')
  85. return
  86. }
  87. let token = wx.getStorageSync('token')
  88. let header = {
  89. 'content-type': 'multipart/form-data'
  90. }
  91. if (token) {
  92. header.token = token
  93. }
  94. return new Promise((resolve, reject) => {
  95. if (params.sendBefore) {
  96. params.sendBefore()
  97. }
  98. wx.uploadFile({
  99. url: app.globalData.globalUrl + params.url,
  100. filePath: params.filePath,
  101. name: params.name ? params.name : 'file',
  102. header: header,
  103. formData: params.formData ? params.formData : {},
  104. success: res => {
  105. resolve(res)
  106. },
  107. fail: err => {
  108. reject(err)
  109. },
  110. complete: () => {
  111. if (params.complete) {
  112. params.complete()
  113. }
  114. }
  115. })
  116. })
  117. }
  118. //获取当前用户登录角色(1是企业,2是服务商,3是运营商,99是未知)
  119. const getInfo = () => {
  120. let userInfo = wx.getStorageSync('userInfo')
  121. return userInfo
  122. }
  123. const getCurrEntInfo=()=>{
  124. let entInfo = wx.getStorageSync('company')
  125. return entInfo
  126. }
  127. //当前登录账号的默认企业
  128. // const getCompany = params => {
  129. // return new Promise((resolve, reject) => {
  130. // let params = {
  131. // mobile: wx.getStorageSync('userInfo').mobile
  132. // }
  133. // axios({
  134. // method: 'get',
  135. // url: '/auth/comm/user/findDefaultChoose',
  136. // data: params,
  137. // success: res => {
  138. // resolve(res)
  139. // },
  140. // sendBefore: () => {
  141. // if (params.sendBefore) {
  142. // params.sendBefore()
  143. // }
  144. // },
  145. // error: res => {
  146. // if (params.error) {
  147. // params.error(res)
  148. // }
  149. // },
  150. // complete: () => {
  151. // if (params.complete) {
  152. // params.complete()
  153. // }
  154. // },
  155. // noLogin: res => {
  156. // if (params.noLogin) {
  157. // params.noLogin()
  158. // } else {
  159. // toLogin()
  160. // }
  161. // }
  162. // })
  163. // })
  164. // }
  165. //服务商用户
  166. const getIsp = () => {
  167. return new Promise((resolve, reject) => {
  168. let params = {
  169. mobile: wx.getStorageSync('userInfo').mobile
  170. }
  171. axios({
  172. method: 'get',
  173. url: '/auth/isp/info/findByMobile',
  174. data: params,
  175. success: res => {
  176. resolve(res)
  177. },
  178. sendBefore: () => {
  179. if (params.sendBefore) {
  180. params.sendBefore()
  181. }
  182. },
  183. error: res => {
  184. if (params.error) {
  185. params.error(res)
  186. }
  187. },
  188. complete: () => {
  189. if (params.complete) {
  190. params.complete()
  191. }
  192. },
  193. noLogin: res => {
  194. if (params.noLogin) {
  195. params.noLogin()
  196. } else {
  197. toLogin()
  198. }
  199. }
  200. })
  201. })
  202. }
  203. //运营商用户
  204. const getOper = () => {
  205. return new Promise((resolve, reject) => {
  206. let params = {
  207. mobile: wx.getStorageSync('userInfo').mobile
  208. }
  209. axios({
  210. method: 'get',
  211. url: '/auth/oper/user/findManageInfoByMobile',
  212. data: params,
  213. success: res => {
  214. resolve(res)
  215. },
  216. sendBefore: () => {
  217. if (params.sendBefore) {
  218. params.sendBefore()
  219. }
  220. },
  221. error: res => {
  222. if (params.error) {
  223. params.error(res)
  224. }
  225. },
  226. complete: () => {
  227. if (params.complete) {
  228. params.complete()
  229. }
  230. },
  231. noLogin: res => {
  232. if (params.noLogin) {
  233. params.noLogin()
  234. } else {
  235. toLogin()
  236. }
  237. }
  238. })
  239. })
  240. }
  241. //获取个人信息
  242. const getUser = params => {
  243. return new Promise((resolve, reject) => {
  244. let userInfo = wx.getStorageSync('userInfo')
  245. let iparams = {
  246. mobile: userInfo.mobile
  247. }
  248. axios({
  249. method: 'get',
  250. url: '/auth/comm/user/personalInfo/find',
  251. data: iparams,
  252. success: res => {
  253. resolve(res)
  254. },
  255. sendBefore: () => {
  256. if (params.sendBefore) {
  257. params.sendBefore()
  258. }
  259. },
  260. error: res => {
  261. if (params.error) {
  262. params.error(res)
  263. }
  264. },
  265. complete: () => {
  266. if (params.complete) {
  267. params.complete()
  268. }
  269. },
  270. noLogin: res => {
  271. if (params.noLogin) {
  272. params.noLogin()
  273. } else {
  274. toLogin()
  275. }
  276. }
  277. })
  278. })
  279. }
  280. //跳转登录页
  281. const toLogin = () => {
  282. wx.navigateTo({
  283. url: '/pages/login/login'
  284. })
  285. }
  286. //获取图片信息
  287. const getImgInfo = url => {
  288. return new Promise((resolve, reject) => {
  289. wx.getImageInfo({
  290. src: url,
  291. success: res => {
  292. resolve(res)
  293. },
  294. fail: err => {
  295. reject(err)
  296. }
  297. })
  298. })
  299. }
  300. //执行上一页的函数
  301. const funPrev = callback => {
  302. let pages = getCurrentPages()
  303. let prevPage = pages[pages.length - 2]
  304. callback(prevPage)
  305. }
  306. //格式化时间
  307. const formatTime = (date, type = 1) => {
  308. if (!(date instanceof Date)) {
  309. date = new Date(date)
  310. }
  311. const curDate = new Date()
  312. const curYear = curDate.getFullYear()
  313. const curMonth = curDate.getMonth() + 1
  314. const curDay = curDate.getDate()
  315. const year = date.getFullYear()
  316. const month = date.getMonth() + 1
  317. const day = date.getDate()
  318. const hour = date.getHours()
  319. const minute = date.getMinutes()
  320. const second = date.getSeconds()
  321. if (type === 1) {
  322. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  323. }
  324. }
  325. //格式化数字
  326. const formatNumber = n => {
  327. n = n.toString()
  328. return n[1] ? n : '0' + n
  329. }
  330. //获取时间简写
  331. const getShortTime = timeStamp => {
  332. let result = ''
  333. let minute = 1000 * 60
  334. let hour = minute * 60
  335. let day = hour * 24
  336. let halfamonth = day * 15
  337. let month = day * 30
  338. let now = new Date().getTime()
  339. let diffValue = now - timeStamp
  340. if (diffValue < 0) {
  341. return
  342. }
  343. let monthC = diffValue / month
  344. let weekC = diffValue / (7 * day)
  345. let dayC = diffValue / day
  346. let hourC = diffValue / hour
  347. let minC = diffValue / minute
  348. if (monthC >= 1) {
  349. result = '' + parseInt(monthC) + '月前'
  350. } else if (weekC >= 1) {
  351. result = '' + parseInt(weekC) + '周前'
  352. } else if (dayC >= 1) {
  353. result = '' + parseInt(dayC) + '天前'
  354. } else if (hourC >= 1) {
  355. result = '' + parseInt(hourC) + '小时前'
  356. } else if (minC >= 1) {
  357. result = '' + parseInt(minC) + '分钟前'
  358. } else
  359. result = '刚刚'
  360. return result
  361. }
  362. //获取系统信息
  363. const getSystemInfo = () => {
  364. return new Promise((resolve, reject) => {
  365. wx.getSystemInfo({
  366. success: res => {
  367. resolve(res)
  368. },
  369. fail: err => {
  370. reject(err)
  371. }
  372. })
  373. })
  374. }
  375. //去掉所有空格
  376. const trimAll = text => {
  377. text = text + ''
  378. return text.replace(/\s/ig, '')
  379. }
  380. //验证手机号码是否正确
  381. const testMobile = mobile => {
  382. mobile = trimAll(mobile)
  383. if (!(/^1\d{10}$/.test(mobile))) {
  384. return false
  385. } else {
  386. return true
  387. }
  388. }
  389. //验证邮箱是否正确
  390. const testEmail = email => {
  391. email = trimAll(email)
  392. if (!(/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(email))) {
  393. return false
  394. } else {
  395. return true
  396. }
  397. }
  398. //错误提示
  399. const toast = text => {
  400. setTimeout(() => {
  401. wx.showToast({
  402. title: text,
  403. icon: 'none',
  404. duration: 2500
  405. })
  406. }, 10)
  407. }
  408. //获取登录态login的code
  409. const getLoginCode = () => {
  410. return new Promise((resolve, reject) => {
  411. wx.showLoading({
  412. title: '参数获取中...',
  413. mask: true
  414. })
  415. wx.login({
  416. success: res => {
  417. if (res.code) {
  418. resolve(res)
  419. } else {
  420. reject(res)
  421. }
  422. },
  423. fail: err => {
  424. reject(err)
  425. },
  426. complete: () => {
  427. wx.hideLoading()
  428. }
  429. })
  430. })
  431. }
  432. // 获取当前时间
  433. const getNowDate = date => {
  434. const year = date.getFullYear()
  435. const month = date.getMonth() + 1
  436. const day = date.getDate()
  437. return [year, month,day].map(formatNumber).join('-')
  438. }
  439. //字典
  440. const code = {
  441. '0': '请求成功',
  442. '10000': '系统服务异常,请联系管理员',
  443. '10001': '传入数据错误',
  444. '10002': '账号不存在',
  445. '10003': '密码不正确',
  446. '10004': '账号已被锁定,请联系管理员',
  447. '10005': '60秒内不能重复发送短信',
  448. '10006': '调用接口获取短信验证码失败',
  449. '10007': '手机号码验证错误',
  450. '10008': '手机短信验证码校验错误',
  451. '10009': '手机短信校验超时',
  452. '10010': '请求第三方接口错误',
  453. '10011': '数据库操作失败',
  454. '10012': '未知渠道,请检查渠道相关参数',
  455. '10013': '注册用户类型错误',
  456. '10014': '对应的用户已经存在',
  457. '10015': '系统参数配置错误',
  458. '10016': '修改命令状态未找到对应数据记录',
  459. '10017': '未知命令执行状态',
  460. '10018': '超级管理员及角色不允许修改',
  461. '10019': '新增角色名已存在',
  462. '10020': '两次输入密码不一致',
  463. '10021': '对应的管理人员不存在',
  464. '10022': '注册添加用户类型与请求路径不匹配',
  465. '10023': '登录失败',
  466. '10024': '用户层级管理关系数据错误',
  467. '10025': '无该企业的操作权限',
  468. '10026': '注册管理者无法在用户管理模块删除',
  469. '10027': '该产品需绑定在对应的企业',
  470. '10028': '企业下产品编码重复',
  471. '10029': '客户编码重复',
  472. '10030': '未找到对应的开票企业信息',
  473. '10031': '该发票类型或状态不允许红冲',
  474. '10032': '未找到企业对应的服务商',
  475. '10033': 'MQ消息未找到对应的指令',
  476. '10034': '根据指令id 未找到对应的开票信息',
  477. '10035': 'MQ返回结果发票请求流水号和本地流水号不一致',
  478. '10036': 'MQ 返回结果解析为空',
  479. '10037': '根据指令id 未找到对应的红冲信息',
  480. '10038': 'MQ推送指令消息失败',
  481. '10039': '设置默认选定项没有对应的数据'
  482. }
  483. //保存到相册
  484. const savePhoto = tempUrl => {
  485. wx.saveImageToPhotosAlbum({
  486. filePath: tempUrl,
  487. success: res => {
  488. wx.hideLoading()
  489. toast('保存成功,快去相册查看吧')
  490. },
  491. fail: err => {
  492. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
  493. wx.showModal({
  494. title: '提示',
  495. content: '需要您授权保存相册',
  496. showCancel: false,
  497. success: () => {
  498. wx.openSetting({
  499. success: settingdata => {
  500. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  501. wx.showModal({
  502. title: '提示',
  503. content: '获取权限成功,请再次尝试将二维码保存到相册',
  504. showCancel: false
  505. })
  506. } else {
  507. wx.showModal({
  508. title: '提示',
  509. content: '获取权限失败,将无法保存二维码到相册哦',
  510. showCancel: false
  511. })
  512. }
  513. }
  514. })
  515. }
  516. })
  517. }
  518. },
  519. complete: () => {
  520. wx.hideLoading()
  521. }
  522. })
  523. }
  524. //网络请求
  525. const axios = params => {
  526. let token = ''
  527. if (wx.getStorageSync('token')) {
  528. token = wx.getStorageSync('token')
  529. }
  530. if (!params.method) {
  531. params.method = 'get'
  532. }
  533. if (params.sendBefore) {
  534. params.sendBefore()
  535. }
  536. if (!params.responseType) {
  537. params.responseType = 'text'
  538. }
  539. if (!params.header) {
  540. params.header = {
  541. 'content-type': 'application/json'
  542. }
  543. }
  544. if (token) {
  545. params.header.token = token
  546. }
  547. wx.request({
  548. method: params.method,
  549. url: app.globalData.globalUrl + params.url,
  550. data: params.data,
  551. responseType: params.responseType,
  552. header: params.header,
  553. success: res => {
  554. let result = res.data
  555. console.log('params.url=>' + params.url, params.data, res.data)
  556. if (result.code === 0) { //code为0,说明请求成功
  557. if (params.success) {
  558. params.success(result)
  559. } else {
  560. console.log(params.url + '接口请求成功=>请求结果为=>', res)
  561. }
  562. } else { //code不为0,说明请求失败
  563. if (result.status === 403 || result.status==401) { //status为403,说明未登录或者登录态失效,则跳转登录
  564. if (params.noLogin) {
  565. params.noLogin()
  566. } else {
  567. toLogin()
  568. }
  569. } else { //status不为403,则表示有错误,弹出错误
  570. if (params.error) {
  571. params.error(result)
  572. } else {
  573. if (result.msg) {
  574. toast(result.msg)
  575. }
  576. console.log(params.url + '接口请求错误=>请求结果为=>', result)
  577. }
  578. }
  579. }
  580. },
  581. complete: res => {
  582. if (params.complete) {
  583. params.complete(res)
  584. } else {
  585. console.log(params.url + '=>complete=>', res)
  586. }
  587. },
  588. fail: res => {
  589. if (params.fail) {
  590. params.fail(res)
  591. } else {
  592. console.log(params.url + '-fail', res)
  593. }
  594. }
  595. })
  596. }
  597. module.exports = {
  598. decimal,
  599. unique,
  600. openDoc,
  601. loginOut,
  602. uploadFile,
  603. chooseImage,
  604. getInfo,
  605. getCurrEntInfo,
  606. // getCompany,
  607. getIsp,
  608. getOper,
  609. getUser,
  610. toLogin,
  611. getImgInfo,
  612. funPrev,
  613. formatTime,
  614. getShortTime,
  615. getSystemInfo,
  616. trimAll,
  617. testMobile,
  618. testEmail,
  619. toast,
  620. getLoginCode,
  621. savePhoto,
  622. axios,
  623. getNowDate
  624. }