util.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import http from '../utils/http'
  2. import baseUrl from '../utils/env'
  3. const formatDate = (date, fmt = 'yyyy-MM-dd') => {
  4. var o = {
  5. "M+": date.getMonth() + 1, //月份
  6. "d+": date.getDate(), //日
  7. "h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时
  8. "H+": date.getHours(), //小时
  9. "m+": date.getMinutes(), //分
  10. "s+": date.getSeconds(), //秒
  11. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  12. "S": date.getMilliseconds() //毫秒
  13. };
  14. if (/(y+)/.test(fmt))
  15. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  16. for (var k in o)
  17. if (new RegExp("(" + k + ")").test(fmt))
  18. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  19. return fmt;
  20. }
  21. const formatNumber = n => {
  22. n = n.toString()
  23. return n[1] ? n : '0' + n
  24. }
  25. const checkMobile = mobile => {
  26. return /^1[234578]\d{9}$/.test(mobile)
  27. }
  28. const error = msg => {
  29. wx.showToast({
  30. icon: 'none',
  31. title: msg,
  32. })
  33. }
  34. const success = msg => {
  35. wx.showToast({
  36. title: msg,
  37. })
  38. }
  39. const firstCase = str => {
  40. return str.charAt(0).toUpperCase() + str.slice(1)
  41. }
  42. const uploadFile = (path, cb = null) => {
  43. wx.uploadFile({
  44. filePath: path,
  45. name: 'file',
  46. url: baseUrl + 'uploadFile',
  47. success: function (res) {
  48. var data = JSON.parse(res.data)
  49. if (data.code == 0) {
  50. typeof cb === "function" && cb(data)
  51. } else {
  52. error('上传文件失败')
  53. }
  54. }
  55. })
  56. }
  57. const callLogin = (data, redirect, cb) => {
  58. http({
  59. url: 'loginByWechat',
  60. data: data,
  61. loadTitle: redirect ? '登录中' : '',
  62. success: function (res) {
  63. if (res.code == 0) {
  64. getApp().updateUserInfo(res.data)
  65. if(res.data.session_key) {
  66. wx.setStorageSync('session_key', res.data.session_key)
  67. }
  68. if (redirect) {
  69. wx.switchTab({
  70. url: '/pages/index/index',
  71. })
  72. }
  73. typeof cb === "function" && cb(res.data)
  74. }
  75. }
  76. })
  77. }
  78. const wechatLogin = (e, redirect = false, cb = null, bind = false) => {
  79. if (e.detail.errMsg == 'getUserInfo:ok') {
  80. wx.checkSession({
  81. success () {
  82. callLogin(Object.assign({}, e.detail, {
  83. bind: bind,
  84. session_key: wx.getStorageSync('session_key')
  85. }), redirect, cb)
  86. },
  87. fail () {
  88. wx.login({
  89. success(res) {
  90. if (res.code) {
  91. callLogin(Object.assign({}, e.detail, {
  92. code: res.code,
  93. bind: bind
  94. }), redirect, cb)
  95. }
  96. }
  97. }) //重新登录
  98. }
  99. })
  100. }
  101. }
  102. const checkPass = (str) => {
  103. return /^(\d+[a-zA-Z]+|[a-zA-Z]+\d+)([0-9a-zA-Z]*)$/.test(str)
  104. }
  105. const beforeClose = (action) => {
  106. return false
  107. };
  108. module.exports = {
  109. formatDate,
  110. checkMobile: checkMobile,
  111. error,
  112. success,
  113. firstCase,
  114. uploadFile,
  115. wechatLogin,
  116. checkPass,
  117. beforeClose
  118. }