index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. var that = this;
  78. util.wechatLogin(e, false, function (res) {
  79. that.submit()
  80. }, true)
  81. },
  82. submit: function () {
  83. if (!this.data.name) {
  84. util.error('真实姓名必填')
  85. return false;
  86. }
  87. if (!this.data.projectIndex < 0) {
  88. util.error('所属项目必填')
  89. return false;
  90. }
  91. if (!this.data.roleIndex < 0) {
  92. util.error('申请角色不能为空')
  93. return false;
  94. }
  95. if (this.data.remark.length < 10) {
  96. util.error('备注需10个字以上')
  97. return false;
  98. }
  99. if(!this.data.phone) {
  100. util.error('手机号不能为空')
  101. return false;
  102. }
  103. var project = this.data.projects[this.data.projectIndex]
  104. var role = this.data.roles[this.data.roleIndex]
  105. http({
  106. url: 'user-auth/create',
  107. data: {
  108. name: this.data.name,
  109. project_id: project ? project.id : '',
  110. project_role_id: role ? role.id : '',
  111. remark: this.data.remark,
  112. phone: this.data.phone
  113. },
  114. success: function (res) {
  115. util.success('操作成功')
  116. }
  117. })
  118. },
  119. bindPickerChange: function (e) {
  120. var name = e.currentTarget.dataset.name
  121. this.setData({
  122. [name]: e.detail.value
  123. })
  124. },
  125. /**
  126. * 生命周期函数--监听页面初次渲染完成
  127. */
  128. onReady: function () {
  129. },
  130. /**
  131. * 生命周期函数--监听页面显示
  132. */
  133. onShow: function () {
  134. api.getByName(this, 'projects/getAll', 'projects')
  135. api.getByName(this, 'project-roles/getAll', 'roles', {
  136. 'limit': true
  137. })
  138. },
  139. /**
  140. * 生命周期函数--监听页面隐藏
  141. */
  142. onHide: function () {
  143. },
  144. /**
  145. * 生命周期函数--监听页面卸载
  146. */
  147. onUnload: function () {
  148. },
  149. /**
  150. * 页面相关事件处理函数--监听用户下拉动作
  151. */
  152. onPullDownRefresh: function () {
  153. },
  154. /**
  155. * 页面上拉触底事件的处理函数
  156. */
  157. onReachBottom: function () {
  158. },
  159. /**
  160. * 用户点击右上角分享
  161. */
  162. onShareAppMessage: function () {
  163. }
  164. })