index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. const util = require('../../utils/util.js');
  5. const jmsUtil = require('../../utils/jmsUtil.js');
  6. const user = require('../../utils/user.js');
  7. const api = require('../../api/api.js');
  8. const jms = require('../../api/jms.js');
  9. Page({
  10. data: {
  11. userInfo: {},
  12. mobile: '',
  13. captcha: '', //验证码
  14. code: '', //小程序登录code
  15. isUat: false,
  16. isPro: true,
  17. isJms: false,
  18. jmsMobile: '',
  19. jmsCaptcha: '',
  20. loginType: '' ,//0 全部显示 1:显示体验版 2:显示加盟商
  21. disabled:false,//控制获取验证码按钮是否禁用
  22. countdown:'获取验证码',
  23. timer:null//用于保存定时器
  24. },
  25. onShareAppMessage: function () {
  26. let that = this;
  27. //获取分享id 用来追踪链接
  28. //引荐人和非引荐人的 链接不同
  29. var path = '/pages/index/index';
  30. if (that.data.loginType)
  31. path = '/pages/index/index?type=' + that.data.loginType;
  32. return {
  33. title: "鲸致生活-服务者小程序",
  34. path: path,
  35. imageUrl: '/static/images/logo.png'
  36. }
  37. },
  38. onLoad(options) {
  39. //1 体验版 2加盟商兼职
  40. let loginType = options.type;
  41. if (!loginType)
  42. loginType = wx.getStorageSync('loginType');
  43. console.info(loginType);
  44. if (loginType) {
  45. this.setData({
  46. loginType: loginType
  47. })
  48. wx.setStorageSync('loginType', loginType);
  49. if (loginType == 2) {
  50. this.setData({
  51. isJms: true,
  52. isUat: false,
  53. isPro: false
  54. })
  55. } else if (loginType == 1) {
  56. this.setData({
  57. isJms: false,
  58. isUat: true,
  59. isPro: false
  60. })
  61. }
  62. console.info(loginType)
  63. }
  64. let userInfo = wx.getStorageSync('userInfo');
  65. if (userInfo && userInfo.type == 1) return; //小哥登录 不验证地理位置
  66. this.getLoginCode();
  67. // map.getCity().then(res => {}).catch(res => {
  68. // if (res == 3) {
  69. // wx.showModal({
  70. // title: '提醒',
  71. // showCancel: false,
  72. // content: '当前区域,无服务门店'
  73. // })
  74. // } else {
  75. // wx.showModal({
  76. // title: '提醒',
  77. // showCancel: false,
  78. // content: '您未授权,部分功能将受限'
  79. // })
  80. // }
  81. // })
  82. },
  83. onUatChange(e) { //是否体验版
  84. console.info(e);
  85. this.setData({
  86. isUat: true,
  87. isPro: false,
  88. isJms: false,
  89. });
  90. // this.setData({
  91. // isUat: e.detail,
  92. // isPro:!e.detail,
  93. // isJms:!e.detail,
  94. // });
  95. console.log('uat....', e.detail);
  96. },
  97. onProChange(e) {
  98. this.setData({
  99. isPro: true,
  100. isUat: false,
  101. isJms: false,
  102. });
  103. // this.setData({
  104. // isPro: e.detail,
  105. // isUat:!e.detail,
  106. // isJms:!e.detail,
  107. // });
  108. },
  109. onJmsChange(e) {
  110. this.setData({
  111. isPro: false,
  112. isUat: false,
  113. isJms: true,
  114. });
  115. // this.setData({
  116. // isPro: !e.detail,
  117. // isUat:!e.detail,
  118. // isJms:e.detail,
  119. // });
  120. },
  121. goShelter() {
  122. wx.navigateTo({
  123. url: '/pages/shelter/index',
  124. })
  125. },
  126. onShow() {
  127. let that = this;
  128. //验证是否登陆过
  129. user.checkLogin().then(res => {
  130. app.globalData.hasLogin = true;
  131. let userInfo = wx.getStorageSync('userInfo');
  132. if (userInfo.isPartJob === true) {
  133. // 兼职人员
  134. wx.redirectTo({
  135. url: '/pages/temp/serviceOrder/serviceOrder',
  136. })
  137. } else {
  138. wx.switchTab({
  139. url: '/pages/new/serviceOrder/serviceOrder',
  140. success: function (e) {
  141. var page = getCurrentPages().pop();
  142. if (page == undefined || page == null) return;
  143. page.onLoad();
  144. }
  145. })
  146. }
  147. }).catch(res => {
  148. that.getLoginCode();
  149. })
  150. },
  151. getLoginCode() {
  152. let that = this;
  153. user.login().then(res => {
  154. console.info(res)
  155. that.setData({
  156. code: res.code
  157. })
  158. })
  159. },
  160. getPhoneNumber: function (e) {
  161. wx.setStorageSync('isUat', false);
  162. // var ivObj = e.detail.iv
  163. // var telObj = e.detail.encryptedData
  164. if (this.data.mobile.length != 11 || this.data.mobile.startsWith("1") == false) {
  165. wx.showToast({
  166. title: '请输入正确手机号',
  167. icon: 'none',
  168. duration: 3000
  169. });
  170. return;
  171. } else if (this.data.captcha == '') {
  172. wx.showToast({
  173. title: '请输入验证码',
  174. icon: 'none',
  175. duration: 3000
  176. });
  177. return;
  178. }
  179. var that = this;
  180. //------执行Login---------
  181. util.request(api.AuthLoginByCaptcha, {
  182. mobile: that.data.mobile,
  183. captcha: that.data.captcha,
  184. // userInfo: res.userInfo,
  185. code: that.data.code
  186. }, 'POST').then(function (res) {
  187. // util.request(api.AuthRegister, {
  188. // code: that.data.code,
  189. // encryptedData: telObj,
  190. // iv: ivObj,
  191. // loginType: 1
  192. // }, 'POST').then(function (res) {
  193. console.info(res)
  194. if (res.errno === 0) {
  195. //存储用户信息
  196. console.info("登录成功2")
  197. app.globalData.hasLogin = true;
  198. wx.setStorageSync('userInfo', res.data.userInfo);
  199. wx.setStorageSync('token', res.data.token);
  200. that.setData({
  201. mobile: res.data.mobile
  202. })
  203. wx.showToast({
  204. title: '授权注册成功',
  205. icon: 'success',
  206. duration: 2000,
  207. complete: function () {
  208. // if(res.data.userInfo.type==1)
  209. // wx.navigateTo({
  210. // url: '/pages/pre/index',
  211. // })
  212. // else
  213. // wx.navigateTo({
  214. // url: '/pages/servant/servant',
  215. // })
  216. wx.switchTab({
  217. url: '/pages/new/serviceOrder/serviceOrder',
  218. success: function (e) {
  219. var page = getCurrentPages().pop();
  220. if (page == undefined || page == null) return;
  221. page.onLoad();
  222. }
  223. })
  224. }
  225. })
  226. } else {
  227. console.info(res);
  228. that.getLoginCode();
  229. wx.showToast({
  230. title: res.errmsg,
  231. duration: 3000,
  232. icon: 'none'
  233. });
  234. }
  235. });
  236. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  237. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
  238. wx.showModal({
  239. title: '警告',
  240. content: '您点击了拒绝授权,功能无法使用!!!',
  241. showCancel: false,
  242. confirmText: '确定',
  243. success: function (res) {
  244. // 用户没有授权成功,不需要改变 isHide 的值
  245. if (res.confirm) {
  246. wx.switchTab({
  247. url: '/pages/index/index',
  248. success: function (e) {
  249. var page = getCurrentPages().pop();
  250. if (page == undefined || page == null) return;
  251. page.onLoad();
  252. }
  253. })
  254. };
  255. }
  256. })
  257. }
  258. },
  259. /**
  260. * 如果勾选了体验版 流程如下
  261. * 1,先用手机号和验证码 去正式库验证通过
  262. * 2,将api.WxApiRoot修改为体验测试版本的WxApiRoot_Uat
  263. * 3,再次提交验证 将输入手机号修改为体验指定的手机号,完成登录
  264. * @param {*} e
  265. */
  266. getUserProfile(e) {
  267. wx.setStorageSync('isUat', true);
  268. if (this.data.mobile.length != 11 || this.data.mobile.startsWith("1") == false) {
  269. wx.showToast({
  270. title: '请输入正确手机号',
  271. icon: 'none',
  272. duration: 3000
  273. });
  274. return;
  275. } else if (this.data.captcha == '') {
  276. wx.showToast({
  277. title: '请输入验证码',
  278. icon: 'none',
  279. duration: 3000
  280. });
  281. return;
  282. }
  283. let that = this;
  284. wx.getUserProfile({
  285. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  286. success: (res) => {
  287. console.log(res)
  288. this.setData({
  289. userInfo: res.userInfo,
  290. hasUserInfo: true
  291. })
  292. //登录
  293. util.request(api.AuthLoginByCaptcha, {
  294. mobile: that.data.mobile,
  295. captcha: that.data.captcha,
  296. userInfo: res.userInfo,
  297. code: that.data.code
  298. }, "POST").then(res => {
  299. //存储用户信息
  300. console.info("登录成功")
  301. if (res.errno == 0) {
  302. //如果是uat版 再次验证登录
  303. if (that.data.isUat) {
  304. console.info('uat')
  305. //替换链接
  306. api.WxApiRoot = api.WxApiRoot_Uat;
  307. util.request(api.AuthLoginUat,{
  308. phoneNumber: that.data.mobile
  309. }, "GET").then(ress => {
  310. app.globalData.hasLogin = true;
  311. wx.setStorageSync('userInfo', ress.data.userInfo);
  312. wx.setStorageSync('token', ress.data.token);
  313. console.info(wx.getStorageSync('token'))
  314. that.setData({
  315. mobile: ress.data.mobile
  316. });
  317. wx.showToast({
  318. title: '授权注册成功',
  319. icon: 'success',
  320. duration: 2000,
  321. complete: function () {
  322. wx.switchTab({
  323. url: '/pages/new/serviceOrder/serviceOrder',
  324. success: function (e) {
  325. var page = getCurrentPages().pop();
  326. if (page == undefined || page == null) return;
  327. page.onLoad();
  328. }
  329. })
  330. }
  331. })
  332. })
  333. } else {
  334. console.info('not uat')
  335. app.globalData.hasLogin = true;
  336. wx.setStorageSync('userInfo', res.data.userInfo);
  337. wx.setStorageSync('token', res.data.token);
  338. that.setData({
  339. mobile: res.data.mobile
  340. });
  341. wx.showToast({
  342. title: '授权注册成功',
  343. icon: 'success',
  344. duration: 2000,
  345. complete: function () {
  346. wx.switchTab({
  347. url: '/pages/new/serviceOrder/serviceOrder',
  348. success: function (e) {
  349. var page = getCurrentPages().pop();
  350. if (page == undefined || page == null) return;
  351. page.onLoad();
  352. }
  353. })
  354. }
  355. })
  356. }
  357. } else {
  358. // util.showErrorToast(res.errmsg);
  359. wx.showToast({
  360. title: res.errmsg,
  361. icon:'none',
  362. duration: 3000
  363. });
  364. }
  365. }).catch(res => {
  366. })
  367. }
  368. })
  369. },
  370. // 开始倒计时的函数
  371. startCountdown: function() {
  372. let that = this;
  373. let countdown = 60; // 设置倒计时时间
  374. that.setData({
  375. countdown: countdown + '秒后重试',
  376. disabled:true
  377. });
  378. that.data.timer = setInterval(() => {
  379. countdown--;
  380. if (countdown <= 0) {
  381. clearInterval(that.data.timer); // 清除定时器
  382. that.setData({
  383. countdown: '获取验证码',
  384. timer: null,
  385. disabled:false
  386. });
  387. } else {
  388. that.setData({
  389. countdown: countdown + '秒后重试',
  390. });
  391. }
  392. }, 1000);
  393. },
  394. getCaptcha() {
  395. const that=this
  396. if (this.data.mobile.length != 11 || this.data.mobile.startsWith("1") == false) {
  397. wx.showToast({
  398. title: '请输入正确手机号',
  399. icon: 'none',
  400. duration: 3000
  401. });
  402. return;
  403. }
  404. console.info(api.AuthCaptcha)
  405. util.request(api.AuthCaptcha, {
  406. mobile: this.data.mobile
  407. }, "POST").then(res => {
  408. if (res.errno == 0){
  409. wx.showToast({
  410. title: '验证码发送成功',
  411. duration: 3000
  412. });
  413. that.startCountdown()
  414. }
  415. else
  416. wx.showToast({
  417. title: res.errmsg,
  418. duration: 3000
  419. });
  420. }).catch(res => {
  421. console.info('getCaptcha error')
  422. wx.showToast({
  423. title: res.errMsg,
  424. icon: 'error',
  425. duration: 3000
  426. });
  427. })
  428. },
  429. bindMobileInput(e) {
  430. this.setData({
  431. mobile: e.detail.value
  432. });
  433. },
  434. bindcaptchaInput(e) {
  435. this.setData({
  436. captcha: e.detail.value
  437. });
  438. },
  439. goInvite() {
  440. wx.navigateTo({
  441. url: '/pages/upgrade/invite/invite',
  442. })
  443. },
  444. goTemp() {
  445. wx.navigateTo({
  446. url: '/pages/temp/login/login',
  447. });
  448. },
  449. bindJmsMobileInput(e) {
  450. this.setData({
  451. jmsMobile: e.detail.value
  452. });
  453. },
  454. bindJmsCaptchaInput(e) {
  455. this.setData({
  456. jmsCaptcha: e.detail.value
  457. });
  458. },
  459. getJmsCaptcha() {
  460. const that =this
  461. if (this.data.jmsMobile.length != 11 || this.data.jmsMobile.startsWith("1") == false) {
  462. wx.showToast({
  463. title: '请输入正确手机号',
  464. icon: 'none',
  465. duration: 3000
  466. });
  467. return;
  468. }
  469. jmsUtil.request(jms.SendLoginCode, {
  470. tel: this.data.jmsMobile
  471. }, "POST").then(res => {
  472. if (res.errno == 0){
  473. wx.showToast({
  474. title: '验证码发送成功',
  475. duration: 3000
  476. });
  477. that.startCountdown();
  478. }
  479. else
  480. wx.showToast({
  481. title: res.errmsg,
  482. duration: 3000,
  483. icon: 'none'
  484. });
  485. }).catch(res => {
  486. console.info('getCaptcha error')
  487. wx.showToast({
  488. title: res.errmsg,
  489. icon: 'none',
  490. duration: 3000
  491. });
  492. })
  493. },
  494. loginTemp() {
  495. if (this.data.jmsMobile.length != 11 || this.data.jmsMobile.startsWith("1") == false) {
  496. wx.showToast({
  497. title: '请输入正确手机号',
  498. icon: 'none',
  499. duration: 3000
  500. });
  501. return;
  502. } else if (this.data.jmsCaptcha == '') {
  503. wx.showToast({
  504. title: '请输入验证码',
  505. icon: 'none',
  506. duration: 3000
  507. });
  508. return;
  509. } else {
  510. //登录
  511. jmsUtil.request(jms.PartJobLogin, {
  512. tel: this.data.jmsMobile,
  513. verifyCode: this.data.jmsCaptcha,
  514. }, "POST").then(res => {
  515. //存储用户信息
  516. console.info("登录成功")
  517. if (res.errno == 0) {
  518. app.globalData.hasLogin = true;
  519. let user = res.data.user;
  520. user.isPartJob = res.data.isPartJob;
  521. wx.setStorageSync('userInfo', user);
  522. wx.setStorageSync('token', res.data.token);
  523. wx.redirectTo({
  524. url: '/pages/temp/serviceOrder/serviceOrder',
  525. });
  526. } else if (res.errno === 301) {
  527. wx.showModal({
  528. title: '提示',
  529. content: '您还未注册,是否现在去注册?',
  530. // showCancel:false,
  531. complete: (res) => {
  532. if (res.cancel) {
  533. }
  534. if (res.confirm) {
  535. wx.navigateTo({
  536. url: '/pages/temp/register/register',
  537. })
  538. }
  539. }
  540. })
  541. } else {
  542. // util.showErrorToast(res.errmsg);
  543. wx.showToast({
  544. title: res.errmsg,
  545. icon: 'none',
  546. duration: 3000
  547. });
  548. }
  549. }).catch(err => {
  550. })
  551. }
  552. },
  553. bindJmsMobileInput(e) {
  554. this.setData({
  555. jmsMobile: e.detail.value
  556. });
  557. },
  558. bindJmsCaptchaInput(e) {
  559. this.setData({
  560. jmsCaptcha: e.detail.value
  561. });
  562. },
  563. goRegister() {
  564. wx.navigateTo({
  565. url: '/pages/temp/register/register',
  566. })
  567. },
  568. checkProBtn() {
  569. wx.navigateTo({
  570. url: '/pages/temp/jzxhAppletsUserPro/jzxhAppletsUserPro'
  571. })
  572. },
  573. chooseLogin(){
  574. this.setData({
  575. isJms:!this.data.isJms
  576. })
  577. }
  578. })