identity.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. <!-- #ifdef MP-WEIXIN -->
  8. <text>获得你的公开信息(昵称、头像、地区等)</text>
  9. <!-- #endif -->
  10. <!-- #ifdef MP-ALIPAY -->
  11. <text>获得你的公开信息(昵称、头像、地区等)</text>
  12. <!-- #endif -->
  13. </view>
  14. <!-- #ifdef MP-WEIXIN -->
  15. <button :loading="getUserInfoLoading" :disabled="getUserInfoLoading" @click="getUserInfo">
  16. 授权获取
  17. </button>
  18. <!-- #endif -->
  19. <!-- #ifdef MP-ALIPAY -->
  20. <!--
  21. <button :loading="getUserInfoLoading" open-type="getAuthorize" scope="userInfo"
  22. @getAuthorize="getUserInfo" @onError="onAuthError">
  23. 本地授权获取
  24. </button>
  25. -->
  26. <button @click="getAliInfo" :loading="getUserInfoLoading" :disabled="getUserInfoLoading">
  27. 授权获取
  28. </button>
  29. <!-- #endif -->
  30. </view>
  31. </template>
  32. <script>
  33. import {
  34. mapState,
  35. mapMutations
  36. } from "vuex";
  37. export default {
  38. data() {
  39. return {
  40. code: '',
  41. getUserInfoLoading: false
  42. }
  43. },
  44. created() {
  45. if (store.state.userInfo.nickName !== undefined) {
  46. uni.redirectTo({
  47. url: "/pages/authen/login"
  48. })
  49. return
  50. }
  51. },
  52. computed: {
  53. // ...mapState(["appId", "stationId", "phoneNumber", "countryCode", "purePhoneNumber", "openId", "unionId",
  54. // "userInfo"
  55. // ]),
  56. // ...mapState({
  57. // appId: state => state.appId,
  58. // phoneNumber: state => state.user.phoneNumber,
  59. // // countryCode: state => state.user.countryCode,
  60. // // purePhoneNumber: state => state.user.purePhoneNumber,
  61. // openId: state => state.user.openId,
  62. // unionId: state => state.user.unionId,
  63. // userInfo: state => state.user.userInfo,
  64. // stationId: state => state.station.stationId,
  65. // }),
  66. },
  67. async created() {
  68. let [err, loginData] = await uni.login();
  69. this.code = loginData.code
  70. },
  71. methods: {
  72. ...mapMutations({
  73. updateUserInfo: "updateUserInfo",
  74. clearUserInfo: "clearUserInfo"
  75. }),
  76. ...mapMutations({
  77. updateOpenId: "updateOpenId",
  78. updateUnionId: "updateUnionId",
  79. updateUserInfo: "updateUserInfo",
  80. updatePhoneNumber: "updatePhoneNumber",
  81. updateCountryCode: "updateCountryCode",
  82. updatePurePhoneNumber: "updatePurePhoneNumber",
  83. resetApplet: "resetApplet",
  84. clearUserAuthen:"clearUserAuthen"
  85. }),
  86. //#ifdef MP-ALIPAY
  87. getAliInfo(){
  88. let userInfo = undefined
  89. my.getAuthCode({
  90. // scopes: ['auth_user',"auth_base","auth_zhima"],
  91. scopes: ['auth_user',"auth_base"],
  92. // 主动授权:auth_user,静默授权:auth_base。或者其它scope。如需同时获取用户多项授权,可在 scopes 中传入多个 scope 值。
  93. success: async (res) => {
  94. console.log(res)
  95. if (res.authCode) {
  96. // 认证成功
  97. // 调用自己的服务端接口,让服务端进行后端的授权认证
  98. const profileData = await this.$Request({
  99. url: "/getAliUserInfo",
  100. method: "GET",
  101. data:{
  102. authCode:res.authCode
  103. }
  104. })
  105. console.log(profileData)
  106. userInfo = profileData.data
  107. userInfo = {
  108. nickName:userInfo.userId, // 先骗过auth认证
  109. avatarUrl:userInfo.avatar
  110. // gender:userInfo.gender
  111. }
  112. const userId = profileData.data.userId;
  113. this.updateUserInfo(userInfo)
  114. this.updateUnionId(userId)
  115. this.updateOpenId(userId)
  116. const [redirectErr, redirectData] = await uni.redirectTo({
  117. url: "/pages/authen/login"
  118. })
  119. }
  120. },
  121. fail:(err)=>{
  122. this.clearUserAuthen();
  123. uni.showToast({
  124. title: e.message,
  125. icon:"error"
  126. })
  127. }
  128. });
  129. },
  130. //#endif
  131. //#ifdef MP-WEIXIN
  132. async getUserInfo(res) {
  133. let userInfo = undefined
  134. try {
  135. this.getUserInfoLoading = true;
  136. uni.showLoading({
  137. title: '获取中...',
  138. mask: true
  139. });
  140. const profileData = await wx.getUserProfile({
  141. lang: "zh_CN",
  142. desc: "更好的消费推送体验"
  143. })
  144. userInfo = profileData.userInfo
  145. userInfo = {
  146. nickName:userInfo.nickName,
  147. avatarUrl:userInfo.avatarUrl,
  148. gender:userInfo.gender
  149. }
  150. this.updateUserInfo(userInfo)
  151. this.getUserInfoLoading = false
  152. uni.hideLoading();
  153. const [redirectErr, redirectData] = await uni.redirectTo({
  154. url: "/pages/authen/login"
  155. })
  156. } catch (e) {
  157. this.getUserInfoLoading = false
  158. uni.hideLoading();
  159. uni.showToast({
  160. title: '请重新授权~',
  161. icon:"error"
  162. })
  163. this.clearUserInfo()
  164. }
  165. },
  166. //#endif
  167. },
  168. onLoad() { //默认加载
  169. }
  170. }
  171. </script>
  172. <style lang="scss">
  173. page {
  174. width: 750rpx;
  175. background-color: #f0f2f5;
  176. min-height: 100vh;
  177. .container {
  178. text-align: center;
  179. .icon {
  180. margin-top: 414rpx;
  181. width: 508rpx;
  182. height: 388rpx;
  183. margin-top: 180rpx;
  184. display: inline-block;
  185. }
  186. .hr {
  187. margin-top: 120rpx;
  188. display: inline-block;
  189. width: 618rpx;
  190. height: 2rpx;
  191. box-sizing: border-box;
  192. border: 1rpx solid rgba(0, 0, 0, 0.1);
  193. }
  194. .content {
  195. margin-top: 90rpx;
  196. display: inline-flex;
  197. flex-direction: column;
  198. justify-content: center;
  199. width: 616rpx;
  200. text:nth-child(1) {
  201. height: 44rpx;
  202. font-size: 32rpx;
  203. color: #000000;
  204. line-height: 44rpx;
  205. text-align: left;
  206. }
  207. text:nth-child(2) {
  208. height: 44rpx;
  209. font-size: 28rpx;
  210. text-align: left;
  211. color: rgba(0, 0, 0, 0.25);
  212. line-height: 44rpx;
  213. }
  214. }
  215. button {
  216. margin-top: 90rpx;
  217. display: inline-block;
  218. width: 616rpx;
  219. height: 76rpx;
  220. background: linear-gradient(123deg, #12A273 0%, #12A273 100%);
  221. border-radius: 38rpx;
  222. font-size: 32rpx;
  223. color: #FFFFFF;
  224. line-height: 76rpx;
  225. }
  226. }
  227. }
  228. </style>