login.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. const util = require('../../../utils/jmsUtil.js');
  5. const user = require('../../../utils/user.js');
  6. const api = require('../../../api/jms.js');
  7. Page({
  8. data: {
  9. isChecked:false,
  10. userInfo: {},
  11. mobile: '',
  12. captcha: '', //验证码
  13. code: '', //小程序登录code
  14. isUat: false,
  15. isPro:true
  16. },
  17. onLoad() {
  18. let userInfo = wx.getStorageSync('userInfo');
  19. if (userInfo && userInfo.type == 1) {
  20. }
  21. },
  22. onUatChange(e) { //是否体验版
  23. console.info(e);
  24. this.setData({
  25. isUat: e.detail,
  26. isPro:!e.detail,
  27. });
  28. console.log('uat....',e.detail);
  29. },
  30. onProChange(e){
  31. this.setData({
  32. isPro: e.detail,
  33. isUat:!e.detail
  34. });
  35. },
  36. goShelter() {
  37. wx.navigateTo({
  38. url: '/pages/shelter/index',
  39. })
  40. },
  41. onShow() {
  42. let that = this;
  43. //验证是否登陆过
  44. user.checkLogin().then(res => {
  45. app.globalData.hasLogin = true;
  46. wx.redirectTo({
  47. url: '/pages/temp/serviceOrder/serviceOrder',
  48. });
  49. }).catch(res => {
  50. })
  51. },
  52. getLoginCode() {
  53. let that = this;
  54. user.login().then(res => {
  55. console.info(res)
  56. that.setData({
  57. code: res.code
  58. })
  59. })
  60. },
  61. getPhoneNumber: function (e) {
  62. wx.setStorageSync('isUat', false);
  63. var ivObj = e.detail.iv
  64. var telObj = e.detail.encryptedData
  65. var that = this;
  66. //------执行Login---------
  67. util.request(api.AuthRegister, {
  68. code: that.data.code,
  69. encryptedData: telObj,
  70. iv: ivObj,
  71. loginType: 1
  72. }, 'POST').then(function (res) {
  73. console.info(res)
  74. if (res.errno === 0) {
  75. //存储用户信息
  76. console.info("登录成功2")
  77. app.globalData.hasLogin = true;
  78. wx.setStorageSync('userInfo', res.data.userInfo);
  79. wx.setStorageSync('token', res.data.token);
  80. that.setData({
  81. mobile: res.data.mobile
  82. })
  83. wx.showToast({
  84. title: '授权注册成功',
  85. icon: 'success',
  86. duration: 2000,
  87. complete: function () {
  88. // if(res.data.userInfo.type==1)
  89. // wx.navigateTo({
  90. // url: '/pages/pre/index',
  91. // })
  92. // else
  93. // wx.navigateTo({
  94. // url: '/pages/servant/servant',
  95. // })
  96. wx.switchTab({
  97. url: '/pages/upgrade/serviceOrder/serviceOrder',
  98. success: function (e) {
  99. var page = getCurrentPages().pop();
  100. if (page == undefined || page == null) return;
  101. page.onLoad();
  102. }
  103. })
  104. }
  105. })
  106. } else {
  107. console.info(res);
  108. that.getLoginCode();
  109. wx.showToast({
  110. title: res.errmsg,
  111. duration: 3000,
  112. icon: 'none'
  113. });
  114. }
  115. });
  116. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  117. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
  118. wx.showModal({
  119. title: '警告',
  120. content: '您点击了拒绝授权,功能无法使用!!!',
  121. showCancel: false,
  122. confirmText: '确定',
  123. success: function (res) {
  124. // 用户没有授权成功,不需要改变 isHide 的值
  125. if (res.confirm) {
  126. wx.switchTab({
  127. url: '/pages/index/index',
  128. success: function (e) {
  129. var page = getCurrentPages().pop();
  130. if (page == undefined || page == null) return;
  131. page.onLoad();
  132. }
  133. })
  134. };
  135. }
  136. })
  137. }
  138. },
  139. /**
  140. * 如果勾选了体验版 流程如下
  141. * 1,先用手机号和验证码 去正式库验证通过
  142. * 2,将api.WxApiRoot修改为体验测试版本的WxApiRoot_Uat
  143. * 3,再次提交验证 将输入手机号修改为体验指定的手机号,完成登录
  144. * @param {*} e
  145. */
  146. getUserProfile(e) {
  147. wx.setStorageSync('isUat', true);
  148. let that = this
  149. wx.getUserProfile({
  150. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  151. success: (res) => {
  152. console.log(res)
  153. this.setData({
  154. userInfo: res.userInfo,
  155. hasUserInfo: true
  156. })
  157. //登录
  158. util.request(api.AuthLoginByCaptcha, {
  159. mobile: that.data.mobile,
  160. captcha: that.data.captcha,
  161. userInfo: res.userInfo,
  162. code: that.data.code
  163. }, "POST").then(res => {
  164. //存储用户信息
  165. console.info("登录成功")
  166. if (res.errno == 0) {
  167. //如果是uat版 再次验证登录
  168. if (that.data.isUat) {
  169. console.info('uat')
  170. //替换链接
  171. api.WxApiRoot=api.WxApiRoot_Uat;
  172. util.request(api.AuthLoginUat).then(ress => {
  173. app.globalData.hasLogin = true;
  174. wx.setStorageSync('userInfo', ress.data.userInfo);
  175. wx.setStorageSync('token', ress.data.token);
  176. console.info(wx.getStorageSync('token'))
  177. that.setData({
  178. mobile: ress.data.mobile
  179. });
  180. wx.showToast({
  181. title: '授权注册成功',
  182. icon: 'success',
  183. duration: 2000,
  184. complete: function () {
  185. wx.switchTab({
  186. url: '/pages/upgrade/serviceOrder/serviceOrder',
  187. success: function (e) {
  188. var page = getCurrentPages().pop();
  189. if (page == undefined || page == null) return;
  190. page.onLoad();
  191. }
  192. })
  193. }
  194. })
  195. })
  196. } else {
  197. console.info('not uat')
  198. app.globalData.hasLogin = true;
  199. wx.setStorageSync('userInfo', res.data.userInfo);
  200. wx.setStorageSync('token', res.data.token);
  201. that.setData({
  202. mobile: res.data.mobile
  203. });
  204. wx.showToast({
  205. title: '授权注册成功',
  206. icon: 'success',
  207. duration: 2000,
  208. complete: function () {
  209. wx.switchTab({
  210. url: '/pages/upgrade/serviceOrder/serviceOrder',
  211. success: function (e) {
  212. var page = getCurrentPages().pop();
  213. if (page == undefined || page == null) return;
  214. page.onLoad();
  215. }
  216. })
  217. }
  218. })
  219. }
  220. } else {
  221. util.showErrorToast(res.errmsg);
  222. // wx.showToast({
  223. // title: res.errmsg,
  224. // duration: 3000
  225. // });
  226. }
  227. }).catch(res => {
  228. })
  229. }
  230. })
  231. },
  232. getCaptcha() {
  233. if (this.data.mobile.length != 11 || this.data.mobile.startsWith("1") == false) {
  234. wx.showToast({
  235. title: '请输入正确手机号',
  236. icon: 'none',
  237. duration: 3000
  238. });
  239. return;
  240. }
  241. util.request(api.SendLoginCode, {
  242. tel: this.data.mobile
  243. }, "POST").then(res => {
  244. if (res.errno == 0)
  245. wx.showToast({
  246. title: '验证码发送成功',
  247. duration: 3000
  248. });
  249. else
  250. wx.showToast({
  251. title: res.errmsg,
  252. duration: 3000,
  253. icon:'none'
  254. });
  255. }).catch(res => {
  256. console.info('getCaptcha error')
  257. wx.showToast({
  258. title: res.errmsg,
  259. icon:'none',
  260. duration: 3000
  261. });
  262. })
  263. },
  264. loginTemp(){
  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. }else{
  280. //登录
  281. util.request(api.PartJobLogin, {
  282. tel: this.data.mobile,
  283. verifyCode: this.data.captcha,
  284. }, "POST").then(res => {
  285. //存储用户信息
  286. console.info("登录成功")
  287. if (res.errno == 0) {
  288. app.globalData.hasLogin = true;
  289. let user= res.data.user;
  290. user.isPartJob=res.data.isPartJob;
  291. wx.setStorageSync('userInfo', user);
  292. wx.setStorageSync('token', res.data.token);
  293. wx.redirectTo({
  294. url: '/pages/temp/serviceOrder/serviceOrder',
  295. });
  296. }else if(res.errno === 301)
  297. {
  298. wx.showModal({
  299. title: '提示',
  300. content: '您还未注册,是否现在去注册?',
  301. // showCancel:false,
  302. complete: (res) => {
  303. if (res.cancel) {
  304. }
  305. if (res.confirm) {
  306. wx.navigateTo({
  307. url: '/pages/temp/register/register',
  308. })
  309. }
  310. }
  311. })
  312. } else {
  313. // util.showErrorToast(res.errmsg);
  314. wx.showToast({
  315. title: res.errmsg,
  316. icon:'none',
  317. duration: 3000
  318. });
  319. }
  320. }).catch(err => {
  321. })
  322. }
  323. },
  324. bindMobileInput(e) {
  325. this.setData({
  326. mobile: e.detail.value
  327. });
  328. },
  329. bindcaptchaInput(e) {
  330. this.setData({
  331. captcha: e.detail.value
  332. });
  333. },
  334. goRegister() {
  335. wx.navigateTo({
  336. url: '/pages/temp/register/register',
  337. })
  338. },
  339. checkProBtn(){
  340. wx.navigateTo({
  341. url: '/pages/temp/jzxhAppletsUserPro/jzxhAppletsUserPro'
  342. })
  343. },
  344. checkChange(e){
  345. this.setData({
  346. isChecked: e.detail,
  347. });
  348. }
  349. })