index.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. this.select_user_info()
  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 select_user_info() {
  211. const res = await this.$myRequest({
  212. host: 'code',
  213. url: '/HotWater/userget_stu.action',
  214. method: 'POST',
  215. header: {
  216. 'content-type': 'application/x-www-form-urlencoded'
  217. },
  218. data: {
  219. card_number: this.userinfo.card_number
  220. }
  221. });
  222. // console.log(res, 'select_user_info');
  223. if (res.data.info) {
  224. let cnumber = this.userinfo.card_number
  225. if (cnumber == null || cnumber == undefined || cnumber == '') {
  226. // 之前未绑定用户信息,现在去绑定用户信息
  227. this.send_save_user_info()
  228. }
  229. } else {
  230. uni.showToast({
  231. icon: 'success',
  232. title: '获取用户信息失败'
  233. })
  234. }
  235. },
  236. /**
  237. * 绑定用户信息
  238. */
  239. async send_save_user_info() {
  240. const res = await this.$myRequest({
  241. host: 'code',
  242. url: '/HotWater/userstu_info.action',
  243. method: 'POST',
  244. header: {
  245. 'content-type': 'application/x-www-form-urlencoded'
  246. },
  247. data: {
  248. user: JSON.stringify(this.userinfo)
  249. }
  250. });
  251. // console.log('send_save_user_info:', res)
  252. let error = res.data.erro;
  253. if (error == '未获取到数据!') {
  254. uni.showToast({
  255. title: '绑定用户失败',
  256. duration: 2000
  257. })
  258. } else {
  259. uni.showToast({
  260. icon: 'success',
  261. title: '绑定用户成功',
  262. duration: 2000
  263. })
  264. }
  265. },
  266. /**
  267. * 获取应用有效凭证
  268. */
  269. async get_valid_credentials() {
  270. const res = await this.$myRequest({
  271. host: 'wecard',
  272. url: '/cgi-bin/oauth2/token',
  273. method: 'POST',
  274. header: {
  275. 'content-type': 'application/json'
  276. },
  277. data: {
  278. 'app_key': this.appkey,
  279. 'app_secret': this.app_secret,
  280. "grant_type": "client_credentials",
  281. "scope": "base",
  282. "ocode": this.ocode
  283. }
  284. });
  285. if (typeof(res.data.access_token) != 'undefined') {
  286. // 获取到应用有效凭证,保存到页面变量中
  287. this.access_token_for_amount = res.data.access_token
  288. // 用户充值钱包余额查询
  289. this.request_user_amount()
  290. } else {
  291. uni.showToast({
  292. title: '获取凭证失败',
  293. duration: 2000
  294. })
  295. }
  296. },
  297. /**
  298. * 用户充值钱包余额查询
  299. */
  300. async request_user_amount() {
  301. const res = await this.$myRequest({
  302. host: 'wecard',
  303. url: "/cgi-bin/pay/app/wallet-recharge-balance?access_token=" + this
  304. .access_token_for_amount + "&sub_appid=" + this.sub_appid + "&user_id=" + this
  305. .userinfo.card_number
  306. });
  307. if (res.data.code === 0) {
  308. // 用户余额保存到页面变量中
  309. this.amount = res.data.data.amount;
  310. } else {
  311. uni.showToast({
  312. icon: 'none',
  313. title: '余额查询失败:\n' + res.data.message,
  314. duration: 2000
  315. })
  316. }
  317. }
  318. }
  319. }
  320. </script>
  321. <style scoped lang="scss">
  322. .container {
  323. display: flex;
  324. flex-direction: column;
  325. width: 750rpx;
  326. font-family: Microsoft YaHei-3970(82674968);
  327. color: #333333;
  328. .banner {
  329. width: 100%;
  330. height: 360rpx;
  331. image {
  332. width: 100%;
  333. height: 100%;
  334. }
  335. }
  336. .nav {
  337. position: relative;
  338. width: 100%;
  339. .menu,
  340. .reset {
  341. margin: 25rpx auto;
  342. width: 640rpx;
  343. }
  344. .menu_item {
  345. display: inline-flex;
  346. flex-direction: column;
  347. text-align: center;
  348. width: 25%;
  349. padding: 30rpx 0 39rpx;
  350. border-radius: 20rpx;
  351. font-family: Microsoft YaHei-3970(82674968);
  352. color: #333333;
  353. image {
  354. width: 90rpx;
  355. height: 90rpx;
  356. margin: 0 auto;
  357. }
  358. text {
  359. height: 29rpx;
  360. line-height: 54rpx;
  361. font-size: 30rpx;
  362. color: #333333;
  363. }
  364. }
  365. .reset {
  366. position: fixed;
  367. display: flex;
  368. flex-direction: column;
  369. align-items: flex-end;
  370. right: 55rpx;
  371. bottom: 55rpx;
  372. font-family: Microsoft YaHei-3970(82674968);
  373. color: #333333;
  374. .icon-zhongzhi {
  375. margin: 0 auto;
  376. padding-top: 20rpx;
  377. color: $my-color-primary;
  378. width: 90rpx;
  379. height: 70rpx;
  380. font-size: 80rpx;
  381. }
  382. }
  383. }
  384. }
  385. </style>