App.vue 16 KB

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