index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. // 通过wxcode换取access_token
  223. this.get_access_token(wxcode)
  224. },
  225. /**
  226. * 通过wxcode换取access_token
  227. */
  228. async get_access_token(param_wxcode) {
  229. const res = await this.$myRequest({
  230. host: 'wecard',
  231. url: '/connect/oauth2/token',
  232. method: 'POST',
  233. header: {
  234. 'content-type': 'application/json'
  235. },
  236. data: {
  237. 'wxcode': param_wxcode,
  238. 'app_key': this.appkey,
  239. 'app_secret': this.app_secret,
  240. 'grant_type': 'authorization_code',
  241. 'redirect_uri': 'mnp://' + this.appid
  242. }
  243. })
  244. if (res.data.refresh_token == '' || typeof(res.data.refresh_token) == 'undefined') {
  245. uni.showToast({
  246. title: '未获得token'
  247. });
  248. } else {
  249. // 通过access_token换取用户信息
  250. this.get_user_info(res.data.access_token);
  251. }
  252. },
  253. /**
  254. * 通过access_token换取用户信息
  255. */
  256. async get_user_info(param_access_token) {
  257. const res = await this.$myRequest({
  258. host: 'wecard',
  259. url: '/connect/oauth/get-user-info',
  260. method: 'POST',
  261. header: {
  262. 'content-type': 'application/json'
  263. },
  264. data: {
  265. "access_token": param_access_token
  266. }
  267. });
  268. // console.log(res);
  269. if (res.data.errcode == 0 && res.data.errmsg == 'OK') {
  270. try {
  271. this.userinfo = res.data
  272. this.$store.state.userInfo = this.userinfo
  273. // 获取code,然后去检查是否存在数据库中
  274. this.getCode()
  275. } catch (e) {
  276. console.log(e)
  277. }
  278. } else {
  279. uni.showToast({
  280. title: '未获得用户信息,请领取校园卡并激活',
  281. duration: 3000
  282. });
  283. }
  284. },
  285. /**
  286. * 获得code
  287. */
  288. getCode() {
  289. uni.login({
  290. success: (res) => {
  291. // console.log('reshui', res);
  292. if (res.code) {
  293. // 检查用户是否存在第三方库
  294. this.chk_user_is_in_db(res.code)
  295. } else {
  296. uni.showToast({
  297. title: res.errMsg,
  298. icon: 'none'
  299. });
  300. }
  301. }
  302. })
  303. },
  304. /**
  305. * 检查数据库中是否有该用户信息
  306. */
  307. async chk_user_is_in_db(param_code) {
  308. const res = await this.$myRequest({
  309. // host: 'local',
  310. host: this.ceshi,
  311. url: '/HotWaters/wpopenid.action',
  312. method: 'POST',
  313. header: {
  314. 'content-type': 'application/x-www-form-urlencoded'
  315. },
  316. data: {
  317. "userinfo": JSON.stringify(this.userinfo),
  318. "code": param_code
  319. }
  320. });
  321. // console.log(res);
  322. if (res.data.code == 0 || res.data.code == 100) {
  323. // 存储用户信息
  324. uni.setStorageSync('userinfo_storage_key', this.userinfo)
  325. // 提示授权成功
  326. uni.showToast({
  327. icon: 'success',
  328. title: '授权成功',
  329. duration: 800,
  330. success: (res) => {
  331. if (this.from != 0 || this.from != '0') {
  332. uni.navigateBack({
  333. delta: 1
  334. })
  335. }
  336. }
  337. })
  338. } else {
  339. uni.showToast({
  340. icon: 'none',
  341. title: '授权失败:' + res.data.msg,
  342. success: (res) => {}
  343. })
  344. }
  345. }
  346. }
  347. }
  348. </script>
  349. <style scoped lang="scss">
  350. .container {
  351. display: flex;
  352. flex-direction: column;
  353. width: 750rpx;
  354. font-family: Microsoft YaHei-3970(82674968);
  355. color: #333333;
  356. .banner {
  357. width: 100%;
  358. height: 360rpx;
  359. image {
  360. width: 100%;
  361. height: 100%;
  362. }
  363. }
  364. .nav {
  365. position: relative;
  366. width: 100%;
  367. .menu,
  368. .reset {
  369. margin: 25rpx auto;
  370. width: 640rpx;
  371. }
  372. .menu_item {
  373. display: inline-flex;
  374. flex-direction: column;
  375. text-align: center;
  376. width: 25%;
  377. padding: 30rpx 0 39rpx;
  378. border-radius: 20rpx;
  379. font-family: Microsoft YaHei-3970(82674968);
  380. color: #333333;
  381. image {
  382. width: 90rpx;
  383. height: 90rpx;
  384. margin: 0 auto;
  385. }
  386. text {
  387. height: 29rpx;
  388. line-height: 54rpx;
  389. font-size: 30rpx;
  390. color: #333333;
  391. }
  392. }
  393. .reset {
  394. position: fixed;
  395. display: flex;
  396. flex-direction: column;
  397. align-items: flex-end;
  398. right: 55rpx;
  399. bottom: 55rpx;
  400. font-family: Microsoft YaHei-3970(82674968);
  401. color: #333333;
  402. .icon-zhongzhi {
  403. margin: 0 auto;
  404. padding-top: 20rpx;
  405. color: $my-color-primary;
  406. width: 90rpx;
  407. height: 70rpx;
  408. font-size: 80rpx;
  409. }
  410. }
  411. }
  412. .qr_code {
  413. display: flex;
  414. justify-content: center;
  415. align-items: center;
  416. width: 750rpx;
  417. height: 100%;
  418. position: fixed;
  419. left: 0;
  420. top: 0;
  421. background-color: #FFFFFF;
  422. }
  423. }
  424. </style>