main.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import Vue from 'vue'
  2. import store from './store'
  3. import App from './App'
  4. import * as filters from './filters'
  5. import * as config from './config'
  6. //自定义modal
  7. import initModal from "@/components/zhangxu-showModal/initModal.js";
  8. import showModal from '@/components/zhangxu-showModal/show-modal';
  9. initModal(Vue);
  10. Vue.component('show-modal', showModal);
  11. Object.keys(filters).forEach(key => {
  12. Vue.filter(key, filters[key])
  13. })
  14. //#ifdef H5
  15. let jweixin = require('./components/jweixin-module')
  16. let jwx = require('./components/jweixin-module/jwx')
  17. Vue.mixin({
  18. onShow() {
  19. // jwx.configWeiXin(jwx => {
  20. // })
  21. }
  22. })
  23. //#endif
  24. const defConfig = config.def
  25. const msg = (title, duration = 1500, mask = false, icon = 'none') => {
  26. //统一提示方便全局修改
  27. if (Boolean(title) === false) {
  28. return;
  29. }
  30. uni.showToast({
  31. title,
  32. duration,
  33. mask,
  34. icon
  35. });
  36. }
  37. let userInfo = undefined
  38. const logout = () => {
  39. userInfo = undefined
  40. uni.removeStorage({
  41. key: 'userInfo'
  42. })
  43. }
  44. const setUserInfo = (i) => {
  45. userInfo = i
  46. }
  47. const isVip = () => {
  48. return userInfo && userInfo.level
  49. }
  50. let loginLock = false
  51. const request = (method, endpoint, data = {}, failCallback) => {
  52. //异步请求数据
  53. return new Promise(resolve => {
  54. userInfo = uni.getStorageSync('userInfo')
  55. let accessToken = userInfo ? userInfo.accessToken : ''
  56. let addresses = uni.getStorageSync('addresses') || '';
  57. let baseUrl = config.def().baseUrl
  58. let content = 'application/x-www-form-urlencoded; charset=UTF-8';
  59. if (method === 'post') {
  60. content = 'application/json; charset=UTF-8';
  61. }
  62. uni.request({
  63. url: (endpoint.startsWith('http://') || endpoint.startsWith('https://')) ? endpoint : baseUrl + "/" +
  64. endpoint,
  65. method: method,
  66. data: data,
  67. header: {
  68. 'Content-Type': content,
  69. 'Authorization': 'Bearer ' + accessToken
  70. },
  71. success: (res) => {
  72. if (endpoint.startsWith('http://') || endpoint.startsWith('https://')) {
  73. return resolve(res.data)
  74. }
  75. if (res.data.code === 200) {
  76. //#ifdef H5
  77. if ((!addresses || !addresses.addressesName) && accessToken) {
  78. uni.showModal({
  79. title: '地图提示',
  80. content: '您尚未选择,您的位置?',
  81. showCancel: true,
  82. confirmText: '选择',
  83. success: (e) => {
  84. if (e.confirm) {
  85. uni.chooseLocation({
  86. success: (res1) => {
  87. console.log(res1)
  88. if (res1.name === "") {
  89. return
  90. }
  91. let addressesInfo = {};
  92. addressesInfo.lng = res1.longitude;
  93. addressesInfo.lat = res1.latitude;
  94. addressesInfo.addressesName = res1.name;
  95. uni.setStorageSync('addresses', addressesInfo);
  96. uni.reLaunch({
  97. url: '/pages/index/index?type=1'
  98. });
  99. },
  100. fail: (e) => {
  101. console.log(e)
  102. }
  103. });
  104. }
  105. },
  106. fail: () => {}
  107. })
  108. }
  109. //#endif
  110. resolve(res.data);
  111. } else if (res.data.code === 401) {
  112. if (failCallback) {
  113. failCallback(res.data)
  114. }
  115. if (!loginLock) {
  116. loginLock = true
  117. uni.showModal({
  118. title: '登录提示',
  119. content: '您尚未登录,是否立即登录?',
  120. showCancel: true,
  121. confirmText: '登录',
  122. success: (e) => {
  123. if (e.confirm) {
  124. uni.navigateTo({
  125. url: '/pages/public/login'
  126. })
  127. }
  128. },
  129. fail: () => {},
  130. complete: () => {
  131. loginLock = false
  132. }
  133. })
  134. }
  135. } else {
  136. if (failCallback) {
  137. failCallback(res.data)
  138. } else {
  139. uni.showToast({
  140. title: res.data.msg,
  141. icon: 'none'
  142. })
  143. }
  144. }
  145. }
  146. })
  147. })
  148. }
  149. const uploadImg = (num = 9, successCallback, failCallback= undefined) => {
  150. userInfo = uni.getStorageSync('userInfo');
  151. var accessToken = userInfo ? userInfo.accessToken : '';
  152. let baseUrl = config.def().baseUrl
  153. uni.chooseImage({
  154. count: 1,
  155. sourceType: ['album'],
  156. success: res => {
  157. uni.getImageInfo({
  158. src: res.tempFilePaths[0],
  159. success: image => {
  160. console.log(image)
  161. uni.showLoading({
  162. title: '图片上传中',
  163. mask: true
  164. })
  165. uni.uploadFile({
  166. url: baseUrl + `/oss/app/upload`,
  167. file: image,
  168. filePath: image.path,
  169. header: {
  170. Authorization: 'Bearer ' + accessToken,
  171. },
  172. name: 'file',
  173. success: res => {
  174. if (successCallback) {
  175. successCallback(JSON.parse(res.data).data.url)
  176. }
  177. },
  178. fail: err => {
  179. if (failCallback) {
  180. failCallback(err)
  181. } else {
  182. uni.showToast({
  183. title: '上传图片失败',
  184. icon: 'none',
  185. duration: 2000,
  186. })
  187. }
  188. },
  189. complete: res => {
  190. uni.hideLoading()
  191. },
  192. })
  193. },
  194. fail: err => {
  195. uni.showToast({
  196. title: '获取图片信息失败',
  197. icon: 'none',
  198. duration: 2000,
  199. })
  200. },
  201. })
  202. },
  203. })
  204. }
  205. function get_suffix(filename) {
  206. var pos = filename.lastIndexOf('.')
  207. var suffix = ''
  208. if (pos != -1) {
  209. suffix = filename.substring(pos)
  210. }
  211. return suffix;
  212. }
  213. function random_string(len) {
  214. len = len || 32;
  215. var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  216. var maxPos = chars.length;
  217. var pwd = '';
  218. for (var i = 0; i < len; i++) {
  219. pwd += chars.charAt(Math.floor(Math.random() * maxPos));
  220. }
  221. return pwd;
  222. }
  223. const prePage = () => {
  224. let pages = getCurrentPages();
  225. let prePage = pages[pages.length - 2];
  226. // #ifdef H5
  227. return prePage;
  228. // #endif
  229. return prePage.$vm;
  230. }
  231. const globalData = {}
  232. let deviceType = ''
  233. // #ifdef MP-WEIXIN
  234. // 微信小程序编译的代码
  235. deviceType = 'routine'
  236. // #endif
  237. // #ifdef APP-PLUS
  238. // App平台编译的代码
  239. deviceType = 'app'
  240. Vue.prototype.$platform = uni.getSystemInfoSync().platform
  241. // #endif
  242. // #ifdef H5
  243. // H5编译的代码
  244. // 判断是否是微信浏览器
  245. async function init() {
  246. if (isWeixin()) {
  247. deviceType = 'weixin'
  248. let wechatInit = wechat()
  249. if (wechatInit) {
  250. await oAuth()
  251. }
  252. } else {
  253. deviceType = 'weixinh5'
  254. }
  255. }
  256. init()
  257. // #endif
  258. Vue.config.productionTip = false
  259. Vue.prototype.$fire = new Vue();
  260. Vue.prototype.$store = store;
  261. Vue.prototype.$deviceType = deviceType
  262. Vue.prototype.$api = {
  263. msg,
  264. prePage,
  265. request,
  266. uploadImg,
  267. logout,
  268. isVip,
  269. setUserInfo,
  270. defConfig,
  271. globalData
  272. };
  273. //#ifdef H5
  274. Vue.prototype.$jweixin = jweixin;
  275. //#endif
  276. //zj 2.17 统一获取地理信息
  277. //isChoose 是否 chooseLocation 接口返回的经纬度 该接口返回的address没做省市区划分
  278. Vue.prototype.$getLocation = async (isChoose = false, latitude = '', longitude = '') => {
  279. var ret = {}
  280. if (isChoose) {
  281. ret.latitude = res.latitude
  282. ret.longitude = res.longitude
  283. } else {
  284. var [err, res] = await uni.getLocation({
  285. type: 'wgs84'
  286. })
  287. if (res && res.errMsg === 'getLocation:ok') {
  288. ret.latitude = res.latitude
  289. ret.longitude = res.longitude
  290. } else {
  291. console.log(err.errMsg)
  292. return false
  293. }
  294. }
  295. var that = this;
  296. var [error, geocodeData] = await uni.request({
  297. url: "https://restapi.amap.com/v3/geocode/regeo",
  298. method: "GET",
  299. data: {
  300. key: that.$api.defConfig().mapKey,
  301. location: ret.longitude + ',' + ret.latitude,
  302. extensions: 'all',
  303. // poitype:'120000|060000'返回poi限定范围
  304. }
  305. })
  306. if (geocodeData.statusCode === 200) {
  307. console.log(geocodeData)
  308. var data = geocodeData.data.regeocode.addressComponent
  309. for (var key in data) {
  310. ret[key] = data[key]
  311. }
  312. ret.pois = geocodeData.data.regeocode.pois
  313. return ret
  314. } else {
  315. console.log("高德地图报错信息error:"+error)
  316. console.log("高德地图报错信息geocodeData:"+geocodeData)
  317. return false
  318. }
  319. }
  320. App.mpType = 'app'
  321. const app = new Vue({
  322. ...App
  323. })
  324. store.commit('updateDevicetype', deviceType)
  325. app.$mount()