loginMethods.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. };
  61. },
  62. created() {
  63. let obj = this;
  64. // #ifdef APP-PLUS
  65. let system = uni.getStorageSync('platform');
  66. console.log(system);
  67. // 判断是否为ios
  68. if (system == 'ios') {
  69. obj.is_ios = true;
  70. }
  71. // uni.getSystemInfo({
  72. // success(e) {
  73. // if (+e.system.split('.')[0] >= 13) {
  74. // obj.is_apple_login = true;
  75. // }
  76. // }
  77. // })
  78. // #endif
  79. },
  80. methods: {
  81. ...mapMutations('user', ['setUserInfo', 'login']),
  82. wecahtLogin(type) {
  83. let obj = this;
  84. // #ifdef H5
  85. let weichatBrowser = uni.getStorageSync('weichatBrowser');
  86. if (weichatBrowser) {
  87. loginWinxin();
  88. }
  89. // #endif
  90. // #ifdef APP-PLUS
  91. uni.login({
  92. provider: type,
  93. success(e) {
  94. console.log('授权返回结果',e);
  95. uni.getUserInfo({
  96. provider: type,
  97. success(es) {
  98. console.log('用户信息',es);
  99. if (type === 'weixin') {
  100. loginWx(es.userInfo)
  101. .then(e => {
  102. console.log(e,'回调数据');
  103. uni.setStorageSync('token', e.data.token);
  104. getUserInfo({}).then(e => {
  105. obj.login();
  106. // 保存返回用户数据
  107. obj.setUserInfo(e.data);
  108. obj.$emit('onLogin',e.data)
  109. }).catch((e)=>{
  110. console.log('错误',e);
  111. });
  112. })
  113. .catch(e => {
  114. console.log(e);
  115. uni.showModal({
  116. content: JSON.stringify(e),
  117. success() {},
  118. fail() {}
  119. });
  120. });
  121. }
  122. if (type === 'apple') {
  123. console.log(es.userInfo);
  124. applelogin({
  125. account: es.userInfo.openId,
  126. })
  127. .then(function(e) {
  128. console.log(e, 'token')
  129. uni.setStorageSync('token', e.data.token);
  130. getUserInfo({}).then(e => {
  131. obj.login();
  132. // 保存返回用户数据
  133. obj.setUserInfo(e.data);
  134. //成功跳转首页
  135. uni.switchTab({
  136. url: '/pages/index/index'
  137. });
  138. });
  139. })
  140. .catch(function(e) {
  141. console.log(e);
  142. });
  143. }
  144. },
  145. fail(es) {
  146. console.log(es,'错误');
  147. uni.showModal({
  148. content: JSON.stringify(es),
  149. success() {
  150. // obj.login();
  151. // // 保存返回用户数据
  152. // obj.setUserInfo(e.data);
  153. // //成功跳转首页
  154. // uni.switchTab({
  155. // url: '/pages/index/index'
  156. // });
  157. }
  158. });
  159. }
  160. });
  161. },
  162. fail(e) {
  163. console.log(e,'错误');
  164. uni.showModal({
  165. title: '提示',
  166. content: JSON.stringify(e),
  167. showCancel: false
  168. });
  169. }
  170. });
  171. // #endif
  172. },
  173. },
  174. };
  175. </script>
  176. <style lang="scss">
  177. .other {
  178. margin-top: 60rpx;
  179. .fenge {
  180. width: 30%;
  181. height: 2rpx;
  182. background-color: #eeeeee;
  183. }
  184. .qita {
  185. font-size: 28rpx;
  186. color: #999999;
  187. }
  188. }
  189. .weixin {
  190. width: 75rpx;
  191. height: 75rpx;
  192. margin: 25rpx auto;
  193. image {
  194. width: 100%;
  195. height: 100%;
  196. }
  197. }
  198. .weixin_text {
  199. text-align: center;
  200. font-size: 28rpx;
  201. color: #999999;
  202. }
  203. /* #ifdef APP-PLUS */
  204. .ios_login {
  205. width: 260rpx;
  206. border-radius: 12rpx;
  207. justify-content: center;
  208. border: 1px solid #212121;
  209. margin: 24rpx auto;
  210. padding: 10rpx;
  211. background-color: #FFFFFF;
  212. .loginIcon {
  213. width: 50rpx;
  214. height: 50rpx;
  215. }
  216. .weixin_text {
  217. line-height: 1;
  218. margin-left: 20rpx;
  219. font-size: 24rpx !important;
  220. color: #212121 !important;
  221. }
  222. }
  223. /* #endif */
  224. /* #ifdef H5 */
  225. .ios_login {
  226. width: 350rpx;
  227. border-radius: 12rpx;
  228. justify-content: center;
  229. border: 1px solid #212121;
  230. margin: 24rpx auto;
  231. padding: 15rpx;
  232. background-color: #212121;
  233. color: #ffffff;
  234. font-size: 32rpx;
  235. .loginIcon {
  236. font-size: 35rpx;
  237. width: 35rpx;
  238. height: 35rpx;
  239. }
  240. .weixin_text {
  241. line-height: 1;
  242. margin-left: 20rpx;
  243. color: #ffffff !important;
  244. }
  245. }
  246. /* #endif */
  247. </style>