login.vue 9.2 KB

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