index.vue 12 KB

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