login.vue 8.6 KB

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