login.vue 9.3 KB

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