index.vue 10 KB

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