login.vue 8.2 KB

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