login.vue 9.4 KB

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