index.vue 11 KB

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