index.vue 10 KB

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