login.vue 9.5 KB

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