btnAuth.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <!-- 昵称(邮箱或者个人主页) -->
  2. <template>
  3. <view class="page">
  4. <view class="hd">
  5. <image class="logo" src="/static/images/logo.png"></image>
  6. <view class="title">安徽微同科技有限公司欢迎您!</view>
  7. </view>
  8. <view class="bd">
  9. <view class="top_line"></view>
  10. <text class="m_name">安徽微同科技有限公司申请获得以下权限</text>
  11. <text class="s_name">· 获得你的公开信息(昵称、头像等)</text>
  12. </view>
  13. <view class="btn spacing">
  14. <!-- 需要使用 button 来授权登录 -->
  15. <button class="weui_btn weui_btn_primary" v-if="canIUseGetUserProfile" @tap="getUserProfile"> 微信登录 </button>
  16. <button class="weui_btn weui_btn_primary" v-else open-type="getUserInfo" @getuserinfo="bindGetUserInfo">
  17. 微信登录
  18. </button>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. const util = require("@/utils/util.js")
  24. const api = require('@/utils/api.js');
  25. export default {
  26. data() {
  27. return {
  28. canIUseGetUserProfile: false,
  29. navUrl: '',
  30. code: ''
  31. }
  32. },
  33. methods: {
  34. getUserProfile() {
  35. let that = this;
  36. // 推荐使用uni.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  37. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  38. uni.getUserProfile({
  39. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  40. success: (resp) => {
  41. //登录远程服务器
  42. if (that.code) {
  43. //登录远程服务器
  44. that.loginByWeixin(resp)
  45. } else {
  46. uni.login({
  47. success: function(resp) {
  48. if (resp.code) {
  49. that.code = resp.code
  50. that.loginByWeixin(resp)
  51. }
  52. }
  53. });
  54. }
  55. }
  56. })
  57. },
  58. bindGetUserInfo: function(e) {
  59. //登录远程服务器
  60. this.loginByWeixin(e.detail)
  61. },
  62. loginByWeixin: function(userInfo) {
  63. let that = this;
  64. //登录远程服务器
  65. if (that.code) {
  66. util.request(api.AuthLoginByWeixin, {
  67. code: that.code,
  68. userInfo: userInfo
  69. }, 'POST', 'application/json').then(res => {
  70. if (res.errno === 0) {
  71. //存储用户信息
  72. uni.setStorageSync('userInfo', res.data.userInfo);
  73. uni.setStorageSync('X-Nideshop-Token', res.data.token);
  74. uni.setStorageSync('userId', res.data.userId);
  75. } else {
  76. // util.showErrorToast(res.errmsg)
  77. uni.showModal({
  78. title: '提示',
  79. content: res.errmsg,
  80. showCancel: false
  81. });
  82. }
  83. });
  84. }
  85. if (that.navUrl && that.navUrl == '/pages/index/index') {
  86. uni.switchTab({
  87. url: that.navUrl,
  88. })
  89. } else if (that.navUrl) {
  90. uni.redirectTo({
  91. url: that.navUrl,
  92. })
  93. }
  94. }
  95. },
  96. onLoad: function(options) {
  97. let that = this;
  98. if (uni.getStorageSync("navUrl")) {
  99. that.navUrl = uni.getStorageSync("navUrl")
  100. } else {
  101. that.navUrl = '/pages/index/index'
  102. }
  103. if (uni.getUserProfile) {
  104. that.canIUseGetUserProfile = true
  105. }
  106. uni.login({
  107. success: function(res) {
  108. if (res.code) {
  109. that.code = res.code
  110. }
  111. }
  112. });
  113. }
  114. }
  115. </script>
  116. <style lang="scss">
  117. view,
  118. text {
  119. font-family: PingFangSC-Light, helvetica, 'Heiti SC';
  120. font-size: 29rpx;
  121. color: #333;
  122. }
  123. .hd {
  124. display: flex;
  125. width: 100%;
  126. flex-direction: column;
  127. align-items: center;
  128. justify-content: center;
  129. }
  130. .hd .logo {
  131. width: 260rpx;
  132. height: 260rpx;
  133. margin-top: 40rpx;
  134. }
  135. .hd .title {
  136. text-align: center;
  137. font-size: 36rpx;
  138. color: #000;
  139. }
  140. .bd {
  141. width: 100%;
  142. padding: 50rpx;
  143. }
  144. .bd .top_line {
  145. width: 100%;
  146. height: 1rpx;
  147. background: #ccc;
  148. margin-top: 20rpx;
  149. margin-bottom: 50rpx;
  150. }
  151. .bd .m_name {
  152. display: block;
  153. font-size: 36rpx;
  154. color: #000;
  155. }
  156. .bd .s_name {
  157. margin-top: 25rpx;
  158. display: block;
  159. font-size: 34rpx;
  160. color: #8a8a8a;
  161. }
  162. .btn {
  163. padding: 120rpx 50rpx 0;
  164. }
  165. .weui_btn_primary {
  166. background-color: #04be02;
  167. }
  168. .weui_btn {
  169. position: relative;
  170. display: block;
  171. margin-left: auto;
  172. margin-right: auto;
  173. padding-left: 14px;
  174. padding-right: 14px;
  175. box-sizing: border-box;
  176. font-size: 18px;
  177. text-align: center;
  178. text-decoration: none;
  179. color: #fff;
  180. line-height: 2.33333333;
  181. border-radius: 5px;
  182. -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  183. overflow: hidden;
  184. }
  185. .weui_btn:after {
  186. content: " ";
  187. width: 200%;
  188. height: 200%;
  189. position: absolute;
  190. top: 0;
  191. left: 0;
  192. border: 1px solid rgba(0, 0, 0, 0.2);
  193. -webkit-transform: scale(0.5);
  194. -ms-transform: scale(0.5);
  195. transform: scale(0.5);
  196. -webkit-transform-origin: 0 0;
  197. -ms-transform-origin: 0 0;
  198. transform-origin: 0 0;
  199. box-sizing: border-box;
  200. border-radius: 10px;
  201. }
  202. </style>