userLogin.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // pages/userLogin/userLogin.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. spashTextValue: '- 智慧服务 智创未来 -',
  10. splashImageUrl: '/images/tu.png',
  11. splashImageUrl2: '/images/zi.png',
  12. code: '',
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad: function (options) {
  18. app.getScene(options);
  19. this.getCode();
  20. },
  21. getCode(){
  22. let that = this;
  23. wx.login({
  24. success: function (ret) {
  25. that.setData({
  26. code: ret.code
  27. })
  28. }
  29. });
  30. },
  31. getPhoneNumber(e){
  32. // console.log(e);
  33. var that = this;
  34. wx.showLoading({
  35. title: '加载中',
  36. mask: true
  37. });
  38. let orgId = wx.getStorageSync("orgId");
  39. wx.request({
  40. url: app.globalData.serverUrl + 'login/wxpLogin',
  41. data: {
  42. orgId: orgId,
  43. code: this.data.code,
  44. iv: e.detail.iv,
  45. encryptedData: e.detail.encryptedData
  46. },
  47. method: 'POST',
  48. header: {
  49. 'content-type': 'application/x-www-form-urlencoded'
  50. },
  51. success: function (res) {
  52. console.log(res);
  53. wx.hideLoading();
  54. if (res.data.code == 0) {
  55. try {
  56. app.logout();
  57. wx.setStorageSync("userId", res.data.data.userId);
  58. wx.setStorageSync("token", res.data.data.token);
  59. wx.setStorageSync("orgId", res.data.data.orgId);
  60. } catch (e) { };
  61. var userinfo = {
  62. userId: res.data.data.userId,
  63. token: res.data.data.token,
  64. };
  65. app.globalData.userinfo = userinfo;
  66. wx.switchTab({
  67. url: '/pages/index/index'
  68. });
  69. }else{
  70. wx.showModal({
  71. title: '提示',
  72. content: res.data.message,
  73. showCancel: false
  74. })
  75. }
  76. that.getCode();
  77. },
  78. fail:function(e){
  79. console.log(e);
  80. wx.hideLoading();
  81. wx.showModal({
  82. title: '提示',
  83. content: '登录失败,请重试',
  84. showCancel: false
  85. })
  86. that.getCode();
  87. }
  88. })
  89. },
  90. getUserInfo(){
  91. wx.getUserProfile({
  92. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  93. success: (e) => {
  94. wx.showLoading({
  95. title: '加载中',
  96. mask: true
  97. });
  98. let orgId = wx.getStorageSync("orgId");
  99. wx.request({
  100. url: app.globalData.serverUrl + 'login/wxLogin',
  101. data: {
  102. orgId: orgId,
  103. code: this.data.code,
  104. iv: e.iv,
  105. encryptedData: e.encryptedData
  106. },
  107. method: 'POST',
  108. header: {
  109. 'content-type': 'application/x-www-form-urlencoded'
  110. },
  111. success: function (res) {
  112. console.log(res);
  113. wx.hideLoading();
  114. if (res.data.code == 0) {
  115. try {
  116. app.logout();
  117. wx.setStorageSync("userId", res.data.data.userId);
  118. wx.setStorageSync("token", res.data.data.token);
  119. } catch (e) { };
  120. var userinfo = {
  121. userId: res.data.data.userId,
  122. token: res.data.data.token,
  123. };
  124. app.globalData.userinfo = userinfo;
  125. wx.switchTab({
  126. url: '/pages/index/index'
  127. });
  128. }else{
  129. wx.showModal({
  130. title: '提示',
  131. content: res.data.message,
  132. showCancel: false
  133. })
  134. }
  135. },
  136. fail:function(){
  137. wx.hideLoading();
  138. wx.showModal({
  139. title: '提示',
  140. content: '登录失败,请重试',
  141. showCancel: false
  142. })
  143. }
  144. })
  145. }
  146. });
  147. },
  148. //提交
  149. onSubmit: function(){
  150. var username = this.data.username;
  151. var password = this.data.password;
  152. //去除开始结尾空格
  153. if (username.replace(/^(\s|\xA0)+|(\s|\xA0)+$/g, '') && password){
  154. wx.showLoading({
  155. title: '登录中',
  156. mask: true
  157. });
  158. wx.login({
  159. success: function (loginRes) {
  160. },
  161. fail:function(e){
  162. wx.hideLoading();
  163. wx.showModal({
  164. title: '提示',
  165. content: '登录失败,请重试',
  166. showCancel: false
  167. })
  168. }
  169. })
  170. }else{
  171. wx.showModal({
  172. title: '提示',
  173. content: '账号或密码为空!',
  174. showCancel: false
  175. })
  176. }
  177. },
  178. goYY(){
  179. var token = wx.getStorageSync("visitorToken");
  180. if (!token || token == undefined) { //未登录
  181. wx.login({
  182. success: function (loginRes) {
  183. var serverUrl = app.globalData.visitorServerUrl;
  184. if (loginRes) {
  185. wx.request({
  186. url: serverUrl + 'oauth/checkCode',
  187. data: {
  188. type: 2,
  189. code: loginRes.code//临时登录凭证
  190. },
  191. method: 'POST',
  192. header: {
  193. 'content-type': 'application/x-www-form-urlencoded'
  194. },
  195. success: function (res) {
  196. if (res.data.code == -101) {
  197. wx.removeStorageSync("visitorToken");
  198. wx.redirectTo({
  199. url: '/pages/splash/splash?type=visitor'
  200. })
  201. }else if (res.data.code == 0) {
  202. try {
  203. wx.setStorageSync("visitorToken", res.data.data.token);
  204. wx.setStorageSync("visitorUserId", res.data.data.userId);
  205. } catch (e) {
  206. app.logout();
  207. wx.showModal({
  208. title: '提示',
  209. content: '登录异常!',
  210. showCancel: false
  211. })
  212. return false;
  213. };
  214. wx.redirectTo({
  215. url: '/pages/visitor/index'
  216. });
  217. }
  218. },
  219. fail: function (err) {
  220. wx.showModal({
  221. title: '提示',
  222. content: '登录异常!',
  223. showCancel: false
  224. })
  225. }
  226. })
  227. } else {
  228. wx.showModal({
  229. title: '提示',
  230. content: '登录异常!',
  231. showCancel: false
  232. })
  233. }
  234. }
  235. })
  236. }else{
  237. wx.navigateTo({
  238. url: '/pages/visitor/index'
  239. });
  240. }
  241. },
  242. })