login.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <template>
  2. <view class="container">
  3. <view class="container_text">
  4. <!-- <image class="banner-img" src="/static/img/img01.png" mode="scaleToFill"> -->
  5. </image>
  6. </view>
  7. <view class="loginTitle"><text>登录</text></view>
  8. <view class="login_text">
  9. <view class="login_input flex">
  10. <view class="login_img"><image src="/static/icon/img03.png"></image></view>
  11. <view class="login_name"><input class="uni-input" v-model="username" focus placeholder="请输入手机号" /></view>
  12. </view>
  13. <view class="login_input flex">
  14. <view class="login_img"><image src="/static/icon/img04.png"></image></view>
  15. <view class="login_name"><input class="uni-input" type="password" v-model="passward" focus placeholder="请输入密码" /></view>
  16. </view>
  17. <view><button type="green" class="uni-button uni-button-green" @click="toLogin">登录</button></view>
  18. <view><button type="green" class="uni-button uni-button-green uni-button-green-plain" plain="true" hover-class="none" @click="register">注册</button></view>
  19. <navigator url="./forget"><view class="forget">忘记密码</view></navigator>
  20. <view class="flex other">
  21. <view class="fenge"></view>
  22. <view class="qita">其他方式登录</view>
  23. <view class="fenge"></view>
  24. </view>
  25. <!-- #ifndef APP-PLUS -->
  26. <view class="weixin" @click="wecahtLogin"><image src="/static/img/img05.png"></image></view>
  27. <view class="weixin_text" @click="wecahtLogin">微信登录</view>
  28. <!-- #endif -->
  29. <!-- #ifdef APP-PLUS -->
  30. <block v-if="!is_ios">
  31. <view class="weixin" @click="wecahtLogin"><image src="/static/img/img05.png" mode="scaleToFill"></image></view>
  32. <view class="weixin_text" @click="wecahtLogin">微信登录</view>
  33. </block>
  34. <block v-else>
  35. <view class="ios_login flex" @click="wecahtLogin('weixin')">
  36. <text class="iconfont iconweixin"></text>
  37. <text class="weixin_text">微信登录</text>
  38. </view>
  39. <view v-if="is_apple_login" class="ios_login flex" @click="wecahtLogin('apple')">
  40. <image class="loginIcon" src="/static/icon/appleIcon.png" mode=" scaleToFill"></image>
  41. <text class="weixin_text">通过Apple登录</text>
  42. </view>
  43. </block>
  44. <!-- #endif -->
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import { mapMutations } from 'vuex';
  50. import { login } from '@/api/login.js';
  51. import { getUserInfo } from '@/api/user.js';
  52. // #ifdef APP-PLUS
  53. // applelogin接口需要开发编写,基础项目中可能没有
  54. import { applelogin } from '@/api/set.js';
  55. // loginWx接口需要开发编写,基础项目中可能没有
  56. import { loginWx } from '@/api/login.js';
  57. // #endif
  58. // #ifdef H5
  59. import { loginWinxin } from '@/utils/wxAuthorized';
  60. // #endif
  61. export default {
  62. data() {
  63. return {
  64. username: '',
  65. passward: '',
  66. // #ifdef APP-PLUS
  67. is_ios: false, //判断是否为ios手机
  68. is_apple_login: false //是否有ios授权登录功能
  69. // #endif
  70. };
  71. },
  72. onLoad() {
  73. let obj = this;
  74. // #ifdef APP-PLUS
  75. let system = uni.getStorageSync('platform');
  76. // 判断是否为ios
  77. if (system == 'ios') {
  78. obj.is_ios = true;
  79. }
  80. uni.getSystemInfo({
  81. success(e) {
  82. if (+e.system.split('.')[0] >= 13) {
  83. obj.is_apple_login = true;
  84. }
  85. }
  86. });
  87. // #endif
  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('token', e.data.token)
  197. getUserInfo({}).then(e => {
  198. obj.login();
  199. // 保存返回用户数据
  200. obj.setUserInfo(e.data);
  201. let ur = '/pages/index/index';
  202. //成功跳转首页
  203. uni.switchTab({
  204. url: ur,
  205. fail(e) {
  206. uni.navigateTo({
  207. url: ur,
  208. fail(e) {
  209. uni.navigateTo({
  210. url: '/pages/index/index'
  211. });
  212. }
  213. });
  214. }
  215. });
  216. });
  217. })
  218. .catch(function(e) {
  219. console.log(e);
  220. });
  221. },
  222. //跳转注册页
  223. register() {
  224. uni.navigateTo({
  225. url: `/pages/public/register`
  226. });
  227. },
  228. // 后退
  229. navBack() {
  230. uni.navigateBack();
  231. }
  232. }
  233. };
  234. </script>
  235. <style lang="scss">
  236. /* #ifdef APP-PLUS */
  237. .ios_login {
  238. width: 260rpx;
  239. border-radius: 12rpx;
  240. justify-content: center;
  241. border: 1px solid #212121;
  242. margin: 24rpx auto;
  243. padding: 10rpx;
  244. background-color: #212121;
  245. color: #ffffff;
  246. .loginIcon {
  247. width: 50rpx;
  248. height: 50rpx;
  249. }
  250. .weixin_text {
  251. line-height: 1;
  252. margin-left: 20rpx;
  253. color: #ffffff !important;
  254. }
  255. }
  256. /* #endif */
  257. .ios_login {
  258. width: 350rpx;
  259. border-radius: 12rpx;
  260. justify-content: center;
  261. border: 1px solid #212121;
  262. margin: 24rpx auto;
  263. padding: 15rpx;
  264. background-color: #212121;
  265. color: #ffffff;
  266. font-size: 32rpx;
  267. .loginIcon {
  268. font-size: 35rpx;
  269. width: 35rpx;
  270. height: 35rpx;
  271. }
  272. .weixin_text {
  273. line-height: 1;
  274. margin-left: 20rpx;
  275. color: #ffffff !important;
  276. }
  277. }
  278. page {
  279. height: 100%;
  280. }
  281. .container {
  282. width: 100%;
  283. height: 100%;
  284. background-size: 100%;
  285. }
  286. .container_text {
  287. width: 100%;
  288. height: 500rpx;
  289. top: 0rpx;
  290. background-color: #ff4c4c;
  291. .banner-img {
  292. width: 100%;
  293. height: 100%;
  294. }
  295. }
  296. .login_text {
  297. margin: auto 30rpx;
  298. position: relative;
  299. padding: 100rpx 102rpx;
  300. background-color: #ffffff;
  301. margin-top: -180rpx;
  302. border-radius: 20rpx;
  303. .login_input {
  304. border-bottom: 1px solid #f0f0f0;
  305. margin-bottom: 65rpx;
  306. .login_img image {
  307. height: 35rpx;
  308. width: 29rpx;
  309. margin-right: 20rpx;
  310. }
  311. .uni-input {
  312. text-align: left;
  313. width: 470rpx;
  314. font-size: 28rpx !important;
  315. }
  316. .login_name {
  317. color: #333333;
  318. }
  319. }
  320. .other {
  321. margin-top: 60rpx;
  322. .fenge {
  323. width: 30%;
  324. height: 2rpx;
  325. background-color: #eeeeee;
  326. }
  327. .qita {
  328. font-size: 28rpx;
  329. color: #999999;
  330. }
  331. }
  332. .weixin {
  333. width: 75rpx;
  334. height: 75rpx;
  335. margin: 25rpx auto;
  336. }
  337. .weixin image {
  338. width: 100%;
  339. height: 100%;
  340. }
  341. .weixin_text {
  342. text-align: center;
  343. font-size: 28rpx;
  344. color: #999999;
  345. }
  346. .forget {
  347. font-size: 28rpx;
  348. width: 100%;
  349. text-align: right;
  350. color: #999999;
  351. }
  352. .uni-button-green {
  353. color: #ffffff;
  354. background-color: #ff4c4c;
  355. margin: 40rpx 10rpx;
  356. border-radius: 50rpx;
  357. }
  358. .uni-button-green-plain {
  359. border: 1px solid #ff4c4c;
  360. margin: 40rpx 10rpx;
  361. border-radius: 50rpx;
  362. color: #ff4c4c;
  363. background-color: #ffffff;
  364. }
  365. .uni-button {
  366. height: 85rpx;
  367. line-height: 85rpx;
  368. }
  369. }
  370. .loginTitle {
  371. position: absolute;
  372. top: 250rpx;
  373. width: 100%;
  374. text-align: center;
  375. color: white;
  376. font-size: 40rpx;
  377. }
  378. </style>