index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <template>
  2. <view class="container">
  3. <view class="banner">
  4. <image src="../../static/images/banner2x.png" mode=""></image>
  5. </view>
  6. <view class="nav">
  7. <view class="menu">
  8. <navigator :url="'/pages/reshui/reshui'" open-type="navigate" class="menu_item">
  9. <image src="../../static/images/shower2x.png" mode=""></image>
  10. <text>洗 浴</text>
  11. </navigator>
  12. <navigator :url="'/pagesElectric/jiaofei/jiaofei?o=index'" open-type="navigate" class="menu_item">
  13. <image src="../../static/images/recharge2x.png" mode=""></image>
  14. <text>水电充值</text>
  15. </navigator>
  16. <navigator :url="'/pagesAir/shareAir/shareAir'" open-type="navigate" class="menu_item">
  17. <image src="../../static/images/air.png" mode=""></image>
  18. <text>共享空调</text>
  19. </navigator>
  20. <navigator :url="'/pagesBus/bus/bus'" open-type="navigate" class="menu_item">
  21. <image src="../../static/images/bus.png" mode=""></image>
  22. <text>校车预约</text>
  23. </navigator>
  24. <!-- <navigator :url="'/pagesClockIn/index/index'" open-type="navigate" class="menu_item">
  25. <image src="../../static/images/clockIn.png" mode=""></image>
  26. <text>校园打卡</text>
  27. </navigator> -->
  28. <!-- <navigator :url="'/pagesClockIn/money/money'" open-type="navigate" class="menu_item">
  29. <image src="../../static/images/money.png" mode=""></image>
  30. <text>学费缴纳</text>
  31. </navigator> -->
  32. </view>
  33. </view>
  34. <view v-if="showLogin">
  35. <login :ocode="ocode" :appkey="appkey" scope="snsapi_userinfo" :visible="visible" @success="login_success_callback" @cancel="login_cancel_callback"
  36. @fail="login_fail_callback" />
  37. </view>
  38. <Popup :showPopup="showPopup" @close="showPopup = false">
  39. <view class="pupup-content">
  40. <text class="pupup-title">请截屏,再识别二维码领取校园卡</text>
  41. <image src="../../static/images/qr.jpg" mode="aspectFill" class="pupup-img"></image>
  42. </view>
  43. </Popup>
  44. </view>
  45. </template>
  46. <script>
  47. import Popup from '@/components/Popup.vue';
  48. export default {
  49. components: {
  50. Popup
  51. },
  52. data() {
  53. return {
  54. ceshi: 'code',
  55. huanjing: '部署环境', // 部署环境是key,用来获取环境
  56. visible: false, // 是否授权可见
  57. showLogin: true, // 是否启动授权
  58. appkey: '3183DC96A6DABA8D', // 商户appkey
  59. appid: 'wxd6f090391d410534', // 获取用户信息
  60. ocode: '1015730314', // 获取用户信息
  61. app_secret: '05742955578EC5BD29B7BC4CAC5AFACA', // 获取用户信息
  62. userinfo: '', // 用户信息
  63. showQR_code: false, // 显示校园卡二维码
  64. validation_failed: false, // 验证失败
  65. validation_times: 0, // 授权次数
  66. from: 0, // 跳转参数
  67. execed_onload: false,
  68. showPopup: false
  69. }
  70. },
  71. onLoad(options) {
  72. // console.log(options)
  73. // 测试环境
  74. if (typeof options.from != 'undefined') {
  75. this.from = options.from;
  76. }
  77. // 是否是测试环境,查询数据接口中参数的值决定,方便以后测试
  78. this.isTestEnvironment();
  79. // 检查是否存在用户信息
  80. this.has_user_info();
  81. this.execed_onload = true;
  82. },
  83. onShow() {
  84. if (!this.execed_onload) {
  85. // 检查是否存在用户信息
  86. this.has_user_info();
  87. }
  88. },
  89. methods: {
  90. /**
  91. * 控制环境,test为true测试环境,false则是正式环境
  92. */
  93. async isTestEnvironment() {
  94. const res = await this.$myRequest({
  95. host: this.ceshi,
  96. url: '/HotWaters/conEnvi.action',
  97. method: 'POST',
  98. header: {
  99. 'content-type': 'application/x-www-form-urlencoded'
  100. },
  101. data: {
  102. name: this.huanjing
  103. }
  104. });
  105. // console.log(res);
  106. if (res.data.mess == '返回成功') {
  107. // value为0 测试环境, 为1 部署环境;amount限制启动金额
  108. this.$store.state.amount = res.data.data[0].amount;
  109. if (res.data.data[0].value == 0) {
  110. // 测试模式
  111. this.$store.state.test = true;
  112. } else if (res.data.data[0].value == 2) {
  113. // 系统正在维护...
  114. this.$store.state.test = 'weihuzhong';
  115. uni.showModal({
  116. title: '提示信息',
  117. content: '系统维护中...\r\n暂时请不要使用!\r\n避免造成不必要的损失!',
  118. showCancel: false
  119. });
  120. } else {
  121. // 正式模式
  122. this.$store.state.test = false;
  123. }
  124. } else {
  125. uni.showToast({
  126. title: res.data.mess,
  127. icon: 'success'
  128. });
  129. }
  130. },
  131. /**
  132. * 是否有用户信息
  133. */
  134. has_user_info() {
  135. try {
  136. // console.log(111111);
  137. const value = uni.getStorageSync('userinfo_storage_key');
  138. // console.log(value);
  139. if (value == '') {
  140. this.showLogin = true;
  141. this.visible = true;
  142. } else {
  143. this.showLogin = false;
  144. this.userinfo = value;
  145. this.$store.state.userInfo = this.userinfo;
  146. }
  147. } catch (e) {
  148. // console.log(e)
  149. uni.showToast({
  150. title: '异常:' + e,
  151. duration: 3000
  152. });
  153. }
  154. },
  155. /**
  156. * 授权登陆取消回调
  157. */
  158. login_cancel_callback() {
  159. this.reauthorization();
  160. },
  161. /**
  162. * 重新授权
  163. */
  164. reauthorization() {
  165. this.validation_times = this.validation_times + 1;
  166. if (this.validation_times >= 2) {
  167. this.login_fail_callback();
  168. } else {
  169. try {
  170. // uni.removeStorageSync('userinfo_storage_key')
  171. setTimeout(() => {
  172. this.showLogin = false
  173. this.userinfo = {}
  174. }, 30);
  175. setTimeout(() => {
  176. this.showLogin = true
  177. this.visible = true
  178. }, 30);
  179. } catch (e) {
  180. console.log(e);
  181. }
  182. }
  183. },
  184. /**
  185. * 授权登陆取消回调
  186. */
  187. login_fail_callback() {
  188. var _this = this;
  189. _this.userinfo = {};
  190. if (!_this.validation_failed) {
  191. _this.validation_failed = false;
  192. uni.showModal({
  193. title: '提示',
  194. content: '授权:请先领取校园卡、并激活!',
  195. // cancelText: '取消',
  196. confirmText: '领取',
  197. success: res1 => {
  198. if (res1.confirm) {
  199. // 截屏,用户利用微信识别二维码添加校园卡、激活
  200. _this.showPopup = true;
  201. } else if (res1.cancel) {
  202. // console.log('用户点击取消');
  203. // uni.showToast({
  204. // icon: 'none',
  205. // title: '10秒后自动隐藏二维码,可重新授权调起!',
  206. // duration: 1000,
  207. // success: (com) => {
  208. // this.showQR_code = true
  209. // setTimeout(() => {
  210. // this.showQR_code = false
  211. // }, 10000)
  212. // }
  213. // });
  214. }
  215. }
  216. });
  217. }
  218. },
  219. /**
  220. * 授权登陆成功回调
  221. */
  222. login_success_callback: function({
  223. detail
  224. }) {
  225. const {
  226. wxcode = ''
  227. } = detail;
  228. this.validation_failed = false;
  229. // 屏蔽用户操作
  230. uni.showLoading({
  231. title: '获取数据中…',
  232. mask: true
  233. });
  234. // 通过wxcode换取access_token
  235. this.get_access_token(wxcode);
  236. },
  237. /**
  238. * 通过wxcode换取access_token
  239. */
  240. async get_access_token(param_wxcode) {
  241. const res = await this.$myRequest({
  242. host: 'wecard',
  243. url: '/connect/oauth2/token',
  244. method: 'POST',
  245. header: {
  246. 'content-type': 'application/json'
  247. },
  248. data: {
  249. wxcode: param_wxcode,
  250. app_key: this.appkey,
  251. app_secret: this.app_secret,
  252. grant_type: 'authorization_code',
  253. redirect_uri: 'mnp://' + this.appid
  254. }
  255. });
  256. if (res.data.refresh_token == '' || typeof res.data.refresh_token == 'undefined') {
  257. uni.hideLoading();
  258. uni.showToast({
  259. title: '未获得token'
  260. });
  261. } else {
  262. // 通过access_token换取用户信息
  263. this.get_user_info(res.data.access_token);
  264. }
  265. },
  266. /**
  267. * 通过access_token换取用户信息
  268. */
  269. async get_user_info(param_access_token) {
  270. const res = await this.$myRequest({
  271. host: 'wecard',
  272. url: '/connect/oauth/get-user-info',
  273. method: 'POST',
  274. header: {
  275. 'content-type': 'application/json'
  276. },
  277. data: {
  278. access_token: param_access_token
  279. }
  280. });
  281. // console.log(res);
  282. if (res.data.errcode == 0 && res.data.errmsg == 'OK') {
  283. try {
  284. this.userinfo = res.data;
  285. this.$store.state.userInfo = res.data;
  286. // 获取code,然后去检查是否存在数据库中
  287. this.getCode();
  288. } catch (e) {
  289. uni.hideLoading();
  290. console.log(e);
  291. }
  292. } else {
  293. uni.hideLoading();
  294. uni.showToast({
  295. title: '未获得用户信息,请领取校园卡并激活',
  296. duration: 3000
  297. });
  298. }
  299. },
  300. /**
  301. * 获得code
  302. */
  303. getCode() {
  304. var _this = this;
  305. uni.login({
  306. success: res => {
  307. // console.log('reshui', res);
  308. if (res.code) {
  309. // 检查用户是否存在第三方库
  310. _this.chk_user_is_in_db(res.code);
  311. } else {
  312. uni.hideLoading();
  313. uni.showToast({
  314. title: res.errMsg,
  315. icon: 'none'
  316. });
  317. }
  318. },
  319. fail() {
  320. uni.hideLoading();
  321. }
  322. });
  323. },
  324. /**
  325. * 检查数据库中是否有该用户信息
  326. */
  327. async chk_user_is_in_db(param_code) {
  328. var _this = this;
  329. const res = await _this.$myRequest({
  330. // host: 'local',
  331. host: _this.ceshi,
  332. url: '/HotWaters/wpopenid.action',
  333. method: 'POST',
  334. header: {
  335. 'content-type': 'application/x-www-form-urlencoded'
  336. },
  337. data: {
  338. userinfo: JSON.stringify(_this.userinfo),
  339. code: param_code
  340. }
  341. });
  342. // console.log(res);
  343. if (res.data.code == 0 || res.data.code == 100) {
  344. // 存储用户信息
  345. uni.setStorageSync('userinfo_storage_key', _this.userinfo);
  346. uni.hideLoading();
  347. // 提示授权成功
  348. uni.showToast({
  349. icon: 'success',
  350. title: '授权成功',
  351. duration: 800,
  352. success: res => {
  353. if (_this.from != 0 || _this.from != '0') {
  354. uni.navigateBack({
  355. delta: 1
  356. });
  357. }
  358. }
  359. });
  360. } else {
  361. uni.hideLoading();
  362. uni.showToast({
  363. icon: 'none',
  364. title: '授权失败:' + res.data.msg,
  365. success: res => {}
  366. });
  367. }
  368. }
  369. }
  370. }
  371. </script>
  372. <style scoped lang="scss">
  373. .container {
  374. display: flex;
  375. flex-direction: column;
  376. width: 750rpx;
  377. font-family: Microsoft YaHei-3970(82674968);
  378. color: #333333;
  379. .banner {
  380. width: 100%;
  381. height: 360rpx;
  382. image {
  383. width: 100%;
  384. height: 100%;
  385. }
  386. }
  387. .nav {
  388. position: relative;
  389. width: 100%;
  390. .menu,
  391. .reset {
  392. margin: 25rpx auto;
  393. width: 640rpx;
  394. }
  395. .menu_item {
  396. display: inline-flex;
  397. flex-direction: column;
  398. text-align: center;
  399. width: 25%;
  400. padding: 30rpx 0 39rpx;
  401. border-radius: 20rpx;
  402. font-family: Microsoft YaHei-3970(82674968);
  403. color: #333333;
  404. image {
  405. width: 90rpx;
  406. height: 90rpx;
  407. margin: 0 auto;
  408. }
  409. text {
  410. height: 29rpx;
  411. line-height: 54rpx;
  412. font-size: 30rpx;
  413. color: #333333;
  414. }
  415. }
  416. .reset {
  417. position: fixed;
  418. display: flex;
  419. flex-direction: column;
  420. align-items: flex-end;
  421. right: 55rpx;
  422. bottom: 55rpx;
  423. font-family: Microsoft YaHei-3970(82674968);
  424. color: #333333;
  425. .icon-zhongzhi {
  426. margin: 0 auto;
  427. padding-top: 20rpx;
  428. color: $my-color-primary;
  429. width: 90rpx;
  430. height: 70rpx;
  431. font-size: 80rpx;
  432. }
  433. }
  434. }
  435. .pupup-content {
  436. display: flex;
  437. flex-direction: column;
  438. justify-content: center;
  439. align-items: center;
  440. height: 100%;
  441. .pupup-title {
  442. padding-bottom: 20rpx;
  443. }
  444. .pupup-img {
  445. height: 90%;
  446. }
  447. }
  448. }
  449. </style>