login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <template>
  2. <view class="page-container">
  3. <view class="welcome-title">
  4. 智能营销系统,欢迎您!
  5. </view>
  6. <view class="main-card" style="margin-top:20%">
  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 >
  27. <view class="form-group">
  28. <view class="input-wrapper">
  29. <uni-icons type="person" size="24" color="#cccccc"></uni-icons>
  30. <input class="input-field" type="number" v-model="phone" placeholder="请输入手机号" placeholder-class="placeholder" />
  31. </view>
  32. <view class="input-wrapper">
  33. <uni-icons type="locked" size="24" color="#cccccc"></uni-icons>
  34. <input class="input-field" type="number" v-model="code" placeholder="请输入验证码" placeholder-class="placeholder" />
  35. <text
  36. class="code-btn"
  37. :class="{ 'disabled': isCountingDown }"
  38. @click="getVerificationCode"
  39. >
  40. {{ codeBtnText }}
  41. </text>
  42. </view>
  43. </view>
  44. <button class="login-btn" @click="handleLogin">登 录</button>
  45. </view>
  46. <!-- <view v-if="!showTel">-->
  47. <!-- <view>-->
  48. <!-- 请点击下方按钮进行授权登录-->
  49. <!-- </view>-->
  50. <!-- <button class="login-btn" open-type="getPhoneNumber" @getphonenumber="handlePhone">-->
  51. <!-- 授权登录-->
  52. <!-- </button>-->
  53. <!-- </view>-->
  54. <!-- <view style="font-size: 12px;margin-top:12%;" @click="showTel = !showTel">-->
  55. <!-- <view v-if="!showTel">使用手机号验证码登录</view>-->
  56. <!-- <view v-if="showTel">一键登录</view>-->
  57. <!-- </view>-->
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. // 1. 导入你的API方法
  63. import { getVerificationCode, login ,oneKeyLogin} from '@/api/hexiao.js';
  64. import {getAdminInfo, getAdminToken, setAdminInfo, setAdminToken} from "@/utils/auth";
  65. import {
  66. getIsNoLogin
  67. } from '@/api/login'
  68. export default {
  69. data() {
  70. return {
  71. showTel: false,
  72. userType: 'distributor',
  73. phone: '',
  74. code: '',
  75. codeBtnText: '获取手机验证码',
  76. isCountingDown: false,
  77. countdown: 60,
  78. timer: null,
  79. imgurl:"https://hyscancode.oss-cn-hangzhou.aliyuncs.com/xiaochengxu",
  80. tenantId:0,
  81. }
  82. },
  83. onUnload() {
  84. if (this.timer) {
  85. clearInterval(this.timer);
  86. this.timer = null;
  87. }
  88. },
  89. onLoad(opt){
  90. if(opt.uid){
  91. this.tenantId = opt.uid;
  92. }
  93. let self = this;
  94. uni.showLoading({
  95. mask: true
  96. })
  97. wx.login({
  98. success(resLogin) {
  99. if (resLogin.code) {
  100. getIsNoLogin({jsCode:resLogin.code}).then(res => {
  101. uni.hideLoading();
  102. if (res.code === 0){
  103. setAdminInfo(res.data);
  104. setAdminToken(res.data.sessionId)
  105. self.doLoginSuccess();
  106. }
  107. // 跳转登陆页
  108. })
  109. }else{
  110. uni.hideLoading();
  111. }
  112. }
  113. });
  114. // let token = getAdminToken()
  115. // if(token){
  116. // this.doLoginSuccess();
  117. // }
  118. },
  119. methods: {
  120. handlePhone(e) {
  121. if (e.detail.code) {
  122. // this.loginDate.iv = e.detail.iv;
  123. // this.loginDate.encryptedData = e.detail.encryptedData;
  124. uni.showLoading({ title: '登录中...' });
  125. let userType = 0;
  126. if(this.userType === 'distributor'){
  127. userType = 1;
  128. }
  129. if(this.userType === 'salesman'){
  130. userType = 2;
  131. }
  132. let self = this;
  133. uni.login({
  134. provider: 'weixin', //使用微信登录
  135. success: function (loginRes) {
  136. let jsCode = loginRes.code;
  137. oneKeyLogin( e.detail.code,userType,jsCode,self.tenantId).then(res => {
  138. // 登录成功 (res.code === 0)
  139. uni.hideLoading();
  140. if (res.code === 0) {
  141. setAdminToken(res.data.sessionId)
  142. self.setUserInfo(res.data);
  143. self.doLoginSuccess();
  144. } else {
  145. console.log('登录失败!' + res.msg);
  146. uni.showToast({ title: res.msg || '登录失败', icon: 'none' });
  147. }
  148. }).catch(err => {
  149. // 登录失败 (res.code !== 0 或网络错误)
  150. uni.hideLoading();
  151. console.error('登录失败:', err);
  152. uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
  153. });
  154. }
  155. });
  156. }
  157. },
  158. doLoginSuccess(){
  159. let info = getAdminInfo()
  160. let userType = info.roleType;
  161. let pageUrl = '/pages/hexiao/ywy/index';
  162. if(userType === 1){
  163. pageUrl = '/pages/hexiao/jxs/index';
  164. }
  165. if(userType === 2){
  166. pageUrl = '/pages/hexiao/ywy/index';
  167. }
  168. uni.reLaunch({
  169. url: pageUrl
  170. });
  171. },
  172. switchUserType(type) {
  173. this.userType = type;
  174. },
  175. // --- 更新:调用获取验证码的API ---
  176. getVerificationCode() {
  177. if (this.isCountingDown) {
  178. return;
  179. }
  180. if(!this.phone) {
  181. uni.showToast({ title: '请输入手机号', icon: 'none' });
  182. return;
  183. }
  184. if(this.timer){
  185. clearInterval(this.timer);
  186. this.timer = null;
  187. }
  188. let userType = 0;
  189. if(this.userType === 'distributor'){
  190. userType = 1;
  191. }
  192. if(this.userType === 'salesman'){
  193. userType = 2;
  194. }
  195. // 调用API
  196. getVerificationCode(this.phone,userType,this.tenantId).then(res => {
  197. if(res.code !== 0){
  198. uni.showToast({ title: res.msg || '找不到用户信息,请确认', icon: 'none' });
  199. return;
  200. }
  201. console.log('验证码接口成功返回:', res);
  202. uni.showToast({ title: res.msg || '验证码已发送', icon: 'success' });
  203. // 开始倒计时
  204. this.isCountingDown = true;
  205. this.codeBtnText = `${this.countdown}s后重新获取`;
  206. this.timer = setInterval(() => {
  207. this.countdown--;
  208. if (this.countdown > 0) {
  209. this.codeBtnText = `${this.countdown}s后重新获取`;
  210. } else {
  211. clearInterval(this.timer);
  212. this.timer = null;
  213. this.countdown = 60;
  214. this.isCountingDown = false;
  215. this.codeBtnText = '获取手机验证码';
  216. }
  217. }, 1000);
  218. }).catch(err => {
  219. // if(res.code === -99){
  220. // uni.showToast({ title: err.msg || '找不到用户信息,请确认', icon: 'none' });
  221. // return;
  222. // }
  223. // console.error('验证码接口失败返回:', err);
  224. // uni.showToast({ title: err.msg || '发送失败,请重试', icon: 'none' });
  225. });
  226. },
  227. setUserInfo(res){
  228. setAdminInfo(res);
  229. },
  230. // --- 更新:调用登录的API ---
  231. handleLogin() {
  232. if(!this.phone || !this.code) {
  233. uni.showToast({ title: '请填写完整信息', icon: 'none' });
  234. return;
  235. }
  236. uni.showLoading({ title: '登录中...' });
  237. let userType = 0;
  238. if(this.userType === 'distributor'){
  239. userType = 1;
  240. }
  241. if(this.userType === 'salesman'){
  242. userType = 2;
  243. }
  244. let self = this;
  245. uni.login({
  246. provider: 'weixin', //使用微信登录
  247. success: function (loginRes) {
  248. let jsCode = loginRes.code;
  249. login(self.phone, self.code,userType,jsCode,this.tenantId).then(res => {
  250. // 登录成功 (res.code === 0)
  251. uni.hideLoading();
  252. if (res.code === 0) {
  253. setAdminToken(res.data.sessionId)
  254. self.setUserInfo(res.data);
  255. self.doLoginSuccess();
  256. } else {
  257. console.log('登录失败!' + res.msg);
  258. uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
  259. }
  260. }).catch(err => {
  261. // 登录失败 (res.code !== 0 或网络错误)
  262. uni.hideLoading();
  263. console.error('登录失败:', err);
  264. uni.showToast({ title: err.msg || '登录失败', icon: 'none' });
  265. });
  266. }
  267. });
  268. // 调用API
  269. }
  270. }
  271. }
  272. </script>
  273. <style lang="scss">
  274. .page-container {
  275. width: 100vw;
  276. height: 100vh;
  277. display: flex;
  278. flex-direction: column;
  279. align-items: center;
  280. overflow: hidden;
  281. min-height: 100vh;
  282. background-image: url("https://hyscancode.oss-cn-hangzhou.aliyuncs.com/xiaochengxu/cjx/hexiao/login_bg.png");
  283. background-size: contain;
  284. background-position: center;
  285. background-repeat: no-repeat;
  286. position: relative;
  287. }
  288. .welcome-title {
  289. font-size: 48rpx;
  290. font-weight: bold;
  291. color: #488CFF;
  292. margin-top: 28%;
  293. margin-bottom: 40rpx;
  294. }
  295. .main-card {
  296. width: 88%;
  297. border-radius: 40rpx;
  298. padding: 40rpx;
  299. box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.05);
  300. display: flex;
  301. flex-direction: column;
  302. align-items: center;
  303. position: relative;
  304. top: 10%;
  305. height: 100%;
  306. }
  307. .logo {
  308. width: 150rpx;
  309. height: 150rpx;
  310. background-color: #f0f0f0;
  311. margin-bottom: 40rpx;
  312. border-radius: 20rpx;
  313. }
  314. .user-type-selector {
  315. display: flex;
  316. justify-content: center;
  317. width: 100%;
  318. margin-bottom: 50rpx;
  319. .type-button {
  320. display: flex;
  321. align-items: center;
  322. justify-content: center;
  323. font-size: 30rpx;
  324. padding: 15rpx 40rpx;
  325. border-radius: 50rpx;
  326. transition: all 0.3s ease;
  327. border: 1px solid #4a8af4;
  328. color: #4a8af4;
  329. background-color: #fff;
  330. margin: 0 15rpx;
  331. .uni-icons {
  332. margin-right: 10rpx;
  333. }
  334. &.active {
  335. background-color: #4a8af4;
  336. color: #ffffff;
  337. border-color: #4a8af4;
  338. }
  339. }
  340. }
  341. .form-group {
  342. width: 100%;
  343. }
  344. .input-wrapper {
  345. display: flex;
  346. align-items: center;
  347. width: 100%;
  348. padding: 20rpx 0;
  349. border-bottom: 1px solid #f0f0f0;
  350. margin-bottom: 30rpx;
  351. .input-field {
  352. flex: 1;
  353. font-size: 30rpx;
  354. margin-left: 20rpx;
  355. }
  356. .placeholder {
  357. color: #cccccc;
  358. }
  359. .code-btn {
  360. font-size: 28rpx;
  361. color: #4a8af4;
  362. white-space: nowrap;
  363. padding-left: 20rpx;
  364. transition: color 0.3s; // 添加颜色过渡效果
  365. // --- 新增:置灰样式 ---
  366. &.disabled {
  367. color: #cccccc;
  368. }
  369. }
  370. }
  371. .login-btn {
  372. width: 100%;
  373. background: #4a8af4;
  374. color: #ffffff;
  375. font-size: 32rpx;
  376. border-radius: 50rpx;
  377. margin-top: 40rpx;
  378. box-shadow: 0 10rpx 20rpx rgba(74, 138, 244, 0.3);
  379. }
  380. .version-info {
  381. margin-top: 40rpx;
  382. font-size: 24rpx;
  383. color: #b0b0b0;
  384. }
  385. </style>