index.vue 8.2 KB

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