loginMethods.vue 5.4 KB

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