index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <template>
  2. <view class="login-content">
  3. <view class="login-content-info">
  4. <view class="login-bgm">
  5. <image class="login-logo" :src="logoUrl" mode="scaleToFill"></image>
  6. <text>{{ title }}</text>
  7. </view>
  8. <view class="login-info">
  9. <view class="login-info-box">
  10. <text class="login-info-title">账号密码登录</text>
  11. <view class="login-info-form">
  12. <u--form
  13. labelWidth="0"
  14. :borderBottom="false"
  15. :model="form"
  16. :rules="rules"
  17. ref="uForm">
  18. <u-form-item prop="name">
  19. <u--input
  20. v-model="form.name"
  21. placeholder="请输入账号"
  22. border="surround"
  23. shape="circle"
  24. clearable
  25. prefixIcon="account-fill"
  26. prefixIconStyle="font-size: 22px;color: #909399"
  27. ></u--input>
  28. </u-form-item>
  29. <u-form-item prop="password">
  30. <u--input
  31. v-model="form.password"
  32. placeholder="请输入密码"
  33. border="surround"
  34. shape="circle"
  35. :type="passwordType"
  36. clearable
  37. prefixIcon="lock-fill"
  38. prefixIconStyle="font-size: 22px;color: #909399"
  39. >
  40. <template slot="suffix">
  41. <u-icon @click="setPasswordType" name="eye"></u-icon>
  42. </template>
  43. </u--input>
  44. </u-form-item>
  45. </u--form>
  46. </view>
  47. <view class="login-info-submit">
  48. <u-button
  49. class="login-info-submit-but"
  50. @click="submit()"
  51. :loading="loading"
  52. loadingText="登录中..."
  53. >登录</u-button>
  54. </view>
  55. <view class="login-info-tip">暂不支持自行注册</view>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. export default {
  63. data() {
  64. return {
  65. title: '《伟大转折》剧目全民分享系统',
  66. logoUrl: this.$commonConfig.staticUrl + "login/logo.png",
  67. loading: false,
  68. code: null,
  69. h5OpenId: null,
  70. form: {
  71. name: '',
  72. password: '',
  73. },
  74. rules: {
  75. 'name': {
  76. type: 'string',
  77. required: true,
  78. message: '请填写账号',
  79. trigger: ['blur', 'change']
  80. },
  81. 'password': {
  82. type: 'string',
  83. required: true,
  84. message: '请填写密码',
  85. trigger: ['blur', 'change']
  86. },
  87. },
  88. passwordType: 'password'
  89. }
  90. },
  91. onReady() {
  92. //如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
  93. this.$refs.uForm.setRules(this.rules)
  94. },
  95. onLoad(e) {
  96. // #ifdef H5
  97. if(this.distribution_user_info && this.distribution_user_info.accessToken){
  98. this.personsLoginCheck()
  99. }else {
  100. if(!e.code) { // 微信第三方登录失败
  101. this.redirectToAuth()
  102. }else {
  103. this.code = e.code
  104. }
  105. }
  106. // #endif
  107. },
  108. onShow() {
  109. this.passwordType = 'password'
  110. this.form.name = uni.getStorageSync('userName') || '' // 清除缓存
  111. this.form.password = uni.getStorageSync('userPassword') || '' // 清除缓存
  112. },
  113. methods: {
  114. /**
  115. *
  116. * 提交登录
  117. *
  118. */
  119. submit() {
  120. this.loading = true
  121. this.$refs.uForm.validate().then(res => {
  122. this.login()
  123. }).catch(errors => {
  124. this.$u.toast('校验失败')
  125. this.loading = false
  126. })
  127. },
  128. /**
  129. * 登录
  130. */
  131. async login(){
  132. try{
  133. // #ifdef H5
  134. const node_dev = process.env.H_NODE_ENV;
  135. if(node_dev && !this.h5OpenId){
  136. let wxinfo = await this.$u.api.wxinfoH5({code:this.code});
  137. this.h5OpenId = wxinfo.data.openid;
  138. }
  139. // #endif
  140. let res = await this.$u.api.login({
  141. "mobile": this.form.name,
  142. "password": this.form.password,
  143. "h5OpenId": this.h5OpenId
  144. })
  145. if(res && res.code ===200) {
  146. this.loading = false
  147. if(res.data && res.data.accessToken){
  148. this.$u.vuex('distribution_user_info', res.data);
  149. let backUrl = uni.getStorageSync('backUrlDistribution')
  150. uni.setStorageSync('userName',this.form.name)
  151. uni.setStorageSync('userPassword',this.form.password)
  152. console.log("backUrl===",backUrl)
  153. if(backUrl && backUrl != '/pages/login/index') {
  154. uni.reLaunch({
  155. url: '/'+backUrl
  156. })
  157. }else {
  158. uni.reLaunch({
  159. url: '/pages/main/index'
  160. })
  161. }
  162. } else {
  163. // 清空用户数据缓存
  164. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  165. this.$u.vuex('distribution_user_info', {});
  166. this.$set(this.form,'password','')
  167. if(!this.h5OpenId) {
  168. this.redirectToAuth()
  169. }
  170. }
  171. } else {
  172. // 清空用户数据缓存
  173. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  174. this.$u.vuex('distribution_user_info', {});
  175. this.$set(this.form,'password','')
  176. if(!this.h5OpenId) {
  177. this.redirectToAuth()
  178. }
  179. }
  180. }catch(e){
  181. //TODO handle the exception
  182. console.error("e===",e)
  183. this.loading = false
  184. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  185. this.$u.vuex('distribution_user_info', {});
  186. this.$set(this.form,'password','')
  187. if(!this.h5OpenId) {
  188. this.redirectToAuth()
  189. }
  190. }
  191. },
  192. /** 公众号 微信授权登录 */
  193. redirectToAuth() {
  194. const node_dev = process.env.H_NODE_ENV;
  195. if(node_dev){
  196. const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.$commonConfig.appid}&redirect_uri=${this.$commonConfig.redirectUri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
  197. window.location.href = authUrl;
  198. }
  199. },
  200. /** 检测是否已登录 */
  201. async personsLoginCheck() {
  202. try{
  203. let res = await this.$u.api.login({
  204. "noSign": 1,
  205. })
  206. if(res && res.code ===200) {
  207. if(res.data && res.data.accessToken){
  208. this.$u.vuex('distribution_user_info', res.data);
  209. let backUrl = uni.getStorageSync('backUrlDistribution')
  210. if(backUrl) {
  211. uni.reLaunch({
  212. url: backUrl
  213. })
  214. }else {
  215. uni.reLaunch({
  216. url: '/pages/main/index'
  217. })
  218. }
  219. } else {
  220. // 清空用户数据缓存
  221. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  222. this.$u.vuex('distribution_user_info', {});
  223. this.redirectToAuth()
  224. }
  225. } else {
  226. // 清空用户数据缓存
  227. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  228. this.$u.vuex('distribution_user_info', {});
  229. this.redirectToAuth()
  230. }
  231. }catch(e){
  232. //TODO handle the exception
  233. uni.setStorageSync('lifeDataDistribution',{}); // 清除缓存
  234. this.$u.vuex('distribution_user_info', {});
  235. this.redirectToAuth()
  236. }
  237. },
  238. // 设置密码类型
  239. setPasswordType(){
  240. if(this.passwordType=='password'){
  241. this.passwordType = 'text'
  242. }else {
  243. this.passwordType = 'password'
  244. }
  245. }
  246. }
  247. }
  248. </script>
  249. <style lang="scss" scoped>
  250. .login-content {
  251. width: 100%;
  252. height: 100%;
  253. position: relative;
  254. box-sizing: border-box;
  255. --bgm-height: 630rpx;
  256. }
  257. /** 背景 */
  258. .login-content-box {
  259. width: 100%;
  260. height: 100%;
  261. box-sizing: border-box;
  262. }
  263. .login-bgm {
  264. width: 100%;
  265. height: 630rpx;
  266. background-image: url("#{$image-beas-url}login/bgm.png");
  267. background-size: 100% auto;
  268. background-repeat: no-repeat;
  269. box-sizing: border-box;
  270. display: flex;
  271. flex-direction: column;
  272. justify-content: center;
  273. align-items: center;
  274. .login-logo {
  275. width: 240rpx;
  276. height: 172rpx;
  277. padding: 50rpx 0;
  278. }
  279. >text {
  280. font-size: 32rpx;
  281. font-family: SourceHanSansCN, SourceHanSansCN;
  282. font-weight: bold;
  283. color: #FFFFFF;
  284. }
  285. }
  286. .login-info {
  287. width: 100%;
  288. height: calc(100% - var(--bgm-height) + 40rpx);
  289. background-color: #fff;
  290. border-radius: 28rpx 28rpx 0rpx 0rpx;
  291. position: absolute;
  292. bottom: 0;
  293. left: 0;
  294. z-index: 22;
  295. box-sizing: border-box;
  296. padding: 80rpx;
  297. .login-info-box {
  298. width: 100%;
  299. height: 100%;
  300. display: flex;
  301. flex-direction: column;
  302. box-sizing: border-box;
  303. .login-info-title {
  304. font-size: 32rpx;
  305. font-family: SourceHanSansCN, SourceHanSansCN;
  306. font-weight: bold;
  307. color: #2D2D2D;
  308. padding-bottom: 40rpx;
  309. }
  310. .login-info-form {
  311. }
  312. .login-info-submit {
  313. width: 100%;
  314. height: 80rpx;
  315. padding: 40rpx 0 20rpx;
  316. flex-shrink: 0;
  317. .login-info-submit-but {
  318. display: flex;
  319. justify-content: center;
  320. align-items: center;
  321. border-radius: 40rpx;
  322. width: 100% !important;
  323. height: 100% !important;
  324. background-color: var(--gd-bgm-color);
  325. color: #fff;
  326. }
  327. }
  328. .login-info-tip {
  329. width: 100%;
  330. text-align: center;
  331. font-size: 24rpx;
  332. font-family: SourceHanSansCN, SourceHanSansCN;
  333. font-weight: 400;
  334. color: #C2C2C2;
  335. }
  336. }
  337. }
  338. /** tabar */
  339. .main-tabar {
  340. width: 80vw;
  341. height: 100rpx;
  342. position: fixed;
  343. bottom: 20rpx;
  344. left: 50%;
  345. transform: translateX(-50%);
  346. box-sizing: border-box;
  347. .main-tabar-info {
  348. width: 100%;
  349. height: 100%;
  350. display: flex;
  351. justify-content: space-between;
  352. box-shadow: rgba(0, 0, 0, 0.4) 0px 2px 6px 0px;
  353. border-radius: 40rpx;
  354. .main-tabar-item {
  355. flex: 1;
  356. display: flex;
  357. align-items: center;
  358. flex-direction: column;
  359. justify-content: center;
  360. image {
  361. width: 50rpx;
  362. height: 50rpx;
  363. }
  364. text {
  365. font-size: 24rpx;
  366. }
  367. }
  368. }
  369. }
  370. </style>