main.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. // 新增:直接上传文件路径的方法(不再弹出选择图片对话框)
  206. const uploadImgByPath = (filePath, successCallback, failCallback = undefined) => {
  207. userInfo = uni.getStorageSync('userInfo');
  208. var accessToken = userInfo ? userInfo.accessToken : '';
  209. let baseUrl = config.def().baseUrl
  210. uni.getImageInfo({
  211. src: filePath,
  212. success: image => {
  213. console.log(image)
  214. uni.showLoading({
  215. title: '图片上传中',
  216. mask: true
  217. })
  218. uni.uploadFile({
  219. url: baseUrl + `/oss/app/upload`,
  220. file: image,
  221. filePath: image.path,
  222. header: {
  223. Authorization: 'Bearer ' + accessToken,
  224. },
  225. name: 'file',
  226. success: res => {
  227. if (successCallback) {
  228. successCallback(JSON.parse(res.data).data.url)
  229. }
  230. },
  231. fail: err => {
  232. if (failCallback) {
  233. failCallback(err)
  234. } else {
  235. uni.showToast({
  236. title: '上传图片失败',
  237. icon: 'none',
  238. duration: 2000,
  239. })
  240. }
  241. },
  242. complete: res => {
  243. uni.hideLoading()
  244. },
  245. })
  246. },
  247. fail: err => {
  248. uni.showToast({
  249. title: '获取图片信息失败',
  250. icon: 'none',
  251. duration: 2000,
  252. })
  253. },
  254. })
  255. }
  256. function get_suffix(filename) {
  257. var pos = filename.lastIndexOf('.')
  258. var suffix = ''
  259. if (pos != -1) {
  260. suffix = filename.substring(pos)
  261. }
  262. return suffix;
  263. }
  264. function random_string(len) {
  265. len = len || 32;
  266. var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  267. var maxPos = chars.length;
  268. var pwd = '';
  269. for (var i = 0; i < len; i++) {
  270. pwd += chars.charAt(Math.floor(Math.random() * maxPos));
  271. }
  272. return pwd;
  273. }
  274. const prePage = () => {
  275. let pages = getCurrentPages();
  276. let prePage = pages[pages.length - 2];
  277. // #ifdef H5
  278. return prePage;
  279. // #endif
  280. return prePage.$vm;
  281. }
  282. const globalData = {}
  283. let deviceType = ''
  284. // #ifdef MP-WEIXIN
  285. // 微信小程序编译的代码
  286. deviceType = 'routine'
  287. // #endif
  288. // #ifdef APP-PLUS
  289. // App平台编译的代码
  290. deviceType = 'app'
  291. Vue.prototype.$platform = uni.getSystemInfoSync().platform
  292. // #endif
  293. // #ifdef H5
  294. // H5编译的代码
  295. // 判断是否是微信浏览器
  296. async function init() {
  297. if (isWeixin()) {
  298. deviceType = 'weixin'
  299. let wechatInit = wechat()
  300. if (wechatInit) {
  301. await oAuth()
  302. }
  303. } else {
  304. deviceType = 'weixinh5'
  305. }
  306. }
  307. init()
  308. // #endif
  309. Vue.config.productionTip = false
  310. Vue.prototype.$fire = new Vue();
  311. Vue.prototype.$store = store;
  312. Vue.prototype.$deviceType = deviceType
  313. Vue.prototype.$api = {
  314. msg,
  315. prePage,
  316. request,
  317. uploadImg,
  318. uploadImgByPath,
  319. logout,
  320. isVip,
  321. setUserInfo,
  322. defConfig,
  323. globalData
  324. };
  325. //#ifdef H5
  326. Vue.prototype.$jweixin = jweixin;
  327. //#endif
  328. //zj 2.17 统一获取地理信息
  329. //isChoose 是否 chooseLocation 接口返回的经纬度 该接口返回的address没做省市区划分
  330. Vue.prototype.$getLocation = async (isChoose = false, latitude = '', longitude = '') => {
  331. var ret = {}
  332. if (isChoose) {
  333. ret.latitude = res.latitude
  334. ret.longitude = res.longitude
  335. } else {
  336. var [err, res] = await uni.getLocation({
  337. type: 'wgs84'
  338. })
  339. if (res && res.errMsg === 'getLocation:ok') {
  340. ret.latitude = res.latitude
  341. ret.longitude = res.longitude
  342. } else {
  343. console.log(err.errMsg)
  344. return false
  345. }
  346. }
  347. var that = this;
  348. var [error, geocodeData] = await uni.request({
  349. url: "https://restapi.amap.com/v3/geocode/regeo",
  350. method: "GET",
  351. data: {
  352. key: that.$api.defConfig().mapKey,
  353. location: ret.longitude + ',' + ret.latitude,
  354. extensions: 'all',
  355. // poitype:'120000|060000'返回poi限定范围
  356. }
  357. })
  358. if (geocodeData.statusCode === 200) {
  359. console.log(geocodeData)
  360. var data = geocodeData.data.regeocode.addressComponent
  361. for (var key in data) {
  362. ret[key] = data[key]
  363. }
  364. ret.pois = geocodeData.data.regeocode.pois
  365. return ret
  366. } else {
  367. console.log("高德地图报错信息error:"+error)
  368. console.log("高德地图报错信息geocodeData:"+geocodeData)
  369. return false
  370. }
  371. }
  372. App.mpType = 'app'
  373. const app = new Vue({
  374. ...App
  375. })
  376. store.commit('updateDevicetype', deviceType)
  377. app.$mount()