index.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 v-if="showLogin">
  25. <login :ocode="ocode" :appkey="appkey" scope="snsapi_userinfo" :visible="visible"
  26. @success="login_success_callback()" @cancel="login_cancel_callback()" @fail="login_fail_callback()" />
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. data() {
  33. return {
  34. ceshi: 'code',
  35. huanjing: '部署环境', // 部署环境是key,用来获取环境
  36. visible: false,
  37. showLogin: true,
  38. appkey: '5AA49F3E4CACA380',
  39. appid: 'wx2fc3f45732fae5d3', // 获取用户信息
  40. ocode: '1015730314', // 获取用户信息
  41. app_secret: '58D34C81D82B35179ED896C4362B0FC0', // 获取用户信息
  42. my_display: false,
  43. userinfo: '',
  44. wxcode: '',
  45. access_token: '',
  46. code: '',
  47. from: 0
  48. }
  49. },
  50. onLoad(options) {
  51. console.log(options)
  52. if (options && JSON.stringify(options) != '{}' && typeof(options.from) != 'undefined') {
  53. this.from = options.from
  54. }
  55. // 是否是测试环境,查询数据接口中参数的值决定,方便以后测试
  56. this.isTestEnvironment()
  57. this.has_user_info()
  58. },
  59. methods: {
  60. /**
  61. * 控制环境,test为true测试环境,false则是正式环境
  62. */
  63. async isTestEnvironment() {
  64. const res = await this.$myRequest({
  65. host: this.ceshi,
  66. url: '/HotWaters/conEnvi.action',
  67. method: 'POST',
  68. header: {
  69. 'content-type': 'application/x-www-form-urlencoded'
  70. },
  71. data: {
  72. name: this.huanjing
  73. }
  74. })
  75. // console.log(res);
  76. if (res.data.mess == '返回成功') {
  77. // 0 测试环境, 1 部署环境
  78. if (res.data.data[0].value == 0) {
  79. this.$store.state.test = true
  80. } else {
  81. this.$store.state.test = false
  82. }
  83. } else {
  84. uni.showToast({
  85. title: res.data.mess,
  86. icon: 'success'
  87. });
  88. }
  89. },
  90. /**
  91. * 是否有用户信息
  92. */
  93. has_user_info() {
  94. try {
  95. this.userinfo = this.$store.state.userInfo
  96. const value = uni.getStorageSync('userinfo_storage_key');
  97. if (value == '') {
  98. if (typeof(this.userinfo) == 'undefined' || JSON.stringify(this.userinfo) == '{}') {
  99. this.showLogin = true
  100. this.visible = true
  101. } else {
  102. this.showLogin = false
  103. this.my_display = true
  104. }
  105. } else {
  106. this.showLogin = false
  107. this.userinfo = value
  108. this.$store.state.userInfo = this.userinfo
  109. this.my_display = true
  110. }
  111. } catch (e) {
  112. // console.log(e)
  113. uni.showToast({
  114. title: '异常:' + e,
  115. duration: 3000
  116. })
  117. }
  118. },
  119. /**
  120. * 重新授权
  121. */
  122. reauthorization() {
  123. try {
  124. uni.removeStorageSync('userinfo_storage_key');
  125. setTimeout(() => {
  126. this.showLogin = false
  127. this.userinfo = {}
  128. this.my_display = false
  129. }, 30)
  130. setTimeout(() => {
  131. this.showLogin = true
  132. this.visible = true
  133. }, 30)
  134. } catch (e) {
  135. console.log(e)
  136. }
  137. },
  138. /**
  139. * 授权登陆q取消回调
  140. */
  141. login_cancel_callback() {
  142. this.userinfo = {};
  143. uni.showToast({
  144. icon: 'none',
  145. title: '需要先授权,请点击右下角【重新授权】按钮'
  146. });
  147. },
  148. /**
  149. * 授权登陆q取消回调
  150. */
  151. login_fail_callback() {
  152. this.userinfo = {};
  153. uni.showToast({
  154. icon: 'none',
  155. title: '授权失败,请先领取校园卡、激活',
  156. duration: 3000,
  157. success: (res) => {
  158. uni.showModal({
  159. title: '提示',
  160. content: '是否将校园卡二维码,保存到相册?',
  161. showCancel: false,
  162. success: res => {
  163. if (res.confirm) {
  164. // 出现二维码,用户扫描二维码添加校园卡、激活
  165. uni.downloadFile({
  166. url: 'https://www.ecjtuit.edu.cn/img/zs.jpg',
  167. success: (res) => {
  168. console.log(res);
  169. if (res.statusCode === 200) {
  170. uni.saveImageToPhotosAlbum({
  171. filePath: res.tempFilePath,
  172. success: function() {
  173. uni.showToast({
  174. title: "保存成功",
  175. icon: "none"
  176. });
  177. },
  178. fail: function() {
  179. uni.showToast({
  180. title: "保存失败,请稍后重试",
  181. icon: "none"
  182. });
  183. }
  184. });
  185. }
  186. }
  187. })
  188. } else if (res.cancel) {
  189. console.log('用户点击取消');
  190. }
  191. }
  192. });
  193. }
  194. });
  195. },
  196. /**
  197. * 授权登陆成功回调
  198. */
  199. login_success_callback: function({
  200. detail
  201. }) {
  202. const {
  203. wxcode = ""
  204. } = detail
  205. // 获取wxcode
  206. this.wxcode = wxcode
  207. // 通过wxcode换取access_token
  208. this.get_access_token()
  209. },
  210. /**
  211. * 通过wxcode换取access_token
  212. */
  213. async get_access_token() {
  214. const res = await this.$myRequest({
  215. host: 'wecard',
  216. url: '/connect/oauth2/token',
  217. method: 'POST',
  218. header: {
  219. 'content-type': 'application/json'
  220. },
  221. data: {
  222. 'wxcode': this.wxcode,
  223. 'app_key': this.appkey,
  224. 'app_secret': this.app_secret,
  225. 'grant_type': 'authorization_code',
  226. 'redirect_uri': 'mnp://' + this.appid
  227. }
  228. })
  229. if (res.data.refresh_token == '' || typeof(res.data.refresh_token) == 'undefined') {
  230. uni.showToast({
  231. title: '未获得token'
  232. });
  233. } else {
  234. this.access_token = res.data.access_token;
  235. // 通过access_token换取用户信息
  236. this.get_user_info();
  237. }
  238. },
  239. /**
  240. * 通过access_token换取用户信息
  241. */
  242. async get_user_info() {
  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": this.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. this.code = res.code
  279. // 检查用户是否存在第三方库
  280. this.chk_user_is_in_db()
  281. } else {
  282. uni.showToast({
  283. title: res.errMsg,
  284. icon: 'none'
  285. });
  286. }
  287. }
  288. })
  289. },
  290. /**
  291. * 检查数据库中是否有该用户信息
  292. */
  293. async chk_user_is_in_db() {
  294. const res = await this.$myRequest({
  295. // host: 'local',
  296. host: this.ceshi,
  297. url: '/HotWaters/wpopenid.action',
  298. method: 'POST',
  299. header: {
  300. 'content-type': 'application/x-www-form-urlencoded'
  301. },
  302. data: {
  303. "userinfo": JSON.stringify(this.userinfo),
  304. "code": this.code
  305. }
  306. });
  307. // console.log(res);
  308. if (res.data.code == 0 || res.data.code == 100) {
  309. // 存储用户信息
  310. uni.setStorageSync('userinfo_storage_key', this.userinfo)
  311. // 提示授权成功
  312. uni.showToast({
  313. icon: 'success',
  314. title: '授权成功',
  315. duration: 800,
  316. success: (res) => {
  317. this.my_display = true
  318. if (this.from != 0) {
  319. uni.navigateBack({
  320. delta: 1
  321. })
  322. }
  323. }
  324. })
  325. } else {
  326. uni.showToast({
  327. icon: 'success',
  328. title: '授权失败:' + res.data.mess,
  329. success: (res) => {
  330. this.my_display = false
  331. }
  332. })
  333. }
  334. }
  335. }
  336. }
  337. </script>
  338. <style scoped lang="scss">
  339. .container {
  340. display: flex;
  341. flex-direction: column;
  342. width: 750rpx;
  343. font-family: Microsoft YaHei-3970(82674968);
  344. color: #333333;
  345. .banner {
  346. width: 100%;
  347. height: 360rpx;
  348. image {
  349. width: 100%;
  350. height: 100%;
  351. }
  352. }
  353. .nav {
  354. position: relative;
  355. width: 100%;
  356. .menu,
  357. .reset {
  358. margin: 25rpx auto;
  359. width: 640rpx;
  360. }
  361. .menu_item {
  362. display: inline-flex;
  363. flex-direction: column;
  364. text-align: center;
  365. width: 25%;
  366. padding: 30rpx 0 39rpx;
  367. border-radius: 20rpx;
  368. font-family: Microsoft YaHei-3970(82674968);
  369. color: #333333;
  370. image {
  371. width: 90rpx;
  372. height: 90rpx;
  373. margin: 0 auto;
  374. }
  375. text {
  376. height: 29rpx;
  377. line-height: 54rpx;
  378. font-size: 30rpx;
  379. color: #333333;
  380. }
  381. }
  382. .reset {
  383. position: fixed;
  384. display: flex;
  385. flex-direction: column;
  386. align-items: flex-end;
  387. right: 55rpx;
  388. bottom: 55rpx;
  389. font-family: Microsoft YaHei-3970(82674968);
  390. color: #333333;
  391. .icon-zhongzhi {
  392. margin: 0 auto;
  393. padding-top: 20rpx;
  394. color: $my-color-primary;
  395. width: 90rpx;
  396. height: 70rpx;
  397. font-size: 80rpx;
  398. }
  399. }
  400. }
  401. }
  402. </style>