identity.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view class="container">
  3. <image src="../../static/icon/login.png" mode="scaleToFill" class="icon"></image>
  4. <view class="hr"></view>
  5. <view class="content">
  6. <text>申请获取以下权限</text>
  7. <text>获得你的公开信息(昵称、头像、地区等)</text>
  8. </view>
  9. <!-- #ifdef MP-WEIXIN -->
  10. <button :loading="getUserInfoLoading" :disabled="getUserInfoLoading" @click="getUserInfo">
  11. 授权获取
  12. </button>
  13. <!-- #endif -->
  14. <!-- #ifdef MP-ALIPAY -->
  15. <button :loading="getUserInfoLoading" :disabled="getUserInfoLoading" open-type="getAuthorize" scope="userInfo"
  16. @getAuthorize="getUserInfo" @onError="onAuthError">
  17. 支付宝授权获取
  18. </button>
  19. <button @click="getCode">
  20. code获取
  21. </button>
  22. <button @click="getCode" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  23. code获取
  24. </button>
  25. <!-- #endif -->
  26. </view>
  27. </template>
  28. <script>
  29. import {
  30. mapState,
  31. mapMutations
  32. } from "vuex";
  33. export default {
  34. data() {
  35. return {
  36. code: '',
  37. getUserInfoLoading: false
  38. }
  39. },
  40. created() {
  41. if (store.state.userInfo.nickName !== undefined) {
  42. uni.redirectTo({
  43. url: "/pages/authen/login"
  44. })
  45. return
  46. }
  47. },
  48. computed: {
  49. ...mapState(["appId", "stationId", "phoneNumber", "countryCode", "purePhoneNumber", "openId", "unionId",
  50. "userInfo"
  51. ])
  52. },
  53. async created() {
  54. let [err, loginData] = await uni.login();
  55. this.code = loginData.code
  56. },
  57. methods: {
  58. getPhoneNumber(){
  59. my.getPhoneNumber({
  60. success: (res) => {
  61. let encryptedData = res.response;
  62. console.log(encryptedData)
  63. },
  64. fail: (res) => {
  65. console.log(res);
  66. console.log('getPhoneNumber_fail');
  67. },
  68. });
  69. },
  70. getCode(){
  71. my.getAuthCode({
  72. scopes: ['auth_user',"auth_base","auth_zhima"],
  73. // 主动授权:auth_user,静默授权:auth_base。或者其它scope。如需同时获取用户多项授权,可在 scopes 中传入多个 scope 值。
  74. success: (res) => {
  75. console.log(res)
  76. // if (res.authCode) {
  77. // // 认证成功
  78. // // 调用自己的服务端接口,让服务端进行后端的授权认证,并且利用session,需要解决跨域问题
  79. // my.request({
  80. // url: 'https://isv.com/auth', // 该url是您自己的服务地址,实现的功能是服务端拿到authcode去开放平台进行token验证
  81. // data: {
  82. // authcode: res.authCode,
  83. // },
  84. // success: () => {
  85. // // 授权成功并且服务器端登录成功
  86. // },
  87. // fail: () => {
  88. // // 根据自己的业务场景来进行错误处理
  89. // },
  90. // });
  91. // }
  92. },
  93. });
  94. },
  95. test(res) {
  96. console.log("你好")
  97. console.log(res)
  98. },
  99. ...mapMutations({
  100. updateUserInfo: "updateUserInfo",
  101. }),
  102. async getUserInfo(res) {
  103. let userInfo = undefined
  104. //#ifdef MP-WEIXIN
  105. try {
  106. this.getUserInfoLoading = true;
  107. uni.showLoading({
  108. title: '获取中...',
  109. mask: true
  110. });
  111. const profileData = await wx.getUserProfile({
  112. lang: "zh_CN",
  113. desc: "更好的推送体验"
  114. })
  115. userInfo = profileData.userInfo
  116. this.updateUserInfo(userInfo)
  117. this.getUserInfoLoading = false
  118. uni.hideLoading();
  119. const [redirectErr, redirectData] = await uni.redirectTo({
  120. url: "/pages/authen/login"
  121. })
  122. } catch (e) {
  123. this.getUserInfoLoading = false
  124. uni.hideLoading();
  125. uni.showToast({
  126. title: '授权失败,请刷新小程序'
  127. })
  128. }
  129. //#endif
  130. //#ifdef MP-ALIPAY
  131. my.getOpenUserInfo({
  132. fail: (res) => {},
  133. success: (res) => {
  134. let userInfo = JSON.parse(res.response).response // 以下方的报文格式解析两层 response0
  135. console.log(userInfo);
  136. },
  137. });
  138. //#endif
  139. // //#ifndef
  140. // uni.showToast({
  141. // title: '暂时只支持微信和支付宝小程序'
  142. // })
  143. // //#endif
  144. },
  145. },
  146. onLoad() { //默认加载
  147. }
  148. }
  149. </script>
  150. <style lang="scss">
  151. page {
  152. width: 750rpx;
  153. background-color: #f0f2f5;
  154. min-height: 100vh;
  155. .container {
  156. text-align: center;
  157. .icon {
  158. margin-top: 414rpx;
  159. width: 508rpx;
  160. height: 388rpx;
  161. margin-top: 180rpx;
  162. display: inline-block;
  163. }
  164. .hr {
  165. margin-top: 120rpx;
  166. display: inline-block;
  167. width: 618rpx;
  168. height: 2rpx;
  169. box-sizing: border-box;
  170. border: 1rpx solid rgba(0, 0, 0, 0.1);
  171. }
  172. .content {
  173. margin-top: 90rpx;
  174. display: inline-flex;
  175. flex-direction: column;
  176. justify-content: center;
  177. width: 616rpx;
  178. text:nth-child(1) {
  179. height: 44rpx;
  180. font-size: 32rpx;
  181. color: #000000;
  182. line-height: 44rpx;
  183. text-align: left;
  184. }
  185. text:nth-child(2) {
  186. height: 44rpx;
  187. font-size: 28rpx;
  188. text-align: left;
  189. color: rgba(0, 0, 0, 0.25);
  190. line-height: 44rpx;
  191. }
  192. }
  193. button {
  194. margin-top: 90rpx;
  195. display: inline-block;
  196. width: 616rpx;
  197. height: 76rpx;
  198. background: linear-gradient(123deg, #12A273 0%, #12A273 100%);
  199. border-radius: 38rpx;
  200. font-size: 32rpx;
  201. color: #FFFFFF;
  202. line-height: 76rpx;
  203. }
  204. }
  205. }
  206. </style>