index.js 7.5 KB

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