login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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" 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">
  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. obj.username = uni.getStorageSync('uname') || ''
  111. obj.passward = uni.getStorageSync('upass') || ''
  112. // #ifdef APP-PLUS
  113. let system = uni.getStorageSync('platform');
  114. // 判断是否为ios
  115. if (system == 'ios') {
  116. obj.is_ios = true;
  117. }
  118. uni.getSystemInfo({
  119. success(e) {
  120. if (+e.system.split('.')[0] >= 13) {
  121. obj.is_apple_login = true;
  122. }
  123. }
  124. })
  125. // #endif
  126. this.getDeviceMac()
  127. },
  128. methods: {
  129. ...mapMutations('user', ['setUserInfo', 'login']),
  130. goDown() {
  131. getAppInfo().then(res => {
  132. console.log(res)
  133. window.open(res.data.app_down)
  134. })
  135. },
  136. //获取设备的mac地址
  137. getDeviceMac() {
  138. let obj = this
  139. // #ifdef APP-PLUS
  140. let oaid = uni.getStorageSync('oaid')
  141. console.log('取值', oaid)
  142. if (oaid) {
  143. obj.deviceId = oaid
  144. } else {
  145. plus.device.getOAID({
  146. success: function(e) {
  147. obj.deviceId = e.oaid
  148. uni.setStorageSync('oaid', e.oaid)
  149. console.log('uuid', e.oaid)
  150. },
  151. fail: function(e) {
  152. console.log('getDeviceInfo failed: ' + JSON.stringify(e));
  153. }
  154. });
  155. }
  156. // #endif
  157. // #ifdef H5
  158. uni.getSystemInfo({
  159. success(e) {
  160. obj.deviceId = e.deviceId
  161. },
  162. fail(err) {
  163. console.log(err)
  164. }
  165. })
  166. // #endif
  167. },
  168. // 微信登录
  169. wecahtLogin(type) {
  170. let obj = this;
  171. // #ifdef H5
  172. let weichatBrowser = uni.getStorageSync('weichatBrowser');
  173. if (weichatBrowser) {
  174. loginWinxin();
  175. }
  176. // #endif
  177. // #ifdef APP-PLUS
  178. uni.login({
  179. provider: type,
  180. success(e) {
  181. uni.getUserInfo({
  182. provider: type,
  183. success(es) {
  184. if (type === 'weixin') {
  185. loginWx(es.userInfo)
  186. .then(e => {
  187. uni.setStorageSync('token', e.data.token);
  188. getUserInfo({}).then(e => {
  189. obj.login();
  190. // 保存返回用户数据
  191. obj.setUserInfo(e.data);
  192. //成功跳转首页
  193. uni.switchTab({
  194. url: '/pages/index/index'
  195. });
  196. });
  197. })
  198. .catch(e => {
  199. console.log(e);
  200. uni.showModal({
  201. content: JSON.stringify(e),
  202. success() {},
  203. fail() {}
  204. });
  205. });
  206. }
  207. if (type === 'apple') {
  208. console.log(es.userInfo);
  209. applelogin({
  210. account: es.userInfo.openId,
  211. })
  212. .then(function(e) {
  213. console.log(e, 'token')
  214. uni.setStorageSync('token', e.data.token);
  215. getUserInfo({}).then(e => {
  216. obj.login();
  217. // 保存返回用户数据
  218. obj.setUserInfo(e.data);
  219. //成功跳转首页
  220. uni.switchTab({
  221. url: '/pages/index/index'
  222. });
  223. });
  224. })
  225. .catch(function(e) {
  226. console.log(e);
  227. });
  228. }
  229. },
  230. fail(es) {
  231. uni.showModal({
  232. content: JSON.stringify(es),
  233. success() {
  234. // obj.login();
  235. // // 保存返回用户数据
  236. // obj.setUserInfo(e.data);
  237. // //成功跳转首页
  238. // uni.switchTab({
  239. // url: '/pages/index/index'
  240. // });
  241. }
  242. });
  243. }
  244. });
  245. },
  246. fail(e) {
  247. uni.showModal({
  248. title: '提示',
  249. content: JSON.stringify(e),
  250. showCancel: false
  251. });
  252. }
  253. });
  254. // #endif
  255. },
  256. //登录
  257. async toLogin() {
  258. let obj = this;
  259. obj.logining = true;
  260. if (obj.username == '') {
  261. obj.$api.msg('请输入手机号');
  262. return;
  263. }
  264. if (obj.passward == '') {
  265. obj.$api.msg('请输入密码');
  266. return;
  267. }
  268. login({
  269. account: obj.username,
  270. password: obj.passward,
  271. })
  272. .then(function(e) {
  273. // 保存账号密码
  274. uni.setStorageSync('uname',obj.username)
  275. uni.setStorageSync('upass',obj.passward)
  276. uni.setStorageSync('token', e.data.token);
  277. obj.$store.commit('hasLogin', true);
  278. getUserInfo({}).then(e => {
  279. obj.login();
  280. // 保存返回用户数据
  281. obj.setUserInfo(e.data);
  282. // #ifdef H5
  283. let weichatBrowser = uni.getStorageSync('weichatBrowser');
  284. if(weichatBrowser) {
  285. loginWinxin();
  286. }else {
  287. obj.goIndex()
  288. }
  289. // #endif
  290. // #ifndef H5
  291. obj.goIndex()
  292. // #endif
  293. });
  294. })
  295. .catch(function(e) {
  296. console.log(e);
  297. });
  298. },
  299. goIndex() {
  300. let ur = uni.getStorageSync('present') || '/pages/index/index';
  301. //成功跳转首页
  302. uni.switchTab({
  303. url: ur,
  304. fail(e) {
  305. uni.navigateTo({
  306. url: ur,
  307. fail(e) {
  308. uni.navigateTo({
  309. url: '/pages/index/index',
  310. });
  311. }
  312. });
  313. }
  314. });
  315. },
  316. //跳转注册页
  317. register() {
  318. uni.navigateTo({
  319. url: `/pages/public/register`
  320. });
  321. },
  322. // 后退
  323. navBack() {
  324. uni.navigateBack();
  325. }
  326. }
  327. };
  328. </script>
  329. <style lang="scss">
  330. /* #ifdef APP-PLUS */
  331. .ios_login {
  332. width: 260rpx;
  333. border-radius: 12rpx;
  334. justify-content: center;
  335. border: 1px solid #212121;
  336. margin: 24rpx auto;
  337. padding: 10rpx;
  338. background-color: #212121;
  339. color: #ffffff;
  340. .loginIcon {
  341. width: 50rpx;
  342. height: 50rpx;
  343. }
  344. .weixin_text {
  345. line-height: 1;
  346. margin-left: 20rpx;
  347. color: #ffffff !important;
  348. }
  349. }
  350. /* #endif */
  351. .ios_login {
  352. width: 350rpx;
  353. border-radius: 12rpx;
  354. justify-content: center;
  355. border: 1px solid #212121;
  356. margin: 24rpx auto;
  357. padding: 15rpx;
  358. background-color: #212121;
  359. color: #ffffff;
  360. font-size: 32rpx;
  361. .loginIcon {
  362. font-size: 35rpx;
  363. width: 35rpx;
  364. height: 35rpx;
  365. }
  366. .weixin_text {
  367. line-height: 1;
  368. margin-left: 20rpx;
  369. color: #ffffff !important;
  370. }
  371. }
  372. page {
  373. height: 100%;
  374. }
  375. .container {
  376. width: 100%;
  377. height: 100%;
  378. background-size: 100%;
  379. }
  380. .container_text {
  381. width: 100%;
  382. height: 500rpx;
  383. top: 0rpx;
  384. .banner-img {
  385. width: 100%;
  386. height: 100%;
  387. background-color: $base-color;
  388. }
  389. }
  390. .login_text {
  391. margin: auto 30rpx;
  392. position: relative;
  393. padding: 100rpx 102rpx;
  394. background-color: #ffffff;
  395. margin-top: -180rpx;
  396. border-radius: 20rpx;
  397. .login_input {
  398. border-bottom: 1px solid #f0f0f0;
  399. margin-bottom: 65rpx;
  400. .login_img image {
  401. height: 35rpx;
  402. width: 29rpx;
  403. margin-right: 20rpx;
  404. }
  405. .uni-input {
  406. text-align: left;
  407. width: 470rpx;
  408. font-size: 28rpx !important;
  409. }
  410. .login_name {
  411. color: #333333;
  412. }
  413. }
  414. .other {
  415. margin-top: 60rpx;
  416. .fenge {
  417. width: 30%;
  418. height: 2rpx;
  419. background-color: #eeeeee;
  420. }
  421. .qita {
  422. font-size: 28rpx;
  423. color: #999999;
  424. }
  425. }
  426. .weixin {
  427. width: 75rpx;
  428. height: 75rpx;
  429. margin: 25rpx auto;
  430. }
  431. .weixin image {
  432. width: 100%;
  433. height: 100%;
  434. }
  435. .weixin_text {
  436. text-align: center;
  437. font-size: 28rpx;
  438. color: #999999;
  439. }
  440. .forget {
  441. font-size: 28rpx;
  442. width: 100%;
  443. text-align: right;
  444. color: #999999;
  445. }
  446. .uni-button-green {
  447. color: #ffffff;
  448. background-color: $base-color;
  449. margin: 40rpx 10rpx;
  450. border-radius: 50rpx;
  451. }
  452. .uni-button-green-plain {
  453. border: 1px solid $base-color;
  454. margin: 40rpx 10rpx;
  455. border-radius: 50rpx;
  456. color: $base-color;
  457. background-color: #ffffff;
  458. }
  459. .uni-button {
  460. height: 85rpx;
  461. line-height: 85rpx;
  462. }
  463. }
  464. .loginTitle {
  465. position: absolute;
  466. top: 250rpx;
  467. width: 100%;
  468. text-align: center;
  469. color: white;
  470. font-size: 40rpx;
  471. }
  472. </style>