index.vue 11 KB

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