App.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <script>
  2. import Vue from 'vue'
  3. import api from 'utils/api'
  4. import util from 'utils/util'
  5. import __config from 'config/env';
  6. import JMessage from 'public/jmessage-wxapplet-sdk/jmessage-wxapplet-sdk-1.4.0.min.js'
  7. // #ifdef APP-PLUS
  8. import APPUpdate from "@/public/APPUpdate/index.js"; //App版本更新
  9. // #endif
  10. export default {
  11. globalData: {
  12. isIPad: false,
  13. isIos: false,
  14. isAndroid: false,
  15. systemInfo: {},
  16. shoppingCartCount: 0, //购物车数量
  17. tenantId: null, //租户Id
  18. appId: null, //公众号appId
  19. componentAppId: null, //第三方平台appid
  20. theme: { //主题定义
  21. backgroundColor: null, //背景颜色,支持渐变色
  22. themeColor: null, //主题颜色
  23. tabbarBackgroundColor: null, //tabbar背景颜色
  24. tabbarColor: null, //tabBar上的文字默认颜色
  25. tabbarSelectedColor: null, //tabBar上的文字选中时的颜色
  26. tabbarBorderStyle: null, //tabBar上边框的颜色
  27. tabbarItem: [] //tabBar明细设置
  28. },
  29. JIM: null, //极光JMessage
  30. },
  31. onLaunch: async function() {
  32. // #ifdef MP
  33. //小程序平台检测新版本
  34. this.updateManager();
  35. //this.doLogin();
  36. // #endif
  37. // 原生app版本更新检测
  38. // #ifdef APP-PLUS
  39. // APPUpdate();
  40. // #endif
  41. //隐藏原生tabbar
  42. uni.hideTabBar()
  43. // #ifdef H5
  44. // H5平台获取参数中的租户ID、公众号appID,并存入globalData
  45. let local = location.href
  46. let tenantId = util.getUrlParam(local, "tenant_id");
  47. let appId = util.getUrlParam(local, "app_id");
  48. let componentAppId = util.getUrlParam(local, "component_appid");
  49. this.globalData.tenantId = tenantId;
  50. this.globalData.appId = appId;
  51. this.globalData.componentAppId = componentAppId;
  52. // #endif
  53. // #ifdef APP-PLUS
  54. // APP平台需要从配置文件中获取租户ID
  55. let tenantId = __config.tenantId;
  56. this.globalData.tenantId = tenantId;
  57. // #endif
  58. //设置全局样式
  59. this.setGlobalStyle();
  60. if (uni.getStorageSync('wx_token_value')) {}
  61. // 上报设备信息
  62. await this.deviceReport()
  63. },
  64. methods: {
  65. // 同步上报设备信息
  66. async deviceReport() {
  67. let info = uni.getSystemInfoSync()
  68. this.globalData.systemInfo = info
  69. this.globalData.deviceId = info.deviceId
  70. this.globalData.isIPad = (info.devicePixelRatio == 2 && info.windowWidth > 420)
  71. if(info.system.toLowerCase().indexOf("ios") > -1){
  72. this.globalData.isIos = true
  73. }
  74. if(info.system.toLowerCase().indexOf("android") > -1){
  75. this.globalData.isAndroid = true
  76. }
  77. this.$log('device-id: ' + info.deviceId)
  78. console.log('system: ', info.system)
  79. console.log('model: ', info.model)
  80. for (let k in info) {
  81. console.log(k + ': ', info[k])
  82. }
  83. console.log('globalSystemInfo: ', info)
  84. // APP上报设备信息
  85. let res = await api.deviceReport(info)
  86. //TODO 待处理店铺信息
  87. if (res.data && JSON.stringify(res.data) != "{}") {
  88. //店铺ID放入缓存
  89. uni.setStorageSync(__config.shopIdSessionKey, res.data.id)
  90. } else {
  91. uni.removeStorageSync(__config.shopIdSessionKey)
  92. }
  93. console.log('^^^^^^^^^^^^^^^^****************^^^^^^^^^^^^^^^^^')
  94. console.log('isIPad: ', this.globalData.isIPad)
  95. console.log('isIos: ', this.globalData.isIos)
  96. console.log('isAndroid: ', this.globalData.isAndroid)
  97. },
  98. //设置全局样式
  99. setGlobalStyle() {
  100. uni.getSystemInfo({
  101. success: function(e) {
  102. // [2020-08-01]更新ColorUI 修复ios 状态栏错位
  103. // #ifndef MP
  104. Vue.prototype.StatusBar = e.statusBarHeight;
  105. if (e.platform == 'android') {
  106. Vue.prototype.CustomBar = e.statusBarHeight + 50;
  107. } else {
  108. Vue.prototype.CustomBar = e.statusBarHeight + 45;
  109. };
  110. // #endif
  111. // #ifdef MP-WEIXIN || MP-QQ
  112. Vue.prototype.StatusBar = e.statusBarHeight;
  113. let capsule = wx.getMenuButtonBoundingClientRect();
  114. if (capsule) {
  115. Vue.prototype.Custom = capsule;
  116. // Vue.prototype.capsuleSafe = uni.upx2px(750) - capsule.left + uni.upx2px(750) - capsule.right;
  117. Vue.prototype.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
  118. } else {
  119. Vue.prototype.CustomBar = e.statusBarHeight + 50;
  120. }
  121. // #endif
  122. // #ifdef MP-ALIPAY
  123. Vue.prototype.StatusBar = e.statusBarHeight;
  124. Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
  125. // #endif
  126. // 处理IPAD头部高度问题
  127. if(e.devicePixelRatio == 2 && e.windowWidth > 420){
  128. if(e.system.toLowerCase().indexOf("ios") > -1){
  129. Vue.prototype.CustomBar = Vue.prototype.CustomBar + 20
  130. }
  131. if(e.system.toLowerCase().indexOf("android") > -1){
  132. Vue.prototype.CustomBar = Vue.prototype.CustomBar
  133. }
  134. }
  135. }
  136. })
  137. //let themeMobile = res.data;
  138. //定义默认配置
  139. let backgroundColor = 'blue';
  140. let themeColor = 'purple';
  141. let tabbarBackgroundColor = '#ffffff';
  142. let tabbarColor = '#666666';
  143. let tabbarSelectedColor = '#f43f3b';
  144. let tabbarBorderStyle = '#black';
  145. let tabbarItem = [{
  146. index: 0,
  147. text: '首页',
  148. iconPath: '/static/public/img/icon-1/1-001.png',
  149. selectedIconPath: '/static/public/img/icon-1/1-002.png'
  150. },
  151. {
  152. index: 1,
  153. text: '分类',
  154. iconPath: '/static/public/img/icon-1/2-001.png',
  155. selectedIconPath: '/static/public/img/icon-1/2-002.png'
  156. },
  157. {
  158. index: 2,
  159. text: '消息',
  160. iconPath: '/static/public/img/icon-1/3-001.png',
  161. selectedIconPath: '/static/public/img/icon-1/3-002.png'
  162. },
  163. {
  164. index: 3,
  165. text: '购物车',
  166. iconPath: '/static/public/img/icon-1/4-001.png',
  167. selectedIconPath: '/static/public/img/icon-1/4-002.png'
  168. },
  169. {
  170. index: 4,
  171. text: '我的',
  172. iconPath: '/static/public/img/icon-1/5-001.png',
  173. selectedIconPath: '/static/public/img/icon-1/5-002.png'
  174. }
  175. ]
  176. //将默认配置换成后台数据
  177. this.globalData.theme.backgroundColor = backgroundColor
  178. this.globalData.theme.themeColor = themeColor
  179. this.globalData.theme.tabbarBackgroundColor = tabbarBackgroundColor
  180. this.globalData.theme.tabbarColor = tabbarColor
  181. this.globalData.theme.tabbarSelectedColor = tabbarSelectedColor
  182. this.globalData.theme.tabbarBorderStyle = tabbarBorderStyle
  183. //this.globalData.theme.tabbarItem = tabbarItem
  184. },
  185. // #ifdef MP
  186. //小程序平台检测新版本
  187. updateManager() {
  188. const updateManager = uni.getUpdateManager();
  189. updateManager.onUpdateReady(function() {
  190. uni.showModal({
  191. title: '更新提示',
  192. content: '新版本已经准备好,是否重启应用?',
  193. success(res) {
  194. if (res.confirm) {
  195. updateManager.applyUpdate();
  196. }
  197. }
  198. });
  199. });
  200. },
  201. // #endif
  202. /**
  203. * 获取用户信息(接口方式)
  204. */
  205. getAccountInfoSync() {
  206. let params = {}
  207. if (this.getAccountId()) {
  208. params['accountId'] = this.getAccountId()
  209. }
  210. return api.getCurrentUserSync(params)
  211. },
  212. /**
  213. * 获取缓存中用户信息(不存在返回undefined)
  214. */
  215. getAccountInfo() {
  216. let userInfo = uni.getStorageSync(__config.loginWxUserInfoKey)
  217. if (userInfo)
  218. return userInfo;
  219. return undefined
  220. },
  221. /**
  222. * 获取缓存中用户Id(无值返回undefined)
  223. */
  224. getAccountId() {
  225. let account = this.getAccountInfo()
  226. return account ? account.accountId : undefined
  227. },
  228. /**
  229. * 获取缓存中用户手机号(无值返回undefined)
  230. */
  231. getAccountMobile() {
  232. let account = this.getAccountInfo()
  233. return account ? account.mobile : undefined
  234. },
  235. /**
  236. * 获取缓存中用户是否授权过微信自身的用户信息(无值返回false)
  237. */
  238. getAccountIsAuth() {
  239. let retFlag = false
  240. let account = this.getAccountInfo()
  241. if (!account)
  242. return retFlag
  243. if (account.headerImg) {
  244. retFlag = true
  245. }
  246. return retFlag
  247. },
  248. //页面初始化方法,供每个页面调用
  249. initPage() {
  250. let that = this;
  251. return new Promise((resolve, reject) => {
  252. //小程序或公众号H5,每个页面都进行登录校验
  253. if (util.isMiniPg() || (that.globalData.appId && util.isWeiXinBrowser())) {
  254. console.info('initPage : ' + __config.tokenValueKey, uni.getStorageSync(__config
  255. .tokenValueKey))
  256. if (!uni.getStorageSync(__config.tokenValueKey)) {
  257. //无thirdSession,进行登录
  258. that.doLogin().then(res => {
  259. resolve("success");
  260. });
  261. } else {
  262. if (util.isMiniPg()) { //小程序需要检查登录态是否过期
  263. uni.checkSession({
  264. success() {
  265. //session_key 未过期,并且在本生命周期一直有效
  266. console.log('session_key 未过期')
  267. resolve("success");
  268. },
  269. fail() {
  270. // session_key 已经失效,需要重新执行登录流程
  271. console.log('session_key 已经失效')
  272. that.doLogin().then(res => {
  273. resolve("success");
  274. });
  275. }
  276. })
  277. } else {
  278. resolve("success");
  279. }
  280. }
  281. } else {
  282. resolve("success");
  283. }
  284. });
  285. },
  286. //登录操作
  287. doLogin() {
  288. uni.showLoading({
  289. title: '登录中'
  290. });
  291. return new Promise((resolve, reject) => {
  292. // #ifdef MP-WEIXIN
  293. //微信小程序登录
  294. this.loginWxMa().then(res => {
  295. resolve("success");
  296. });
  297. // #endif
  298. // #ifdef H5
  299. //微信公众号H5
  300. if (util.isWeiXinBrowser()) {
  301. let local = location.href
  302. let code = util.getUrlParam(local, "code");
  303. let state = util.getUrlParam(local, "state");
  304. //授权code登录
  305. if (code) { //有code
  306. if (state == 'snsapi_base' || state == 'snsapi_userinfo') { //登录授权
  307. this.loginWxMp(code, state).then(res => {
  308. resolve("success");
  309. });
  310. }
  311. } else { //无code则发起网页授权
  312. //微信公众号H5,页面授权登录
  313. let appId = this.globalData.appId;
  314. let pages = getCurrentPages();
  315. let currentPage = pages[pages.length - 1];
  316. let route = currentPage.route;
  317. let redirectUri = location.href;
  318. let componentAppId_str = this.globalData.componentAppId ? '&component_appid=' + this
  319. .globalData.componentAppId : '';
  320. redirectUri = encodeURIComponent(redirectUri);
  321. let wx_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appId +
  322. '&redirect_uri=' + redirectUri + componentAppId_str +
  323. '&response_type=code&scope=snsapi_base&state=snsapi_base#wechat_redirect';
  324. location.href = wx_url;
  325. }
  326. }
  327. // #endif
  328. });
  329. },
  330. //微信小程序登录
  331. // #ifdef MP-WEIXIN
  332. loginWxMa() {
  333. return new Promise((resolve, reject) => {
  334. let that = this;
  335. uni.login({
  336. success: function(res) {
  337. if (res.code) {
  338. let params = {
  339. jsCode: res.code
  340. }
  341. api.doGet(__config.authConfig.apiLoginUrl, params).then(res => {
  342. uni.hideLoading();
  343. let session = res.data;
  344. /* 是否需要重新授权 */
  345. if (session.isNeedLogin) {
  346. let authLoginPath = __config.authConfig.loginPath
  347. let currentUrl = util.getCurrentPageUrlWithArgs()
  348. console.info('当前页面URL: ', currentUrl)
  349. console.info('登录授权模式: ', __config.authConfig.mode)
  350. console.info('登录授权URL: ', authLoginPath)
  351. uni.removeStorageSync(__config.tokenNameKey)
  352. uni.removeStorageSync(__config.tokenValueKey)
  353. uni.setStorageSync(__config.loginWxSessionKey, session)
  354. uni.reLaunch({
  355. url: `${authLoginPath}?reUrl=${encodeURIComponent(currentUrl)}`
  356. })
  357. } else {
  358. if (session.isLogin) {
  359. uni.setStorageSync(__config.tokenNameKey, session
  360. .tokenName);
  361. uni.setStorageSync(__config.tokenValueKey, session
  362. .tokenValue);
  363. /**
  364. * 缓存用户信息
  365. */
  366. let wxMemberInfo = util.parseUser(session.userInfo)
  367. uni.setStorageSync(__config.loginWxUserInfoKey,
  368. wxMemberInfo)
  369. }
  370. resolve("success");
  371. }
  372. });
  373. }
  374. }
  375. });
  376. });
  377. },
  378. // #endif
  379. //公众号登录
  380. // #ifdef H5
  381. loginWxMp(code, state) {
  382. let that = this;
  383. return new Promise((resolve, reject) => {
  384. let that = this
  385. api.loginWxMp({
  386. jsCode: code,
  387. scope: state
  388. }).then(res => {
  389. //公众号h5网页授权时url会产生code、state参数,防止code、state被复制,需自动剔除
  390. let query = that.$Route.query;
  391. delete query.code;
  392. delete query.state;
  393. util.resetPageUrl(query);
  394. let userInfo = res.data;
  395. uni.setStorageSync('third_session', userInfo.thirdSession);
  396. uni.setStorageSync('user_info', userInfo);
  397. //获取购物车数量
  398. that.shoppingCartCount();
  399. resolve("success");
  400. }).catch(res => {
  401. });
  402. });
  403. },
  404. // #endif
  405. }
  406. };
  407. </script>
  408. <style lang="scss">
  409. /* #ifndef APP-PLUS-NVUE */
  410. @import "./app.css";
  411. /* #endif */
  412. /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
  413. /* @import "@/uni_modules/uview-ui/index.scss"; */
  414. </style>