login.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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">
  42. <image src="/static/img/img05.png" mode="scaleToFill"></image>
  43. </view>
  44. <view class="weixin_text" @click="wecahtLogin">微信登录</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. let obj = this;
  118. // #ifdef H5
  119. let weichatBrowser = uni.getStorageSync('weichatBrowser');
  120. if (weichatBrowser) {
  121. loginWinxin();
  122. }
  123. // #endif
  124. // #ifdef APP-PLUS
  125. uni.login({
  126. provider: type,
  127. success(e) {
  128. uni.getUserInfo({
  129. provider: type,
  130. success(es) {
  131. if (type === 'weixin') {
  132. loginWx(es.userInfo)
  133. .then((e) => {
  134. uni.setStorageSync('token', e.data.token);
  135. getUserInfo({}).then(e => {
  136. obj.login();
  137. // 保存返回用户数据
  138. obj.setUserInfo(e.data);
  139. //成功跳转首页
  140. uni.switchTab({
  141. url: '/pages/index/index'
  142. });
  143. });
  144. })
  145. .catch(e => {
  146. console.log(e);
  147. uni.showModal({
  148. content: JSON.stringify(e),
  149. success() {},
  150. fail() {}
  151. });
  152. });
  153. }
  154. if (type === 'apple') {
  155. console.log(es.userInfo);
  156. applelogin({
  157. account: es.userInfo.openId,
  158. })
  159. .then(function(e) {
  160. console.log(e, 'token')
  161. uni.setStorageSync('token', e.data.token);
  162. getUserInfo({}).then(e => {
  163. obj.login();
  164. // 保存返回用户数据
  165. obj.setUserInfo(e.data);
  166. //成功跳转首页
  167. uni.switchTab({
  168. url: '/pages/index/index'
  169. });
  170. });
  171. })
  172. .catch(function(e) {
  173. console.log(e);
  174. });
  175. }
  176. },
  177. fail(es) {
  178. uni.showModal({
  179. content: JSON.stringify(es),
  180. success() {
  181. // obj.login();
  182. // // 保存返回用户数据
  183. // obj.setUserInfo(e.data);
  184. // //成功跳转首页
  185. // uni.switchTab({
  186. // url: '/pages/index/index'
  187. // });
  188. }
  189. });
  190. }
  191. });
  192. },
  193. fail(e) {
  194. uni.showModal({
  195. title: '提示',
  196. content: JSON.stringify(e),
  197. showCancel: false
  198. });
  199. }
  200. });
  201. // #endif
  202. },
  203. //登录
  204. async toLogin() {
  205. let obj = this;
  206. obj.logining = true;
  207. if (obj.username == '') {
  208. obj.$api.msg('请输入手机号');
  209. return;
  210. }
  211. if (obj.passward == '') {
  212. obj.$api.msg('请输入密码');
  213. return;
  214. }
  215. login({
  216. account: obj.username,
  217. password: obj.passward
  218. })
  219. .then(function(e) {
  220. uni.setStorageSync('token', e.data.token);
  221. obj.$store.commit('hasLogin', true);
  222. getUserInfo({}).then(e => {
  223. obj.login();
  224. // 保存返回用户数据
  225. obj.setUserInfo(e.data);
  226. let ur = uni.getStorageSync('present') || '/pages/index/index';
  227. //成功跳转首页
  228. uni.switchTab({
  229. url: ur,
  230. fail(e) {
  231. uni.navigateTo({
  232. url: ur,
  233. fail(e) {
  234. uni.navigateTo({
  235. url: '/pages/index/index',
  236. });
  237. }
  238. });
  239. }
  240. });
  241. });
  242. })
  243. .catch(function(e) {
  244. console.log(e);
  245. });
  246. },
  247. //跳转注册页
  248. register() {
  249. uni.navigateTo({
  250. url: `/pages/public/register`
  251. });
  252. },
  253. // 后退
  254. navBack() {
  255. uni.navigateBack();
  256. }
  257. }
  258. };
  259. </script>
  260. <style lang="scss">
  261. /* #ifdef APP-PLUS */
  262. .ios_login {
  263. width: 260rpx;
  264. border-radius: 12rpx;
  265. justify-content: center;
  266. border: 1px solid #212121;
  267. margin: 24rpx auto;
  268. padding: 10rpx;
  269. background-color: #212121;
  270. color: #ffffff;
  271. .loginIcon {
  272. width: 50rpx;
  273. height: 50rpx;
  274. }
  275. .weixin_text {
  276. line-height: 1;
  277. margin-left: 20rpx;
  278. color: #ffffff !important;
  279. }
  280. }
  281. /* #endif */
  282. .ios_login {
  283. width: 350rpx;
  284. border-radius: 12rpx;
  285. justify-content: center;
  286. border: 1px solid #212121;
  287. margin: 24rpx auto;
  288. padding: 15rpx;
  289. background-color: #212121;
  290. color: #ffffff;
  291. font-size: 32rpx;
  292. .loginIcon {
  293. font-size: 35rpx;
  294. width: 35rpx;
  295. height: 35rpx;
  296. }
  297. .weixin_text {
  298. line-height: 1;
  299. margin-left: 20rpx;
  300. color: #ffffff !important;
  301. }
  302. }
  303. page {
  304. height: 100%;
  305. }
  306. .container {
  307. width: 100%;
  308. height: 100%;
  309. background-size: 100%;
  310. }
  311. .container_text {
  312. width: 100%;
  313. height: 500rpx;
  314. top: 0rpx;
  315. .banner-img {
  316. width: 100%;
  317. height: 100%;
  318. }
  319. }
  320. .login_text {
  321. margin: auto 30rpx;
  322. position: relative;
  323. padding: 100rpx 102rpx;
  324. background-color: #ffffff;
  325. margin-top: -180rpx;
  326. border-radius: 20rpx;
  327. .login_input {
  328. border-bottom: 1px solid #f0f0f0;
  329. margin-bottom: 65rpx;
  330. .login_img image {
  331. height: 35rpx;
  332. width: 29rpx;
  333. margin-right: 20rpx;
  334. }
  335. .uni-input {
  336. text-align: left;
  337. width: 470rpx;
  338. font-size: 28rpx !important;
  339. }
  340. .login_name {
  341. color: #333333;
  342. }
  343. }
  344. .other {
  345. margin-top: 60rpx;
  346. .fenge {
  347. width: 30%;
  348. height: 2rpx;
  349. background-color: #eeeeee;
  350. }
  351. .qita {
  352. font-size: 28rpx;
  353. color: #999999;
  354. }
  355. }
  356. .weixin {
  357. width: 75rpx;
  358. height: 75rpx;
  359. margin: 25rpx auto;
  360. }
  361. .weixin image {
  362. width: 100%;
  363. height: 100%;
  364. }
  365. .weixin_text {
  366. text-align: center;
  367. font-size: 28rpx;
  368. color: #999999;
  369. }
  370. .forget {
  371. font-size: 28rpx;
  372. width: 100%;
  373. text-align: right;
  374. color: #999999;
  375. }
  376. .uni-button-green {
  377. color: #ffffff;
  378. background-color: $base-color;
  379. margin: 40rpx 10rpx;
  380. border-radius: 50rpx;
  381. }
  382. .uni-button-green-plain {
  383. border: 1px solid $base-color;
  384. margin: 40rpx 10rpx;
  385. border-radius: 50rpx;
  386. color: $base-color;
  387. background-color: #ffffff;
  388. }
  389. .uni-button {
  390. height: 85rpx;
  391. line-height: 85rpx;
  392. }
  393. }
  394. .loginTitle {
  395. position: absolute;
  396. top: 250rpx;
  397. width: 100%;
  398. text-align: center;
  399. color: white;
  400. font-size: 40rpx;
  401. }
  402. </style>