login.vue 9.2 KB

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