index.vue 11 KB

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