login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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">
  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"
  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">
  28. <view class="forget">忘记密码</view>
  29. </navigator> -->
  30. <view class="flex">
  31. <!-- #ifdef H5 -->
  32. <!-- <view class="forget" style="text-align: left;" @click="goDown">下载APP</view> -->
  33. <!-- #endif -->
  34. <view class="forget">
  35. <navigator url="/pages/set/password">忘记密码</navigator>
  36. </view>
  37. </view>
  38. <!-- <view class="flex other">
  39. <view class="fenge"></view>
  40. <view class="qita">其他方式登录</view>
  41. <view class="fenge"></view>
  42. </view> -->
  43. <!-- #ifndef APP-PLUS -->
  44. <!-- <view class="weixin" @click="wecahtLogin">
  45. <image src="/static/img/img05.png"></image>
  46. </view>
  47. <view class="weixin_text" @click="wecahtLogin">微信登录</view> -->
  48. <!-- #endif -->
  49. <!-- #ifdef APP-PLUS -->
  50. <!-- <block v-if="!is_ios">
  51. <view class="weixin" @click="wecahtLogin">
  52. <image src="/static/img/img05.png" mode="scaleToFill"></image>
  53. </view>
  54. <view class="weixin_text" @click="wecahtLogin">微信登录</view>
  55. </block>
  56. <block v-else>
  57. <view class="ios_login flex" @click="wecahtLogin('weixin')">
  58. <text class="iconfont iconweixin"></text>
  59. <text class="weixin_text">微信登录</text>
  60. </view>
  61. <view v-if='is_apple_login' class="ios_login flex" @click="wecahtLogin('apple')">
  62. <image class="loginIcon" src="/static/icon/appleIcon.png" mode=" scaleToFill"></image>
  63. <text class="weixin_text">通过Apple登录</text>
  64. </view>
  65. </block> -->
  66. <!-- #endif -->
  67. </view>
  68. </view>
  69. </template>
  70. <script>
  71. import {
  72. mapMutations
  73. } from 'vuex';
  74. import {
  75. login,
  76. getAppInfo
  77. } from '@/api/login.js';
  78. import {
  79. getUserInfo
  80. } from '@/api/user.js';
  81. // #ifdef APP-PLUS
  82. // applelogin接口需要开发编写,基础项目中可能没有
  83. import {
  84. applelogin
  85. } from '@/api/set.js';
  86. // loginWx接口需要开发编写,基础项目中可能没有
  87. import {
  88. loginWx,
  89. } from '@/api/login.js';
  90. // #endif
  91. // #ifdef H5
  92. import {
  93. loginWinxin
  94. } from '@/utils/wxAuthorized';
  95. // #endif
  96. export default {
  97. data() {
  98. return {
  99. deviceId: '',
  100. username: '',
  101. passward: '',
  102. // #ifdef APP-PLUS
  103. is_ios: false, //判断是否为ios手机
  104. is_apple_login: false, //是否有ios授权登录功能
  105. // #endif
  106. };
  107. },
  108. onLoad() {
  109. let obj = this;
  110. // #ifdef APP-PLUS
  111. let system = uni.getStorageSync('platform');
  112. // 判断是否为ios
  113. if (system == 'ios') {
  114. obj.is_ios = true;
  115. }
  116. uni.getSystemInfo({
  117. success(e) {
  118. if (+e.system.split('.')[0] >= 13) {
  119. obj.is_apple_login = true;
  120. }
  121. }
  122. })
  123. // #endif
  124. this.getDeviceMac()
  125. },
  126. methods: {
  127. ...mapMutations('user', ['setUserInfo', 'login']),
  128. goDown() {
  129. getAppInfo().then(res => {
  130. console.log(res)
  131. window.open(res.data.app_down)
  132. })
  133. },
  134. //获取设备的mac地址
  135. getDeviceMac() {
  136. let obj = this
  137. // #ifdef APP-PLUS
  138. let oaid = uni.getStorageSync('oaid')
  139. console.log('取值', oaid)
  140. if (oaid) {
  141. obj.deviceId = oaid
  142. } else {
  143. plus.device.getOAID({
  144. success: function(e) {
  145. obj.deviceId = e.oaid
  146. uni.setStorageSync('oaid', e.oaid)
  147. console.log('uuid', e.oaid)
  148. },
  149. fail: function(e) {
  150. console.log('getDeviceInfo failed: ' + JSON.stringify(e));
  151. }
  152. });
  153. }
  154. // #endif
  155. // #ifdef H5
  156. uni.getSystemInfo({
  157. success(e) {
  158. obj.deviceId = e.deviceId
  159. },
  160. fail(err) {
  161. console.log(err)
  162. }
  163. })
  164. // #endif
  165. },
  166. // 微信登录
  167. wecahtLogin(type) {
  168. let obj = this;
  169. // #ifdef H5
  170. let weichatBrowser = uni.getStorageSync('weichatBrowser');
  171. if (weichatBrowser) {
  172. loginWinxin();
  173. }
  174. // #endif
  175. // #ifdef APP-PLUS
  176. uni.login({
  177. provider: type,
  178. success(e) {
  179. uni.getUserInfo({
  180. provider: type,
  181. success(es) {
  182. if (type === 'weixin') {
  183. loginWx(es.userInfo)
  184. .then(e => {
  185. uni.setStorageSync('token', e.data.token);
  186. getUserInfo({}).then(e => {
  187. obj.login();
  188. // 保存返回用户数据
  189. obj.setUserInfo(e.data);
  190. //成功跳转首页
  191. uni.switchTab({
  192. url: '/pages/index/index'
  193. });
  194. });
  195. })
  196. .catch(e => {
  197. console.log(e);
  198. uni.showModal({
  199. content: JSON.stringify(e),
  200. success() {},
  201. fail() {}
  202. });
  203. });
  204. }
  205. if (type === 'apple') {
  206. console.log(es.userInfo);
  207. applelogin({
  208. account: es.userInfo.openId,
  209. })
  210. .then(function(e) {
  211. console.log(e, 'token')
  212. uni.setStorageSync('token', e.data.token);
  213. getUserInfo({}).then(e => {
  214. obj.login();
  215. // 保存返回用户数据
  216. obj.setUserInfo(e.data);
  217. //成功跳转首页
  218. uni.switchTab({
  219. url: '/pages/index/index'
  220. });
  221. });
  222. })
  223. .catch(function(e) {
  224. console.log(e);
  225. });
  226. }
  227. },
  228. fail(es) {
  229. uni.showModal({
  230. content: JSON.stringify(es),
  231. success() {
  232. // obj.login();
  233. // // 保存返回用户数据
  234. // obj.setUserInfo(e.data);
  235. // //成功跳转首页
  236. // uni.switchTab({
  237. // url: '/pages/index/index'
  238. // });
  239. }
  240. });
  241. }
  242. });
  243. },
  244. fail(e) {
  245. uni.showModal({
  246. title: '提示',
  247. content: JSON.stringify(e),
  248. showCancel: false
  249. });
  250. }
  251. });
  252. // #endif
  253. },
  254. //登录
  255. async toLogin() {
  256. let obj = this;
  257. obj.logining = true;
  258. if (obj.username == '') {
  259. obj.$api.msg('请输入手机号');
  260. return;
  261. }
  262. if (obj.passward == '') {
  263. obj.$api.msg('请输入密码');
  264. return;
  265. }
  266. login({
  267. account: obj.username,
  268. password: obj.passward,
  269. deviceId: obj.deviceId
  270. })
  271. .then(function(e) {
  272. uni.setStorageSync('token', e.data.token);
  273. obj.$store.commit('hasLogin', true);
  274. getUserInfo({}).then(e => {
  275. obj.login();
  276. // 保存返回用户数据
  277. obj.setUserInfo(e.data);
  278. let ur = uni.getStorageSync('present') || '/pages/index/index';
  279. //成功跳转首页
  280. uni.switchTab({
  281. url: ur,
  282. fail(e) {
  283. uni.navigateTo({
  284. url: ur,
  285. fail(e) {
  286. uni.navigateTo({
  287. url: '/pages/index/index',
  288. });
  289. }
  290. });
  291. }
  292. });
  293. });
  294. })
  295. .catch(function(e) {
  296. console.log(e);
  297. });
  298. },
  299. //跳转注册页
  300. register() {
  301. uni.navigateTo({
  302. url: `/pages/public/register`
  303. });
  304. },
  305. // 后退
  306. navBack() {
  307. uni.navigateBack();
  308. }
  309. }
  310. };
  311. </script>
  312. <style lang="scss">
  313. /* #ifdef APP-PLUS */
  314. .ios_login {
  315. width: 260rpx;
  316. border-radius: 12rpx;
  317. justify-content: center;
  318. border: 1px solid #212121;
  319. margin: 24rpx auto;
  320. padding: 10rpx;
  321. background-color: #212121;
  322. color: #ffffff;
  323. .loginIcon {
  324. width: 50rpx;
  325. height: 50rpx;
  326. }
  327. .weixin_text {
  328. line-height: 1;
  329. margin-left: 20rpx;
  330. color: #ffffff !important;
  331. }
  332. }
  333. /* #endif */
  334. .ios_login {
  335. width: 350rpx;
  336. border-radius: 12rpx;
  337. justify-content: center;
  338. border: 1px solid #212121;
  339. margin: 24rpx auto;
  340. padding: 15rpx;
  341. background-color: #212121;
  342. color: #ffffff;
  343. font-size: 32rpx;
  344. .loginIcon {
  345. font-size: 35rpx;
  346. width: 35rpx;
  347. height: 35rpx;
  348. }
  349. .weixin_text {
  350. line-height: 1;
  351. margin-left: 20rpx;
  352. color: #ffffff !important;
  353. }
  354. }
  355. page {
  356. height: 100%;
  357. }
  358. .container {
  359. width: 100%;
  360. height: 100%;
  361. background-size: 100%;
  362. }
  363. .container_text {
  364. width: 100%;
  365. height: 500rpx;
  366. top: 0rpx;
  367. .banner-img {
  368. width: 100%;
  369. height: 100%;
  370. background-color: $base-color;
  371. }
  372. }
  373. .login_text {
  374. margin: auto 30rpx;
  375. position: relative;
  376. padding: 100rpx 102rpx;
  377. background-color: #ffffff;
  378. margin-top: -180rpx;
  379. border-radius: 20rpx;
  380. .login_input {
  381. border-bottom: 1px solid #f0f0f0;
  382. margin-bottom: 65rpx;
  383. .login_img image {
  384. height: 35rpx;
  385. width: 29rpx;
  386. margin-right: 20rpx;
  387. }
  388. .uni-input {
  389. text-align: left;
  390. width: 470rpx;
  391. font-size: 28rpx !important;
  392. }
  393. .login_name {
  394. color: #333333;
  395. }
  396. }
  397. .other {
  398. margin-top: 60rpx;
  399. .fenge {
  400. width: 30%;
  401. height: 2rpx;
  402. background-color: #eeeeee;
  403. }
  404. .qita {
  405. font-size: 28rpx;
  406. color: #999999;
  407. }
  408. }
  409. .weixin {
  410. width: 75rpx;
  411. height: 75rpx;
  412. margin: 25rpx auto;
  413. }
  414. .weixin image {
  415. width: 100%;
  416. height: 100%;
  417. }
  418. .weixin_text {
  419. text-align: center;
  420. font-size: 28rpx;
  421. color: #999999;
  422. }
  423. .forget {
  424. font-size: 28rpx;
  425. width: 100%;
  426. text-align: right;
  427. color: #999999;
  428. }
  429. .uni-button-green {
  430. color: #ffffff;
  431. background-color: $base-color;
  432. margin: 40rpx 10rpx;
  433. border-radius: 50rpx;
  434. }
  435. .uni-button-green-plain {
  436. border: 1px solid $base-color;
  437. margin: 40rpx 10rpx;
  438. border-radius: 50rpx;
  439. color: $base-color;
  440. background-color: #ffffff;
  441. }
  442. .uni-button {
  443. height: 85rpx;
  444. line-height: 85rpx;
  445. }
  446. }
  447. .loginTitle {
  448. position: absolute;
  449. top: 250rpx;
  450. width: 100%;
  451. text-align: center;
  452. color: white;
  453. font-size: 40rpx;
  454. }
  455. </style>