index.vue 11 KB

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