login.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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"><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. //成功跳转首页
  225. uni.switchTab({
  226. url: ur,
  227. fail(e) {
  228. uni.navigateTo({
  229. url: ur,
  230. fail(e) {
  231. uni.navigateTo({
  232. url: '/pages/index/index',
  233. });
  234. }
  235. });
  236. }
  237. });
  238. });
  239. })
  240. .catch(function(e) {
  241. console.log(e);
  242. });
  243. },
  244. //跳转注册页
  245. register() {
  246. uni.navigateTo({
  247. url: `/pages/public/register`
  248. });
  249. },
  250. // 后退
  251. navBack() {
  252. uni.navigateBack();
  253. }
  254. }
  255. };
  256. </script>
  257. <style lang="scss">
  258. /* #ifdef APP-PLUS */
  259. .ios_login {
  260. width: 260rpx;
  261. border-radius: 12rpx;
  262. justify-content: center;
  263. border: 1px solid #212121;
  264. margin: 24rpx auto;
  265. padding: 10rpx;
  266. background-color: #212121;
  267. color: #ffffff;
  268. .loginIcon {
  269. width: 50rpx;
  270. height: 50rpx;
  271. }
  272. .weixin_text {
  273. line-height: 1;
  274. margin-left: 20rpx;
  275. color: #ffffff !important;
  276. }
  277. }
  278. /* #endif */
  279. .ios_login {
  280. width: 350rpx;
  281. border-radius: 12rpx;
  282. justify-content: center;
  283. border: 1px solid #212121;
  284. margin: 24rpx auto;
  285. padding: 15rpx;
  286. background-color: #212121;
  287. color: #ffffff;
  288. font-size: 32rpx;
  289. .loginIcon {
  290. font-size: 35rpx;
  291. width: 35rpx;
  292. height: 35rpx;
  293. }
  294. .weixin_text {
  295. line-height: 1;
  296. margin-left: 20rpx;
  297. color: #ffffff !important;
  298. }
  299. }
  300. page {
  301. height: 100%;
  302. }
  303. .container {
  304. width: 100%;
  305. height: 100%;
  306. background-size: 100%;
  307. }
  308. .container_text {
  309. width: 100%;
  310. height: 500rpx;
  311. top: 0rpx;
  312. .banner-img {
  313. width: 100%;
  314. height: 100%;
  315. }
  316. }
  317. .login_text {
  318. margin: auto 30rpx;
  319. position: relative;
  320. padding: 100rpx 102rpx;
  321. background-color: #ffffff;
  322. margin-top: -180rpx;
  323. border-radius: 20rpx;
  324. .login_input {
  325. border-bottom: 1px solid #f0f0f0;
  326. margin-bottom: 65rpx;
  327. .login_img image {
  328. height: 35rpx;
  329. width: 29rpx;
  330. margin-right: 20rpx;
  331. }
  332. .uni-input {
  333. text-align: left;
  334. width: 470rpx;
  335. font-size: 28rpx !important;
  336. }
  337. .login_name {
  338. color: #333333;
  339. }
  340. }
  341. .other {
  342. margin-top: 60rpx;
  343. .fenge {
  344. width: 30%;
  345. height: 2rpx;
  346. background-color: #eeeeee;
  347. }
  348. .qita {
  349. font-size: 28rpx;
  350. color: #999999;
  351. }
  352. }
  353. .weixin {
  354. width: 75rpx;
  355. height: 75rpx;
  356. margin: 25rpx auto;
  357. }
  358. .weixin image {
  359. width: 100%;
  360. height: 100%;
  361. }
  362. .weixin_text {
  363. text-align: center;
  364. font-size: 28rpx;
  365. color: #999999;
  366. }
  367. .forget {
  368. font-size: 28rpx;
  369. width: 100%;
  370. text-align: right;
  371. color: #999999;
  372. }
  373. .uni-button-green {
  374. color: #ffffff;
  375. background-color: #5dbc7c;
  376. margin: 40rpx 10rpx;
  377. border-radius: 50rpx;
  378. }
  379. .uni-button-green-plain {
  380. border: 1px solid #5dbc7c;
  381. margin: 40rpx 10rpx;
  382. border-radius: 50rpx;
  383. color: #5dbc7c;
  384. background-color: #ffffff;
  385. }
  386. .uni-button {
  387. height: 85rpx;
  388. line-height: 85rpx;
  389. }
  390. }
  391. .loginTitle {
  392. position: absolute;
  393. top: 250rpx;
  394. width: 100%;
  395. text-align: center;
  396. color: white;
  397. font-size: 40rpx;
  398. }
  399. </style>