index.vue 10 KB

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