login.vue 9.1 KB

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