index.vue 10 KB

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