index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // pages/bind/index.js
  2. import util from '../../utils/util'
  3. import http from '../../utils/http'
  4. import api from '../../utils/api'
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. projects: [],
  11. projectIndex: -1,
  12. roles: [],
  13. roleIndex: -1,
  14. name: '',
  15. remark: '',
  16. phone: ''
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: function (options) {
  22. },
  23. onInputChange: function (e) {
  24. var name = e.currentTarget.dataset.name
  25. var val = e.detail.value
  26. this.setData({
  27. [name]: val
  28. })
  29. },
  30. updatePhoneNumber(e) {
  31. var that = this
  32. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  33. wx.checkSession({
  34. success: () => {
  35. var data = Object.assign({}, e.detail, {
  36. session_key: wx.getStorageSync('session_key')
  37. })
  38. console.log(data)
  39. that.parsePhone(data)
  40. },
  41. fail: () => {
  42. wx.login({
  43. success: function (res) {
  44. var data = Object.assign({}, e.detail, {
  45. code: res.code
  46. })
  47. console.log(data)
  48. that.parsePhone(data)
  49. }
  50. })
  51. }
  52. })
  53. }
  54. },
  55. parsePhone(data) {
  56. var that = this
  57. http({
  58. url: 'parsePhone',
  59. data: data,
  60. success: function (res) {
  61. console.log(res)
  62. if (res.code == 0 && res.data.phoneNumber) {
  63. that.setData({
  64. phone: res.data.phoneNumber
  65. })
  66. if (res.data.session_key) {
  67. wx.setStorageSync('session_key', res.data.session_key)
  68. }
  69. util.success('手机号获取成功')
  70. } else {
  71. util.error('发生错误')
  72. }
  73. }
  74. })
  75. },
  76. getUserInfo: function (e) {
  77. this.submit()
  78. },
  79. submit: function () {
  80. if (!this.data.name) {
  81. util.error('真实姓名必填')
  82. return false;
  83. }
  84. if (!this.data.projectIndex < 0) {
  85. util.error('所属项目必填')
  86. return false;
  87. }
  88. if (!this.data.roleIndex < 0) {
  89. util.error('申请角色不能为空')
  90. return false;
  91. }
  92. // if (this.data.remark.length < 10) {
  93. // util.error('备注需10个字以上')
  94. // return false;
  95. // }
  96. // if(!this.data.phone) {
  97. // util.error('手机号不能为空')
  98. // return false;
  99. // }
  100. var project = this.data.projects[this.data.projectIndex]
  101. var role = this.data.roles[this.data.roleIndex]
  102. var userInfo = getApp().globalData.userInfo
  103. var phone = userInfo ? userInfo.phone : ''
  104. http({
  105. url: 'user-auth/create',
  106. data: {
  107. name: this.data.name,
  108. project_id: project ? project.id : '',
  109. project_role_id: role ? role.id : '',
  110. remark: this.data.remark,
  111. phone: phone
  112. },
  113. success: function (res) {
  114. util.success('操作成功');
  115. wx.navigateBack({
  116. delta: 0,
  117. })
  118. }
  119. })
  120. },
  121. bindPickerChange: function (e) {
  122. var name = e.currentTarget.dataset.name
  123. this.setData({
  124. [name]: e.detail.value
  125. })
  126. },
  127. /**
  128. * 生命周期函数--监听页面初次渲染完成
  129. */
  130. onReady: function () {
  131. },
  132. /**
  133. * 生命周期函数--监听页面显示
  134. */
  135. onShow: function () {
  136. api.getByName(this, 'projects/getAll', 'projects')
  137. api.getByName(this, 'project-roles/getAll', 'roles', {
  138. 'limit': true
  139. })
  140. },
  141. /**
  142. * 生命周期函数--监听页面隐藏
  143. */
  144. onHide: function () {
  145. },
  146. /**
  147. * 生命周期函数--监听页面卸载
  148. */
  149. onUnload: function () {
  150. },
  151. /**
  152. * 页面相关事件处理函数--监听用户下拉动作
  153. */
  154. onPullDownRefresh: function () {
  155. },
  156. /**
  157. * 页面上拉触底事件的处理函数
  158. */
  159. onReachBottom: function () {
  160. },
  161. /**
  162. * 用户点击右上角分享
  163. */
  164. onShareAppMessage: function () {
  165. }
  166. })