index.vue 11 KB

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