index.vue 11 KB

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