login.vue 9.6 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="../set/password">
  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"><image src="/static/img/img05.png"></image></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"><image src="/static/img/img05.png" mode="scaleToFill"></image></view>
  40. <view class="weixin_text" @click="wecahtLogin">微信登录</view>
  41. </block>
  42. <block v-else>
  43. <view class="ios_login flex" @click="wecahtLogin('weixin')">
  44. <text class="iconfont iconweixin"></text>
  45. <text class="weixin_text">微信登录</text>
  46. </view>
  47. <view v-if='is_apple_login' class="ios_login flex" @click="wecahtLogin('apple')">
  48. <image class="loginIcon" src="/static/icon/appleIcon.png" mode=" scaleToFill"></image>
  49. <text class="weixin_text">通过Apple登录</text>
  50. </view>
  51. </block> -->
  52. <!-- #endif -->
  53. </view>
  54. </view>
  55. </template>
  56. <script>
  57. import {
  58. mapMutations
  59. } from 'vuex';
  60. import {
  61. login
  62. } from '@/api/login.js';
  63. import {
  64. getUserInfo
  65. } from '@/api/user.js';
  66. // #ifdef APP-PLUS
  67. // applelogin接口需要开发编写,基础项目中可能没有
  68. import {
  69. applelogin
  70. } from '@/api/set.js';
  71. // loginWx接口需要开发编写,基础项目中可能没有
  72. import {
  73. loginWx
  74. } from '@/api/login.js';
  75. // #endif
  76. // #ifdef H5
  77. import {
  78. loginWinxin
  79. } from '@/utils/wxAuthorized';
  80. // #endif
  81. export default {
  82. data() {
  83. return {
  84. username: '',
  85. passward: '',
  86. // #ifdef APP-PLUS
  87. is_ios: false, //判断是否为ios手机
  88. is_apple_login: false, //是否有ios授权登录功能
  89. // #endif
  90. };
  91. },
  92. onLoad() {
  93. let obj = this;
  94. // #ifdef APP-PLUS
  95. let system = uni.getStorageSync('platform');
  96. // 判断是否为ios
  97. if (system == 'ios') {
  98. obj.is_ios = true;
  99. }
  100. uni.getSystemInfo({
  101. success(e) {
  102. if (+e.system.split('.')[0] >= 13) {
  103. obj.is_apple_login = true;
  104. }
  105. }
  106. })
  107. // #endif
  108. },
  109. methods: {
  110. ...mapMutations('user', ['setUserInfo', 'login']),
  111. // 微信登录
  112. wecahtLogin(type) {
  113. let obj = this;
  114. // #ifdef H5
  115. let weichatBrowser = uni.getStorageSync('weichatBrowser');
  116. if (weichatBrowser) {
  117. loginWinxin();
  118. }
  119. // #endif
  120. // #ifdef APP-PLUS
  121. uni.login({
  122. provider: type,
  123. success(e) {
  124. uni.getUserInfo({
  125. provider: type,
  126. success(es) {
  127. if (type === 'weixin') {
  128. loginWx(es.userInfo)
  129. .then(e => {
  130. uni.setStorageSync('token', e.data.token);
  131. getUserInfo({}).then(e => {
  132. obj.login();
  133. // 保存返回用户数据
  134. obj.setUserInfo(e.data);
  135. //成功跳转首页
  136. uni.switchTab({
  137. url: '/pages/index/index'
  138. });
  139. });
  140. })
  141. .catch(e => {
  142. console.log(e);
  143. uni.showModal({
  144. content: JSON.stringify(e),
  145. success() {},
  146. fail() {}
  147. });
  148. });
  149. }
  150. if (type === 'apple') {
  151. console.log(es.userInfo);
  152. applelogin({
  153. account: es.userInfo.openId,
  154. })
  155. .then(function(e) {
  156. console.log(e, 'token')
  157. uni.setStorageSync('token', e.data.token);
  158. getUserInfo({}).then(e => {
  159. obj.login();
  160. // 保存返回用户数据
  161. obj.setUserInfo(e.data);
  162. //成功跳转首页
  163. uni.switchTab({
  164. url: '/pages/index/index'
  165. });
  166. });
  167. })
  168. .catch(function(e) {
  169. console.log(e);
  170. });
  171. }
  172. },
  173. fail(es) {
  174. uni.showModal({
  175. content: JSON.stringify(es),
  176. success() {
  177. // obj.login();
  178. // // 保存返回用户数据
  179. // obj.setUserInfo(e.data);
  180. // //成功跳转首页
  181. // uni.switchTab({
  182. // url: '/pages/index/index'
  183. // });
  184. }
  185. });
  186. }
  187. });
  188. },
  189. fail(e) {
  190. uni.showModal({
  191. title: '提示',
  192. content: JSON.stringify(e),
  193. showCancel: false
  194. });
  195. }
  196. });
  197. // #endif
  198. },
  199. //登录
  200. async toLogin() {
  201. let obj = this;
  202. obj.logining = true;
  203. if (obj.username == '') {
  204. obj.$api.msg('请输入手机号');
  205. return;
  206. }
  207. if (obj.passward == '') {
  208. obj.$api.msg('请输入密码');
  209. return;
  210. }
  211. login({
  212. account: obj.username,
  213. password: obj.passward
  214. })
  215. .then(function(e) {
  216. console.log(e.data.token, "123456");
  217. uni.setStorageSync('token', e.data.token);
  218. // obj.$store.commit('hasLogin', true);
  219. getUserInfo({}).then(e => {
  220. obj.login();
  221. // 保存返回用户数据
  222. obj.setUserInfo(e.data);
  223. // let ur = uni.getStorageSync('present') || '/pages/index/index';
  224. uni.switchTab({
  225. url: '/pages/index/index'
  226. })
  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: #5dbc7c;
  379. margin: 40rpx 10rpx;
  380. border-radius: 50rpx;
  381. }
  382. .uni-button-green-plain {
  383. border: 1px solid #5dbc7c;
  384. margin: 40rpx 10rpx;
  385. border-radius: 50rpx;
  386. color: #5dbc7c;
  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>