identity.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. 手机号获取
  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. // ...mapState({
  53. // appId: state => state.appId,
  54. // phoneNumber: state => state.user.phoneNumber,
  55. // // countryCode: state => state.user.countryCode,
  56. // // purePhoneNumber: state => state.user.purePhoneNumber,
  57. // openId: state => state.user.openId,
  58. // unionId: state => state.user.unionId,
  59. // userInfo: state => state.user.userInfo,
  60. // stationId: state => state.station.stationId,
  61. // }),
  62. },
  63. async created() {
  64. let [err, loginData] = await uni.login();
  65. this.code = loginData.code
  66. },
  67. methods: {
  68. getPhoneNumber(){
  69. my.getPhoneNumber({
  70. success: (res) => {
  71. let encryptedData = res.response;
  72. console.log(encryptedData)
  73. },
  74. fail: (res) => {
  75. console.log(res);
  76. console.log('getPhoneNumber_fail');
  77. },
  78. });
  79. },
  80. getCode(){
  81. my.getAuthCode({
  82. scopes: ['auth_user',"auth_base","auth_zhima"],
  83. // 主动授权:auth_user,静默授权:auth_base。或者其它scope。如需同时获取用户多项授权,可在 scopes 中传入多个 scope 值。
  84. success: (res) => {
  85. console.log(res)
  86. // if (res.authCode) {
  87. // // 认证成功
  88. // // 调用自己的服务端接口,让服务端进行后端的授权认证,并且利用session,需要解决跨域问题
  89. // my.request({
  90. // url: 'https://isv.com/auth', // 该url是您自己的服务地址,实现的功能是服务端拿到authcode去开放平台进行token验证
  91. // data: {
  92. // authcode: res.authCode,
  93. // },
  94. // success: () => {
  95. // // 授权成功并且服务器端登录成功
  96. // },
  97. // fail: () => {
  98. // // 根据自己的业务场景来进行错误处理
  99. // },
  100. // });
  101. // }
  102. },
  103. });
  104. },
  105. test(res) {
  106. console.log("你好")
  107. console.log(res)
  108. },
  109. ...mapMutations({
  110. updateUserInfo: "updateUserInfo",
  111. clearUserInfo: "clearUserInfo"
  112. }),
  113. async getUserInfo(res) {
  114. let userInfo = undefined
  115. //#ifdef MP-WEIXIN
  116. try {
  117. this.getUserInfoLoading = true;
  118. uni.showLoading({
  119. title: '获取中...',
  120. mask: true
  121. });
  122. const profileData = await wx.getUserProfile({
  123. lang: "zh_CN",
  124. desc: "更好的消费推送体验"
  125. })
  126. userInfo = profileData.userInfo
  127. userInfo = {
  128. nickName:userInfo.nickName,
  129. avatarUrl:userInfo.avatarUrl,
  130. gender:userInfo.gender
  131. }
  132. this.updateUserInfo(userInfo)
  133. this.getUserInfoLoading = false
  134. uni.hideLoading();
  135. const [redirectErr, redirectData] = await uni.redirectTo({
  136. url: "/pages/authen/login"
  137. })
  138. } catch (e) {
  139. this.getUserInfoLoading = false
  140. uni.hideLoading();
  141. uni.showToast({
  142. title: '请重新授权~',
  143. icon:"error"
  144. })
  145. this.clearUserInfo()
  146. }
  147. //#endif
  148. //#ifdef MP-ALIPAY
  149. my.getOpenUserInfo({
  150. fail: (res) => {},
  151. success: (res) => {
  152. let userInfo = JSON.parse(res.response).response // 以下方的报文格式解析两层 response0
  153. console.log(userInfo);
  154. },
  155. });
  156. //#endif
  157. // //#ifndef
  158. // uni.showToast({
  159. // title: '暂时只支持微信和支付宝小程序'
  160. // })
  161. // //#endif
  162. },
  163. },
  164. onLoad() { //默认加载
  165. }
  166. }
  167. </script>
  168. <style lang="scss">
  169. page {
  170. width: 750rpx;
  171. background-color: #f0f2f5;
  172. min-height: 100vh;
  173. .container {
  174. text-align: center;
  175. .icon {
  176. margin-top: 414rpx;
  177. width: 508rpx;
  178. height: 388rpx;
  179. margin-top: 180rpx;
  180. display: inline-block;
  181. }
  182. .hr {
  183. margin-top: 120rpx;
  184. display: inline-block;
  185. width: 618rpx;
  186. height: 2rpx;
  187. box-sizing: border-box;
  188. border: 1rpx solid rgba(0, 0, 0, 0.1);
  189. }
  190. .content {
  191. margin-top: 90rpx;
  192. display: inline-flex;
  193. flex-direction: column;
  194. justify-content: center;
  195. width: 616rpx;
  196. text:nth-child(1) {
  197. height: 44rpx;
  198. font-size: 32rpx;
  199. color: #000000;
  200. line-height: 44rpx;
  201. text-align: left;
  202. }
  203. text:nth-child(2) {
  204. height: 44rpx;
  205. font-size: 28rpx;
  206. text-align: left;
  207. color: rgba(0, 0, 0, 0.25);
  208. line-height: 44rpx;
  209. }
  210. }
  211. button {
  212. margin-top: 90rpx;
  213. display: inline-block;
  214. width: 616rpx;
  215. height: 76rpx;
  216. background: linear-gradient(123deg, #12A273 0%, #12A273 100%);
  217. border-radius: 38rpx;
  218. font-size: 32rpx;
  219. color: #FFFFFF;
  220. line-height: 76rpx;
  221. }
  222. }
  223. }
  224. </style>