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. console.log(that.$qs.stringify(params));
  108. that.$axios({
  109. url: url,
  110. method: method,
  111. params: method === 'GET' ? params :null,
  112. data: method === 'POST' ? that.$qs.stringify(params) : null,//将params序列化
  113. headers:headerParam
  114. })
  115. .then(function(response){
  116. if (typeof(success)==='function') {
  117. success(response.data);
  118. }
  119. if (response.data.code==='200'||response.data.code==200 || response.data.code == "-2") {
  120. }else{
  121. that.$notify({
  122. title: '提示',
  123. message: response.data.message,//提示错误
  124. type: 'warning',
  125. duration:'2000'
  126. });
  127. }
  128. })
  129. .catch(function(error){
  130. console.log(error);
  131. })
  132. }
  133. //发送邮件
  134. /**
  135. * [sendEmail description]
  136. * @param {[type]} that [this]
  137. * @param {[type]} params [请求数据]
  138. * @return {[type]} [description]
  139. */
  140. var sendEmail = function(that,params,operation){
  141. var url;
  142. if (operation=='email') {
  143. url=that.urlApi.sendEmail;
  144. }else{
  145. url=that.urlApi.sendMessage;
  146. }
  147. getInfo(that,url,'POST',params,function(res){
  148. if (res.code===200) {
  149. that.$notify({
  150. title: '提示',
  151. message: '发送成功',
  152. type: 'success',
  153. duration:'2000'
  154. });
  155. timer(that);
  156. }else{
  157. }
  158. });
  159. }
  160. //发送短信
  161. /**
  162. * [sendMessage description]
  163. * @param {[type]} that [this]
  164. * @param {[type]} params [请求数据]
  165. * @return {[type]} [description]
  166. */
  167. var sendMessage = function(that,params){
  168. getInfo(that,that.urlApi.sendMessage,'POST',params,function(res){
  169. if (res.code===200) {//注意返回的200是数字还是字符串
  170. that.$notify({
  171. title: '提示',
  172. message: '发送成功',
  173. type: 'success',
  174. duration:'2000'
  175. });
  176. timer(that);
  177. }
  178. });
  179. }
  180. //验证手机号码或者邮箱的格式
  181. /**
  182. * [typeVerify description]
  183. * @param {[type]} str [手机号或者邮箱地址]
  184. * @param {[type]} operation [手机还是邮箱]
  185. * @param {[type]} that [this]
  186. * @return {[type]} [description]
  187. */
  188. var typeVerify = function(str,operation,that){
  189. var emailtype = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
  190. var phonetype = /^1[3|5|7|8][0-9]\d{4,8}$/;
  191. var res;
  192. if (operation=='email') {
  193. res = emailtype.test(str);
  194. if (!res) {
  195. that.$notify({
  196. title:'提示',
  197. message:"邮箱格式错误",
  198. type:'warning',
  199. duration:2000
  200. });
  201. }
  202. return !res;
  203. }else{
  204. res = phonetype.test(str);
  205. if (!res) {
  206. that.$notify({
  207. title:'提示',
  208. message:"手机号码格式错误",
  209. type:'warning',
  210. duration:2000
  211. });
  212. }
  213. return !res;
  214. }
  215. }
  216. //检测个输入框中是否有值存在
  217. /**
  218. * [inputTest description]
  219. * @param {[type]} that [this]
  220. * @return {[type]} [description]
  221. */
  222. var inputTest = function(that){
  223. switch(that.logintype){
  224. case 0:
  225. if (that.regInfo.intype == "") {
  226. that.$notify({
  227. title: '提示',
  228. message: that.tiptip,
  229. type: 'warning',
  230. duration:'1500'
  231. })
  232. console.log("register");
  233. return false;
  234. };
  235. if (that.regInfo.verifyCode == "") {
  236. that.$notify({
  237. title: '提示',
  238. message: "验证码不能为空",
  239. type: 'warning',
  240. duration:'1500'
  241. })
  242. console.log("这里呢?");
  243. return false;
  244. };
  245. if (that.regInfo.passWord == "") {
  246. that.$notify({
  247. title: '提示',
  248. message: "密码不能为空",
  249. type: 'warning',
  250. duration:'1500'
  251. })
  252. return false;
  253. };
  254. if (that.regInfo.verifyCode == "") {
  255. that.$notify({
  256. title: '提示',
  257. message: "验证码不能为空",
  258. type: 'warning',
  259. duration:'1500'
  260. })
  261. return false;
  262. };
  263. if (that.warnRead == false) {
  264. that.$notify({
  265. title: '提示',
  266. message: "请阅读风险警告",
  267. type: 'warning',
  268. duration:'1500'
  269. })
  270. return false;
  271. };
  272. return true;
  273. case 1:
  274. if (that.loginInfo.intype == "") {
  275. that.$notify({
  276. title: '提示',
  277. message: that.tiptip,
  278. type: 'warning',
  279. duration:'1500'
  280. })
  281. return false;
  282. }
  283. //密码是否正确不用在这里检测,后台会检测
  284. if (that.loginInfo.passWord == "") {
  285. that.$notify({
  286. title: '提示',
  287. message: "密码不能为空",
  288. type: 'warning',
  289. duration:'1500'
  290. })
  291. return false;
  292. };
  293. return true;
  294. case 2:
  295. if (that.operation == "mobile") {
  296. if (that.resetInfo.intype == "") {
  297. that.$notify({
  298. title: '提示',
  299. message: that.tiptip,
  300. type: 'warning',
  301. duration:'1500'
  302. })
  303. return false;
  304. }
  305. }else{
  306. if (that.resetInfo.verifyCode == "") {
  307. that.$notify({
  308. title: '提示',
  309. message: "验证码不能为空",
  310. type: 'warning',
  311. duration:'1500'
  312. })
  313. return false;
  314. }
  315. if (that.resetInfo.oldPassWord == "") {
  316. that.$notify({
  317. title: '提示',
  318. message: "请输入新密码",
  319. type: 'warning',
  320. duration:'1500'
  321. })
  322. return false;
  323. }
  324. if (that.resetInfo.newPassWord == "") {
  325. that.$notify({
  326. title: '提示',
  327. message: "请再次输入新密码",
  328. type: 'warning',
  329. duration:'1500'
  330. })
  331. return false;
  332. }
  333. if (that.resetInfo.oldPassWord == that.resetInfo.newPassWord) {
  334. that.$notify({
  335. title: '提示',
  336. message: '两次输入的密码不同',
  337. type: 'warning',
  338. duration:'1500'
  339. })
  340. return false;
  341. }
  342. }
  343. return true;
  344. }
  345. }
  346. //注册
  347. /**
  348. * [goRegister description]
  349. * @param {[type]} that [this]
  350. * @param {[type]} warnRead [是否阅读风险提示]
  351. * @return {[type]} [description]
  352. */
  353. var goRegister = function(that,flag){
  354. if (flag == false) {
  355. return false;
  356. }else{
  357. let params = {};
  358. if (that.operation=="email") {
  359. params={
  360. "email":that.regInfo.intype,
  361. "password":that.regInfo.passWord,
  362. "code":that.regInfo.verifyCode,
  363. "type":"email"
  364. }
  365. }else{
  366. params={
  367. "mobile":that.regInfo.intype,
  368. "password":that.regInfo.passWord,
  369. "code":that.regInfo.verifyCode,
  370. "type":"sms",
  371. "area":that.coutry
  372. }
  373. }
  374. getInfo(that,that.urlApi.newRegist,"POST",params,function(res){
  375. if (res.code==200) {
  376. that.registered = false;//初始注册状态
  377. that.$notify({
  378. title: '成功',
  379. message: '注册成功!',
  380. type: 'success',
  381. duration:'1500'
  382. })
  383. goLogin(that,"register",true);
  384. }else if (res.code == 1001 && that.operation == "email") {
  385. that.registered = true;
  386. }
  387. })
  388. }
  389. }
  390. //登录
  391. var goLogin = function(that,state,flag){
  392. if (flag != true) {
  393. return false;
  394. }
  395. let params = {
  396. "password":state=="register" ? that.regInfo.passWord : that.loginInfo.passWord,
  397. "redirect_uri":"http://33.cn",
  398. "os":"web",
  399. "platform":"fx66"
  400. };
  401. if (that.operation == "email") {
  402. params.email = state=="register" ? that.regInfo.intype : that.loginInfo.intype;
  403. params.type = "email";
  404. }else{
  405. params.area = that.coutry;
  406. params.mobile = state=="register" ? that.regInfo.intype : that.loginInfo.intype;
  407. params.type = "sms"
  408. }
  409. getInfo(that,that.urlApi.newLogin,"POST",params,function(res){
  410. if (res.code == 200) {
  411. that.$notify({
  412. title: '成功',
  413. message: '登录成功!',
  414. type: 'success',
  415. duration:'1500'
  416. })
  417. that.loginInfo.userName = res.data.username;
  418. that.logintype = 3;
  419. setcookie("token",res.data.access_token,res.data.expires_in);
  420. setcookie("username",res.data.username,res.data.expires_in);
  421. bus.$emit("reUsername");
  422. getStatus(that);
  423. }
  424. });
  425. }
  426. //退出
  427. var loginOut = function(that){
  428. getInfo(that,that.urlApi.loginout,'GET','',function(res){
  429. if (res.code == 200 || res.code == "-2") {
  430. that.$notify({
  431. title: '提示',
  432. message: '退出成功',
  433. type: 'success',
  434. duration:'1500'
  435. });
  436. deletecookie("token");
  437. deletecookie("username");
  438. that.userstate=0;
  439. that.$router.push({path:"/home"});
  440. bus.$emit("goout");
  441. }
  442. })
  443. }
  444. //找回密码发送信件
  445. var resetCode = function(that){
  446. inputTest(that);
  447. if (that.resetInfo.intype == '') {
  448. return false;
  449. }
  450. let params = {
  451. "email":that.operation=="email"? that.resetInfo.intype:"",
  452. "mobile":that.operation=="mobile"? that.resetInfo.intype:"",
  453. "os":"web",
  454. "type":that.operation=="email"?"email":"sms",
  455. "platkey":"fx66",
  456. "ticket":"",
  457. "businessId":""
  458. }
  459. getInfo(that,that.urlApi.resetEmailPassword,"POST",params,function(res){
  460. console.log(res.code);
  461. if (res.code = 200) {
  462. that.$notify({
  463. title: '成功',
  464. message: '发送成功',
  465. type: 'success',
  466. duration:'1500'
  467. });
  468. timer(that);
  469. }
  470. })
  471. }
  472. //修改密码
  473. var goReset = function(that,flag){
  474. if (!flag) {
  475. return false;
  476. }
  477. let params = {
  478. "type":"sms",
  479. "password":that.resetInfo.oldPassWord,
  480. "password2":that.resetInfo.newPassWord,
  481. "os":"web",
  482. "resettoken":that.resetInfo.verifyCode
  483. }
  484. getInfo(that,that.urlApi.resetPwd,"POST",params,function(res){
  485. if (res.code = 200) {
  486. that.$notify({
  487. title: '提示',
  488. message: '修改密码成功,请使用新密码登录',
  489. type: 'success',
  490. duration:'1500'
  491. });
  492. that.logintype = 1;
  493. }
  494. })
  495. }
  496. //获取状态
  497. var getStatus = function(that){
  498. getInfo(that,that.urlApi.applyStatus,"POST",'',function(res){
  499. if (res.code == 200) {
  500. if (res.data.isApply == 1) {
  501. switch (res.data.info.status){
  502. case 0:
  503. that.applicationStatus = 1;
  504. break;
  505. case 1:
  506. that.applicationStatus = 2;
  507. break;
  508. case -1:
  509. that.applicationStatus = -1;
  510. break;
  511. }
  512. }else{
  513. that.applicationStatus = 0;
  514. }
  515. }
  516. })
  517. }
  518. export{
  519. applyNow,
  520. getStatus,
  521. goReset,
  522. resetCode,
  523. loginOut,
  524. goLogin,
  525. goRegister,
  526. inputTest,
  527. sendMessage,
  528. sendEmail,
  529. setcookie,
  530. getcookie,
  531. deletecookie,
  532. getInfo,
  533. typeVerify
  534. }