index.js 15 KB

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