login.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <view class="container">
  3. <view class="container_text"><image class="banner-img" src="/static/img/img01.png" mode="scaleToFill"></image></view>
  4. <view class="loginTitle"><text>登录</text></view>
  5. <view class="login_text">
  6. <view class="login_input flex">
  7. <view class="login_img"><image src="/static/icon/img03.png"></image></view>
  8. <view class="login_name"><input class="uni-input" v-model="username" focus placeholder="请输入手机号" /></view>
  9. </view>
  10. <view class="login_input flex">
  11. <view class="login_img"><image src="/static/icon/img04.png"></image></view>
  12. <view class="login_name"><input class="uni-input" type="password" v-model="passward" focus placeholder="请输入密码" /></view>
  13. </view>
  14. <view><button type="green" class="uni-button uni-button-green" @click="toLogin">登录</button></view>
  15. <view><button type="green" class="uni-button uni-button-green uni-button-green-plain" plain="true" hover-class="none" @click="register">注册</button></view>
  16. <navigator url="./forget"><view class="forget">忘记密码</view></navigator>
  17. <view class="flex other">
  18. <view class="fenge"></view>
  19. <view class="qita">其他方式登录</view>
  20. <view class="fenge"></view>
  21. </view>
  22. <!-- #ifndef APP-PLUS -->
  23. <view class="weixin" @click="wecahtLogin"><image src="/static/img/img05.png"></image></view>
  24. <view class="weixin_text" @click="wecahtLogin">微信登录</view>
  25. <!-- #endif -->
  26. <!-- #ifdef APP-PLUS -->
  27. <block v-if="!is_ios">
  28. <view class="weixin" @click="wecahtLogin"><image src="/static/img/img05.png" mode="scaleToFill"></image></view>
  29. <view class="weixin_text" @click="wecahtLogin">微信登录</view>
  30. </block>
  31. <block v-else>
  32. <view class="ios_login flex" @click="wecahtLogin('weixin')">
  33. <text class="iconfont iconweixin"></text>
  34. <text class="weixin_text">微信登录</text>
  35. </view>
  36. <view v-if="is_apple_login" class="ios_login flex" @click="wecahtLogin('apple')">
  37. <image class="loginIcon" src="/static/icon/appleIcon.png" mode=" scaleToFill"></image>
  38. <text class="weixin_text">通过Apple登录</text>
  39. </view>
  40. </block>
  41. <!-- #endif -->
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. import { mapMutations } from 'vuex';
  47. import { login } from '@/api/login.js';
  48. import { getUserInfo } from '@/api/user.js';
  49. // #ifdef APP-PLUS
  50. // applelogin接口需要开发编写,基础项目中可能没有
  51. import { applelogin } from '@/api/set.js';
  52. // loginWx接口需要开发编写,基础项目中可能没有
  53. import { loginWx } from '@/api/login.js';
  54. // #endif
  55. // #ifdef H5
  56. import { loginWinxin } from '@/utils/wxAuthorized';
  57. // #endif
  58. export default {
  59. data() {
  60. return {
  61. username: '',
  62. passward: '',
  63. // #ifdef APP-PLUS
  64. is_ios: false, //判断是否为ios手机
  65. is_apple_login: false //是否有ios授权登录功能
  66. // #endif
  67. };
  68. },
  69. onLoad() {
  70. let obj = this;
  71. // #ifdef APP-PLUS
  72. let system = uni.getStorageSync('platform');
  73. // 判断是否为ios
  74. if (system == 'ios') {
  75. obj.is_ios = true;
  76. }
  77. uni.getSystemInfo({
  78. success(e) {
  79. if (+e.system.split('.')[0] >= 13) {
  80. obj.is_apple_login = true;
  81. }
  82. }
  83. });
  84. // #endif
  85. },
  86. methods: {
  87. ...mapMutations('user', ['setUserInfo', 'login']),
  88. // 微信登录
  89. wecahtLogin(type) {
  90. let obj = this;
  91. // #ifdef H5
  92. let weichatBrowser = uni.getStorageSync('weichatBrowser');
  93. if (weichatBrowser) {
  94. loginWinxin();
  95. }
  96. // #endif
  97. // #ifdef APP-PLUS
  98. uni.login({
  99. provider: type,
  100. success(e) {
  101. uni.getUserInfo({
  102. provider: type,
  103. success(es) {
  104. if (type === 'weixin') {
  105. loginWx(es.userInfo)
  106. .then(e => {
  107. uni.setStorageSync('token', e.data.token);
  108. getUserInfo({}).then(e => {
  109. obj.login();
  110. // 保存返回用户数据
  111. obj.setUserInfo(e.data);
  112. //成功跳转首页
  113. uni.switchTab({
  114. url: '/pages/index/index'
  115. });
  116. });
  117. })
  118. .catch(e => {
  119. console.log(e);
  120. uni.showModal({
  121. content: JSON.stringify(e),
  122. success() {},
  123. fail() {}
  124. });
  125. });
  126. }
  127. if (type === 'apple') {
  128. console.log(es.userInfo);
  129. applelogin({
  130. account: es.userInfo.openId
  131. })
  132. .then(function(e) {
  133. console.log(e, 'token');
  134. uni.setStorageSync('token', e.data.token);
  135. getUserInfo({}).then(e => {
  136. obj.login();
  137. // 保存返回用户数据
  138. obj.setUserInfo(e.data);
  139. //成功跳转首页
  140. uni.switchTab({
  141. url: '/pages/index/index'
  142. });
  143. });
  144. })
  145. .catch(function(e) {
  146. console.log(e);
  147. });
  148. }
  149. },
  150. fail(es) {
  151. uni.showModal({
  152. content: JSON.stringify(es),
  153. success() {
  154. // obj.login();
  155. // // 保存返回用户数据
  156. // obj.setUserInfo(e.data);
  157. // //成功跳转首页
  158. // uni.switchTab({
  159. // url: '/pages/index/index'
  160. // });
  161. }
  162. });
  163. }
  164. });
  165. },
  166. fail(e) {
  167. uni.showModal({
  168. title: '提示',
  169. content: JSON.stringify(e),
  170. showCancel: false
  171. });
  172. }
  173. });
  174. // #endif
  175. },
  176. //登录
  177. async toLogin() {
  178. let obj = this;
  179. obj.logining = true;
  180. if (obj.username == '') {
  181. obj.$api.msg('请输入手机号');
  182. return;
  183. }
  184. if (obj.passward == '') {
  185. obj.$api.msg('请输入密码');
  186. return;
  187. }
  188. login({
  189. account: obj.username,
  190. password: obj.passward
  191. })
  192. .then(function(e) {
  193. uni.setStorageSync('token', e.data.token);
  194. obj.$store.commit('hasLogin', true);
  195. getUserInfo({}).then(e => {
  196. obj.login();
  197. // 保存返回用户数据
  198. obj.setUserInfo(e.data);
  199. let ur = uni.getStorageSync('present') || '/pages/index/index';
  200. //成功跳转首页
  201. uni.switchTab({
  202. url: ur,
  203. fail(e) {
  204. uni.navigateTo({
  205. url: ur,
  206. fail(e) {
  207. uni.navigateTo({
  208. url: '/pages/index/index'
  209. });
  210. }
  211. });
  212. }
  213. });
  214. });
  215. })
  216. .catch(function(e) {
  217. console.log(e);
  218. });
  219. },
  220. //跳转注册页
  221. register() {
  222. uni.navigateTo({
  223. url: `/pages/public/register`
  224. });
  225. },
  226. // 后退
  227. navBack() {
  228. uni.navigateBack();
  229. }
  230. }
  231. };
  232. </script>
  233. <style lang="scss">
  234. /* #ifdef APP-PLUS */
  235. .ios_login {
  236. width: 260rpx;
  237. border-radius: 12rpx;
  238. justify-content: center;
  239. border: 1px solid #212121;
  240. margin: 24rpx auto;
  241. padding: 10rpx;
  242. background-color: #212121;
  243. color: #ffffff;
  244. .loginIcon {
  245. width: 50rpx;
  246. height: 50rpx;
  247. }
  248. .weixin_text {
  249. line-height: 1;
  250. margin-left: 20rpx;
  251. color: #ffffff !important;
  252. }
  253. }
  254. /* #endif */
  255. .ios_login {
  256. width: 350rpx;
  257. border-radius: 12rpx;
  258. justify-content: center;
  259. border: 1px solid #212121;
  260. margin: 24rpx auto;
  261. padding: 15rpx;
  262. background-color: #212121;
  263. color: #ffffff;
  264. font-size: 32rpx;
  265. .loginIcon {
  266. font-size: 35rpx;
  267. width: 35rpx;
  268. height: 35rpx;
  269. }
  270. .weixin_text {
  271. line-height: 1;
  272. margin-left: 20rpx;
  273. color: #ffffff !important;
  274. }
  275. }
  276. page {
  277. height: 100%;
  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: #5dbc7c;
  352. margin: 40rpx 10rpx;
  353. border-radius: 50rpx;
  354. }
  355. .uni-button-green-plain {
  356. border: 1px solid #5dbc7c;
  357. margin: 40rpx 10rpx;
  358. border-radius: 50rpx;
  359. color: #5dbc7c;
  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>