抖音平台小程序

util.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import appInfo from "./appInfo";
  2. let BASE_URL = 'https://dou.wenxingshuju.com' // 请求地址 - 生产环境
  3. // let BASE_URL = 'https://dou.wenxingshuju.com' // 请求地址 - 测试环境
  4. // #ifdef H5
  5. BASE_URL = '' //线上模式
  6. // BASE_URL = '/api' // 开发模式
  7. // #endif
  8. const LOGIN_ERR_CODES = [ // 登录状态失效错误码
  9. '2008',
  10. '2005',
  11. '4001',
  12. '4002',
  13. ]
  14. const handleToLogin = () => {
  15. uni.clearStorageSync()
  16. uni.reLaunch({
  17. url: '/pages/subPackages/login/loginPhone'
  18. });
  19. }
  20. const req = (url, method, data, selfHeader, isShowLoading = true) => {
  21. const dataes = {
  22. app_id: appInfo.appId || '',
  23. open_id: uni.getStorageSync('openid') || '',
  24. }
  25. const header = {
  26. // 'source': uni.getStorageSync('channel_source') || '',
  27. // 'version': uni.getStorageSync('channel_version') || '',
  28. // 'token': uni.getStorageSync('token') || '',
  29. }
  30. return new Promise((resolve, reject) => {
  31. // S 联调日志
  32. try {
  33. console.log('$$req url => ', url)
  34. console.log('$$req method => ', method)
  35. console.log('$$req params => ', data ? Object.assign(dataes, data) : dataes)
  36. } catch (error) {
  37. console.log(error)
  38. }
  39. // E 联调日志
  40. if (isShowLoading) { // 默认显示loading
  41. uni.showLoading({ title: '加载中...', mask: true })
  42. }
  43. uni.request({
  44. url:`${BASE_URL}${url}`,
  45. data: data ? Object.assign(dataes, data) : dataes,
  46. method: method,
  47. header: selfHeader ? Object.assign(header, selfHeader) : header,
  48. success: function (res) {
  49. uni.hideLoading()
  50. // if (LOGIN_ERR_CODES.includes(String(res.data.errno))) {
  51. // handleToLogin()
  52. // }
  53. resolve(res)
  54. },
  55. fail: function (err) {
  56. uni.hideLoading()
  57. reject(err)
  58. }
  59. })
  60. })
  61. }
  62. const get = (url, data, header = {}, isShowLoading = true) => {
  63. return req(url, 'GET', data, { ...header }, isShowLoading)
  64. }
  65. const post = (url, data, header = {}, isShowLoading = true) => {
  66. return req(url, 'POST', data, {
  67. 'Content-Type': 'application/x-www-form-urlencoded',
  68. ...header,
  69. }, isShowLoading)
  70. }
  71. const postJSON = (url, data, header = {}, isShowLoading = true) => {
  72. return req(url, 'POST', data, {
  73. 'Content-Type': 'application/json',
  74. ...header,
  75. }, isShowLoading)
  76. }
  77. // 获取随机参数 h5使用
  78. const getUuid = () => {
  79. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  80. const r = (Math.random() * 16) | 0,
  81. v = c == 'x' ? r : (r & 0x3) | 0x8
  82. return v.toString(16)
  83. })
  84. }
  85. export {
  86. req,
  87. BASE_URL,
  88. get,
  89. post,
  90. postJSON,
  91. getUuid,
  92. }