index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. // duration: 3000
  362. // });
  363. }
  364. }).catch(res => {
  365. })
  366. }
  367. })
  368. },
  369. // 开始倒计时的函数
  370. startCountdown: function() {
  371. let that = this;
  372. let countdown = 60; // 设置倒计时时间
  373. that.setData({
  374. countdown: countdown + '秒后重试',
  375. disabled:true
  376. });
  377. that.data.timer = setInterval(() => {
  378. countdown--;
  379. if (countdown <= 0) {
  380. clearInterval(that.data.timer); // 清除定时器
  381. that.setData({
  382. countdown: '获取验证码',
  383. timer: null,
  384. disabled:false
  385. });
  386. } else {
  387. that.setData({
  388. countdown: countdown + '秒后重试',
  389. });
  390. }
  391. }, 1000);
  392. },
  393. getCaptcha() {
  394. const that=this
  395. if (this.data.mobile.length != 11 || this.data.mobile.startsWith("1") == false) {
  396. wx.showToast({
  397. title: '请输入正确手机号',
  398. icon: 'none',
  399. duration: 3000
  400. });
  401. return;
  402. }
  403. console.info(api.AuthCaptcha)
  404. util.request(api.AuthCaptcha, {
  405. mobile: this.data.mobile
  406. }, "POST").then(res => {
  407. if (res.errno == 0){
  408. wx.showToast({
  409. title: '验证码发送成功',
  410. duration: 3000
  411. });
  412. that.startCountdown()
  413. }
  414. else
  415. wx.showToast({
  416. title: res.errmsg,
  417. duration: 3000
  418. });
  419. }).catch(res => {
  420. console.info('getCaptcha error')
  421. wx.showToast({
  422. title: res.errMsg,
  423. icon: 'error',
  424. duration: 3000
  425. });
  426. })
  427. },
  428. bindMobileInput(e) {
  429. this.setData({
  430. mobile: e.detail.value
  431. });
  432. },
  433. bindcaptchaInput(e) {
  434. this.setData({
  435. captcha: e.detail.value
  436. });
  437. },
  438. goInvite() {
  439. wx.navigateTo({
  440. url: '/pages/upgrade/invite/invite',
  441. })
  442. },
  443. goTemp() {
  444. wx.navigateTo({
  445. url: '/pages/temp/login/login',
  446. });
  447. },
  448. bindJmsMobileInput(e) {
  449. this.setData({
  450. jmsMobile: e.detail.value
  451. });
  452. },
  453. bindJmsCaptchaInput(e) {
  454. this.setData({
  455. jmsCaptcha: e.detail.value
  456. });
  457. },
  458. getJmsCaptcha() {
  459. const that =this
  460. if (this.data.jmsMobile.length != 11 || this.data.jmsMobile.startsWith("1") == false) {
  461. wx.showToast({
  462. title: '请输入正确手机号',
  463. icon: 'none',
  464. duration: 3000
  465. });
  466. return;
  467. }
  468. jmsUtil.request(jms.SendLoginCode, {
  469. tel: this.data.jmsMobile
  470. }, "POST").then(res => {
  471. if (res.errno == 0){
  472. wx.showToast({
  473. title: '验证码发送成功',
  474. duration: 3000
  475. });
  476. that.startCountdown();
  477. }
  478. else
  479. wx.showToast({
  480. title: res.errmsg,
  481. duration: 3000,
  482. icon: 'none'
  483. });
  484. }).catch(res => {
  485. console.info('getCaptcha error')
  486. wx.showToast({
  487. title: res.errmsg,
  488. icon: 'none',
  489. duration: 3000
  490. });
  491. })
  492. },
  493. loginTemp() {
  494. if (this.data.jmsMobile.length != 11 || this.data.jmsMobile.startsWith("1") == false) {
  495. wx.showToast({
  496. title: '请输入正确手机号',
  497. icon: 'none',
  498. duration: 3000
  499. });
  500. return;
  501. } else if (this.data.jmsCaptcha == '') {
  502. wx.showToast({
  503. title: '请输入验证码',
  504. icon: 'none',
  505. duration: 3000
  506. });
  507. return;
  508. } else {
  509. //登录
  510. jmsUtil.request(jms.PartJobLogin, {
  511. tel: this.data.jmsMobile,
  512. verifyCode: this.data.jmsCaptcha,
  513. }, "POST").then(res => {
  514. //存储用户信息
  515. console.info("登录成功")
  516. if (res.errno == 0) {
  517. app.globalData.hasLogin = true;
  518. let user = res.data.user;
  519. user.isPartJob = res.data.isPartJob;
  520. wx.setStorageSync('userInfo', user);
  521. wx.setStorageSync('token', res.data.token);
  522. wx.redirectTo({
  523. url: '/pages/temp/serviceOrder/serviceOrder',
  524. });
  525. } else if (res.errno === 301) {
  526. wx.showModal({
  527. title: '提示',
  528. content: '您还未注册,是否现在去注册?',
  529. // showCancel:false,
  530. complete: (res) => {
  531. if (res.cancel) {
  532. }
  533. if (res.confirm) {
  534. wx.navigateTo({
  535. url: '/pages/temp/register/register',
  536. })
  537. }
  538. }
  539. })
  540. } else {
  541. // util.showErrorToast(res.errmsg);
  542. wx.showToast({
  543. title: res.errmsg,
  544. icon: 'none',
  545. duration: 3000
  546. });
  547. }
  548. }).catch(err => {
  549. })
  550. }
  551. },
  552. bindJmsMobileInput(e) {
  553. this.setData({
  554. jmsMobile: e.detail.value
  555. });
  556. },
  557. bindJmsCaptchaInput(e) {
  558. this.setData({
  559. jmsCaptcha: e.detail.value
  560. });
  561. },
  562. goRegister() {
  563. wx.navigateTo({
  564. url: '/pages/temp/register/register',
  565. })
  566. },
  567. checkProBtn() {
  568. wx.navigateTo({
  569. url: '/pages/temp/jzxhAppletsUserPro/jzxhAppletsUserPro'
  570. })
  571. },
  572. chooseLogin(){
  573. this.setData({
  574. isJms:!this.data.isJms
  575. })
  576. }
  577. })