index.vue 8.0 KB

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