login.vue 8.2 KB

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