common.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * 页面跳转
  3. p1 非tabar地址 p2动画效果 P3动画时间ms
  4. */
  5. import md5 from '@/ajax/md5.js'
  6. import '@/ajax/lcsign.js'
  7. const to = (url, animationType = 'pop-in') => {
  8. if (url == '#') return uni.showToast({
  9. title: "开发中...",
  10. mask: true,
  11. icon: 'none',
  12. duration: 2000
  13. });
  14. uni.navigateTo({
  15. url: url,
  16. animationType: animationType,
  17. // animationDuration:'300',
  18. success() {
  19. },
  20. fail() {
  21. console.error('跳转失败:' + url)
  22. }
  23. })
  24. }
  25. /**
  26. * [redirect] 关闭当前页面,跳转到应用内的某个页面
  27. p1 非tabar地址 p2动画效果 P3动画时间ms
  28. */
  29. const rdt = (url, animationType = 'pop-in') => {
  30. uni.redirectTo({
  31. url: url,
  32. animationType: animationType,
  33. // animationDuration:'300',
  34. success() {
  35. },
  36. fail() {
  37. console.error('跳转失败:' + url)
  38. }
  39. })
  40. }
  41. const back = () => {
  42. let canNavBack = getCurrentPages();
  43. if (canNavBack && canNavBack.length > 1) {
  44. uni.navigateBack({
  45. delta: 1
  46. });
  47. } else {
  48. history.back()
  49. }
  50. }
  51. //跳转有tabar页面
  52. const totabar = (url, animationType = 'pop-in') => {
  53. uni.switchTab({
  54. url: url,
  55. animationType: animationType,
  56. // animationDuration:'300',
  57. success() {
  58. },
  59. fail() {
  60. console.error('跳转失败:' + url)
  61. }
  62. })
  63. }
  64. /**
  65. * 消息弹窗
  66. p1 标题 p2图标 P3时间ms
  67. */
  68. const pop = (title, icon = 'none', time = '1500') => {
  69. // uni.showToast({
  70. // title:title,
  71. // icon:icon,
  72. // duration: time,
  73. // })
  74. uni.showToast({
  75. title: title,
  76. icon: icon
  77. })
  78. }
  79. //转义html
  80. const turnhtml = (str) => {
  81. var arrEntities = {
  82. lt: "<",
  83. gt: ">",
  84. nbsp: "&nbsp;",
  85. amp: "&",
  86. quot: '"'
  87. };
  88. return str.replace(/&(lt|gt|nbsp|amp|quot|p);/gi, function(all, t) {
  89. return arrEntities[t];
  90. });
  91. }
  92. //判断内外链接
  93. const link = (type, url) => {
  94. if (url == "" || url == "undefined" || url == null) {
  95. return
  96. }
  97. // console.log(url)
  98. if (type == 1) {
  99. uni.navigateTo({
  100. url: '/pages/common/webView?url=' + url
  101. })
  102. } else if (type == 2) {
  103. uni.navigateTo({
  104. url: url
  105. })
  106. }
  107. }
  108. // 上传图片
  109. function upload(url) {
  110. let time = new Date().getTime()
  111. let sign = md5.hex_md5("time=" + time + "&key=woshijiamijiekou").toUpperCase()
  112. return new Promise((resolve, reject) => {
  113. let a = uni.uploadFile({
  114. url: uni.baseUrl + 'data/api.auth.center/upload?sign=' + sign + "&time=" +time +"&hihi="+uni.signStr, // 仅为示例,非真实的接口地址
  115. filePath: url,
  116. name: 'file',
  117. header: {
  118. 'api-token': uni.getStorageSync("token")
  119. },
  120. success: (res) => {
  121. // console.log(JSON.parse(res.data).data.url)
  122. // console.log(JSON.parse(res.data).data.url)
  123. // console.log(res.data)
  124. resolve(JSON.parse(res.data).data.url)
  125. // resolve(res.data.data)
  126. }
  127. });
  128. })
  129. }
  130. // function upload(url) {
  131. // console.log(url)
  132. // return new Promise((resolve, reject) => {
  133. // let a = uni.uploadFile({
  134. // url: uni.baseUrl + 'data/api.auth.center/upload', // 仅为示例,非真实的接口地址
  135. // filePath:hex_md5(url) ,
  136. // name: 'file',
  137. // header: {
  138. // 'api-token': uni.getStorageSync("token")
  139. // },
  140. // success: (res) => {
  141. // // console.log(JSON.parse(res.data).data.url)
  142. // // console.log(JSON.parse(res.data).data.url)
  143. // // console.log(res.data)
  144. // resolve(JSON.parse(res.data).data.url)
  145. // // resolve(res.data.data)
  146. // }
  147. // });
  148. // })
  149. // }
  150. function setclip(val) {
  151. uni.setClipboardData({
  152. data: val,
  153. success() {
  154. uni.showToast({
  155. title: "复制成功"
  156. })
  157. }
  158. })
  159. }
  160. // 导出
  161. export const api = {
  162. to,
  163. pop,
  164. totabar,
  165. turnhtml,
  166. link,
  167. rdt,
  168. back,
  169. upload,
  170. setclip
  171. }