util.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 (redirect) {
  66. wx.switchTab({
  67. url: '/pages/index/index',
  68. })
  69. }
  70. typeof cb === "function" && cb(res.data)
  71. }
  72. }
  73. })
  74. }
  75. const wechatLogin = (e, redirect = false, cb = null, bind = false) => {
  76. if (e.detail.errMsg == 'getUserInfo:ok') {
  77. // wx.checkSession({
  78. // success () {
  79. // that.wechatLogin(e.detail)
  80. // },
  81. // fail () {
  82. // session_key 已经失效,需要重新执行登录流程
  83. wx.login({
  84. success(res) {
  85. if (res.code) {
  86. callLogin(Object.assign({}, e.detail, {
  87. code: res.code,
  88. bind: bind
  89. }), redirect, cb)
  90. }
  91. }
  92. }) //重新登录
  93. // }
  94. // })
  95. }
  96. }
  97. const checkPass = (str) => {
  98. return /^(\d+[a-zA-Z]+|[a-zA-Z]+\d+)([0-9a-zA-Z]*)$/.test(str)
  99. }
  100. module.exports = {
  101. formatDate,
  102. checkMobile: checkMobile,
  103. error,
  104. success,
  105. firstCase,
  106. uploadFile,
  107. wechatLogin,
  108. checkPass
  109. }