index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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" @success="callback()"
  26. @cancel="callback()" @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. sub_appid: '1015730314_1941301045',
  40. appid: 'wx2fc3f45732fae5d3', // 获取用户信息
  41. ocode: '1015730314', // 获取用户信息
  42. app_secret: '58D34C81D82B35179ED896C4362B0FC0', // 获取用户信息
  43. my_display: false,
  44. userinfo: {},
  45. wxcode: '',
  46. access_token: ''
  47. }
  48. },
  49. onLoad() {
  50. // 是否是测试环境,查询数据接口中参数的值决定,方便以后测试
  51. this.isTestEnvironment()
  52. this.has_user_info()
  53. },
  54. methods: {
  55. /**
  56. * 控制环境,test为true测试环境,false则是正式环境
  57. */
  58. async isTestEnvironment() {
  59. const res = await this.$myRequest({
  60. host: this.ceshi,
  61. url: '/HotWaters/conEnvi.action',
  62. method: 'POST',
  63. header: {
  64. 'content-type': 'application/x-www-form-urlencoded'
  65. },
  66. data: {
  67. name: this.huanjing
  68. }
  69. })
  70. // console.log(res);
  71. if (res.data.mess == '返回成功') {
  72. // 0 测试环境, 1 部署环境
  73. if (res.data.data[0].value == 0) {
  74. this.$store.state.test = true
  75. } else {
  76. this.$store.state.test = false
  77. }
  78. } else {
  79. uni.showToast({
  80. title: res.data.mess,
  81. icon: 'success'
  82. });
  83. }
  84. },
  85. /**
  86. * 是否有用户信息
  87. */
  88. has_user_info() {
  89. try {
  90. this.userinfo = this.$store.state.userinfo
  91. const value = uni.getStorageSync('userinfo_storage_key');
  92. if (value == '') {
  93. if (typeof(this.userinfo) == 'undefined' || JSON.stringify(this.userinfo) == '{}') {
  94. this.showLogin = true
  95. this.visible = true
  96. } else {
  97. this.showLogin = false
  98. this.my_display = true
  99. }
  100. } else {
  101. this.showLogin = false
  102. this.userinfo = value
  103. this.my_display = true
  104. }
  105. } catch (e) {
  106. // console.log(e)
  107. uni.showToast({
  108. title: '异常:' + e,
  109. duration: 3000
  110. })
  111. }
  112. },
  113. /**
  114. * 重新授权
  115. */
  116. reauthorization() {
  117. try {
  118. uni.removeStorageSync('userinfo_storage_key');
  119. setTimeout(() => {
  120. this.showLogin = false
  121. this.userinfo = {}
  122. this.my_display = false
  123. }, 30)
  124. setTimeout(() => {
  125. this.showLogin = true
  126. this.visible = true
  127. }, 30)
  128. } catch (e) {
  129. console.log(e)
  130. }
  131. },
  132. /**
  133. * 授权回调
  134. */
  135. callback: function({
  136. detail
  137. }) {
  138. // console.log(detail)
  139. // {wxcode: "7f7Qi9rN8zQ1nifQiysTZ3WIeIWlgcGk"}
  140. if (detail === undefined) {
  141. this.userinfo = {};
  142. uni.showToast({
  143. icon: 'success',
  144. title: '需要先授权'
  145. });
  146. } else {
  147. const {
  148. wxcode = ""
  149. } = detail
  150. if (wxcode.length == 0) {
  151. uni.showToast({
  152. title: '未获得wxcode',
  153. duration: 500
  154. })
  155. } else {
  156. // 获取wxcode
  157. this.wxcode = wxcode
  158. // 通过wxcode换取access_token
  159. this.get_access_token()
  160. }
  161. }
  162. },
  163. /**
  164. * 通过wxcode换取access_token
  165. */
  166. async get_access_token() {
  167. const res = await this.$myRequest({
  168. host: 'wecard',
  169. url: '/connect/oauth2/token',
  170. method: 'POST',
  171. header: {
  172. 'content-type': 'application/json'
  173. },
  174. data: {
  175. 'wxcode': this.wxcode,
  176. 'app_key': this.appkey,
  177. 'app_secret': this.app_secret,
  178. 'grant_type': 'authorization_code',
  179. 'redirect_uri': 'mnp://wx2fc3f45732fae5d3'
  180. }
  181. })
  182. if (res.data.refresh_token == '' || typeof(res.data.refresh_token) == 'undefined') {
  183. uni.showToast({
  184. title: '未获得token'
  185. });
  186. } else {
  187. this.access_token = res.data.access_token;
  188. // 通过access_token换取用户信息
  189. this.get_user_info();
  190. }
  191. },
  192. /**
  193. * 通过access_token换取用户信息
  194. */
  195. async get_user_info() {
  196. const res = await this.$myRequest({
  197. host: 'wecard',
  198. url: '/connect/oauth/get-user-info',
  199. method: 'POST',
  200. header: {
  201. 'content-type': 'application/json'
  202. },
  203. data: {
  204. "access_token": this.access_token
  205. }
  206. });
  207. if (res.data.errcode == 0 && res.data.errmsg == 'OK') {
  208. try {
  209. this.userinfo = res.data
  210. this.$store.state.userinfo = this.userinfo
  211. // 存储用户信息
  212. uni.setStorageSync('userinfo_storage_key', this.userinfo)
  213. // 提示授权成功
  214. uni.showToast({
  215. icon: 'success',
  216. title: '授权成功',
  217. duration: 800,
  218. success: (res) => {
  219. this.my_display = true
  220. }
  221. })
  222. } catch (e) {
  223. console.log(e)
  224. }
  225. } else {
  226. uni.showToast({
  227. title: '未获得用户信息,请领取校园卡并激活',
  228. duration: 3000
  229. });
  230. }
  231. },
  232. /**
  233. * 检查数据库中是否有该用户信息
  234. */
  235. async chk_user_is_in_db() {
  236. const res = await this.$myRequest({
  237. host: this.ceshi,
  238. url: '/',
  239. method: 'POST',
  240. header: {
  241. 'content-type': 'application/json'
  242. },
  243. data: {
  244. "access_token": this.access_token
  245. }
  246. });
  247. if (res) {
  248. console.log(res);
  249. }
  250. }
  251. }
  252. }
  253. </script>
  254. <style scoped lang="scss">
  255. .container {
  256. display: flex;
  257. flex-direction: column;
  258. width: 750rpx;
  259. font-family: Microsoft YaHei-3970(82674968);
  260. color: #333333;
  261. .banner {
  262. width: 100%;
  263. height: 360rpx;
  264. image {
  265. width: 100%;
  266. height: 100%;
  267. }
  268. }
  269. .nav {
  270. position: relative;
  271. width: 100%;
  272. .menu,
  273. .reset {
  274. margin: 25rpx auto;
  275. width: 640rpx;
  276. }
  277. .menu_item {
  278. display: inline-flex;
  279. flex-direction: column;
  280. text-align: center;
  281. width: 25%;
  282. padding: 30rpx 0 39rpx;
  283. border-radius: 20rpx;
  284. font-family: Microsoft YaHei-3970(82674968);
  285. color: #333333;
  286. image {
  287. width: 90rpx;
  288. height: 90rpx;
  289. margin: 0 auto;
  290. }
  291. text {
  292. height: 29rpx;
  293. line-height: 54rpx;
  294. font-size: 30rpx;
  295. color: #333333;
  296. }
  297. }
  298. .reset {
  299. position: fixed;
  300. display: flex;
  301. flex-direction: column;
  302. align-items: flex-end;
  303. right: 55rpx;
  304. bottom: 55rpx;
  305. font-family: Microsoft YaHei-3970(82674968);
  306. color: #333333;
  307. .icon-zhongzhi {
  308. margin: 0 auto;
  309. padding-top: 20rpx;
  310. color: $my-color-primary;
  311. width: 90rpx;
  312. height: 70rpx;
  313. font-size: 80rpx;
  314. }
  315. }
  316. }
  317. }
  318. </style>