index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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
  14. :url="'/pages/jiaofei/jiaofei?o=index&item=' + encodeURIComponent(JSON.stringify(this.userinfo))"
  15. open-type="redirect" class="menu_item">
  16. <image src="../../static/images/recharge2x.png" mode=""></image>
  17. <text>电费充值</text>
  18. </navigator>
  19. </view>
  20. <view class="reset">
  21. <navigator url="" open-type="redirect" @click="reauthorization()" 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: 'wxd6f090391d410534', // 获取用户信息
  42. ocode: '1015730314', // 获取用户信息
  43. app_secret: '58D34C81D82B35179ED896C4362B0FC0', // 获取用户信息
  44. wxcode: '',
  45. access_token: '',
  46. access_token_for_amount: '', // 获取到应用有效凭证
  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://wxd6f090391d410534'
  147. }
  148. })
  149. if (res.data.access_token == '' || res.data.access_token == 'undefined') {
  150. uni.showToast({
  151. title: '未获得token'
  152. })
  153. } else {
  154. this.access_token = res.data.access_token
  155. // 通过access_token换取用户信息
  156. this.get_user_info();
  157. }
  158. },
  159. /**
  160. * 通过access_token换取用户信息
  161. */
  162. async get_user_info() {
  163. const res = await this.$myRequest({
  164. host: 'wecard',
  165. url: '/connect/oauth/get-user-info',
  166. method: 'POST',
  167. header: {
  168. 'content-type': 'application/json'
  169. },
  170. data: {
  171. "access_token": this.access_token
  172. }
  173. });
  174. if (res.data.errcode == 0 && res.data.errmsg == 'OK') {
  175. try {
  176. this.userinfo = res.data
  177. this.userinfo.amount = this.amount
  178. // 微校参数
  179. this.userinfo.appkey = this.appkey
  180. this.userinfo.app_secret = this.app_secret
  181. this.userinfo.ocode = this.ocode
  182. this.userinfo.sub_appid = this.sub_appid
  183. this.userinfo.appid = this.appid
  184. // 存储用户信息
  185. uni.setStorageSync('userinfo_storage_key', this.userinfo)
  186. // 提示授权成功
  187. uni.showToast({
  188. icon: 'success',
  189. title: '授权成功',
  190. duration: 800,
  191. success: (res) => {
  192. this.my_display = true
  193. }
  194. })
  195. } catch (e) {
  196. console.log(e)
  197. }
  198. } else {
  199. uni.showToast({
  200. title: '未获得用户信息',
  201. duration: 1000
  202. });
  203. }
  204. },
  205. /**
  206. * 获取应用有效凭证
  207. */
  208. async get_valid_credentials() {
  209. const res = await this.$myRequest({
  210. host: 'wecard',
  211. url: '/cgi-bin/oauth2/token',
  212. method: 'POST',
  213. header: {
  214. 'content-type': 'application/json'
  215. },
  216. data: {
  217. 'app_key': this.appkey,
  218. 'app_secret': this.app_secret,
  219. "grant_type": "client_credentials",
  220. "scope": "base",
  221. "ocode": this.ocode
  222. }
  223. });
  224. if (typeof(res.data.access_token) != 'undefined') {
  225. // 获取到应用有效凭证,保存到页面变量中
  226. this.access_token_for_amount = res.data.access_token
  227. // 用户充值钱包余额查询
  228. this.request_user_amount()
  229. } else {
  230. uni.showToast({
  231. title: '获取凭证失败',
  232. duration: 2000
  233. })
  234. }
  235. },
  236. /**
  237. * 用户充值钱包余额查询
  238. */
  239. async request_user_amount() {
  240. const res = await this.$myRequest({
  241. host: 'wecard',
  242. url: "/cgi-bin/pay/app/wallet-recharge-balance?access_token=" + this
  243. .access_token_for_amount + "&sub_appid=" + this.sub_appid + "&user_id=" + this
  244. .userinfo.card_number
  245. });
  246. if (res.data.code === 0) {
  247. // 用户余额保存到页面变量中
  248. this.amount = res.data.data.amount;
  249. } else {
  250. uni.showToast({
  251. icon: 'none',
  252. title: '余额查询失败:\n' + res.data.message,
  253. duration: 2000
  254. })
  255. }
  256. }
  257. }
  258. }
  259. </script>
  260. <style scoped lang="scss">
  261. .container {
  262. display: flex;
  263. flex-direction: column;
  264. width: 750rpx;
  265. font-family: Microsoft YaHei-3970(82674968);
  266. color: #333333;
  267. .banner {
  268. width: 100%;
  269. height: 360rpx;
  270. image {
  271. width: 100%;
  272. height: 100%;
  273. }
  274. }
  275. .nav {
  276. position: relative;
  277. width: 100%;
  278. .menu,
  279. .reset {
  280. margin: 25rpx auto;
  281. width: 640rpx;
  282. }
  283. .menu_item {
  284. display: inline-flex;
  285. flex-direction: column;
  286. text-align: center;
  287. width: 25%;
  288. padding: 30rpx 0 39rpx;
  289. border-radius: 20rpx;
  290. font-family: Microsoft YaHei-3970(82674968);
  291. color: #333333;
  292. image {
  293. width: 90rpx;
  294. height: 90rpx;
  295. margin: 0 auto;
  296. }
  297. text {
  298. height: 29rpx;
  299. line-height: 54rpx;
  300. font-size: 30rpx;
  301. color: #333333;
  302. }
  303. }
  304. .reset {
  305. position: fixed;
  306. display: flex;
  307. flex-direction: column;
  308. align-items: flex-end;
  309. right: 55rpx;
  310. bottom: 55rpx;
  311. font-family: Microsoft YaHei-3970(82674968);
  312. color: #333333;
  313. .icon-zhongzhi {
  314. margin: 0 auto;
  315. padding-top: 20rpx;
  316. color: $my-color-primary;
  317. width: 90rpx;
  318. height: 70rpx;
  319. font-size: 80rpx;
  320. }
  321. }
  322. }
  323. }
  324. </style>