main.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. import Vue from 'vue'
  2. import App from './App'
  3. import directive from './common/directive.js'
  4. import utils from './common/utils.js'
  5. import config from './config.js'
  6. import onfire from './common/onfire.js'
  7. import {
  8. gotopage
  9. } from '@/common/gotopage.js'
  10. /* Vuex */
  11. import store from "./store/index.js"
  12. Vue.prototype.$store = store;
  13. // 公共组件
  14. import headerBar from './components/header.vue'
  15. Vue.component('header-bar', headerBar)
  16. // import Vconsole from 'vconsole'
  17. // const vConsole = new Vconsole()
  18. // Vue.use(vConsole)
  19. Vue.prototype.$fire = new onfire()
  20. Vue.config.productionTip = false
  21. App.mpType = 'app'
  22. Vue.prototype.config = config
  23. const app = new Vue({
  24. ...App
  25. })
  26. app.$mount()
  27. Vue.prototype.websiteUrl = config.app_url;
  28. Vue.prototype.app_id = config.app_id;
  29. //h5发布路径
  30. Vue.prototype.h5_addr = config.h5_addr;
  31. /*页面跳转*/
  32. Vue.prototype.gotoPage = gotopage;
  33. //#ifdef H5
  34. app.$router.afterEach((to, from) => {
  35. const u = navigator.userAgent.toLowerCase()
  36. if (u.indexOf("like mac os x") < 0 || u.match(/MicroMessenger/i) != 'micromessenger') return
  37. if (to.path !== global.location.pathname) {
  38. location.assign(config.h5_addr + to.fullPath);
  39. }
  40. })
  41. //#endif
  42. //是否是ios
  43. Vue.prototype.ios = function() {
  44. const u = navigator.userAgent.toLowerCase();
  45. if (u.indexOf("like mac os x") < 0 || u.match(/MicroMessenger/i) != 'micromessenger') {
  46. return false;
  47. }
  48. return true;
  49. };
  50. //get请求
  51. Vue.prototype._get = function(path, data, success, fail, complete) {
  52. data = data || {};
  53. data.token = uni.getStorageSync('token') || '';
  54. data.app_id = this.getAppId();
  55. uni.request({
  56. url: this.websiteUrl + '/index.php/api/' + path,
  57. data: data,
  58. dataType: 'json',
  59. method: 'GET',
  60. success: (res) => {
  61. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  62. return false;
  63. }
  64. if (res.data.code === -2) {
  65. console.log('用户已删除');
  66. console.log(res.data.msg)
  67. this.showError(res.data.msg, function() {
  68. uni.removeStorageSync('token');
  69. this.gotoPage('/pages/index/index', 'reLaunch');
  70. })
  71. } else if (res.data.code === -1) {
  72. // 登录态失效, 重新登录
  73. console.log('登录态失效, 重新登录');
  74. this.doLogin();
  75. } else if (res.data.code === 0) {
  76. this.showError(res.data.msg, function() {
  77. fail && fail(res);
  78. });
  79. return false;
  80. } else {
  81. success && success(res.data);
  82. }
  83. },
  84. fail: (res) => {
  85. fail && fail(res);
  86. },
  87. complete: (res) => {
  88. uni.hideLoading();
  89. complete && complete(res);
  90. },
  91. });
  92. };
  93. //get请求
  94. Vue.prototype._post = function(path, data, success, fail, complete) {
  95. data = data || {};
  96. data.token = uni.getStorageSync('token') || '';
  97. data.app_id = this.getAppId();
  98. uni.request({
  99. url: this.websiteUrl + '/index.php/api/' + path,
  100. data: data,
  101. dataType: 'json',
  102. method: 'POST',
  103. header: {
  104. 'content-type': 'application/x-www-form-urlencoded',
  105. },
  106. success: (res) => {
  107. if (res.statusCode !== 200 || typeof res.data !== 'object') {
  108. return false;
  109. }
  110. if (res.data.code === -1) {
  111. // 登录态失效, 重新登录
  112. console.log('登录态失效, 重新登录');
  113. this.doLogin();
  114. } else if (res.data.code === 0) {
  115. this.showError(res.data.msg, function() {
  116. fail && fail(res);
  117. });
  118. return false;
  119. } else {
  120. success && success(res.data);
  121. }
  122. },
  123. fail: (res) => {
  124. fail && fail(res);
  125. },
  126. complete: (res) => {
  127. uni.hideLoading();
  128. complete && complete(res);
  129. },
  130. });
  131. };
  132. Vue.prototype.doLogin = function() {
  133. let pages = getCurrentPages();
  134. if (pages.length) {
  135. let currentPage = pages[pages.length - 1];
  136. if ("pages/login/login" != currentPage.route &&
  137. "pages/login/weblogin" != currentPage.route &&
  138. "pages/login/openlogin" != currentPage.route) {
  139. uni.setStorageSync("currentPage", currentPage.route);
  140. uni.setStorageSync("currentPageOptions", currentPage.options);
  141. }
  142. }
  143. //公众号
  144. // #ifdef H5
  145. if (this.isWeixin()) {
  146. let invitation_id = uni.getStorageSync('invitation_id') ? uni.getStorageSync('invitation_id') : 0;
  147. window.location.href = this.websiteUrl + '/index.php/api/user.usermp/login?app_id=' + this.getAppId() +
  148. '&referee_id=' + uni.getStorageSync('referee_id') + '&invitation_id=' + invitation_id;
  149. } else {
  150. this.gotoPage("/pages/login/weblogin");
  151. }
  152. // #endif
  153. // #ifdef APP-PLUS
  154. this.gotoPage("/pages/login/openlogin");
  155. return;
  156. // #endif
  157. // 非公众号,跳转授权页面
  158. // #ifndef H5
  159. this.gotoPage("/pages/login/login");
  160. // #endif
  161. };
  162. /**
  163. * 显示失败提示框
  164. */
  165. Vue.prototype.showError = function(msg, callback) {
  166. uni.showModal({
  167. title: '友情提示',
  168. content: msg,
  169. showCancel: false,
  170. success: function(res) {
  171. callback && callback();
  172. }
  173. });
  174. };
  175. /**
  176. * 显示失败提示框
  177. */
  178. Vue.prototype.showSuccess = function(msg, callback) {
  179. uni.showModal({
  180. title: '友情提示',
  181. content: msg,
  182. showCancel: false,
  183. success: function(res) {
  184. callback && callback();
  185. }
  186. });
  187. };
  188. /**
  189. * 获取应用ID
  190. */
  191. Vue.prototype.getAppId = function() {
  192. return this.app_id || 10001;
  193. };
  194. Vue.prototype.compareVersion = function(v1, v2) {
  195. v1 = v1.split('.')
  196. v2 = v2.split('.')
  197. const len = Math.max(v1.length, v2.length)
  198. while (v1.length < len) {
  199. v1.push('0')
  200. }
  201. while (v2.length < len) {
  202. v2.push('0')
  203. }
  204. for (let i = 0; i < len; i++) {
  205. const num1 = parseInt(v1[i])
  206. const num2 = parseInt(v2[i])
  207. if (num1 > num2) {
  208. return 1
  209. } else if (num1 < num2) {
  210. return -1
  211. }
  212. }
  213. return 0
  214. };
  215. /**
  216. * 生成转发的url参数
  217. */
  218. Vue.prototype.getShareUrlParams = function(params) {
  219. let self = this;
  220. return utils.urlEncode(Object.assign({
  221. referee_id: self.getUserId(),
  222. app_id: self.getAppId()
  223. }, params));
  224. };
  225. /**
  226. * 当前用户id
  227. */
  228. Vue.prototype.getUserId = function() {
  229. return uni.getStorageSync('user_id');
  230. };
  231. //#ifdef H5
  232. var jweixin = require('jweixin-module');
  233. Vue.prototype.configWx = function(signPackage, shareParams, params) {
  234. if (signPackage == '') {
  235. return;
  236. }
  237. let self = this;
  238. jweixin.config(JSON.parse(signPackage));
  239. let url_params = self.getShareUrlParams(params);
  240. jweixin.ready(function(res) {
  241. jweixin.updateAppMessageShareData({
  242. title: shareParams.title,
  243. desc: shareParams.desc,
  244. link: self.websiteUrl + self.h5_addr + shareParams.link + '?' + url_params,
  245. imgUrl: shareParams.imgUrl,
  246. success: function() {
  247. }
  248. });
  249. jweixin.updateTimelineShareData({
  250. title: shareParams.title,
  251. desc: shareParams.desc,
  252. link: self.websiteUrl + self.h5_addr + shareParams.link + '?' + url_params,
  253. imgUrl: shareParams.imgUrl,
  254. success: function() {
  255. }
  256. });
  257. });
  258. };
  259. Vue.prototype.configWxScan = function(signPackage) {
  260. if (signPackage == '') {
  261. return;
  262. }
  263. jweixin.config(JSON.parse(signPackage));
  264. return jweixin;
  265. };
  266. //#endif
  267. /**
  268. * 获取当前平台
  269. */
  270. Vue.prototype.getPlatform = function(params) {
  271. let platform = 'wx';
  272. // #ifdef APP-PLUS
  273. platform = 'app';
  274. // #endif
  275. // #ifdef H5
  276. if (this.isWeixin()) {
  277. platform = 'mp';
  278. } else {
  279. platform = 'h5';
  280. }
  281. // #endif
  282. return platform;
  283. };
  284. /**
  285. * 订阅通知,目前仅小程序
  286. */
  287. Vue.prototype.subMessage = function(temlIds, callback) {
  288. let self = this;
  289. // #ifdef MP-WEIXIN
  290. //小程序订阅消息
  291. const version = wx.getSystemInfoSync().SDKVersion;
  292. if (temlIds && temlIds.length != 0 && self.compareVersion(version, '2.8.2') >= 0) {
  293. wx.requestSubscribeMessage({
  294. tmplIds: temlIds,
  295. success(res) {},
  296. fail(res) {},
  297. complete(res) {
  298. callback();
  299. },
  300. });
  301. } else {
  302. callback();
  303. }
  304. // #endif
  305. // #ifndef MP-WEIXIN
  306. callback();
  307. // #endif
  308. };
  309. Vue.prototype.isWeixin = function() {
  310. var ua = navigator.userAgent.toLowerCase();
  311. if (ua.match(/MicroMessenger/i) == "micromessenger") {
  312. return true;
  313. } else {
  314. return false;
  315. }
  316. };
  317. Vue.prototype.topBarTop = function() {
  318. // #ifdef MP-WEIXIN
  319. return uni.getMenuButtonBoundingClientRect().top;
  320. // #endif
  321. // #ifndef MP-WEIXIN
  322. const SystemInfo = uni.getSystemInfoSync();
  323. return SystemInfo.statusBarHeight;
  324. // #endif
  325. };
  326. Vue.prototype.topBarHeight = function() {
  327. // #ifdef MP-WEIXIN
  328. return uni.getMenuButtonBoundingClientRect().height;
  329. // #endif
  330. // #ifndef MP-WEIXIN
  331. return 0
  332. // #endif
  333. };
  334. Vue.prototype.yulan = function(e, i) {
  335. let image_path_arr = [];
  336. if (!Array.isArray(e)) {
  337. image_path_arr = [e];
  338. } else {
  339. if (e[0].file_path) {
  340. e.forEach((item, index) => {
  341. image_path_arr.push(item.file_path)
  342. })
  343. } else {
  344. image_path_arr = e
  345. }
  346. }
  347. let picnum = i * 1;
  348. uni.previewImage({
  349. urls: image_path_arr,
  350. current: picnum,
  351. });
  352. }
  353. Vue.prototype.theme = function() {
  354. return this.$store.state.theme
  355. }