index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. // 存储用户信息
  186. uni.setStorageSync('userinfo_storage_key', this.userinfo)
  187. // 提示授权成功
  188. uni.showToast({
  189. icon: 'success',
  190. title: '授权成功',
  191. duration: 800,
  192. success: (res) => {
  193. this.my_display = true
  194. }
  195. })
  196. } catch (e) {
  197. console.log(e)
  198. }
  199. } else {
  200. uni.showToast({
  201. title: '未获得用户信息',
  202. duration: 1000
  203. });
  204. }
  205. },
  206. /**
  207. * 获取应用有效凭证
  208. */
  209. async get_valid_credentials() {
  210. const res = await this.$myRequest({
  211. host: 'wecard',
  212. url: '/cgi-bin/oauth2/token',
  213. method: 'POST',
  214. header: {
  215. 'content-type': 'application/json'
  216. },
  217. data: {
  218. 'app_key': this.appkey,
  219. 'app_secret': this.app_secret,
  220. "grant_type": "client_credentials",
  221. "scope": "base",
  222. "ocode": this.ocode
  223. }
  224. });
  225. if (typeof(res.data.access_token) != 'undefined') {
  226. // 获取到应用有效凭证,保存到页面变量中
  227. this.access_token_for_amount = res.data.access_token
  228. // 用户充值钱包余额查询
  229. this.request_user_amount()
  230. } else {
  231. uni.showToast({
  232. title: '获取凭证失败',
  233. duration: 2000
  234. })
  235. }
  236. },
  237. /**
  238. * 用户充值钱包余额查询
  239. */
  240. async request_user_amount() {
  241. const res = await this.$myRequest({
  242. host: 'wecard',
  243. url: "/cgi-bin/pay/app/wallet-recharge-balance?access_token=" + this
  244. .access_token_for_amount + "&sub_appid=" + this.sub_appid + "&user_id=" + this
  245. .userinfo.card_number
  246. });
  247. if (res.data.code === 0) {
  248. // 用户余额保存到页面变量中
  249. this.amount = res.data.data.amount;
  250. } else {
  251. uni.showToast({
  252. icon: 'none',
  253. title: '余额查询失败:\n' + res.data.message,
  254. duration: 2000
  255. })
  256. }
  257. }
  258. }
  259. }
  260. </script>
  261. <style scoped lang="scss">
  262. .container {
  263. display: flex;
  264. flex-direction: column;
  265. width: 750rpx;
  266. font-family: Microsoft YaHei-3970(82674968);
  267. color: #333333;
  268. .banner {
  269. width: 100%;
  270. height: 360rpx;
  271. image {
  272. width: 100%;
  273. height: 100%;
  274. }
  275. }
  276. .nav {
  277. position: relative;
  278. width: 100%;
  279. .menu,
  280. .reset {
  281. margin: 25rpx auto;
  282. width: 640rpx;
  283. }
  284. .menu_item {
  285. display: inline-flex;
  286. flex-direction: column;
  287. text-align: center;
  288. width: 25%;
  289. padding: 30rpx 0 39rpx;
  290. border-radius: 20rpx;
  291. font-family: Microsoft YaHei-3970(82674968);
  292. color: #333333;
  293. image {
  294. width: 90rpx;
  295. height: 90rpx;
  296. margin: 0 auto;
  297. }
  298. text {
  299. height: 29rpx;
  300. line-height: 54rpx;
  301. font-size: 30rpx;
  302. color: #333333;
  303. }
  304. }
  305. .reset {
  306. position: fixed;
  307. display: flex;
  308. flex-direction: column;
  309. align-items: flex-end;
  310. right: 55rpx;
  311. bottom: 55rpx;
  312. font-family: Microsoft YaHei-3970(82674968);
  313. color: #333333;
  314. .icon-zhongzhi {
  315. margin: 0 auto;
  316. padding-top: 20rpx;
  317. color: $my-color-primary;
  318. width: 90rpx;
  319. height: 70rpx;
  320. font-size: 80rpx;
  321. }
  322. }
  323. }
  324. }
  325. </style>