index.vue 11 KB

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