index.vue 11 KB

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