login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. isPhone(phone) {
  257. return /^1[1-9]\d{9}$/.test(String(phone).replace(/\D/g, ''))
  258. },
  259. //登录
  260. async toLogin() {
  261. let obj = this;
  262. obj.logining = true;
  263. if (obj.username == '') {
  264. obj.$msg('请输入手机号');
  265. return;
  266. }
  267. if (obj.passward == '') {
  268. obj.$msg('请输入密码');
  269. return;
  270. }
  271. login({
  272. account: obj.username,
  273. password: obj.passward,
  274. login_type: obj.isPhone(obj.username)? 'phone': 'account'
  275. })
  276. .then(function(e) {
  277. // 保存账号密码
  278. uni.setStorageSync('uname',obj.username)
  279. uni.setStorageSync('upass',obj.passward)
  280. uni.setStorageSync('token', e.data.token);
  281. obj.$store.commit('hasLogin', true);
  282. getUserInfo({}).then(e => {
  283. obj.login();
  284. // 保存返回用户数据
  285. obj.setUserInfo(e.data);
  286. // #ifdef H5
  287. let weichatBrowser = uni.getStorageSync('weichatBrowser');
  288. if(weichatBrowser) {
  289. loginWinxin();
  290. }else {
  291. obj.goIndex()
  292. }
  293. // #endif
  294. // #ifndef H5
  295. obj.goIndex()
  296. // #endif
  297. });
  298. })
  299. .catch(function(e) {
  300. console.log(e);
  301. });
  302. },
  303. goIndex() {
  304. let ur = uni.getStorageSync('present') || '/pages/index/index';
  305. //成功跳转首页
  306. uni.switchTab({
  307. url: ur,
  308. fail(e) {
  309. uni.navigateTo({
  310. url: ur,
  311. fail(e) {
  312. uni.navigateTo({
  313. url: '/pages/index/index',
  314. });
  315. }
  316. });
  317. }
  318. });
  319. },
  320. //跳转注册页
  321. register() {
  322. uni.navigateTo({
  323. url: `/pages/public/register`
  324. });
  325. },
  326. // 后退
  327. navBack() {
  328. uni.navigateBack();
  329. }
  330. }
  331. };
  332. </script>
  333. <style lang="scss">
  334. /* #ifdef APP-PLUS */
  335. .ios_login {
  336. width: 260rpx;
  337. border-radius: 12rpx;
  338. justify-content: center;
  339. border: 1px solid #212121;
  340. margin: 24rpx auto;
  341. padding: 10rpx;
  342. background-color: #212121;
  343. color: #ffffff;
  344. .loginIcon {
  345. width: 50rpx;
  346. height: 50rpx;
  347. }
  348. .weixin_text {
  349. line-height: 1;
  350. margin-left: 20rpx;
  351. color: #ffffff !important;
  352. }
  353. }
  354. /* #endif */
  355. .ios_login {
  356. width: 350rpx;
  357. border-radius: 12rpx;
  358. justify-content: center;
  359. border: 1px solid #212121;
  360. margin: 24rpx auto;
  361. padding: 15rpx;
  362. background-color: #212121;
  363. color: #ffffff;
  364. font-size: 32rpx;
  365. .loginIcon {
  366. font-size: 35rpx;
  367. width: 35rpx;
  368. height: 35rpx;
  369. }
  370. .weixin_text {
  371. line-height: 1;
  372. margin-left: 20rpx;
  373. color: #ffffff !important;
  374. }
  375. }
  376. page {
  377. height: 100%;
  378. }
  379. .container {
  380. width: 100%;
  381. height: 100%;
  382. background-size: 100%;
  383. }
  384. .container_text {
  385. width: 100%;
  386. height: 500rpx;
  387. top: 0rpx;
  388. .banner-img {
  389. width: 100%;
  390. height: 100%;
  391. background-color: $base-color;
  392. }
  393. }
  394. .login_text {
  395. margin: auto 30rpx;
  396. position: relative;
  397. padding: 100rpx 102rpx;
  398. background-color: #ffffff;
  399. margin-top: -180rpx;
  400. border-radius: 20rpx;
  401. .login_input {
  402. border-bottom: 1px solid #f0f0f0;
  403. margin-bottom: 65rpx;
  404. .login_img image {
  405. height: 35rpx;
  406. width: 29rpx;
  407. margin-right: 20rpx;
  408. }
  409. .uni-input {
  410. text-align: left;
  411. width: 470rpx;
  412. font-size: 28rpx !important;
  413. }
  414. .login_name {
  415. color: #333333;
  416. }
  417. }
  418. .other {
  419. margin-top: 60rpx;
  420. .fenge {
  421. width: 30%;
  422. height: 2rpx;
  423. background-color: #eeeeee;
  424. }
  425. .qita {
  426. font-size: 28rpx;
  427. color: #999999;
  428. }
  429. }
  430. .weixin {
  431. width: 75rpx;
  432. height: 75rpx;
  433. margin: 25rpx auto;
  434. }
  435. .weixin image {
  436. width: 100%;
  437. height: 100%;
  438. }
  439. .weixin_text {
  440. text-align: center;
  441. font-size: 28rpx;
  442. color: #999999;
  443. }
  444. .forget {
  445. font-size: 28rpx;
  446. width: 100%;
  447. text-align: right;
  448. color: #999999;
  449. }
  450. .uni-button-green {
  451. color: #ffffff;
  452. background-color: $base-color;
  453. margin: 40rpx 10rpx;
  454. border-radius: 50rpx;
  455. }
  456. .uni-button-green-plain {
  457. border: 1px solid $base-color;
  458. margin: 40rpx 10rpx;
  459. border-radius: 50rpx;
  460. color: $base-color;
  461. background-color: #ffffff;
  462. }
  463. .uni-button {
  464. height: 85rpx;
  465. line-height: 85rpx;
  466. }
  467. }
  468. .loginTitle {
  469. position: absolute;
  470. top: 250rpx;
  471. width: 100%;
  472. text-align: center;
  473. color: white;
  474. font-size: 40rpx;
  475. }
  476. </style>