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