index.vue 7.9 KB

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