login.vue 8.4 KB

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