login.vue 11 KB

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