index.vue 8.2 KB

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