index.vue 11 KB

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