login.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // pages/login/login.js
  2. var app = getApp();
  3. const util = require('../../utils/util.js');
  4. const WebIM = require("../../utils/WebIM")["default"];
  5. const CusBase64 = require('../../utils/base64.js');
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. imgPath: app.globalData.imgPath,
  12. phone: '',
  13. vCode: '',
  14. sendText: '发送验证码',
  15. timer: null,
  16. disabled: false,
  17. grant_type: "password",
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function (options) {
  23. console.info(options);
  24. if (options && options.scene) {
  25. // let scene = decodeURIComponent(options.scene);
  26. util.get({
  27. url: '/api/wechat/getQrCodeParams?key=' + options.scene,
  28. success: (res) => {
  29. wx.hideLoading();
  30. console.info(res);
  31. if (res.data.code != 200) {
  32. util.toast(res.data.msg);
  33. } else {
  34. // 判断时间戳是否过期(无需判断)
  35. // let timestamp = res.data.data.timeStamp;
  36. // let now = new Date().getTime();
  37. // console.info(timestamp, now);
  38. // 扫码加群
  39. if ('GROUP' == res.data.data.type) {
  40. wx.setStorageSync('groupId', res.data.data.groupId);
  41. // util.toast('登录成功后方可加入群聊');
  42. wx.showModal({
  43. content: '您需要先登录,才可加入群聊',
  44. showCancel: false
  45. });
  46. }
  47. // 扫码连接WIFI
  48. else if ('WIFI' == res.data.data.type) {
  49. // 连接WIFI代码
  50. }
  51. }
  52. }
  53. });
  54. }
  55. },
  56. /**
  57. * 生命周期函数--监听页面初次渲染完成
  58. */
  59. onReady: function () {
  60. },
  61. /**
  62. * 生命周期函数--监听页面显示
  63. */
  64. onShow: function () {
  65. },
  66. /**
  67. * 生命周期函数--监听页面隐藏
  68. */
  69. onHide: function () {
  70. },
  71. /**
  72. * 生命周期函数--监听页面卸载
  73. */
  74. onUnload: function () {
  75. },
  76. /**
  77. * 页面相关事件处理函数--监听用户下拉动作
  78. */
  79. onPullDownRefresh: function () {
  80. },
  81. /**
  82. * 页面上拉触底事件的处理函数
  83. */
  84. onReachBottom: function () {
  85. },
  86. /**
  87. * 手机号赋值
  88. */
  89. setPhone: function (e) {
  90. this.setData({
  91. phone: e.detail.value
  92. });
  93. },
  94. /**
  95. * 验证码赋值
  96. */
  97. setVcode: function (e) {
  98. this.setData({
  99. vCode: e.detail.value
  100. });
  101. },
  102. /**
  103. * 获取验证码
  104. */
  105. getVerifyCode: function () {
  106. let that = this;
  107. let disabled = that.data.disabled;
  108. if (disabled) {
  109. return;
  110. }
  111. let phone = this.data.phone;
  112. if (!phone) {
  113. util.toast('请输入手机号', 'error');
  114. return false;
  115. }
  116. let reg = /1[3-9]\d{9}/;
  117. if (!reg.test(phone)) {
  118. util.toast('手机号格式有误', 'error');
  119. return false;
  120. }
  121. that.setData({
  122. sendText: '60s后再次发送',
  123. disabled: true
  124. });
  125. // 再次发送短信时间倒计时
  126. that.timeTask();
  127. util.post({
  128. url: '/api/sms/sendSms/' + that.data.phone,
  129. success: (res) => {
  130. wx.hideLoading();
  131. //console.info(res);
  132. if (res.data.code != 200) {
  133. util.toast(res.data.msg);
  134. that.setData({
  135. sendText: '发送验证码',
  136. disabled: false
  137. });
  138. clearInterval(that.data.timer);
  139. } else {
  140. util.toast(res.data.msg);
  141. }
  142. },
  143. });
  144. },
  145. /**
  146. * 60s倒计时定时器
  147. */
  148. timeTask: function () {
  149. let that = this;
  150. let i = 60;
  151. let timer = setInterval(function () {
  152. i--;
  153. if (i < 1) {
  154. that.setData({
  155. sendText: '发送验证码',
  156. disabled: false
  157. });
  158. clearInterval(timer);
  159. } else {
  160. that.setData({
  161. sendText: i + 's后再次发送'
  162. });
  163. }
  164. }, 1000);
  165. that.setData({
  166. timer: timer
  167. });
  168. },
  169. /**
  170. * 登录系统
  171. */
  172. loginSys: function (e) {
  173. let that = this;
  174. let values = e.detail.value;
  175. if (!values.phone) {
  176. util.toast('请输入手机号', 'error');
  177. return false;
  178. }
  179. let reg = /1[3-9]\d{9}/;
  180. if (!reg.test(values.phone)) {
  181. util.toast('手机号格式有误', 'error');
  182. return false;
  183. }
  184. if (!values.code) {
  185. util.toast('请输入验证码', 'error');
  186. return false;
  187. }
  188. values.type = 'CAPTCHA';
  189. util.post({
  190. url: '/login/mobile',
  191. data: values,
  192. header: {
  193. 'Content-Type': 'application/x-www-form-urlencoded'
  194. },
  195. success: (res) => {
  196. wx.hideLoading();
  197. let code = res.data.code;
  198. if ('200' != code) {
  199. util.toast(res.data.msg);
  200. } else {
  201. app.globalData.Authorization = res.data.data.Authorization;
  202. that.getUserInfo();
  203. }
  204. }
  205. });
  206. },
  207. /**
  208. * 登录验证通过后获取用户信息和角色信息
  209. */
  210. getUserInfo: function () {
  211. let that = this;
  212. util.get({
  213. url: '/sys/userInfoAndRole',
  214. success: (res) => {
  215. wx.hideLoading();
  216. if (res.data.code != 200) {
  217. util.toast(res.data.msg);
  218. } else {
  219. let datas = res.data.data;
  220. app.globalData.userInfo = datas;
  221. app.globalData.roleInfo = datas.roleList[0];
  222. wx.setStorage({
  223. key: "myUsername",
  224. data: datas.phone
  225. });
  226. app.conn.open({
  227. // apiUrl: WebIM.config.apiURL,
  228. user: datas.phone,
  229. pwd: CusBase64.CusBASE64.encoder(datas.phone),
  230. grant_type: that.data.grant_type,
  231. appKey: WebIM.config.appkey
  232. });
  233. }
  234. }
  235. });
  236. },
  237. /**
  238. * 微信获取手机号快捷登录
  239. */
  240. getPhoneNumber: function (res) {
  241. let that = this;
  242. if (res.detail.errMsg == 'getPhoneNumber:ok') {
  243. if (!res.detail.code) {
  244. util.toast('请升级微信版本');
  245. return;
  246. }
  247. } else {
  248. util.toast('为了更好地体验请允许授权');
  249. return;
  250. }
  251. //用户按了允许授权按钮
  252. if (res.detail.encryptedData) {
  253. util.post({
  254. url: '/login/mobile',
  255. header: {
  256. 'Content-Type': 'application/x-www-form-urlencoded'
  257. },
  258. data: {
  259. code: res.detail.code,
  260. type: 'QUICK'
  261. },
  262. success: (res) => {
  263. wx.hideLoading();
  264. if (res.data.code != 200) {
  265. util.toast(res.data.msg);
  266. } else {
  267. app.globalData.Authorization = res.data.data.Authorization;
  268. that.getUserInfo();
  269. }
  270. }
  271. });
  272. } else {//用户按了拒绝按钮
  273. wx.showModal({
  274. title: '警告',
  275. content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
  276. showCancel: false,
  277. confirmText: '返回授权',
  278. success: function (res) {
  279. if (res.confirm) {
  280. console.log('用户点击了“返回授权”');
  281. }
  282. }
  283. });
  284. }
  285. },
  286. })