index.vue 11 KB

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