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