index.vue 11 KB

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