main.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import App from './App'
  2. import store from './store'
  3. import Vue from 'vue'
  4. Vue.config.productionTip = false
  5. App.mpType = 'app'
  6. // 引入全局TuniaoUI
  7. import TuniaoUI from 'tuniao-ui'
  8. Vue.use(TuniaoUI)
  9. // 引入TuniaoUI提供的vuex简写方法
  10. let vuexStore = require('@/store/$tn.mixin.js')
  11. Vue.mixin(vuexStore)
  12. // 引入TuniaoUI对小程序分享的mixin封装
  13. let mpShare = require('tuniao-ui/libs/mixin/mpShare.js')
  14. Vue.mixin(mpShare)
  15. const app = new Vue({
  16. store,
  17. ...App
  18. })
  19. const api = {
  20. getToken() {
  21. return uni.getStorageSync("access_token");
  22. },
  23. setToken: function(token) {
  24. uni.setStorageSync("access_token", token);
  25. },
  26. getUser() {
  27. return JSON.parse(uni.getStorageSync("user"));
  28. },
  29. setUser: function(user) {
  30. uni.setStorageSync("user", JSON.stringify(user));
  31. },
  32. isLogin: function() {
  33. return uni.getStorageSync("access_token") ? true : false
  34. },
  35. getUserPermission:function(){
  36. let user =api.getUser();
  37. if(user){
  38. return user.applet_permission;
  39. }
  40. uni.removeStorageSync("access_token")
  41. uni.removeStorageSync("user")
  42. // // #ifdef APP-PLUS || MP
  43. // this.deleteJYJPushAlias();
  44. // // #endif
  45. let url = '/pages/start/start?msg=' + (res.data.msg == undefined ? '' : res.data.msg)
  46. uni.navigateTo({
  47. url: url
  48. })
  49. return 0;
  50. },
  51. apiHost: function() {
  52. //接口地址
  53. // return 'http://192.168.31.75/'
  54. return 'https://ai.my-123.cn/api/'
  55. },
  56. request: function(url, params, method,type,hideLoading) {
  57. //接口请求
  58. if (!hideLoading) {
  59. uni.showLoading({
  60. mask: true,
  61. title: '请稍候...'
  62. })
  63. }
  64. if(method==null||method==""){
  65. method="POST"
  66. }
  67. if(type==null||type==""){
  68. type='application/json'
  69. }
  70. return new Promise((resolve, reject) => {
  71. uni.request({
  72. url: this.apiHost() + url,
  73. data: params,
  74. header: {
  75. 'Authorization':'Basic d3hBcHBsZXQ6d3hBcHBsZXRfc2VjcmV0',
  76. 'tenant_id':'000000',
  77. 'Blade-Auth': "bearer "+this.getToken(),
  78. 'content-type': type,
  79. },
  80. method: method,
  81. // dataType: 'json',
  82. success: (res) => {
  83. !hideLoading && uni.hideLoading()
  84. if (res.data.code == 200) { //访问成功
  85. resolve(res.data.data)
  86. }else if (res.data.code == 401) {
  87. uni.removeStorageSync("access_token")
  88. uni.removeStorageSync("user")
  89. // // #ifdef APP-PLUS || MP
  90. // this.deleteJYJPushAlias();
  91. // // #endif
  92. let url = '/pages/start/start?msg=' + (res.data.msg == undefined ? '' : res.data.msg)
  93. uni.navigateTo({
  94. url: url
  95. })
  96. }else {
  97. this.toast( res.data.msg == null ? "请求失败 , 请重试" : res.data.msg);
  98. reject(res)
  99. }
  100. },
  101. fail: (res) => {
  102. if (!hideLoading) {
  103. this.toast("网络不给力,请稍后再试~")
  104. }
  105. reject(res)
  106. }
  107. })
  108. })
  109. },
  110. toast: function(text, duration, success) {
  111. uni.showToast({
  112. title: text,
  113. icon: success ? 'success' : 'none',
  114. duration: duration || 2000
  115. })
  116. },
  117. formatTime:function(date, state) {
  118. if (!date) {
  119. date = new Date()
  120. }else{
  121. date = new Date(date)
  122. }
  123. let year = date.getFullYear()
  124. let month = date.getMonth() + 1
  125. let day = date.getDate()
  126. // const week = weekFormat(date.getDay())
  127. let hour = date.getHours()
  128. let minute = date.getMinutes()
  129. let second = date.getSeconds()
  130. let milliSecond = date.getMilliseconds()
  131. if(month<10){
  132. month="0"+month;
  133. }
  134. if(day<10){
  135. day="0"+day;
  136. }
  137. if(hour<10){
  138. hour="0"+hour;
  139. }
  140. if(minute<10){
  141. minute="0"+minute;
  142. }
  143. if(second<10){
  144. second="0"+second;
  145. }
  146. if (state == 'Y') {
  147. return year
  148. }
  149. if (state == 'YM') {
  150. return [year, month].join('-')
  151. }
  152. if (state == 'YMD') {
  153. return [year, month, day].join('-')
  154. }
  155. if (state == 'YMDHM') {
  156. return [year, month, day].join('-') + ' ' + [hour, minute].join(':')
  157. }
  158. if (state == 'HM') {
  159. return [hour, minute].join(':')
  160. }
  161. // if (state == 'MWHM') {
  162. // return [month, day].map(formatNumber).join('-') + ' ' + week + ' ' + [hour, minute].map(formatNumber).join(':')
  163. // }
  164. return [year, month, day].join('-')
  165. },
  166. }
  167. // // 引入请求封装
  168. // require('./util/request/index')(app)
  169. Vue.prototype.api = api
  170. app.$mount()