index.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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.showToast({
  155. icon: 'none',
  156. title: '授权失败,请先领取校园卡、再激活!',
  157. duration: 3000,
  158. success: (res) => {
  159. uni.showModal({
  160. title: '提示',
  161. content: '是否将校园卡二维码,保存到相册?',
  162. cancelText: '手动截图',
  163. confirmText: '自动保存',
  164. success: (res1) => {
  165. if (res1.confirm) {
  166. // 出现二维码,用户扫描二维码添加校园卡、激活
  167. uni.downloadFile({
  168. url: 'https://jtishfw.ncjti.edu.cn/jxch-smartmp/HotWaters/image/1.jpg',
  169. success: (res2) => {
  170. // console.log(res2);
  171. if (res2.statusCode == 200) {
  172. uni.saveImageToPhotosAlbum({
  173. filePath: res2.tempFilePath,
  174. success: function() {
  175. uni.showToast({
  176. title: "保存成功!请用微信扫码添加校园卡!",
  177. icon: "none"
  178. });
  179. },
  180. fail: function() {
  181. uni.showToast({
  182. title: "保存失败,请手动截图保存,再用微信扫二维码!",
  183. icon: "none"
  184. });
  185. }
  186. });
  187. }
  188. }
  189. })
  190. } else if (res1.cancel) {
  191. // console.log('用户点击取消');
  192. this.showQR_code = true;
  193. }
  194. }
  195. });
  196. }
  197. });
  198. },
  199. /**
  200. * 授权登陆成功回调
  201. */
  202. login_success_callback: function({
  203. detail
  204. }) {
  205. const {
  206. wxcode = ""
  207. } = detail
  208. // 通过wxcode换取access_token
  209. this.get_access_token(wxcode)
  210. },
  211. /**
  212. * 通过wxcode换取access_token
  213. */
  214. async get_access_token(param_wxcode) {
  215. const res = await this.$myRequest({
  216. host: 'wecard',
  217. url: '/connect/oauth2/token',
  218. method: 'POST',
  219. header: {
  220. 'content-type': 'application/json'
  221. },
  222. data: {
  223. 'wxcode': param_wxcode,
  224. 'app_key': this.appkey,
  225. 'app_secret': this.app_secret,
  226. 'grant_type': 'authorization_code',
  227. 'redirect_uri': 'mnp://' + this.appid
  228. }
  229. })
  230. if (res.data.refresh_token == '' || typeof(res.data.refresh_token) == 'undefined') {
  231. uni.showToast({
  232. title: '未获得token'
  233. });
  234. } else {
  235. // 通过access_token换取用户信息
  236. this.get_user_info(res.data.access_token);
  237. }
  238. },
  239. /**
  240. * 通过access_token换取用户信息
  241. */
  242. async get_user_info(param_access_token) {
  243. const res = await this.$myRequest({
  244. host: 'wecard',
  245. url: '/connect/oauth/get-user-info',
  246. method: 'POST',
  247. header: {
  248. 'content-type': 'application/json'
  249. },
  250. data: {
  251. "access_token": param_access_token
  252. }
  253. });
  254. if (res.data.errcode == 0 && res.data.errmsg == 'OK') {
  255. try {
  256. this.userinfo = res.data
  257. this.$store.state.userInfo = this.userinfo
  258. // 获取code,然后去检查是否存在数据库中
  259. this.getCode()
  260. } catch (e) {
  261. console.log(e)
  262. }
  263. } else {
  264. uni.showToast({
  265. title: '未获得用户信息,请领取校园卡并激活',
  266. duration: 3000
  267. });
  268. }
  269. },
  270. /**
  271. * 获得code
  272. */
  273. getCode() {
  274. uni.login({
  275. success: (res) => {
  276. // console.log('reshui', res);
  277. if (res.code) {
  278. // 检查用户是否存在第三方库
  279. this.chk_user_is_in_db(res.code)
  280. } else {
  281. uni.showToast({
  282. title: res.errMsg,
  283. icon: 'none'
  284. });
  285. }
  286. }
  287. })
  288. },
  289. /**
  290. * 检查数据库中是否有该用户信息
  291. */
  292. async chk_user_is_in_db(param_code) {
  293. const res = await this.$myRequest({
  294. // host: 'local',
  295. host: this.ceshi,
  296. url: '/HotWaters/wpopenid.action',
  297. method: 'POST',
  298. header: {
  299. 'content-type': 'application/x-www-form-urlencoded'
  300. },
  301. data: {
  302. "userinfo": JSON.stringify(this.userinfo),
  303. "code": param_code
  304. }
  305. });
  306. // console.log(res);
  307. if (res.data.code == 0 || res.data.code == 100) {
  308. // 存储用户信息
  309. uni.setStorageSync('userinfo_storage_key', this.userinfo)
  310. // 提示授权成功
  311. uni.showToast({
  312. icon: 'success',
  313. title: '授权成功',
  314. duration: 800,
  315. success: (res) => {
  316. this.my_display = true
  317. if (this.from != 0 || this.from != '0') {
  318. uni.navigateBack({
  319. delta: 1
  320. })
  321. }
  322. }
  323. })
  324. } else {
  325. uni.showToast({
  326. icon: 'success',
  327. title: '授权失败:' + res.data.mess,
  328. success: (res) => {
  329. this.my_display = false
  330. }
  331. })
  332. }
  333. }
  334. }
  335. }
  336. </script>
  337. <style scoped lang="scss">
  338. .container {
  339. display: flex;
  340. flex-direction: column;
  341. width: 750rpx;
  342. font-family: Microsoft YaHei-3970(82674968);
  343. color: #333333;
  344. .banner {
  345. width: 100%;
  346. height: 360rpx;
  347. image {
  348. width: 100%;
  349. height: 100%;
  350. }
  351. }
  352. .nav {
  353. position: relative;
  354. width: 100%;
  355. .menu,
  356. .reset {
  357. margin: 25rpx auto;
  358. width: 640rpx;
  359. }
  360. .menu_item {
  361. display: inline-flex;
  362. flex-direction: column;
  363. text-align: center;
  364. width: 25%;
  365. padding: 30rpx 0 39rpx;
  366. border-radius: 20rpx;
  367. font-family: Microsoft YaHei-3970(82674968);
  368. color: #333333;
  369. image {
  370. width: 90rpx;
  371. height: 90rpx;
  372. margin: 0 auto;
  373. }
  374. text {
  375. height: 29rpx;
  376. line-height: 54rpx;
  377. font-size: 30rpx;
  378. color: #333333;
  379. }
  380. }
  381. .reset {
  382. position: fixed;
  383. display: flex;
  384. flex-direction: column;
  385. align-items: flex-end;
  386. right: 55rpx;
  387. bottom: 55rpx;
  388. font-family: Microsoft YaHei-3970(82674968);
  389. color: #333333;
  390. .icon-zhongzhi {
  391. margin: 0 auto;
  392. padding-top: 20rpx;
  393. color: $my-color-primary;
  394. width: 90rpx;
  395. height: 70rpx;
  396. font-size: 80rpx;
  397. }
  398. }
  399. }
  400. .qr_code {
  401. display: flex;
  402. justify-content: center;
  403. align-items: center;
  404. width: 750rpx;
  405. height: 100%;
  406. position: fixed;
  407. left: 0;
  408. top: 0;
  409. background-color: #FFFFFF;
  410. }
  411. }
  412. </style>