login.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <template>
  2. <view class="page-container">
  3. <view class="welcome-title">
  4. 超吉炫,欢迎您!
  5. </view>
  6. <view class="main-card">
  7. <image class="logo" :src="imgurl+'/cjx/hexiao/lobin_lobin.png'" mode="aspectFit"></image>
  8. <view class="user-type-selector">
  9. <view
  10. class="type-button"
  11. :class="{ 'active': userType === 'distributor' }"
  12. @click="switchUserType('distributor')"
  13. >
  14. <uni-icons type="person" :color="userType === 'distributor' ? '#fff' : '#4a8af4'" size="20"></uni-icons>
  15. <text>经销商</text>
  16. </view>
  17. <view
  18. class="type-button"
  19. :class="{ 'active': userType === 'salesman' }"
  20. @click="switchUserType('salesman')"
  21. >
  22. <uni-icons type="staff" :color="userType === 'salesman' ? '#fff' : '#4a8af4'" size="20"></uni-icons>
  23. <text>业务员</text>
  24. </view>
  25. </view>
  26. <view class="form-group">
  27. <view class="input-wrapper">
  28. <uni-icons type="person" size="24" color="#cccccc"></uni-icons>
  29. <input class="input-field" type="number" v-model="phone" placeholder="请输入手机号" placeholder-class="placeholder" />
  30. </view>
  31. <view class="input-wrapper">
  32. <uni-icons type="locked" size="24" color="#cccccc"></uni-icons>
  33. <input class="input-field" type="number" v-model="code" placeholder="请输入验证码" placeholder-class="placeholder" />
  34. <text
  35. class="code-btn"
  36. :class="{ 'disabled': isCountingDown }"
  37. @click="getVerificationCode"
  38. >
  39. {{ codeBtnText }}
  40. </text>
  41. </view>
  42. </view>
  43. <button class="login-btn" @click="handleLogin">登 录</button>
  44. <!-- <view class="version-info">-->
  45. <!-- 版本号:V1.0.0-->
  46. <!-- </view>-->
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. // 1. 导入你的API方法
  52. import { getVerificationCode, login } from '@/api/hexiao.js';
  53. import {getAdminToken, setAdminInfo, setAdminToken} from "../../../utils/auth";
  54. export default {
  55. data() {
  56. return {
  57. userType: 'distributor',
  58. phone: '18569549881',
  59. code: '888888',
  60. codeBtnText: '获取手机验证码',
  61. isCountingDown: false,
  62. countdown: 60,
  63. timer: null,
  64. imgurl:"https://hyscancode.oss-cn-hangzhou.aliyuncs.com/xiaochengxu",
  65. }
  66. },
  67. onUnload() {
  68. if (this.timer) {
  69. clearInterval(this.timer);
  70. this.timer = null;
  71. }
  72. },
  73. onLoad(){
  74. let token = getAdminToken()
  75. if(token){
  76. this.doLoginSuccess();
  77. }
  78. },
  79. methods: {
  80. doLoginSuccess(){
  81. let userType = 0;
  82. let pageUrl = '/pages/cjx/hexiao/ywy/index';
  83. if(this.userType === 'distributor'){
  84. pageUrl = '/pages/cjx/hexiao/jxs/index';
  85. }
  86. if(this.userType === 'salesman'){
  87. pageUrl = '/pages/cjx/hexiao/ywy/index';
  88. }
  89. uni.reLaunch({
  90. url: pageUrl
  91. });
  92. },
  93. switchUserType(type) {
  94. this.userType = type;
  95. },
  96. // --- 更新:调用获取验证码的API ---
  97. getVerificationCode() {
  98. if (this.isCountingDown) {
  99. return;
  100. }
  101. if(!this.phone) {
  102. uni.showToast({ title: '请输入手机号', icon: 'none' });
  103. return;
  104. }
  105. if(this.timer){
  106. clearInterval(this.timer);
  107. this.timer = null;
  108. }
  109. let userType = 0;
  110. if(this.userType === 'distributor'){
  111. userType = 1;
  112. }
  113. if(this.userType === 'salesman'){
  114. userType = 2;
  115. }
  116. // 调用API
  117. getVerificationCode(this.phone,userType).then(res => {
  118. if(res.code !== 0){
  119. uni.showToast({ title: res.msg || '找不到用户信息,请确认', icon: 'none' });
  120. return;
  121. }
  122. console.log('验证码接口成功返回:', res);
  123. uni.showToast({ title: res.msg || '验证码已发送', icon: 'success' });
  124. // 开始倒计时
  125. this.isCountingDown = true;
  126. this.codeBtnText = `${this.countdown}s后重新获取`;
  127. this.timer = setInterval(() => {
  128. this.countdown--;
  129. if (this.countdown > 0) {
  130. this.codeBtnText = `${this.countdown}s后重新获取`;
  131. } else {
  132. clearInterval(this.timer);
  133. this.timer = null;
  134. this.countdown = 60;
  135. this.isCountingDown = false;
  136. this.codeBtnText = '获取手机验证码';
  137. }
  138. }, 1000);
  139. }).catch(err => {
  140. // if(res.code === -99){
  141. // uni.showToast({ title: err.msg || '找不到用户信息,请确认', icon: 'none' });
  142. // return;
  143. // }
  144. // console.error('验证码接口失败返回:', err);
  145. // uni.showToast({ title: err.msg || '发送失败,请重试', icon: 'none' });
  146. });
  147. },
  148. setUserInfo(res){
  149. setAdminInfo(res);
  150. },
  151. // --- 更新:调用登录的API ---
  152. handleLogin() {
  153. if(!this.phone || !this.code) {
  154. uni.showToast({ title: '请填写完整信息', icon: 'none' });
  155. return;
  156. }
  157. uni.showLoading({ title: '登录中...' });
  158. let userType = 0;
  159. if(this.userType === 'distributor'){
  160. userType = 1;
  161. }
  162. if(this.userType === 'salesman'){
  163. userType = 2;
  164. }
  165. let self = this;
  166. uni.login({
  167. provider: 'weixin', //使用微信登录
  168. success: function (loginRes) {
  169. let jsCode = loginRes.code;
  170. login(self.phone, self.code,userType,jsCode).then(res => {
  171. // 登录成功 (res.code === 0)
  172. uni.hideLoading();
  173. if (res.code === 0) {
  174. setAdminToken(res.data.sessionId)
  175. self.setUserInfo(res.data);
  176. self.doLoginSuccess();
  177. } else {
  178. console.log('登录失败!' + res.msg);
  179. uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
  180. }
  181. }).catch(err => {
  182. // 登录失败 (res.code !== 0 或网络错误)
  183. uni.hideLoading();
  184. console.error('登录失败:', err);
  185. uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
  186. });
  187. }
  188. });
  189. // 调用API
  190. }
  191. }
  192. }
  193. </script>
  194. <style lang="scss">
  195. .page-container {
  196. width: 100vw;
  197. height: 100vh;
  198. display: flex;
  199. flex-direction: column;
  200. align-items: center;
  201. overflow: hidden;
  202. min-height: 100vh;
  203. background-image: url("https://hyscancode.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/cjx/hexiao/login_bg.png");
  204. background-size: contain;
  205. background-position: center;
  206. background-repeat: no-repeat;
  207. position: relative;
  208. }
  209. .welcome-title {
  210. font-size: 48rpx;
  211. font-weight: bold;
  212. color: #488CFF;
  213. margin-top: 28%;
  214. margin-bottom: 40rpx;
  215. }
  216. .main-card {
  217. width: 88%;
  218. border-radius: 40rpx;
  219. padding: 40rpx;
  220. box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.05);
  221. display: flex;
  222. flex-direction: column;
  223. align-items: center;
  224. position: relative;
  225. top: 10%;
  226. height: 100%;
  227. }
  228. .logo {
  229. width: 150rpx;
  230. height: 150rpx;
  231. background-color: #f0f0f0;
  232. margin-bottom: 40rpx;
  233. border-radius: 20rpx;
  234. }
  235. .user-type-selector {
  236. display: flex;
  237. justify-content: center;
  238. width: 100%;
  239. margin-bottom: 50rpx;
  240. .type-button {
  241. display: flex;
  242. align-items: center;
  243. justify-content: center;
  244. font-size: 30rpx;
  245. padding: 15rpx 40rpx;
  246. border-radius: 50rpx;
  247. transition: all 0.3s ease;
  248. border: 1px solid #4a8af4;
  249. color: #4a8af4;
  250. background-color: #fff;
  251. margin: 0 15rpx;
  252. .uni-icons {
  253. margin-right: 10rpx;
  254. }
  255. &.active {
  256. background-color: #4a8af4;
  257. color: #ffffff;
  258. border-color: #4a8af4;
  259. }
  260. }
  261. }
  262. .form-group {
  263. width: 100%;
  264. }
  265. .input-wrapper {
  266. display: flex;
  267. align-items: center;
  268. width: 100%;
  269. padding: 20rpx 0;
  270. border-bottom: 1px solid #f0f0f0;
  271. margin-bottom: 30rpx;
  272. .input-field {
  273. flex: 1;
  274. font-size: 30rpx;
  275. margin-left: 20rpx;
  276. }
  277. .placeholder {
  278. color: #cccccc;
  279. }
  280. .code-btn {
  281. font-size: 28rpx;
  282. color: #4a8af4;
  283. white-space: nowrap;
  284. padding-left: 20rpx;
  285. transition: color 0.3s; // 添加颜色过渡效果
  286. // --- 新增:置灰样式 ---
  287. &.disabled {
  288. color: #cccccc;
  289. }
  290. }
  291. }
  292. .login-btn {
  293. width: 100%;
  294. background: #4a8af4;
  295. color: #ffffff;
  296. font-size: 32rpx;
  297. border-radius: 50rpx;
  298. margin-top: 40rpx;
  299. box-shadow: 0 10rpx 20rpx rgba(74, 138, 244, 0.3);
  300. }
  301. .version-info {
  302. margin-top: 40rpx;
  303. font-size: 24rpx;
  304. color: #b0b0b0;
  305. }
  306. </style>