index.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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?item=' + encodeURIComponent(JSON.stringify(this.userinfo))"
  9. open-type="redirect" class="menu_item">
  10. <image src="../../static/images/shower2x.png" mode=""></image>
  11. <text>洗 浴</text>
  12. </navigator>
  13. <navigator :url="'/pages/jiaofei/jiaofei?item=' + encodeURIComponent(JSON.stringify(this.userinfo))"
  14. open-type="redirect" class="menu_item">
  15. <image src="../../static/images/recharge2x.png" mode=""></image>
  16. <text>电费充值</text>
  17. </navigator>
  18. </view>
  19. <view class="reset">
  20. <navigator url="" open-type="redirect" @click="reauthorization()" class="menu_item">
  21. <text class="iconfont icon-zhongzhi"></text>
  22. <text>重新授权</text>
  23. </navigator>
  24. </view>
  25. </view>
  26. <view v-if="showLogin">
  27. <login :ocode="ocode" :appkey="appkey" scope="snsapi_userinfo" :visible="visible" @success="callback()"
  28. @cancel="callback()" @fail="callback()" />
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. visible: false,
  37. showLogin: true,
  38. appkey: '5AA49F3E4CACA380',
  39. sub_appid: '1015730314_1941301045',
  40. appid: 'wxd6f090391d410534', // 获取用户信息
  41. // appid: 'wx2fc3f45732fae5d3', // 获取用户信息
  42. appid: 'wxd6f090391d410534 ', // 获取用户信息
  43. ocode: '1015730314', // 获取用户信息
  44. app_secret: '58D34C81D82B35179ED896C4362B0FC0', // 获取用户信息
  45. wxcode: '',
  46. access_token: '',
  47. access_token_for_amount: '', // 获取到应用有效凭证
  48. refresh_token: '',
  49. my_display: false,
  50. userinfo: {},
  51. amount: 0.00
  52. }
  53. },
  54. onLoad() {
  55. try {
  56. const value = uni.getStorageSync('userinfo_storage_key');
  57. if (value == '') {
  58. if (JSON.stringify(this.userinfo) == '{}') {
  59. this.showLogin = true
  60. this.visible = true
  61. } else {
  62. this.showLogin = false
  63. this.my_display = true
  64. }
  65. } else {
  66. this.showLogin = false
  67. this.userinfo = value
  68. this.my_display = true
  69. }
  70. // 获取应用有效凭证,并查询余额
  71. this.get_valid_credentials()
  72. } catch (e) {
  73. // console.log(e)
  74. uni.showToast({
  75. icon: 'success',
  76. title: '用户获取失败',
  77. duration: 2000
  78. })
  79. }
  80. },
  81. methods: {
  82. /**
  83. * 重新授权
  84. */
  85. reauthorization() {
  86. try {
  87. uni.removeStorageSync('userinfo_storage_key');
  88. setTimeout(() => {
  89. this.showLogin = false
  90. this.userinfo = {}
  91. this.my_display = false
  92. }, 300)
  93. setTimeout(() => {
  94. this.showLogin = true
  95. this.visible = true
  96. }, 300)
  97. } catch (e) {
  98. console.log(e)
  99. }
  100. },
  101. /**
  102. * 授权回调
  103. */
  104. callback: function({
  105. detail
  106. }) {
  107. // console.log(detail)
  108. // {wxcode: "7f7Qi9rN8zQ1nifQiysTZ3WIeIWlgcGk"}
  109. if (detail === undefined) {
  110. this.userinfo = {};
  111. uni.showToast({
  112. icon: 'success',
  113. title: '需要先授权'
  114. });
  115. } else {
  116. const {
  117. wxcode = ""
  118. } = detail
  119. if (wxcode.length == 0) {
  120. uni.showToast({
  121. title: '未获得wxcode',
  122. duration: 500
  123. })
  124. } else {
  125. // 获取wxcode
  126. this.wxcode = wxcode
  127. // 通过wxcode换取access_token
  128. this.get_access_token()
  129. }
  130. }
  131. },
  132. /**
  133. * 通过wxcode换取access_token
  134. */
  135. async get_access_token() {
  136. const res = await this.$myRequest({
  137. host: 'wecard',
  138. url: '/connect/oauth2/token',
  139. method: 'POST',
  140. header: {
  141. 'content-type': 'application/json'
  142. },
  143. data: {
  144. 'wxcode': this.wxcode,
  145. 'app_key': this.appkey,
  146. 'app_secret': this.app_secret,
  147. 'grant_type': 'authorization_code',
  148. 'redirect_uri': 'mnp://wx2fc3f45732fae5d3'
  149. }
  150. })
  151. if (res.data.refresh_token == '' || res.data.refresh_token == 'undefined') {
  152. uni.showToast({
  153. title: '未获得token'
  154. });
  155. } else {
  156. this.access_token = res.data.access_token;
  157. this.refresh_token = res.data.refresh_token;
  158. // 通过access_token换取用户信息
  159. this.get_user_info();
  160. }
  161. },
  162. /**
  163. * 通过access_token换取用户信息
  164. */
  165. async get_user_info() {
  166. const res = await this.$myRequest({
  167. host: 'wecard',
  168. url: '/connect/oauth/get-user-info',
  169. method: 'POST',
  170. header: {
  171. 'content-type': 'application/json'
  172. },
  173. data: {
  174. "access_token": this.access_token
  175. }
  176. });
  177. if (res.data.errcode == 0 && res.data.errmsg == 'OK') {
  178. try {
  179. this.userinfo = res.data
  180. this.userinfo.amount = this.amount
  181. // 微校参数
  182. this.userinfo.appkey = this.appkey
  183. this.userinfo.app_secret = this.app_secret
  184. this.userinfo.ocode = this.ocode
  185. this.userinfo.sub_appid = this.sub_appid
  186. this.userinfo.appid = this.appid_pass
  187. this.userinfo.app_secret = this.app_secret_pass
  188. this.userinfo.ocode = this.ocode_pass
  189. this.userinfo.access_token = this.access_token
  190. this.userinfo.appid = this.appid
  191. // console.log(this.userinfo)
  192. // console.log(this.access_token)
  193. // 存储用户信息
  194. uni.setStorageSync('userinfo_storage_key', this.userinfo)
  195. // 提示授权成功
  196. uni.showToast({
  197. icon: 'success',
  198. title: '授权成功',
  199. duration: 800,
  200. success: (res) => {
  201. this.my_display = true
  202. }
  203. })
  204. } catch (e) {
  205. console.log(e)
  206. }
  207. } else {
  208. uni.showToast({
  209. title: '未获得用户信息',
  210. duration: 1000
  211. });
  212. }
  213. },
  214. /**
  215. * 获取应用有效凭证
  216. */
  217. async get_valid_credentials() {
  218. const res = await this.$myRequest({
  219. host: 'wecard',
  220. url: '/cgi-bin/oauth2/token',
  221. method: 'POST',
  222. header: {
  223. 'content-type': 'application/json'
  224. },
  225. data: {
  226. 'app_key': this.appkey,
  227. 'app_secret': this.app_secret,
  228. "grant_type": "client_credentials",
  229. "scope": "base",
  230. "ocode": this.ocode
  231. }
  232. });
  233. if (typeof(res.data.access_token) != 'undefined') {
  234. // 获取到应用有效凭证,保存到页面变量中
  235. this.access_token_for_amount = res.data.access_token
  236. // 用户充值钱包余额查询
  237. this.request_user_amount()
  238. } else {
  239. uni.showToast({
  240. title: '获取凭证失败',
  241. duration: 2000
  242. })
  243. }
  244. },
  245. /**
  246. * 用户充值钱包余额查询
  247. */
  248. async request_user_amount() {
  249. const res = await this.$myRequest({
  250. host: 'wecard',
  251. url: "/cgi-bin/pay/app/wallet-recharge-balance?access_token=" + this
  252. .access_token_for_amount + "&sub_appid=" + this.sub_appid + "&user_id=" + this
  253. .userinfo.card_number
  254. });
  255. if (res.data.code === 0) {
  256. // 用户余额保存到页面变量中
  257. this.amount = res.data.data.amount;
  258. } else {
  259. uni.showToast({
  260. icon: 'none',
  261. title: '余额查询失败:\n' + res.data.message,
  262. duration: 2000
  263. })
  264. }
  265. }
  266. }
  267. }
  268. </script>
  269. <style scoped lang="scss">
  270. .container {
  271. display: flex;
  272. flex-direction: column;
  273. width: 750rpx;
  274. font-family: Microsoft YaHei-3970(82674968);
  275. color: #333333;
  276. .banner {
  277. width: 100%;
  278. height: 360rpx;
  279. image {
  280. width: 100%;
  281. height: 100%;
  282. }
  283. }
  284. .nav {
  285. position: relative;
  286. width: 100%;
  287. .menu,
  288. .reset {
  289. margin: 25rpx auto;
  290. width: 640rpx;
  291. }
  292. .menu_item {
  293. display: inline-flex;
  294. flex-direction: column;
  295. text-align: center;
  296. width: 25%;
  297. padding: 30rpx 0 39rpx;
  298. border-radius: 20rpx;
  299. font-family: Microsoft YaHei-3970(82674968);
  300. color: #333333;
  301. image {
  302. width: 90rpx;
  303. height: 90rpx;
  304. margin: 0 auto;
  305. }
  306. text {
  307. height: 29rpx;
  308. line-height: 54rpx;
  309. font-size: 30rpx;
  310. color: #333333;
  311. }
  312. }
  313. .reset {
  314. position: fixed;
  315. display: flex;
  316. flex-direction: column;
  317. align-items: flex-end;
  318. right: 55rpx;
  319. bottom: 55rpx;
  320. font-family: Microsoft YaHei-3970(82674968);
  321. color: #333333;
  322. .icon-zhongzhi {
  323. margin: 0 auto;
  324. padding-top: 20rpx;
  325. color: $my-color-primary;
  326. width: 90rpx;
  327. height: 70rpx;
  328. font-size: 80rpx;
  329. }
  330. }
  331. }
  332. }
  333. </style>