平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mixins.vue 4.9 KiB

3 months ago
2 months ago
3 months ago
3 months ago
2 months ago
3 months ago
2 months ago
3 months ago
2 months ago
3 months ago
3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <script>
  2. import http from '@/http/api.js'
  3. import {
  4. options
  5. } from '@/http/config.js';
  6. import {
  7. Base64
  8. } from '@/utils/base64.js';
  9. export default{
  10. methods:{
  11. // 暂存一个跨页面变量 (与 this.GET_PARAM 成对使用)
  12. SET_PARAM(val) {
  13. this.SET_GLOBAL('pageParam', val)
  14. },
  15. // 获取之前暂存的跨页面变量 (与 this.SET_PARAM 成对使用)
  16. GET_PARAM() {
  17. return this.GET_GLOBAL('pageParam')
  18. },
  19. // 进入某个页面
  20. NAV_TO(url, param, usePageParam) {
  21. uni.navigateTo({
  22. url: this.handleNav(url, param, usePageParam)
  23. })
  24. },
  25. // 返回上个页面
  26. // delta 参数为返回的次数
  27. NAV_BACK(delta = 1) {
  28. uni.navigateBack({
  29. delta
  30. })
  31. },
  32. // 跳转到某个页面,跳转后无法返回
  33. JUMP_TO(url, param, usePageParam) {
  34. uni.redirectTo({
  35. url: this.handleNav(url, param, usePageParam)
  36. })
  37. },
  38. // 转到某个 Tab 页
  39. TAB_TO(url, param) {
  40. uni.switchTab({
  41. url: this.handleNav(url, param, true)
  42. })
  43. },
  44. // 【内部方法】处理页面跳转 url 和参数
  45. handleNav(url, param, usePageParam) {
  46. let query = ''
  47. if (param && usePageParam) {
  48. this.SET_PARAM(param)
  49. } else if (param && !usePageParam) {
  50. query += '?' + Object.entries(param).filter(([k, v]) => k && v).map(([k, v]) => k + '=' + v).join('&')
  51. }
  52. return url + query
  53. },
  54. // 获取一个全局变量
  55. // key 为键名
  56. GET_GLOBAL(key) {
  57. return this.$store.state[key]
  58. },
  59. // 设置一个全局变量
  60. // key 为键名
  61. // val 为值
  62. SET_GLOBAL(key, val) {
  63. this.$store.commit(key, val)
  64. },
  65. // 清空全局变量
  66. CLEAR_GLOBAL() {
  67. this.$store.commit('clear')
  68. uni.removeStorageSync('token')
  69. },
  70. // 将数据写入本地缓存
  71. SET_STORAGE(key, data) {
  72. if (data === null) {
  73. uni.removeStorageSync(key)
  74. } else {
  75. uni.setStorageSync(key, data)
  76. }
  77. },
  78. // 获取之前写入本地缓存的数据
  79. GET_STORAGE(key) {
  80. return uni.getStorageSync(key)
  81. },
  82. // 清空本地缓存
  83. CLEAR_STORAGE() {
  84. uni.clearStorageSync()
  85. },
  86. // 设置页面标题
  87. SET_TITLE(title) {
  88. uni.setNavigationBarTitle({
  89. title
  90. })
  91. },
  92. // 监听一个全局事件
  93. ON(name, func) {
  94. uni.$on(name, func)
  95. },
  96. // 仅单次监听一个全局事件
  97. ONCE(name, func) {
  98. uni.$once(name, func)
  99. },
  100. // 触发一个全局事件
  101. EMIT(name, data) {
  102. uni.$emit(name, data)
  103. },
  104. // 移除全局事件监听器
  105. OFF(name, func) {
  106. uni.$off(name, func)
  107. },
  108. // 二次确认提示框
  109. CONFIRM(content='是否确认执行此操作?'){
  110. return new Promise((resolve)=>{
  111. uni.showModal({
  112. title: '提示',
  113. content: content,
  114. cancelText: "取消",
  115. confirmText: "确认",
  116. confirmColor: '#165DFF',
  117. cancelColor: '#165DFF',
  118. success: function (res) {
  119. if (res.confirm) {
  120. resolve(true)
  121. } else if (res.cancel) {
  122. resolve(false)
  123. }
  124. }
  125. });
  126. })
  127. },
  128. // 展示提示框
  129. // title 为提示文字
  130. // mask 为是否禁止点击
  131. // icon 为显示图标,可以改为 'success'
  132. TOAST(title){
  133. uni.showToast({
  134. icon: "none",
  135. title,
  136. duration: 2000,
  137. });
  138. },
  139. // 停止展示 toast 提示框
  140. HIDE_TOAST() {
  141. uni.hideToast()
  142. },
  143. UPLOAD_FILE(filePath){
  144. let that = this
  145. // 假设有token值需要在头部需要携带
  146. let header = {}
  147. let token = uni.getStorageSync('token');
  148. if (token) {
  149. config.header['Authorization'] = 'Bearer ' + token;
  150. }
  151. return new Promise(resovle=>{
  152. uni.uploadFile({
  153. url:options.baseURL + '/api/blade-resource/oss/endpoint/put-uplaod-file?bucketName=',
  154. filePath,
  155. name:'file',
  156. header,
  157. success(e){
  158. if(e.statusCode == 200){
  159. let res = JSON.parse(e.data)
  160. if(res.code == 200){
  161. resovle(res.data)
  162. }else{
  163. resovle(res.msg)
  164. }
  165. }else{
  166. resovle(false)
  167. }
  168. },
  169. fail(e){
  170. console.log('fail',e)
  171. resovle(false)
  172. },
  173. })
  174. })
  175. },
  176. DELETE_FILE(attachIds){
  177. return http.request({
  178. url: '/api/blade-resource/oss/endpoint/remove-file-attachIds',
  179. method: 'post',
  180. params:{attachIds}
  181. })
  182. },
  183. // 显示 loading 提示框
  184. // title 为提示文字
  185. // mask 为是否禁止点击
  186. LOADING(title, mask = true) {
  187. uni.showLoading({
  188. title,
  189. mask
  190. })
  191. },
  192. // 停止展示 loading 提示框
  193. HIDE_LOADING() {
  194. uni.hideLoading()
  195. },
  196. // url获取文件流
  197. DOWNLOAD_FILE(url){
  198. return uni.downloadFile({
  199. url
  200. })
  201. },
  202. //复制链接
  203. COPYLINK(item){
  204. return new Promise((resolve)=>{
  205. uni.setClipboardData({
  206. data: item, // 要复制的路径
  207. success: function(res) {
  208. resolve(true)
  209. }
  210. });
  211. })
  212. },
  213. },
  214. }
  215. </script>