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