login.js 9.7 KB

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