util.js 2.9 KB

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