index.vue 12 KB

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