login.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <template>
  2. <view class="container">
  3. <view class="container_text"><image class="banner-img" src="/static/img/img01.png" mode="scaleToFill"></image></view>
  4. <view class="loginTitle"><text>登录</text></view>
  5. <view class="login_text">
  6. <view class="login_input flex">
  7. <view class="login_img"><image src="/static/icon/img03.png"></image></view>
  8. <view class="login_name"><input class="uni-input" v-model="username" focus placeholder="请输入手机号" /></view>
  9. </view>
  10. <view class="login_input flex">
  11. <view class="login_img"><image src="/static/icon/img04.png"></image></view>
  12. <view class="login_name"><input class="uni-input" type="password" v-model="passward" focus placeholder="请输入密码" /></view>
  13. </view>
  14. <view><button type="green" class="uni-button uni-button-green" @click="toLogin">登录</button></view>
  15. <view>
  16. <button type="green" class="uni-button uni-button-green uni-button-green-plain" plain="true" hover-class="none" @click="register">注册</button></view>
  17. <navigator url="./forget"><view class="forget">忘记密码</view></navigator>
  18. <view class="flex other">
  19. <view class="fenge"></view>
  20. <view class="qita">其他方式登录</view>
  21. <view class="fenge"></view>
  22. </view>
  23. <!-- #ifndef APP-PLUS -->
  24. <view class="weixin" @click="wecahtLogin"><image src="/static/img/img05.png"></image></view>
  25. <view class="weixin_text" @click="wecahtLogin">微信登录</view>
  26. <!-- #endif -->
  27. <!-- #ifdef APP-PLUS -->
  28. <block v-if="!is_ios">
  29. <view class="weixin" @click="wecahtLogin"><image src="/static/img/img05.png" mode="scaleToFill"></image></view>
  30. <view class="weixin_text" @click="wecahtLogin('weixin')">微信登录</view>
  31. </block>
  32. <block v-else>
  33. <view class="ios_login flex" @click="wecahtLogin('weixin')">
  34. <text class="iconfont iconweixin"></text>
  35. <text class="weixin_text">微信登录</text>
  36. </view>
  37. <view v-if='is_apple_login' class="ios_login flex" @click="wecahtLogin('apple')">
  38. <image class="loginIcon" src="/static/static/icon/appleIcon.png" mode=" scaleToFill"></image>
  39. <text class="weixin_text">通过Apple登录</text>
  40. </view>
  41. </block>
  42. <!-- #endif -->
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. import { mapMutations } from 'vuex';
  48. import { login, getUserInfo, loginWx } from '@/api/login.js';
  49. import { applelogin } from '@/api/set.js';
  50. // #ifdef H5
  51. import { loginWinxin } from '@/utils/wxAuthorized';
  52. // #endif
  53. export default {
  54. data() {
  55. return {
  56. username: '',
  57. passward: '',
  58. // #ifdef APP-PLUS
  59. is_ios: false ,//判断是否为ios手机
  60. is_apple_login:false,//是否有ios授权登录功能
  61. // #endif
  62. };
  63. },
  64. onLoad() {
  65. let obj = this;
  66. // #ifdef APP-PLUS
  67. let system = uni.getStorageSync('platform');
  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. // 微信登录
  84. wecahtLogin(type) {
  85. let obj = this;
  86. // #ifdef H5
  87. let weichatBrowser = uni.getStorageSync('weichatBrowser');
  88. if (weichatBrowser) {
  89. loginWinxin();
  90. }
  91. // #endif
  92. // #ifdef APP-PLUS
  93. uni.login({
  94. provider: type,
  95. success(e) {
  96. uni.getUserInfo({
  97. provider: type,
  98. success(es) {
  99. if(type==='weixin'){
  100. loginWx(es.userInfo)
  101. .then(e => {
  102. uni.setStorageSync('token', e.data.token);
  103. getUserInfo({}).then(e => {
  104. obj.login();
  105. // 保存返回用户数据
  106. obj.setUserInfo(e.data);
  107. //成功跳转首页
  108. uni.switchTab({
  109. url: '/pages/index/index'
  110. });
  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. uni.showModal({
  147. content: JSON.stringify(es),
  148. success() {
  149. // obj.login();
  150. // // 保存返回用户数据
  151. // obj.setUserInfo(e.data);
  152. // //成功跳转首页
  153. // uni.switchTab({
  154. // url: '/pages/index/index'
  155. // });
  156. }
  157. });
  158. }
  159. });
  160. },
  161. fail(e) {
  162. uni.showModal({
  163. title: '提示',
  164. content: JSON.stringify(e),
  165. showCancel: false
  166. });
  167. }
  168. });
  169. // #endif
  170. },
  171. //登录
  172. async toLogin() {
  173. let obj = this;
  174. obj.logining = true;
  175. if (obj.username == '') {
  176. obj.$api.msg('请输入手机号');
  177. return;
  178. }
  179. if (obj.passward == '') {
  180. obj.$api.msg('请输入密码');
  181. return;
  182. }
  183. login({
  184. account: obj.username,
  185. password: obj.passward
  186. })
  187. .then(function(e) {
  188. uni.setStorageSync('token', e.data.token);
  189. getUserInfo({}).then(e => {
  190. obj.login();
  191. // 保存返回用户数据
  192. obj.setUserInfo(e.data);
  193. //成功跳转首页
  194. uni.switchTab({
  195. url: '/pages/index/index'
  196. });
  197. });
  198. })
  199. .catch(function(e) {
  200. console.log(e);
  201. });
  202. },
  203. //跳转注册页
  204. register() {
  205. uni.navigateTo({
  206. url: `/pages/public/register`
  207. });
  208. },
  209. // 后退
  210. navBack() {
  211. uni.navigateBack();
  212. }
  213. }
  214. };
  215. </script>
  216. <style lang="scss">
  217. /* #ifdef APP-PLUS */
  218. .ios_login {
  219. width: 260rpx;
  220. border-radius: 12rpx;
  221. justify-content: center;
  222. border: 1px solid #212121;
  223. margin: 24rpx auto;
  224. padding: 10rpx;
  225. background-color: #212121;
  226. color: #ffffff;
  227. .loginIcon {
  228. width: 50rpx;
  229. height: 50rpx;
  230. }
  231. .weixin_text {
  232. line-height: 1;
  233. margin-left: 20rpx;
  234. color: #ffffff !important;
  235. }
  236. }
  237. /* #endif */
  238. .ios_login {
  239. width: 350rpx;
  240. border-radius: 12rpx;
  241. justify-content: center;
  242. border: 1px solid #212121;
  243. margin: 24rpx auto;
  244. padding: 15rpx;
  245. background-color: #212121;
  246. color: #ffffff;
  247. font-size: 32rpx;
  248. .loginIcon {
  249. font-size: 35rpx;
  250. width: 35rpx;
  251. height: 35rpx;
  252. }
  253. .weixin_text {
  254. line-height: 1;
  255. margin-left: 20rpx;
  256. color: #ffffff !important;
  257. }
  258. }
  259. page {
  260. height: 100%;
  261. }
  262. .container {
  263. width: 100%;
  264. height: 100%;
  265. background-size: 100%;
  266. }
  267. .container_text {
  268. width: 100%;
  269. height: 500rpx;
  270. top: 0rpx;
  271. .banner-img {
  272. width: 100%;
  273. height: 100%;
  274. }
  275. }
  276. .login_text {
  277. margin: auto 30rpx;
  278. position: relative;
  279. padding: 100rpx 102rpx;
  280. background-color: #ffffff;
  281. margin-top: -180rpx;
  282. border-radius: 20rpx;
  283. .login_input {
  284. border-bottom: 1px solid #f0f0f0;
  285. margin-bottom: 65rpx;
  286. .login_img image {
  287. height: 35rpx;
  288. width: 29rpx;
  289. margin-right: 20rpx;
  290. }
  291. .uni-input {
  292. text-align: left;
  293. width: 470rpx;
  294. font-size: 28rpx !important;
  295. }
  296. .login_name {
  297. color: #333333;
  298. }
  299. }
  300. .other {
  301. margin-top: 60rpx;
  302. .fenge {
  303. width: 30%;
  304. height: 2rpx;
  305. background-color: #eeeeee;
  306. }
  307. .qita {
  308. font-size: 28rpx;
  309. color: #999999;
  310. }
  311. }
  312. .weixin {
  313. width: 75rpx;
  314. height: 75rpx;
  315. margin: 25rpx auto;
  316. }
  317. .weixin image {
  318. width: 100%;
  319. height: 100%;
  320. }
  321. .weixin_text {
  322. text-align: center;
  323. font-size: 28rpx;
  324. color: #999999;
  325. }
  326. .forget {
  327. font-size: 28rpx;
  328. width: 100%;
  329. text-align: right;
  330. color: #999999;
  331. }
  332. .uni-button-green {
  333. color: #ffffff;
  334. background-color: #5dbc7c;
  335. margin: 40rpx 10rpx;
  336. border-radius: 50rpx;
  337. }
  338. .uni-button-green-plain {
  339. border: 1px solid #5dbc7c;
  340. margin: 40rpx 10rpx;
  341. border-radius: 50rpx;
  342. color: #5dbc7c;
  343. background-color: #ffffff;
  344. }
  345. .uni-button {
  346. height: 85rpx;
  347. line-height: 85rpx;
  348. }
  349. }
  350. .loginTitle {
  351. position: absolute;
  352. top: 250rpx;
  353. width: 100%;
  354. text-align: center;
  355. color: white;
  356. font-size: 40rpx;
  357. }
  358. </style>