loginMethods.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <view class="">
  3. <view class="flex other">
  4. <view class="fenge"></view>
  5. <view class="qita">其他方式登录</view>
  6. <view class="fenge"></view>
  7. </view>
  8. <!-- #ifndef APP-PLUS -->
  9. <view class="weixin" @click="wecahtLogin">
  10. <image src="../../static/icon/weichat.png" mode=""></image>
  11. </view>
  12. <view class="weixin_text" @click="wecahtLogin">微信登录</view>
  13. <!-- #endif -->
  14. <!-- #ifdef APP-PLUS -->
  15. <block v-if="!is_ios">
  16. <view class="weixin" @click="wecahtLogin('weixin')">
  17. <image src="../../static/icon/weichat.png" mode="scaleToFill"></image>
  18. </view>
  19. <view class="weixin_text" @click="wecahtLogin('weixin')">微信登录</view>
  20. </block>
  21. <block v-else>
  22. <view class="ios_login flex" @click="wecahtLogin('weixin')">
  23. <image class="loginIcon" src="../../static/icon/weichat.png" mode=" scaleToFill"></image>
  24. <text class="weixin_text">微信登录</text>
  25. </view>
  26. <view class="ios_login flex" @click="wecahtLogin('apple')">
  27. <image class="loginIcon" src="../../static/icon/appleIcon.png" mode=" scaleToFill"></image>
  28. <text class="weixin_text">Apple登录</text>
  29. </view>
  30. </block>
  31. <!-- #endif -->
  32. </view>
  33. </template>
  34. <script>
  35. import {
  36. mapMutations
  37. } from 'vuex';
  38. // #ifdef APP-PLUS
  39. // loginWx,applelogin接口需要开发编写,基础项目中可能没有
  40. import {
  41. loginWx,
  42. applelogin
  43. } from '@/api/login.js';
  44. // #endif
  45. // #ifdef H5
  46. import {
  47. loginWinxin
  48. } from '@/utils/wxAuthorized';
  49. // #endif
  50. import {
  51. getUserInfo
  52. } from '@/api/user.js';
  53. export default {
  54. data() {
  55. return {
  56. // #ifdef APP-PLUS
  57. is_ios: false, //判断是否为ios手机
  58. // is_apple_login: false, //是否有ios授权登录功能
  59. // #endif
  60. successUrl:'/pages/home/index'
  61. };
  62. },
  63. created() {
  64. let obj = this;
  65. // #ifdef APP-PLUS
  66. let system = uni.getStorageSync('platform');
  67. console.log(system);
  68. // 判断是否为ios
  69. if (system == 'ios') {
  70. obj.is_ios = true;
  71. }
  72. // uni.getSystemInfo({
  73. // success(e) {
  74. // if (+e.system.split('.')[0] >= 13) {
  75. // obj.is_apple_login = true;
  76. // }
  77. // }
  78. // })
  79. // #endif
  80. },
  81. methods: {
  82. ...mapMutations('user', ['setUserInfo', 'login']),
  83. wecahtLogin(type) {
  84. let obj = this;
  85. // #ifdef H5
  86. let weichatBrowser = uni.getStorageSync('weichatBrowser');
  87. if (weichatBrowser) {
  88. loginWinxin();
  89. }
  90. // #endif
  91. // #ifdef APP-PLUS
  92. uni.login({
  93. provider: type,
  94. success(e) {
  95. console.log('授权返回结果',e);
  96. uni.getUserInfo({
  97. provider: type,
  98. success(es) {
  99. console.log('用户信息',es);
  100. if (type === 'weixin') {
  101. loginWx(es.userInfo)
  102. .then(e => {
  103. console.log(e,'回调数据');
  104. uni.setStorageSync('token', e.data.token);
  105. getUserInfo({}).then(e => {
  106. obj.login();
  107. // 保存返回用户数据
  108. obj.setUserInfo(e.data);
  109. obj.$emit('onLogin',e.data)
  110. }).catch((e)=>{
  111. console.log('错误',e);
  112. });
  113. })
  114. .catch(e => {
  115. console.log(e);
  116. uni.showModal({
  117. content: JSON.stringify(e),
  118. success() {},
  119. fail() {}
  120. });
  121. });
  122. }
  123. if (type === 'apple') {
  124. console.log(es.userInfo);
  125. applelogin({
  126. account: es.userInfo.openId,
  127. })
  128. .then(function(e) {
  129. console.log(e, 'token')
  130. uni.setStorageSync('token', e.data.token);
  131. getUserInfo({}).then(e => {
  132. obj.login();
  133. // 保存返回用户数据
  134. obj.setUserInfo(e.data);
  135. //成功跳转首页
  136. uni.switchTab({
  137. url: obj.successUrl
  138. });
  139. });
  140. })
  141. .catch(function(e) {
  142. console.log(e);
  143. });
  144. }
  145. },
  146. fail(es) {
  147. console.log(es,'错误');
  148. uni.showModal({
  149. content: JSON.stringify(es),
  150. success() {
  151. // obj.login();
  152. // // 保存返回用户数据
  153. // obj.setUserInfo(e.data);
  154. // //成功跳转首页
  155. // uni.switchTab({
  156. // url: this.successUrl
  157. // });
  158. }
  159. });
  160. }
  161. });
  162. },
  163. fail(e) {
  164. console.log(e,'错误');
  165. uni.showModal({
  166. title: '提示',
  167. content: JSON.stringify(e),
  168. showCancel: false
  169. });
  170. }
  171. });
  172. // #endif
  173. },
  174. },
  175. };
  176. </script>
  177. <style lang="scss">
  178. .other {
  179. margin-top: 60rpx;
  180. .fenge {
  181. width: 30%;
  182. height: 2rpx;
  183. background-color: #eeeeee;
  184. }
  185. .qita {
  186. font-size: 28rpx;
  187. color: #999999;
  188. }
  189. }
  190. .weixin {
  191. width: 75rpx;
  192. height: 75rpx;
  193. margin: 25rpx auto;
  194. image {
  195. width: 100%;
  196. height: 100%;
  197. }
  198. }
  199. .weixin_text {
  200. text-align: center;
  201. font-size: 28rpx;
  202. color: #999999;
  203. }
  204. /* #ifdef APP-PLUS */
  205. .ios_login {
  206. width: 260rpx;
  207. border-radius: 12rpx;
  208. justify-content: center;
  209. border: 1px solid #212121;
  210. margin: 24rpx auto;
  211. padding: 10rpx;
  212. background-color: #FFFFFF;
  213. .loginIcon {
  214. width: 50rpx;
  215. height: 50rpx;
  216. }
  217. .weixin_text {
  218. line-height: 1;
  219. margin-left: 20rpx;
  220. font-size: 24rpx !important;
  221. color: #212121 !important;
  222. }
  223. }
  224. /* #endif */
  225. /* #ifdef H5 */
  226. .ios_login {
  227. width: 350rpx;
  228. border-radius: 12rpx;
  229. justify-content: center;
  230. border: 1px solid #212121;
  231. margin: 24rpx auto;
  232. padding: 15rpx;
  233. background-color: #212121;
  234. color: #ffffff;
  235. font-size: 32rpx;
  236. .loginIcon {
  237. font-size: 35rpx;
  238. width: 35rpx;
  239. height: 35rpx;
  240. }
  241. .weixin_text {
  242. line-height: 1;
  243. margin-left: 20rpx;
  244. color: #ffffff !important;
  245. }
  246. }
  247. /* #endif */
  248. </style>