index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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="'/pagesClockIn/index/index'" open-type="navigate" class="menu_item">
  21. <image src="../../static/images/clockIn.png" mode=""></image>
  22. <text>校园打卡</text>
  23. </navigator> -->
  24. <<<<<<< HEAD
  25. =======
  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. >>>>>>> 7a29f577c010db0a0372ee381f1e9f4e53986777
  31. </view>
  32. </view>
  33. <view class="qr_code" v-if="showQR_code">
  34. <image :src="qr_code_url" mode="aspectFit"></image>
  35. </view>
  36. <view v-if="showLogin">
  37. <login :ocode="ocode" :appkey="appkey" scope="snsapi_userinfo" :visible="visible"
  38. @success="login_success_callback()" @cancel="login_cancel_callback()" @fail="login_fail_callback()" />
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. export default {
  44. data() {
  45. return {
  46. ceshi: 'code',
  47. huanjing: '部署环境', // 部署环境是key,用来获取环境
  48. visible: false, // 是否授权可见
  49. showLogin: true, // 是否启动授权
  50. appkey: '3183DC96A6DABA8D', // 商户appkey
  51. appid: 'wxd6f090391d410534', // 获取用户信息
  52. ocode: '1015730314', // 获取用户信息
  53. app_secret: '05742955578EC5BD29B7BC4CAC5AFACA', // 获取用户信息
  54. userinfo: '', // 用户信息
  55. showQR_code: false, // 显示校园卡二维码
  56. validation_failed: false, // 验证失败
  57. validation_times: 0, // 授权次数
  58. from: 0, // 跳转参数
  59. execed_onload: false,
  60. qr_code_url: this.$code_base_url + '/HotWaters/image/1.jpg'
  61. }
  62. },
  63. onLoad(options) {
  64. // console.log(options)
  65. // 测试环境
  66. if (typeof(options.from) != 'undefined') {
  67. this.from = options.from
  68. }
  69. // 是否是测试环境,查询数据接口中参数的值决定,方便以后测试
  70. this.isTestEnvironment()
  71. // 检查是否存在用户信息
  72. this.has_user_info()
  73. this.execed_onload = true
  74. },
  75. onShow() {
  76. if (!this.execed_onload) {
  77. // 检查是否存在用户信息
  78. this.has_user_info()
  79. }
  80. },
  81. methods: {
  82. /**
  83. * 控制环境,test为true测试环境,false则是正式环境
  84. */
  85. async isTestEnvironment() {
  86. const res = await this.$myRequest({
  87. host: this.ceshi,
  88. url: '/HotWaters/conEnvi.action',
  89. method: 'POST',
  90. header: {
  91. 'content-type': 'application/x-www-form-urlencoded'
  92. },
  93. data: {
  94. name: this.huanjing
  95. }
  96. })
  97. // console.log(res);
  98. if (res.data.mess == '返回成功') {
  99. // value为0 测试环境, 为1 部署环境;amount限制启动金额
  100. this.$store.state.amount = res.data.data[0].amount
  101. if (res.data.data[0].value == 0) {
  102. // 测试模式
  103. this.$store.state.test = true
  104. } else if (res.data.data[0].value == 2) {
  105. // 系统正在维护...
  106. this.$store.state.test = 'weihuzhong'
  107. uni.showModal({
  108. title: '提示信息',
  109. content: '系统维护中...\r\n暂时请不要使用!\r\n避免造成不必要的损失!',
  110. showCancel: false
  111. });
  112. } else {
  113. // 正式模式
  114. this.$store.state.test = false
  115. }
  116. } else {
  117. uni.showToast({
  118. title: res.data.mess,
  119. icon: 'success'
  120. });
  121. }
  122. },
  123. /**
  124. * 是否有用户信息
  125. */
  126. has_user_info() {
  127. try {
  128. // console.log(111111);
  129. const value = uni.getStorageSync('userinfo_storage_key');
  130. // console.log(value);
  131. if (value == '') {
  132. this.showLogin = true
  133. this.visible = true
  134. } else {
  135. this.showLogin = false
  136. this.userinfo = value
  137. this.$store.state.userInfo = this.userinfo
  138. }
  139. } catch (e) {
  140. // console.log(e)
  141. uni.showToast({
  142. title: '异常:' + e,
  143. duration: 3000
  144. })
  145. }
  146. },
  147. /**
  148. * 授权登陆取消回调
  149. */
  150. login_cancel_callback() {
  151. this.reauthorization()
  152. },
  153. /**
  154. * 重新授权
  155. */
  156. reauthorization() {
  157. this.validation_times = this.validation_times + 1
  158. if (this.validation_times >= 2) {
  159. this.login_fail_callback()
  160. } else {
  161. try {
  162. uni.removeStorageSync('userinfo_storage_key');
  163. setTimeout(() => {
  164. this.showLogin = false
  165. this.userinfo = {}
  166. }, 30)
  167. setTimeout(() => {
  168. this.showLogin = true
  169. this.visible = true
  170. }, 30)
  171. } catch (e) {
  172. console.log(e)
  173. }
  174. }
  175. },
  176. /**
  177. * 授权登陆取消回调
  178. */
  179. login_fail_callback() {
  180. this.userinfo = {};
  181. // setTimeout(() => {
  182. // this.validation_failed = true
  183. // }, 3000);
  184. if (!this.validation_failed) {
  185. this.validation_failed = false
  186. uni.showModal({
  187. title: '提示',
  188. content: '授权:请先领取校园卡、并激活!',
  189. // cancelText: '取消',
  190. confirmText: '领取',
  191. success: (res1) => {
  192. if (res1.confirm) {
  193. uni.navigateTo({
  194. url: '../qr_code/qr_code'
  195. });
  196. // 出现二维码,用户扫描二维码添加校园卡、激活
  197. // uni.downloadFile({
  198. // url: 'https://chtech.ncjti.edu.cn/shuidian/HotWaters/image/1.jpg',
  199. // success: (res2) => {
  200. // // console.log(res2);
  201. // if (res2.statusCode == 200) {
  202. // uni.saveImageToPhotosAlbum({
  203. // filePath: res2
  204. // .tempFilePath,
  205. // success: function() {
  206. // uni.showToast({
  207. // title: "保存成功!请用微信扫码添加校园卡!",
  208. // icon: "none"
  209. // });
  210. // },
  211. // fail: function() {
  212. // uni.showToast({
  213. // title: "保存失败,请手动截图保存,再用微信扫二维码!",
  214. // icon: "none"
  215. // });
  216. // }
  217. // });
  218. // }
  219. // }
  220. // })
  221. } else if (res1.cancel) {
  222. // console.log('用户点击取消');
  223. // uni.showToast({
  224. // icon: 'none',
  225. // title: '10秒后自动隐藏二维码,可重新授权调起!',
  226. // duration: 1000,
  227. // success: (com) => {
  228. // this.showQR_code = true
  229. // setTimeout(() => {
  230. // this.showQR_code = false
  231. // }, 10000)
  232. // }
  233. // });
  234. }
  235. }
  236. });
  237. }
  238. },
  239. /**
  240. * 授权登陆成功回调
  241. */
  242. login_success_callback: function({
  243. detail
  244. }) {
  245. const {
  246. wxcode = ""
  247. } = 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>