common.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. import bus from "./bus.js"
  2. //计算总时间
  3. var getTotalTime = function(time){
  4. var totalTime = new Date();
  5. totalTime.setTime(totalTime.getTime()+time*1000);
  6. return totalTime;
  7. }
  8. //倒计时
  9. var timer = function(that){
  10. that.verifyState=1;
  11. that.secondNum=60;
  12. clearInterval(that.countDown);//初始化计时器
  13. that.countDown = setInterval(function(){
  14. that.secondNum--;
  15. if (that.secondNum == 0) {
  16. clearInterval(that.countDown);
  17. that.verifyState = 2;//更改按钮状态
  18. that.secondNum = 60;
  19. }
  20. },1000);
  21. }
  22. //设置cookie
  23. /**
  24. * [setcookie description]
  25. * @param {[string]} key [key]
  26. * @param {[type]} value [key的值]
  27. * @param {[type]} expires [cookie的有效期]
  28. * @param {[type]} path [指定路径]
  29. * @param {[type]} domain [指定cookie的域名信息]
  30. * @param {[boolean]} secure [指定cookie只能在加密协议HTTPS下发送到服务器]
  31. * @return {[type]} [description]
  32. */
  33. var setcookie = function(key,value,expires,path,domain,secure){
  34. var cookieText = encodeURIComponent("block_fxees" + key) + '=' + encodeURIComponent(value);
  35. var time = getTotalTime(expires);
  36. if (time instanceof Date) {
  37. cookieText += ';expires=' + time;
  38. }
  39. if (path) {
  40. cookieText += ';path=' + path;
  41. }
  42. if (domain) {
  43. cookieText += ';domain' + domain;
  44. }
  45. if (secure) {
  46. cookieText += ';secure'+ secure;
  47. }
  48. document.cookie = cookieText;
  49. if (localStorage) {
  50. localStorage.setItem(key, value);
  51. }
  52. }
  53. //获取cookie
  54. /**
  55. * [getcookie description]
  56. * @param {[type]} key [description]
  57. * @return {[type]} [key的值]
  58. */
  59. var getcookie = function(key){
  60. var cookieName = encodeURIComponent("block_fxees" + key);
  61. var cookieStart = document.cookie.indexOf(cookieName);
  62. var cookieValue = null;
  63. if (cookieStart > -1) {
  64. var cookieEnd = document.cookie.indexOf(';',cookieStart);
  65. if (cookieEnd == -1) {
  66. cookieEnd = document.cookie.length;
  67. }
  68. cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + 1 + cookieName.length, cookieEnd));
  69. }else if(localStorage){
  70. cookieValue = localStorage.getItem(key);
  71. }
  72. return cookieValue;
  73. }
  74. //删除cookie
  75. /**
  76. * [deletecookie description]
  77. * @param {[type]} key [description]
  78. * @return {[type]} [description]
  79. */
  80. var deletecookie = function(key){
  81. document.cookie = "block_fxees" + key + "= ; expires=" + new Date(0).toUTCString();
  82. localStorage.removeItem(key);
  83. }
  84. //封装axios
  85. /**
  86. * [getInfo description]
  87. * @param {[type]} url [接口地址]
  88. * @param {[type]} method [请求方式]
  89. * @param {[type]} params [请求数据]
  90. * @param {[type]} success [成功后执行的操作]
  91. * @param {[type]} failure [失败后执行的操作]
  92. * @return {[type]} [description]
  93. */
  94. var getInfo = function(that,url,method,params,success){
  95. var token = getcookie("token");
  96. var headerParam;
  97. if (token) {
  98. headerParam = {
  99. "Authorization":"Bearer " + token,
  100. 'Content-Type':'application/x-www-form-urlencoded;'
  101. }
  102. }else{
  103. headerParam = {
  104. 'Content-Type':'application/x-www-form-urlencoded;'
  105. }
  106. }
  107. that.$axios({
  108. url: url,
  109. method: method,
  110. params: method === 'GET' ? params :null,
  111. data: method === 'POST' ? that.$qs.stringify(params) : null,//将params序列化
  112. headers:headerParam
  113. })
  114. .then(function(response){
  115. if (typeof(success)==='function') {
  116. success(response.data);
  117. }
  118. if (response.data.code==='200'||response.data.code==200 || response.data.code == "-2") {
  119. }else{
  120. that.$notify({
  121. title: '提示',
  122. message: response.data.message,//提示错误
  123. type: 'warning',
  124. duration:'2000'
  125. });
  126. }
  127. })
  128. .catch(function(error){
  129. console.log(error);
  130. })
  131. }
  132. //发送邮件
  133. /**
  134. * [sendEmail description]
  135. * @param {[type]} that [this]
  136. * @param {[type]} params [请求数据]
  137. * @return {[type]} [description]
  138. */
  139. var sendEmail = function(that,params,operation){
  140. var url;
  141. if (operation=='email') {
  142. url=that.urlApi.sendEmail;
  143. }else{
  144. url=that.urlApi.sendMessage;
  145. }
  146. getInfo(that,url,'POST',params,function(res){
  147. if (res.code===200) {
  148. that.$notify({
  149. title: '提示',
  150. message: '发送成功',
  151. type: 'success',
  152. duration:'2000'
  153. });
  154. timer(that);
  155. }else{
  156. }
  157. });
  158. }
  159. //发送短信
  160. /**
  161. * [sendMessage description]
  162. * @param {[type]} that [this]
  163. * @param {[type]} params [请求数据]
  164. * @return {[type]} [description]
  165. */
  166. var sendMessage = function(that,params){
  167. getInfo(that,that.urlApi.sendMessage,'POST',params,function(res){
  168. if (res.code===200) {//注意返回的200是数字还是字符串
  169. that.$notify({
  170. title: '提示',
  171. message: '发送成功',
  172. type: 'success',
  173. duration:'2000'
  174. });
  175. timer(that);
  176. }
  177. });
  178. }
  179. //验证手机号码或者邮箱的格式
  180. /**
  181. * [typeVerify description]
  182. * @param {[type]} str [手机号或者邮箱地址]
  183. * @param {[type]} operation [手机还是邮箱]
  184. * @param {[type]} that [this]
  185. * @return {[type]} [description]
  186. */
  187. var typeVerify = function(str,operation,that){
  188. var emailtype = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
  189. var phonetype = /^1[3|5|7|8][0-9]\d{4,8}$/;
  190. var res;
  191. if (operation=='email') {
  192. res = emailtype.test(str);
  193. if (!res) {
  194. that.$notify({
  195. title:'提示',
  196. message:"邮箱格式错误",
  197. type:'warning',
  198. duration:2000
  199. });
  200. }
  201. return !res;
  202. }else{
  203. res = phonetype.test(str);
  204. if (!res) {
  205. that.$notify({
  206. title:'提示',
  207. message:"手机号码格式错误",
  208. type:'warning',
  209. duration:2000
  210. });
  211. }
  212. return !res;
  213. }
  214. }
  215. //检测个输入框中是否有值存在
  216. /**
  217. * [inputTest description]
  218. * @param {[type]} that [this]
  219. * @return {[type]} [description]
  220. */
  221. var inputTest = function(that){
  222. switch(that.logintype){
  223. case 0:
  224. if (that.regInfo.intype == "") {
  225. that.$notify({
  226. title: '提示',
  227. message: that.intip,
  228. type: 'warning',
  229. duration:'1500'
  230. })
  231. console.log("register");
  232. return false;
  233. };
  234. if (that.regInfo.verifyCode == "") {
  235. that.$notify({
  236. title: '提示',
  237. message: "验证码不能为空",
  238. type: 'warning',
  239. duration:'1500'
  240. })
  241. console.log("这里呢?");
  242. return false;
  243. };
  244. if (that.regInfo.passWord == "") {
  245. that.$notify({
  246. title: '提示',
  247. message: "密码不能为空",
  248. type: 'warning',
  249. duration:'1500'
  250. })
  251. return false;
  252. };
  253. if (that.regInfo.verifyCode == "") {
  254. that.$notify({
  255. title: '提示',
  256. message: "验证码不能为空",
  257. type: 'warning',
  258. duration:'1500'
  259. })
  260. return false;
  261. };
  262. if (that.warnRead == false) {
  263. that.$notify({
  264. title: '提示',
  265. message: "请阅读风险警告",
  266. type: 'warning',
  267. duration:'1500'
  268. })
  269. return false;
  270. };
  271. return true;
  272. case 1:
  273. if (that.loginInfo.intype == "") {
  274. that.$notify({
  275. title: '提示',
  276. message: that.intip,
  277. type: 'warning',
  278. duration:'1500'
  279. })
  280. return false;
  281. }
  282. //密码是否正确不用在这里检测,后台会检测
  283. if (that.loginInfo.passWord == "") {
  284. that.$notify({
  285. title: '提示',
  286. message: "密码不能为空",
  287. type: 'warning',
  288. duration:'1500'
  289. })
  290. return false;
  291. };
  292. return true;
  293. case 2:
  294. if (that.operation == "email") {
  295. if (that.resetInfo.intype == "") {
  296. that.$notify({
  297. title: '提示',
  298. message: that.intip,
  299. type: 'warning',
  300. duration:'1500'
  301. })
  302. return false;
  303. }
  304. }else{
  305. if (that.resetInfo.verifyCode == "") {
  306. that.$notify({
  307. title: '提示',
  308. message: "验证码不能为空",
  309. type: 'warning',
  310. duration:'1500'
  311. })
  312. return false;
  313. }
  314. if (that.resetInfo.oldPassWord == "") {
  315. that.$notify({
  316. title: '提示',
  317. message: "请输入新密码",
  318. type: 'warning',
  319. duration:'1500'
  320. })
  321. return false;
  322. }
  323. if (that.resetInfo.newPassWord == "") {
  324. that.$notify({
  325. title: '提示',
  326. message: "请再次输入新密码",
  327. type: 'warning',
  328. duration:'1500'
  329. })
  330. return false;
  331. }
  332. if (that.resetInfo.oldPassWord != that.resetInfo.newPassWord) {
  333. that.$notify({
  334. title: '提示',
  335. message: '两次输入的密码不同',
  336. type: 'warning',
  337. duration:'1500'
  338. })
  339. return false;
  340. }
  341. }
  342. return true;
  343. }
  344. }
  345. //注册
  346. /**
  347. * [goRegister description]
  348. * @param {[type]} that [this]
  349. * @param {[type]} warnRead [是否阅读风险提示]
  350. * @return {[type]} [description]
  351. */
  352. var goRegister = function(that,flag){
  353. if (flag == false) {
  354. return false;
  355. }else{
  356. let params = {};
  357. if (that.operation=="email") {
  358. params={
  359. "email":that.regInfo.intype,
  360. "password":that.regInfo.passWord,
  361. "code":that.regInfo.verifyCode,
  362. "type":"email"
  363. }
  364. }else{
  365. params={
  366. "mobile":that.regInfo.intype,
  367. "password":that.regInfo.passWord,
  368. "code":that.regInfo.verifyCode,
  369. "type":"sms",
  370. "area":that.coutry
  371. }
  372. }
  373. getInfo(that,that.urlApi.newRegist,"POST",params,function(res){
  374. if (res.code==200) {
  375. that.registered = false;//初始注册状态
  376. that.$notify({
  377. title: '成功',
  378. message: '注册成功!',
  379. type: 'success',
  380. duration:'1500'
  381. })
  382. goLogin(that,"register",true);
  383. }else if (res.code == 1001 && that.operation == "email") {
  384. that.registered = true;
  385. }
  386. })
  387. }
  388. }
  389. //登录
  390. var goLogin = function(that,state,flag){
  391. if (flag != true) {
  392. return false;
  393. }
  394. let params = {
  395. "password":state=="register" ? that.regInfo.passWord : that.loginInfo.passWord,
  396. "redirect_uri":"http://33.cn",
  397. "os":"web",
  398. "platform":"fx66"
  399. };
  400. if (that.operation == "email") {
  401. params.email = state=="register" ? that.regInfo.intype : that.loginInfo.intype;
  402. params.type = "email";
  403. }else{
  404. params.area = that.coutry;
  405. params.mobile = state=="register" ? that.regInfo.intype : that.loginInfo.intype;
  406. params.type = "sms"
  407. }
  408. getInfo(that,that.urlApi.newLogin,"POST",params,function(res){
  409. if (res.code == 200) {
  410. that.$notify({
  411. title: '成功',
  412. message: '登录成功!',
  413. type: 'success',
  414. duration:'1500'
  415. })
  416. that.loginInfo.userName = res.data.username;
  417. that.logintype = 3;
  418. setcookie("token",res.data.access_token,res.data.expires_in);
  419. setcookie("username",res.data.username,res.data.expires_in);
  420. bus.$emit("reUsername");
  421. getStatus(that);
  422. }
  423. });
  424. }
  425. //退出
  426. var loginOut = function(that){
  427. getInfo(that,that.urlApi.loginout,'GET','',function(res){
  428. if (res.code == 200 || res.code == "-2") {
  429. that.$notify({
  430. title: '提示',
  431. message: '退出成功',
  432. type: 'success',
  433. duration:'1500'
  434. });
  435. deletecookie("token");
  436. deletecookie("username");
  437. that.userstate=0;
  438. that.$router.push({path:"/home"});
  439. bus.$emit("goout");
  440. }
  441. })
  442. }
  443. //找回密码发送信件
  444. var resetCode = function(that){
  445. if (that.resetInfo.intype == '') {
  446. return false;
  447. }
  448. let params = {
  449. "email":that.operation=="email"? that.resetInfo.intype:"",
  450. "mobile":that.operation=="mobile"? that.resetInfo.intype:"",
  451. "os":"web",
  452. "type":that.operation=="email"? "email":"sms",
  453. "platkey":"fx66",
  454. "ticket":"",
  455. "businessId":"",
  456. "area":that.operation=="mobile"? that.coutry:""
  457. }
  458. getInfo(that,that.urlApi.resetEmailPassword,"POST",params,function(res){
  459. console.log(params);
  460. if (res.code = 200) {
  461. that.$notify({
  462. title: '成功',
  463. message: '发送成功',
  464. type: 'success',
  465. duration:'1500'
  466. });
  467. timer(that);
  468. }
  469. })
  470. }
  471. //修改密码
  472. var goReset = function(that,flag){
  473. if (!flag) {
  474. return false;
  475. }
  476. let params = {
  477. "type":"sms",
  478. "password":that.resetInfo.oldPassWord,
  479. "password2":that.resetInfo.newPassWord,
  480. "os":"web",
  481. "resettoken":that.resetInfo.verifyCode
  482. }
  483. getInfo(that,that.urlApi.resetPwd,"POST",params,function(res){
  484. if (res.code = 200) {
  485. that.$notify({
  486. title: '提示',
  487. message: '修改密码成功,请使用新密码登录',
  488. type: 'success',
  489. duration:'1500'
  490. });
  491. that.logintype = 1;
  492. }
  493. })
  494. }
  495. //获取状态
  496. var getStatus = function(that){
  497. getInfo(that,that.urlApi.applyStatus,"GET",'',function(res){
  498. if (res.code == 200) {
  499. if (res.data.isApplyed == 1) {
  500. switch (res.data.info.status){
  501. case "0":
  502. that.applicationStatus = 1;
  503. break;
  504. case "1":
  505. that.applicationStatus = 2;
  506. break;
  507. case "-1":
  508. that.applicationStatus = -1;
  509. break;
  510. }
  511. }else{
  512. that.applicationStatus = 0;
  513. }
  514. }
  515. })
  516. }
  517. export{
  518. applyNow,
  519. getStatus,
  520. goReset,
  521. resetCode,
  522. loginOut,
  523. goLogin,
  524. goRegister,
  525. inputTest,
  526. sendMessage,
  527. sendEmail,
  528. setcookie,
  529. getcookie,
  530. deletecookie,
  531. getInfo,
  532. typeVerify
  533. }