index.vue 11 KB

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