uniapp微信小程序授权登录

本是古典 何须时尚 2021-07-24 23:16 1176阅读 0赞
  1. <template>
  2. <view>
  3. <LotusLoading :lotusLoadingData="lotusLoadingData" style="width: 100%;height: 100vh;position: absolute;z-index: 99;"></LotusLoading>
  4. <view class="login" v-if="show">
  5. <image src='../../static/login-bg(1).png'></image>
  6. <view class='content'>
  7. <view class="text">尊贵的会员,请您先登录</view>
  8. <button open-type="getUserInfo" lang="zh_CN" @getuserinfo="getUserInfo" class="button font-36 font-blod">微信授权登录</button>
  9. <view class="bottom">
  10. <view class="agree">登录即表示同意</view>
  11. <view class="deal">《服务协议及隐私条款》</view>
  12. </view>
  13. </view>
  14. </view>
  15. <view class="login" v-if="callshow">
  16. <image src='../../static/login-bg(1).png'></image>
  17. <view class="content">
  18. <view class="info">
  19. <view class="top">
  20. <image style="width: 64rpx;height: 64rpx;" src="../../static/iconsuccess.png"></image>
  21. <text class="logo">微信授权成功!</text>
  22. </view>
  23. <view class="middle">为了更好的为您提供服务,请绑定手机</view>
  24. <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" lang="zh_CN" class="button" withCredentials="true">
  25. 授权绑定手机
  26. </button>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. show: false,
  37. callshow:false,
  38. lotusLoadingData: {
  39. isShow: false //设置显示加载中组件true显示false隐藏
  40. },
  41. userInfo: { }
  42. };
  43. },
  44. onLoad(options) {
  45. // Loading
  46. // wx.login
  47. // >> code -> 服务器 /auth/wx-codes
  48. // >> >> 判断是否需要授权 > 不需要授权的话直接登录成功 > 跳转回登录成功页面
  49. // >> >> 判断是否需要授权 > 需要授权 > 取消Loading
  50. if (uni.getStorageSync('token') && uni.getStorageSync('authtoken')) {
  51. uni.switchTab({
  52. url: '../index/index'
  53. }) //如果存在token 和authtoken 就是出于登录状态,直接进入首页
  54. }else{
  55. this.lotusLoadingData.isShow = true
  56. this.submit()
  57. }
  58. },
  59. methods: {
  60. submit() {
  61. const that = this
  62. uni.getProvider({
  63. // 授权
  64. service: 'oauth', //服务商 微信qq等默认的是oauth
  65. success: function(res) {
  66. if (~res.provider.indexOf('weixin')) { // 判断是否微信授权
  67. uni.login({
  68. provider: 'weixin',
  69. success: res => {
  70. const code = res.code;
  71. const data = {
  72. code: code
  73. }
  74. that.$myRequest({
  75. method: 'POST',
  76. url: '/auth/wx-codes',
  77. data: data
  78. }).then(res => {
  79. if(res.statusCode == 200){
  80. uni.setStorageSync('openid', res.data.open_id);
  81. uni.setStorageSync('token', res.data.token)
  82. that.show = false
  83. if (!res.data.is_authorized) { //通过is_authorized来判断是否需要获取authtoken
  84. that.lotusLoadingData.isShow = false
  85. that.show = true
  86. } else {
  87. uni.setStorageSync('authtoken', res.data.auth_token)
  88. that.show = false
  89. that.callshow = false
  90. that.lotusLoadingData.isShow = false
  91. setTimeout(function(){
  92. // uni.switchTab({
  93. // url: '../index/index'
  94. // })
  95. uni.navigateBack({
  96. delta:1
  97. })
  98. },500)
  99. }
  100. }
  101. });
  102. },
  103. fail: () => {
  104. uni.showToast({
  105. title: '微信登录授权失败',
  106. icon: 'none'
  107. });
  108. }
  109. });
  110. } else {
  111. uni.showToast({
  112. title: '请先安装微信或升级版本1',
  113. icon: 'none'
  114. });
  115. }
  116. }
  117. });
  118. },
  119. // 获取手机号 把encryptedData 和iv 存入storage 之后判断用户登录状态要用
  120. getUserInfo(e) {
  121. uni.getUserInfo({
  122. provider: 'weixin',
  123. success: info => {
  124. this.userInfo = info;
  125. uni.setStorageSync('this.userInfo', info)
  126. this.show = false;
  127. this.lotusLoadingData.isShow = false //这个是自己做的自定义组件的loading 加载中你不做也没关系
  128. this.callshow = true
  129. },
  130. fail: () => {
  131. uni.showToast({
  132. title: '微信登录授权失败',
  133. icon: 'none'
  134. });
  135. }
  136. });
  137. },
  138. getPhoneNumber(e) {
  139. uni.getStorageSync('this.userInfo')
  140. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  141. var data = {
  142. token: uni.getStorageSync('token'),
  143. user_info: { //需要传给后台的值
  144. encrypted_data: this.userInfo.encryptedData,
  145. iv: this.userInfo.iv,
  146. cloud_id: "",
  147. },
  148. "phone_number": { //需要传给后台的值
  149. "encrypted_data": e.detail.encryptedData,
  150. "iv": e.detail.iv,
  151. "cloud_id": ""
  152. }
  153. };
  154. this.$myRequest({
  155. method: 'POST',
  156. url: '/auth/wx-authentications',
  157. data: data
  158. }).then(res => {
  159. uni.setStorageSync('authtoken', res.data.auth_token)
  160. if(res.statusCode == 200){
  161. // uni.switchTab({
  162. // url: '../index/index'
  163. // })
  164. uni.navigateBack({
  165. delta:1
  166. })
  167. }else{
  168. this.$api.msg(res.data.message); //这个是自己自己封装的提示,你可以用uni.showtoast
  169. }
  170. });
  171. } else { }
  172. },
  173. }
  174. }
  175. </script>
  176. <style lang="scss">
  177. .login {
  178. height: 100vh;
  179. image {
  180. width: 100%;
  181. height: 100%;
  182. }
  183. .content {
  184. position: absolute;
  185. bottom: 0;
  186. z-index: 999;
  187. background: #ECECEC;
  188. height: 490rpx;
  189. border-top-left-radius: 20rpx;
  190. border-top-right-radius: 20rpx;
  191. display: flex;
  192. flex-direction: column;
  193. align-items: center;
  194. .bottom{
  195. display: flex;
  196. flex-direction: column;
  197. align-items: center;
  198. margin-top: 30rpx;
  199. .agree{
  200. font-size: 28rpx;
  201. font-family: PingFang SC;
  202. font-weight: 400;
  203. line-height: 20px;
  204. color: #000000;
  205. }
  206. .deal{
  207. margin-top: 10rpx;
  208. font-size: 28rpx;
  209. font-family: PingFang SC;
  210. font-weight: 400;
  211. line-height: 20px;
  212. color: #0655EA;
  213. }
  214. }
  215. .info{
  216. background-color: #FFFFFF;
  217. margin: 20rpx;
  218. width: 90%;
  219. height: 100%;
  220. border-radius: 20rpx;
  221. padding: 20rpx;
  222. display: flex;
  223. flex-direction: column;
  224. align-items: center;
  225. .top{
  226. display: flex;
  227. margin-top: 20rpx;
  228. align-items: center;
  229. .logo{
  230. font-size: 50rpx;
  231. font-family: PingFang SC;
  232. font-weight: bold;
  233. color: #000000;
  234. margin-left: 20rpx;
  235. }
  236. }
  237. .middle{
  238. font-size: 36rpx;
  239. font-family: PingFang SC;
  240. font-weight: 400;
  241. margin-top: 50rpx;
  242. color: #000000;
  243. }
  244. }
  245. }
  246. }
  247. .text {
  248. margin-top: 50rpx;
  249. font-size: 36rpx;
  250. font-family: PingFang SC;
  251. font-weight: 400;
  252. color: #000000;
  253. }
  254. button {
  255. width: 380rpx;
  256. height: 88rpx;
  257. background: linear-gradient(180deg, #EACDA3 0%, #E6B980 100%);
  258. border-radius: 66rpx;
  259. margin-top: 80rpx;
  260. }
  261. button::after {
  262. border: none;
  263. }
  264. </style>

发表评论

表情:
评论列表 (有 0 条评论,1176人围观)

还没有评论,来说两句吧...

相关阅读

    相关 程序授权登录

    现在微信小程序非常火爆,很多常规的APP都推出了自己的微信小程序。说到微信小程序,不可避免会触及到微信账号的授权登录,现在来说说一下哈! 首先需要自己的一个小程序,可以到微

    相关 程序授权登录

    现在微信小程序非常火爆,很多常规的APP都推出了自己的微信小程序。说到微信小程序,不可避免会触及到微信账号的授权登录,现在来说说一下哈! 首先需要自己的一个小程序,可以到微