login.vue 9.1 KB

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