index.vue 10 KB

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