util.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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[2345789]\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. duration:1000
  38. })
  39. }
  40. const firstCase = str => {
  41. return str.charAt(0).toUpperCase() + str.slice(1)
  42. }
  43. const uploadFile = (path, cb = null) => {
  44. wx.uploadFile({
  45. filePath: path,
  46. name: 'file',
  47. url: baseUrl + 'uploadFile',
  48. success: function (res) {
  49. var data = JSON.parse(res.data)
  50. console.log(data)
  51. if (data.code == 0) {
  52. typeof cb === "function" && cb(data)
  53. } else {
  54. error('上传文件失败')
  55. }
  56. }
  57. })
  58. }
  59. const callLogin = (data, redirect, cb) => {
  60. http({
  61. url: 'loginByWechat',
  62. data: data,
  63. loadTitle: redirect ? '登录中' : '',
  64. success: function (res) {
  65. if (res.code == 0) {
  66. getApp().updateUserInfo(res.data)
  67. if (res.data.session_key) {
  68. wx.setStorageSync('session_key', res.data.session_key)
  69. }
  70. if (redirect) {
  71. wx.switchTab({
  72. url: '/pages/index/index',
  73. })
  74. }
  75. typeof cb === "function" && cb(res.data)
  76. }
  77. }
  78. })
  79. }
  80. const wechatLogin = (e, redirect = false, cb = null, bind = false) => {
  81. if (e.detail.errMsg == 'getUserInfo:ok') {
  82. wx.checkSession({
  83. success() {
  84. callLogin(Object.assign({}, e.detail, {
  85. bind: bind,
  86. session_key: wx.getStorageSync('session_key')
  87. }), redirect, cb)
  88. },
  89. fail() {
  90. wx.login({
  91. success(res) {
  92. if (res.code) {
  93. callLogin(Object.assign({}, e.detail, {
  94. code: res.code,
  95. bind: bind
  96. }), redirect, cb)
  97. }
  98. }
  99. }) //重新登录
  100. }
  101. })
  102. }
  103. }
  104. const checkPass = (str) => {
  105. return /^(\d+[a-zA-Z]+|[a-zA-Z]+\d+)([0-9a-zA-Z]*)$/.test(str)
  106. }
  107. const beforeClose = (action) => {
  108. return false
  109. };
  110. module.exports = {
  111. formatDate,
  112. checkMobile: checkMobile,
  113. error,
  114. success,
  115. firstCase,
  116. uploadFile,
  117. wechatLogin,
  118. checkPass,
  119. beforeClose
  120. }