电速宝
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // pages/login/index.js
  2. const api = require('../../../api/index.js');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. // 状态栏高度
  9. statusBarHeight: wx.getStorageSync('statusBarHeight'),
  10. // 导航栏高度
  11. navBarHeight: wx.getStorageSync('navBarHeight'),
  12. // 导航栏和状态栏高度
  13. navStatusBarHeight: wx.getStorageSync('navStatusBarHeight'),
  14. // 胶囊
  15. menu:wx.getStorageSync('menu'),
  16. account:'',
  17. password:'',
  18. passwordtype:true,
  19. checked:false,
  20. shakeClass: '', // 控制晃动动画的类名
  21. Loginswitch:true
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad(options) {
  27. // 检查是否需要展示隐私协议
  28. wx.getPrivacySetting({
  29. success: res => {
  30. if (res.needAuthorization) {
  31. // 需要用户同意隐私协议,显示授权弹窗
  32. this.setData({
  33. showPrivacy: true
  34. })
  35. }
  36. }
  37. })
  38. },
  39. onpassword(){
  40. console.log(111);
  41. this.setData({
  42. passwordtype:!this.data.passwordtype
  43. })
  44. },
  45. checkboxChange(e) {
  46. let _this = this
  47. console.log('checkbox发生change事件,携带value值为:', e.currentTarget.dataset.value)
  48. wx.requestSubscribeMessage({
  49. tmplIds: ['t46217wOQ7HBvw0MfP78Zo8FuwYTKBESaGYIpaXccFw','lLN0xmyLgBSnvveLAOhHiEHvHUi286idSuv-kO61TMI'],
  50. success(res) {
  51. console.log(res);
  52. if(res['t46217wOQ7HBvw0MfP78Zo8FuwYTKBESaGYIpaXccFw'] === 'accept') {
  53. // 用户同意订阅
  54. console.log(2222222);
  55. console.log(!e.currentTarget.dataset.value);
  56. _this.setData({
  57. checked:!e.currentTarget.dataset.value
  58. })
  59. }else{
  60. wx.showToast({
  61. title: '您将收不到新工单通知',
  62. icon: 'success',
  63. });
  64. }
  65. },fail: (err) => {
  66. // console.error('请求失败:', err);
  67. console.log(err);
  68. wx.showToast({ title: '请开启订阅消息通知', icon: 'none' });
  69. }
  70. })
  71. },
  72. onHandleLogin(){
  73. wx.getPhoneNumber({
  74. success: (res) => {
  75. const encryptedData = res.encryptedData;
  76. const iv = res.iv;
  77. console.log(encryptedData);
  78. console.log(iv);
  79. // 将encryptedData和iv发送给后端,用于解密手机号码
  80. },
  81. fail: (err) => {
  82. console.error('获取手机号码失败', err);
  83. }
  84. });
  85. },
  86. bindKeyInput1: function (e) {
  87. this.setData({
  88. account: e.detail.value
  89. })
  90. },
  91. bindKeyInput2: function (e) {
  92. this.setData({
  93. password: e.detail.value
  94. })
  95. },
  96. // 隐私协议同意按钮回调
  97. handleAgreePrivacy() {
  98. this.setData({
  99. showPrivacy: false
  100. })
  101. // 用户同意后,再执行登录流程
  102. },
  103. // 获取手机号回调
  104. onGetPhoneNumber(e) {
  105. console.log(e);
  106. let _this = this
  107. if (!this.data.checked) {
  108. return wx.showToast({
  109. title: '请勾选同意',
  110. icon: 'none',
  111. });
  112. }
  113. wx.login({
  114. success: ress => {
  115. console.log(ress);
  116. const code = ress.code;
  117. // 2. 将 code 发送到服务器,换取到 openId 和 phoneNumber
  118. // 这里的 your_server_url 需要替换成你自己的服务器地址
  119. let obj = {
  120. // phonecode:e.detail.code,
  121. phonecode:this.data.account,
  122. code:code
  123. }
  124. api.request(`/sysoperation/login`, 'post',obj,{ isPublic: true })
  125. .then((data) => {
  126. console.log(data);
  127. if (data.code == 200) {
  128. // 设置数据到本地存储
  129. wx.setStorage({
  130. key: 'token', // 存储的key值
  131. data:data.data.token,
  132. success: function(ress) {
  133. console.log(ress);
  134. wx.reLaunch({
  135. url: '/pages/index/index'
  136. });
  137. wx.showToast({
  138. title: '登录成功',
  139. icon: 'success',
  140. });
  141. }
  142. });
  143. }else{
  144. wx.showToast({
  145. title: data.msg,
  146. icon: 'success',
  147. });
  148. }
  149. })
  150. .catch((err) => {
  151. wx.showToast({
  152. title:'登录失败',
  153. icon: 'none',
  154. });
  155. console.error('请求失败:', err);
  156. });
  157. }
  158. })
  159. },
  160. promptcheck(){
  161. return wx.showToast({
  162. title: '请勾选同意',
  163. icon: 'none',
  164. });
  165. },
  166. // 处理登录按钮点击
  167. onLogin() {
  168. // 获取本地存储的数据
  169. if (!this.data.checked) {
  170. return wx.showToast({
  171. title: '请勾选同意',
  172. icon: 'none',
  173. });
  174. }
  175. if(this.data.account==''){
  176. return wx.showToast({
  177. title: '请输入手机号码',
  178. icon: 'none',
  179. });
  180. }else if(this.data.account.length!=11) {
  181. return wx.showToast({
  182. title: '请输入正确手机号码',
  183. icon: 'none',
  184. });
  185. }
  186. let _this = this
  187. wx.login({
  188. success: res => {
  189. console.log(res);
  190. const code = res.code;
  191. // 2. 将 code 发送到服务器,换取到 openId 和 phoneNumber
  192. // 这里的 your_server_url 需要替换成你自己的服务器地址
  193. let obj = {
  194. phone:_this.data.account,
  195. code:code,
  196. sharecode:_this.data.password
  197. }
  198. api.request(`/SysPartnerLogController/loginphone`, 'post',obj,{ isPublic: true })
  199. .then((data) => {
  200. console.log(data);
  201. if (data.code == 200) {
  202. // 设置数据到本地存储
  203. wx.setStorage({
  204. key: 'token', // 存储的key值
  205. data:data.data.token,
  206. success: function(ress) {
  207. console.log(ress);
  208. wx.reLaunch({
  209. url: '/pages/index/index'
  210. });
  211. wx.showToast({
  212. title: '登录成功',
  213. icon: 'success',
  214. });
  215. }
  216. });
  217. }else{
  218. wx.showToast({
  219. title: data.msg,
  220. icon: 'success',
  221. });
  222. }
  223. })
  224. .catch((err) => {
  225. wx.showToast({
  226. title:err.msg+',请使用手机号一键登录',
  227. icon: 'none',
  228. });
  229. console.error('请求失败:', err);
  230. });
  231. }
  232. })
  233. },
  234. onprivacy(e){
  235. console.log(e.target.dataset.name);
  236. wx.navigateTo({
  237. url: `/pages/privacy/index?name=${e.target.dataset.name}`,
  238. })
  239. },
  240. // 切换登录模式
  241. onswitch(e){
  242. console.log(e);
  243. // if (!e.target.dataset.value) {
  244. // return wx.showToast({
  245. // title: '待开发',
  246. // icon: 'none',
  247. // });
  248. // }
  249. this.setData({
  250. Loginswitch:e.target.dataset.value
  251. })
  252. },
  253. /**
  254. * 生命周期函数--监听页面初次渲染完成
  255. */
  256. onReady() {
  257. },
  258. /**
  259. * 生命周期函数--监听页面显示
  260. */
  261. onShow() {
  262. },
  263. /**
  264. * 生命周期函数--监听页面隐藏
  265. */
  266. onHide() {
  267. },
  268. /**
  269. * 生命周期函数--监听页面卸载
  270. */
  271. onUnload() {
  272. },
  273. /**
  274. * 页面相关事件处理函数--监听用户下拉动作
  275. */
  276. onPullDownRefresh() {
  277. },
  278. /**
  279. * 页面上拉触底事件的处理函数
  280. */
  281. onReachBottom() {
  282. },
  283. /**
  284. * 用户点击右上角分享
  285. */
  286. onShareAppMessage() {
  287. }
  288. })