index.vue 9.5 KB

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